Skip to content

Commit d58b319

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 71a9cd6 commit d58b319

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
@@ -200,7 +200,9 @@ func TestWithRequestDeadline(t *testing.T) {
200200
t.Fatalf("test setup failed, expected the new HTTP request context to have no deadline but got: %s", remaning)
201201
}
202202

203-
w := httptest.NewRecorder()
203+
// TODO: remove WithFakeResponseController once
204+
// https://github.com/golang/go/issues/60229 is fixed.
205+
w := responsewritertesting.WithFakeResponseController(httptest.NewRecorder())
204206
withDeadline.ServeHTTP(w, testRequest)
205207

206208
if test.handlerCallCountExpected != callCount {
@@ -248,7 +250,9 @@ func TestWithRequestDeadlineWithClock(t *testing.T) {
248250
// the request has arrived just now.
249251
testRequest = testRequest.WithContext(request.WithReceivedTimestamp(testRequest.Context(), time.Now()))
250252

251-
w := httptest.NewRecorder()
253+
// TODO: remove WithFakeResponseController once
254+
// https://github.com/golang/go/issues/60229 is fixed.
255+
w := responsewritertesting.WithFakeResponseController(httptest.NewRecorder())
252256
withDeadline.ServeHTTP(w, testRequest)
253257

254258
if !hasDeadlineGot {
@@ -274,7 +278,9 @@ func TestWithRequestDeadlineWithInvalidTimeoutIsAudited(t *testing.T) {
274278
withDeadline = WithRequestInfo(withDeadline, &fakeRequestResolver{})
275279

276280
testRequest := newRequest(t, "/api/v1/namespaces?timeout=foo")
277-
w := httptest.NewRecorder()
281+
// TODO: remove WithFakeResponseController once
282+
// https://github.com/golang/go/issues/60229 is fixed.
283+
w := responsewritertesting.WithFakeResponseController(httptest.NewRecorder())
278284
withDeadline.ServeHTTP(w, testRequest)
279285

280286
if handlerInvoked {
@@ -316,7 +322,9 @@ func TestWithRequestDeadlineWithPanic(t *testing.T) {
316322
})
317323

318324
testRequest := newRequest(t, "/api/v1/namespaces?timeout=1s")
319-
w := httptest.NewRecorder()
325+
// TODO: remove WithFakeResponseController once
326+
// https://github.com/golang/go/issues/60229 is fixed.
327+
w := responsewritertesting.WithFakeResponseController(httptest.NewRecorder())
320328
withPanicRecovery.ServeHTTP(w, testRequest)
321329

322330
if panicErrExpected != panicErrGot {
@@ -347,7 +355,9 @@ func TestWithRequestDeadlineWithRequestTimesOut(t *testing.T) {
347355
withDeadline = WithRequestInfo(withDeadline, &fakeRequestResolver{})
348356

349357
testRequest := newRequest(t, fmt.Sprintf("/api/v1/namespaces?timeout=%s", timeout))
350-
w := httptest.NewRecorder()
358+
// TODO: remove WithFakeResponseController once
359+
// https://github.com/golang/go/issues/60229 is fixed.
360+
w := responsewritertesting.WithFakeResponseController(httptest.NewRecorder())
351361
withDeadline.ServeHTTP(w, testRequest)
352362

353363
if errGot != context.DeadlineExceeded {
@@ -393,7 +403,9 @@ func TestWithFailedRequestAudit(t *testing.T) {
393403

394404
withAudit := withFailedRequestAudit(errorHandler, test.statusErr, fakeSink, fakeRuleEvaluator)
395405

396-
w := httptest.NewRecorder()
406+
// TODO: remove WithFakeResponseController once
407+
// https://github.com/golang/go/issues/60229 is fixed.
408+
w := responsewritertesting.WithFakeResponseController(httptest.NewRecorder())
397409
testRequest := newRequest(t, "/apis/v1/namespaces/default/pods")
398410
info := request.RequestInfo{}
399411
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 implementation 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)