From cd66c10a211ff2b8d4ae63cb229092f1f657b399 Mon Sep 17 00:00:00 2001 From: Julien Salleyron Date: Thu, 25 Jan 2018 10:15:10 +0100 Subject: [PATCH 1/3] Need pointer to implement CloseNotifier --- gzip.go | 2 +- gzip_test.go | 22 ++++++++++++++++++++-- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/gzip.go b/gzip.go index b3cb831..20608c5 100644 --- a/gzip.go +++ b/gzip.go @@ -282,7 +282,7 @@ func GzipHandlerWithOpts(opts ...option) (func(http.Handler) http.Handler, error defer gw.Close() if _, ok := w.(http.CloseNotifier); ok { - gwcn := GzipResponseWriterWithCloseNotify{gw} + gwcn := &GzipResponseWriterWithCloseNotify{gw} h.ServeHTTP(gwcn, r) } else { h.ServeHTTP(gw, r) diff --git a/gzip_test.go b/gzip_test.go index ec15433..bc9369d 100644 --- a/gzip_test.go +++ b/gzip_test.go @@ -325,17 +325,35 @@ func TestFlushBeforeWrite(t *testing.T) { } func TestImplementCloseNotifier(t *testing.T) { + request := &http.Request{} + request.Header = http.Header{} + request.Header.Set(acceptEncoding, "gzip") GzipHandler(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request){ _, ok := rw.(http.CloseNotifier) assert.True(t, ok, "response writer must implement http.CloseNotifier") - })).ServeHTTP(&mockRWCloseNotify{}, &http.Request{}) + })).ServeHTTP(&mockRWCloseNotify{}, request) +} + +func TestImplementFlusherAndCloseNotifier(t *testing.T) { + request := &http.Request{} + request.Header = http.Header{} + request.Header.Set(acceptEncoding, "gzip") + GzipHandler(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request){ + _, okCloseNotifier := rw.(http.CloseNotifier) + assert.True(t, okCloseNotifier, "response writer must implement http.CloseNotifier") + _, okFlusher := rw.(http.Flusher) + assert.True(t, okFlusher, "response writer must implement http.Flusher") + })).ServeHTTP(&mockRWCloseNotify{}, request) } func TestNotImplementCloseNotifier(t *testing.T) { + request := &http.Request{} + request.Header = http.Header{} + request.Header.Set(acceptEncoding, "gzip") GzipHandler(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request){ _, ok := rw.(http.CloseNotifier) assert.False(t, ok, "response writer must not implement http.CloseNotifier") - })).ServeHTTP(httptest.NewRecorder(), &http.Request{}) + })).ServeHTTP(httptest.NewRecorder(), request) } From 52f238420b54c917c80c5410f244479501b0b866 Mon Sep 17 00:00:00 2001 From: Julien Salleyron Date: Thu, 25 Jan 2018 14:50:58 +0100 Subject: [PATCH 2/3] Change pointer receiver to value receiver --- gzip.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/gzip.go b/gzip.go index 20608c5..ae1ebe4 100644 --- a/gzip.go +++ b/gzip.go @@ -88,7 +88,7 @@ type GzipResponseWriterWithCloseNotify struct { *GzipResponseWriter } -func (w *GzipResponseWriterWithCloseNotify) CloseNotify() <-chan bool { +func (w GzipResponseWriterWithCloseNotify) CloseNotify() <-chan bool { return w.ResponseWriter.(http.CloseNotifier).CloseNotify() } @@ -282,7 +282,7 @@ func GzipHandlerWithOpts(opts ...option) (func(http.Handler) http.Handler, error defer gw.Close() if _, ok := w.(http.CloseNotifier); ok { - gwcn := &GzipResponseWriterWithCloseNotify{gw} + gwcn := GzipResponseWriterWithCloseNotify{gw} h.ServeHTTP(gwcn, r) } else { h.ServeHTTP(gw, r) From 2f9a872799699020c764a3148dc281b25859efe6 Mon Sep 17 00:00:00 2001 From: Julien Salleyron Date: Thu, 25 Jan 2018 17:23:58 +0100 Subject: [PATCH 3/3] Review --- gzip_test.go | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/gzip_test.go b/gzip_test.go index bc9369d..ba50489 100644 --- a/gzip_test.go +++ b/gzip_test.go @@ -325,8 +325,7 @@ func TestFlushBeforeWrite(t *testing.T) { } func TestImplementCloseNotifier(t *testing.T) { - request := &http.Request{} - request.Header = http.Header{} + request := httptest.NewRequest(http.MethodGet, "/", nil) request.Header.Set(acceptEncoding, "gzip") GzipHandler(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request){ _, ok := rw.(http.CloseNotifier) @@ -335,8 +334,7 @@ func TestImplementCloseNotifier(t *testing.T) { } func TestImplementFlusherAndCloseNotifier(t *testing.T) { - request := &http.Request{} - request.Header = http.Header{} + request := httptest.NewRequest(http.MethodGet, "/", nil) request.Header.Set(acceptEncoding, "gzip") GzipHandler(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request){ _, okCloseNotifier := rw.(http.CloseNotifier) @@ -347,8 +345,7 @@ func TestImplementFlusherAndCloseNotifier(t *testing.T) { } func TestNotImplementCloseNotifier(t *testing.T) { - request := &http.Request{} - request.Header = http.Header{} + request := httptest.NewRequest(http.MethodGet, "/", nil) request.Header.Set(acceptEncoding, "gzip") GzipHandler(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request){ _, ok := rw.(http.CloseNotifier)