Skip to content

Commit 39402ec

Browse files
committed
add support for http.ResponseController to ResponseRecorder
once the fix for golang/go#60229 makes it to the go version, this commit can be dropped
1 parent 216d847 commit 39402ec

File tree

5 files changed

+64
-9
lines changed

5 files changed

+64
-9
lines changed

pkg/controlplane/instance_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ import (
4545
"k8s.io/apimachinery/pkg/version"
4646
"k8s.io/apiserver/pkg/authorization/authorizerfactory"
4747
openapinamer "k8s.io/apiserver/pkg/endpoints/openapi"
48+
responsewritertesting "k8s.io/apiserver/pkg/endpoints/responsewriter/testing"
4849
genericapiserver "k8s.io/apiserver/pkg/server"
4950
"k8s.io/apiserver/pkg/server/options"
5051
"k8s.io/apiserver/pkg/server/resourceconfig"
@@ -217,7 +218,9 @@ func TestVersion(t *testing.T) {
217218
defer etcdserver.Terminate(t)
218219

219220
req, _ := http.NewRequest("GET", "/version", nil)
220-
resp := httptest.NewRecorder()
221+
// TODO: remove WithFakeResponseController once
222+
// https://github.com/golang/go/issues/60229 is fixed.
223+
resp := responsewritertesting.WithFakeResponseController(httptest.NewRecorder())
221224
s.GenericAPIServer.Handler.ServeHTTP(resp, req)
222225
if resp.Code != 200 {
223226
t.Fatalf("expected http 200, got: %d", resp.Code)

staging/src/k8s.io/apiserver/pkg/endpoints/filters/request_deadline_test.go

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,9 @@ func TestWithRequestDeadline(t *testing.T) {
197197
t.Fatalf("test setup failed, expected the new HTTP request context to have no deadline but got: %s", remaning)
198198
}
199199

200-
w := httptest.NewRecorder()
200+
// TODO: remove WithFakeResponseController once
201+
// https://github.com/golang/go/issues/60229 is fixed.
202+
w := responsewritertesting.WithFakeResponseController(httptest.NewRecorder())
201203
withDeadline.ServeHTTP(w, testRequest)
202204

203205
if test.handlerCallCountExpected != callCount {
@@ -245,7 +247,9 @@ func TestWithRequestDeadlineWithClock(t *testing.T) {
245247
// the request has arrived just now.
246248
testRequest = testRequest.WithContext(request.WithReceivedTimestamp(testRequest.Context(), time.Now()))
247249

248-
w := httptest.NewRecorder()
250+
// TODO: remove WithFakeResponseController once
251+
// https://github.com/golang/go/issues/60229 is fixed.
252+
w := responsewritertesting.WithFakeResponseController(httptest.NewRecorder())
249253
withDeadline.ServeHTTP(w, testRequest)
250254

251255
if !hasDeadlineGot {
@@ -271,7 +275,9 @@ func TestWithRequestDeadlineWithInvalidTimeoutIsAudited(t *testing.T) {
271275
withDeadline = WithRequestInfo(withDeadline, &fakeRequestResolver{})
272276

273277
testRequest := newRequest(t, "/api/v1/namespaces?timeout=foo")
274-
w := httptest.NewRecorder()
278+
// TODO: remove WithFakeResponseController once
279+
// https://github.com/golang/go/issues/60229 is fixed.
280+
w := responsewritertesting.WithFakeResponseController(httptest.NewRecorder())
275281
withDeadline.ServeHTTP(w, testRequest)
276282

277283
if handlerInvoked {
@@ -313,7 +319,9 @@ func TestWithRequestDeadlineWithPanic(t *testing.T) {
313319
})
314320

315321
testRequest := newRequest(t, "/api/v1/namespaces?timeout=1s")
316-
w := httptest.NewRecorder()
322+
// TODO: remove WithFakeResponseController once
323+
// https://github.com/golang/go/issues/60229 is fixed.
324+
w := responsewritertesting.WithFakeResponseController(httptest.NewRecorder())
317325
withPanicRecovery.ServeHTTP(w, testRequest)
318326

319327
if panicErrExpected != panicErrGot {
@@ -344,7 +352,9 @@ func TestWithRequestDeadlineWithRequestTimesOut(t *testing.T) {
344352
withDeadline = WithRequestInfo(withDeadline, &fakeRequestResolver{})
345353

346354
testRequest := newRequest(t, fmt.Sprintf("/api/v1/namespaces?timeout=%s", timeout))
347-
w := httptest.NewRecorder()
355+
// TODO: remove WithFakeResponseController once
356+
// https://github.com/golang/go/issues/60229 is fixed.
357+
w := responsewritertesting.WithFakeResponseController(httptest.NewRecorder())
348358
withDeadline.ServeHTTP(w, testRequest)
349359

350360
if errGot != context.DeadlineExceeded {
@@ -390,7 +400,9 @@ func TestWithFailedRequestAudit(t *testing.T) {
390400

391401
withAudit := withFailedRequestAudit(errorHandler, test.statusErr, fakeSink, fakeRuleEvaluator)
392402

393-
w := httptest.NewRecorder()
403+
// TODO: remove WithFakeResponseController once
404+
// https://github.com/golang/go/issues/60229 is fixed.
405+
w := responsewritertesting.WithFakeResponseController(httptest.NewRecorder())
394406
testRequest := newRequest(t, "/apis/v1/namespaces/default/pods")
395407
info := request.RequestInfo{}
396408
testRequest = testRequest.WithContext(request.WithRequestInfo(testRequest.Context(), &info))

staging/src/k8s.io/apiserver/pkg/endpoints/responsewriter/testing/recorder.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,41 @@ limitations under the License.
1717
package testing
1818

1919
import (
20+
"net/http/httptest"
2021
"strings"
22+
"time"
2123
)
2224

25+
// WithFakeResponseController extends a given httptest.ResponseRecorder object
26+
// with a fake implementaion of http.ResonseController.
27+
// NOTE: use this function for testing purposes only.
28+
//
29+
// httptest.ResponseRecorder does not implement SetReadDeadline or
30+
// SetWriteDeadline, see https://github.com/golang/go/issues/60229
31+
// for more details.
32+
// TODO: once https://github.com/golang/go/issues/60229 is fixed
33+
// we can remove this function.
34+
func WithFakeResponseController(w *httptest.ResponseRecorder) *FakeResponseRecorder {
35+
return &FakeResponseRecorder{ResponseRecorder: w}
36+
}
37+
38+
type FakeResponseRecorder struct {
39+
*httptest.ResponseRecorder
40+
41+
ReadDeadlines []time.Time
42+
WriteDeadlines []time.Time
43+
}
44+
45+
func (w *FakeResponseRecorder) SetReadDeadline(deadline time.Time) error {
46+
w.ReadDeadlines = append(w.ReadDeadlines, deadline)
47+
return nil
48+
}
49+
50+
func (w *FakeResponseRecorder) SetWriteDeadline(deadline time.Time) error {
51+
w.WriteDeadlines = append(w.WriteDeadlines, deadline)
52+
return nil
53+
}
54+
2355
func IsStreamReadOrWriteTimeout(err error) bool {
2456
if err == nil {
2557
return false

staging/src/k8s.io/apiserver/pkg/server/config_test.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import (
3737
"k8s.io/apiserver/pkg/authentication/authenticator"
3838
"k8s.io/apiserver/pkg/authentication/user"
3939
"k8s.io/apiserver/pkg/endpoints/request"
40+
responsewritertesting "k8s.io/apiserver/pkg/endpoints/responsewriter/testing"
4041
"k8s.io/apiserver/pkg/server/healthz"
4142
"k8s.io/client-go/informers"
4243
"k8s.io/client-go/kubernetes/fake"
@@ -322,7 +323,10 @@ func TestAuthenticationAuditAnnotationsDefaultChain(t *testing.T) {
322323
t.Errorf("failed to write response: %v", err)
323324
}
324325
}), c)
325-
w := httptest.NewRecorder()
326+
327+
// TODO: remove WithFakeResponseController once
328+
// https://github.com/golang/go/issues/60229 is fixed.
329+
w := responsewritertesting.WithFakeResponseController(httptest.NewRecorder())
326330

327331
h.ServeHTTP(w, httptest.NewRequest("GET", "https://ignored.com", nil))
328332

staging/src/k8s.io/apiserver/pkg/server/genericapiserver_test.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ import (
4646
"k8s.io/apiserver/pkg/endpoints/discovery"
4747
genericapifilters "k8s.io/apiserver/pkg/endpoints/filters"
4848
openapinamer "k8s.io/apiserver/pkg/endpoints/openapi"
49+
responsewritertesting "k8s.io/apiserver/pkg/endpoints/responsewriter/testing"
4950
"k8s.io/apiserver/pkg/registry/rest"
5051
genericfilters "k8s.io/apiserver/pkg/server/filters"
5152
"k8s.io/client-go/informers"
@@ -472,8 +473,11 @@ func TestNotRestRoutesHaveAuth(t *testing.T) {
472473
{"/debug/flags/"},
473474
{"/version"},
474475
} {
475-
resp := httptest.NewRecorder()
476+
// TODO: remove WithFakeResponseController once
477+
// https://github.com/golang/go/issues/60229 is fixed.
478+
resp := responsewritertesting.WithFakeResponseController(httptest.NewRecorder())
476479
req, _ := http.NewRequest("GET", test.route, nil)
480+
477481
s.Handler.ServeHTTP(resp, req)
478482
if resp.Code != 200 {
479483
t.Errorf("route %q expected to work: code %d", test.route, resp.Code)

0 commit comments

Comments
 (0)