Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(Combined)LoggingHandler: Add Go 1.8 http.Pusher support #97

Merged
merged 2 commits into from
Feb 9, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func makeLogger(w http.ResponseWriter) loggingResponseWriter {
return logger
}

type loggingResponseWriter interface {
type commonLoggingResponseWriter interface {
http.ResponseWriter
http.Flusher
Status() int
Expand Down
21 changes: 21 additions & 0 deletions handlers_go18.go
Original file line number Diff line number Diff line change
@@ -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)
}
34 changes: 34 additions & 0 deletions handlers_go18_test.go
Original file line number Diff line number Diff line change
@@ -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", "/"))
}
7 changes: 7 additions & 0 deletions handlers_pre18.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// +build !go1.8

package handlers

type loggingResponseWriter interface {
commonLoggingResponseWriter
}