@@ -23,6 +23,7 @@ import (
23
23
"io"
24
24
"net/http"
25
25
"net/http/httptest"
26
+ "time"
26
27
27
28
. "github.com/onsi/ginkgo"
28
29
. "github.com/onsi/gomega"
@@ -190,6 +191,24 @@ var _ = Describe("Admission Webhooks", func() {
190
191
webhook .ServeHTTP (respRecorder , req .WithContext (ctx ))
191
192
Expect (respRecorder .Body .String ()).To (Equal (expected ))
192
193
})
194
+
195
+ It ("should never run into circular calling if the writer has broken" , func () {
196
+ req := & http.Request {
197
+ Header : http.Header {"Content-Type" : []string {"application/json" }},
198
+ Body : nopCloser {Reader : bytes .NewBufferString (fmt .Sprintf (`{%s,"request":{}}` , gvkJSONv1 ))},
199
+ }
200
+ webhook := & Webhook {
201
+ Handler : & fakeHandler {},
202
+ log : logf .RuntimeLog .WithName ("webhook" ),
203
+ }
204
+
205
+ bw := & brokenWriter {ResponseWriter : respRecorder }
206
+ Eventually (func () int {
207
+ // This should not be blocked by the circular calling of writeResponse and writeAdmissionResponse
208
+ webhook .ServeHTTP (bw , req )
209
+ return respRecorder .Body .Len ()
210
+ }, time .Second * 3 ).Should (Equal (0 ))
211
+ })
193
212
})
194
213
})
195
214
@@ -225,3 +244,11 @@ func (h *fakeHandler) Handle(ctx context.Context, req Request) Response {
225
244
Allowed : true ,
226
245
}}
227
246
}
247
+
248
+ type brokenWriter struct {
249
+ http.ResponseWriter
250
+ }
251
+
252
+ func (bw * brokenWriter ) Write (buf []byte ) (int , error ) {
253
+ return 0 , fmt .Errorf ("mock: write: broken pipe" )
254
+ }
0 commit comments