File tree 2 files changed +26
-2
lines changed
2 files changed +26
-2
lines changed Original file line number Diff line number Diff line change @@ -192,10 +192,16 @@ func (w *GzipResponseWriter) Close() error {
192
192
// http.ResponseWriter if it is an http.Flusher. This makes GzipResponseWriter
193
193
// an http.Flusher.
194
194
func (w * GzipResponseWriter ) Flush () {
195
- if w .gw != nil {
196
- w .gw .Flush ()
195
+ if w .gw == nil {
196
+ // Only flush once startGzip has been called.
197
+ //
198
+ // Flush is thus a no-op until the written body
199
+ // exceeds minSize.
200
+ return
197
201
}
198
202
203
+ w .gw .Flush ()
204
+
199
205
if fw , ok := w .ResponseWriter .(http.Flusher ); ok {
200
206
fw .Flush ()
201
207
}
Original file line number Diff line number Diff line change @@ -306,6 +306,24 @@ func TestStatusCodes(t *testing.T) {
306
306
}
307
307
}
308
308
309
+ func TestFlushBeforeWrite (t * testing.T ) {
310
+ b := []byte (testBody )
311
+ handler := GzipHandler (http .HandlerFunc (func (rw http.ResponseWriter , req * http.Request ) {
312
+ rw .WriteHeader (http .StatusNotFound )
313
+ rw .(http.Flusher ).Flush ()
314
+ rw .Write (b )
315
+ }))
316
+ r := httptest .NewRequest (http .MethodGet , "/" , nil )
317
+ r .Header .Set ("Accept-Encoding" , "gzip" )
318
+ w := httptest .NewRecorder ()
319
+ handler .ServeHTTP (w , r )
320
+
321
+ res := w .Result ()
322
+ assert .Equal (t , http .StatusNotFound , res .StatusCode )
323
+ assert .Equal (t , "gzip" , res .Header .Get ("Content-Encoding" ))
324
+ assert .NotEqual (t , b , w .Body .Bytes ())
325
+ }
326
+
309
327
func TestIgnoreSubsequentWriteHeader (t * testing.T ) {
310
328
handler := GzipHandler (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
311
329
w .WriteHeader (500 )
You can’t perform that action at this time.
0 commit comments