-
Notifications
You must be signed in to change notification settings - Fork 272
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #97 from nwidger/master
(Combined)LoggingHandler: Add Go 1.8 http.Pusher support
- Loading branch information
Showing
4 changed files
with
63 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", "/")) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
// +build !go1.8 | ||
|
||
package handlers | ||
|
||
type loggingResponseWriter interface { | ||
commonLoggingResponseWriter | ||
} |