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_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", "/")) +} 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 +}