From 356173fcc4061fc563bd47ddb331cfb3e6003075 Mon Sep 17 00:00:00 2001 From: Niels Widger Date: Thu, 9 Feb 2017 14:59:46 -0500 Subject: [PATCH 1/2] (Combined)LoggingHandler: Add Go 1.8 http.Pusher support When building with Go 1.8, embed http.Pusher interface in loggingResponseWriter interface and define a Push wrapper method on responseLogger. Without this change, users of handlers.LoggingHandler or handlers.CombinedLoggingHandler cannot take advantage of the new http.Pusher interface since responseLogger does not satisfy the http.Pusher interface. When building with pre-1.8 Go, the loggingResponseWriter and responseLogger types remain unchanged. --- handlers.go | 2 +- handlers_go18.go | 21 +++++++++++++++++++++ handlers_pre18.go | 7 +++++++ 3 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 handlers_go18.go create mode 100644 handlers_pre18.go diff --git a/handlers.go b/handlers.go index 9544d2f..551f8ff 100644 --- a/handlers.go +++ b/handlers.go @@ -94,7 +94,7 @@ func makeLogger(w http.ResponseWriter) loggingResponseWriter { return logger } -type loggingResponseWriter interface { +type commonLoggingResponseWriter interface { http.ResponseWriter http.Flusher Status() int diff --git a/handlers_go18.go b/handlers_go18.go new file mode 100644 index 0000000..35eb8d4 --- /dev/null +++ b/handlers_go18.go @@ -0,0 +1,21 @@ +// +build go1.8 + +package handlers + +import ( + "fmt" + "net/http" +) + +type loggingResponseWriter interface { + commonLoggingResponseWriter + http.Pusher +} + +func (l *responseLogger) Push(target string, opts *http.PushOptions) error { + p, ok := l.w.(http.Pusher) + if !ok { + return fmt.Errorf("responseLogger does not implement http.Pusher") + } + return p.Push(target, opts) +} diff --git a/handlers_pre18.go b/handlers_pre18.go new file mode 100644 index 0000000..197836a --- /dev/null +++ b/handlers_pre18.go @@ -0,0 +1,7 @@ +// +build !go1.8 + +package handlers + +type loggingResponseWriter interface { + commonLoggingResponseWriter +} From 602b3261615a76d00a2ffb2114d11ee71240bd6a Mon Sep 17 00:00:00 2001 From: Niels Widger Date: Thu, 9 Feb 2017 15:42:00 -0500 Subject: [PATCH 2/2] (Combined)LoggingHandler: Add tests for http.Push support Added two tests TestLoggingHandlerWithPush and TestCombinedLoggingHandlerWithPush in new handlers_go18_test.go file which is only built with Go 1.8 or higher. Tests ensure that the ResponseWriter passed to a handler wrapped with handlers.LoggingHandler or handlers.CombinedLoggingHandler satisfy the new http.Pusher interface. --- handlers_go18_test.go | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 handlers_go18_test.go diff --git a/handlers_go18_test.go b/handlers_go18_test.go new file mode 100644 index 0000000..c8cfa72 --- /dev/null +++ b/handlers_go18_test.go @@ -0,0 +1,34 @@ +// +build go1.8 + +package handlers + +import ( + "io/ioutil" + "net/http" + "net/http/httptest" + "testing" +) + +func TestLoggingHandlerWithPush(t *testing.T) { + handler := http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { + if _, ok := w.(http.Pusher); !ok { + t.Fatalf("%T from LoggingHandler does not satisfy http.Pusher interface when built with Go >=1.8", w) + } + w.WriteHeader(200) + }) + + logger := LoggingHandler(ioutil.Discard, handler) + logger.ServeHTTP(httptest.NewRecorder(), newRequest("GET", "/")) +} + +func TestCombinedLoggingHandlerWithPush(t *testing.T) { + handler := http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { + if _, ok := w.(http.Pusher); !ok { + t.Fatalf("%T from CombinedLoggingHandler does not satisfy http.Pusher interface when built with Go >=1.8", w) + } + w.WriteHeader(200) + }) + + logger := CombinedLoggingHandler(ioutil.Discard, handler) + logger.ServeHTTP(httptest.NewRecorder(), newRequest("GET", "/")) +}