-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
HTTPHandler: Expose http.ResponseWriter optional interfaces (#18)
* checkpoint * Expose all optional http.ResponseWriter interfaces * http.Pusher only exists in go1.8+ * Add Go 1.9 to CI
- Loading branch information
Showing
7 changed files
with
530 additions
and
30 deletions.
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
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,90 @@ | ||
package stats | ||
|
||
import ( | ||
"io" | ||
"net/http" | ||
"net/http/httptest" | ||
"strconv" | ||
"strings" | ||
"sync" | ||
"testing" | ||
) | ||
|
||
func TestHttpHandler_ServeHTTP(t *testing.T) { | ||
t.Parallel() | ||
|
||
sink := NewMockSink() | ||
store := NewStore(sink, false) | ||
|
||
h := NewStatHandler( | ||
store, | ||
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
if code, err := strconv.Atoi(r.Header.Get("code")); err == nil { | ||
w.WriteHeader(code) | ||
} | ||
|
||
io.Copy(w, r.Body) | ||
r.Body.Close() | ||
})).(*httpHandler) | ||
|
||
wg := sync.WaitGroup{} | ||
wg.Add(2) | ||
|
||
go func() { | ||
r, _ := http.NewRequest(http.MethodGet, "/", strings.NewReader("foo")) | ||
w := httptest.NewRecorder() | ||
h.ServeHTTP(w, r) | ||
store.Flush() | ||
|
||
if w.Body.String() != "foo" { | ||
t.Errorf("wanted %q body, got %q", "foo", w.Body.String()) | ||
} | ||
|
||
if w.Code != http.StatusOK { | ||
t.Errorf("wanted 200, got %d", w.Code) | ||
} | ||
|
||
wg.Done() | ||
}() | ||
|
||
go func() { | ||
r := httptest.NewRequest(http.MethodGet, "/", strings.NewReader("bar")) | ||
r.Header.Set("code", strconv.Itoa(http.StatusNotFound)) | ||
w := httptest.NewRecorder() | ||
h.ServeHTTP(w, r) | ||
store.Flush() | ||
|
||
if w.Body.String() != "bar" { | ||
t.Errorf("wanted %q body, got %q", "bar", w.Body.String()) | ||
} | ||
|
||
if w.Code != http.StatusNotFound { | ||
t.Errorf("wanted 404, got %d", w.Code) | ||
} | ||
|
||
wg.Done() | ||
}() | ||
|
||
wg.Wait() | ||
|
||
timer, ok := sink.Timers[requestTimer] | ||
if !ok { | ||
t.Errorf("wanted a %q timer, none found", requestTimer) | ||
} else if timer != 2 { | ||
t.Error("wanted 2, got", timer) | ||
} | ||
|
||
c, ok := sink.Counters["200"] | ||
if !ok { | ||
t.Error("wanted a '200' counter, none found") | ||
} else if c != 1 { | ||
t.Error("wanted 1, got", c) | ||
} | ||
|
||
c, ok = sink.Counters["404"] | ||
if !ok { | ||
t.Error("wanted a '404' counter, none found") | ||
} else if c != 1 { | ||
t.Error("wanted 1, got", c) | ||
} | ||
} |
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,113 @@ | ||
// +build go1.8 | ||
|
||
package stats | ||
|
||
import "net/http" | ||
|
||
func (h *httpHandler) wrapResponse(w http.ResponseWriter) http.ResponseWriter { | ||
rw := &responseWriter{ | ||
ResponseWriter: w, | ||
handler: h, | ||
} | ||
|
||
flusher, canFlush := w.(http.Flusher) | ||
hijacker, canHijack := w.(http.Hijacker) | ||
pusher, canPush := w.(http.Pusher) | ||
closeNotifier, canNotify := w.(http.CloseNotifier) | ||
|
||
if canFlush && canHijack && canPush && canNotify { | ||
return struct { | ||
http.ResponseWriter | ||
http.Flusher | ||
http.Hijacker | ||
http.Pusher | ||
http.CloseNotifier | ||
}{rw, flusher, hijacker, pusher, closeNotifier} | ||
} else if canFlush && canHijack && canPush { | ||
return struct { | ||
http.ResponseWriter | ||
http.Flusher | ||
http.Hijacker | ||
http.Pusher | ||
}{rw, flusher, hijacker, pusher} | ||
} else if canFlush && canHijack && canNotify { | ||
return struct { | ||
http.ResponseWriter | ||
http.Flusher | ||
http.Hijacker | ||
http.CloseNotifier | ||
}{rw, flusher, hijacker, closeNotifier} | ||
} else if canFlush && canPush && canNotify { | ||
return struct { | ||
http.ResponseWriter | ||
http.Flusher | ||
http.Pusher | ||
http.CloseNotifier | ||
}{rw, flusher, pusher, closeNotifier} | ||
} else if canHijack && canPush && canNotify { | ||
return struct { | ||
http.ResponseWriter | ||
http.Hijacker | ||
http.Pusher | ||
http.CloseNotifier | ||
}{rw, hijacker, pusher, closeNotifier} | ||
} else if canFlush && canHijack { | ||
return struct { | ||
http.ResponseWriter | ||
http.Flusher | ||
http.Hijacker | ||
}{rw, flusher, hijacker} | ||
} else if canFlush && canPush { | ||
return struct { | ||
http.ResponseWriter | ||
http.Flusher | ||
http.Pusher | ||
}{rw, flusher, pusher} | ||
} else if canFlush && canNotify { | ||
return struct { | ||
http.ResponseWriter | ||
http.Flusher | ||
http.CloseNotifier | ||
}{rw, flusher, closeNotifier} | ||
} else if canHijack && canPush { | ||
return struct { | ||
http.ResponseWriter | ||
http.Hijacker | ||
http.Pusher | ||
}{rw, hijacker, pusher} | ||
} else if canHijack && canNotify { | ||
return struct { | ||
http.ResponseWriter | ||
http.Hijacker | ||
http.CloseNotifier | ||
}{rw, hijacker, closeNotifier} | ||
} else if canPush && canNotify { | ||
return struct { | ||
http.ResponseWriter | ||
http.Pusher | ||
http.CloseNotifier | ||
}{rw, pusher, closeNotifier} | ||
} else if canFlush { | ||
return struct { | ||
http.ResponseWriter | ||
http.Flusher | ||
}{rw, flusher} | ||
} else if canHijack { | ||
return struct { | ||
http.ResponseWriter | ||
http.Hijacker | ||
}{rw, hijacker} | ||
} else if canPush { | ||
return struct { | ||
http.ResponseWriter | ||
http.Pusher | ||
}{rw, pusher} | ||
} else if canNotify { | ||
return struct { | ||
http.ResponseWriter | ||
http.CloseNotifier | ||
}{rw, closeNotifier} | ||
} | ||
|
||
return rw | ||
} |
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,60 @@ | ||
// +build !go1.8 | ||
|
||
package stats | ||
|
||
import "net/http" | ||
|
||
func (h *httpHandler) wrapResponse(w http.ResponseWriter) http.ResponseWriter { | ||
rw := &responseWriter{ | ||
ResponseWriter: w, | ||
handler: h, | ||
} | ||
|
||
flusher, canFlush := w.(http.Flusher) | ||
hijacker, canHijack := w.(http.Hijacker) | ||
closeNotifier, canNotify := w.(http.CloseNotifier) | ||
|
||
if canFlush && canHijack && canNotify { | ||
return struct { | ||
http.ResponseWriter | ||
http.Flusher | ||
http.Hijacker | ||
http.CloseNotifier | ||
}{rw, flusher, hijacker, closeNotifier} | ||
} else if canFlush && canHijack { | ||
return struct { | ||
http.ResponseWriter | ||
http.Flusher | ||
http.Hijacker | ||
}{rw, flusher, hijacker} | ||
} else if canFlush && canNotify { | ||
return struct { | ||
http.ResponseWriter | ||
http.Flusher | ||
http.CloseNotifier | ||
}{rw, flusher, closeNotifier} | ||
} else if canHijack && canNotify { | ||
return struct { | ||
http.ResponseWriter | ||
http.Hijacker | ||
http.CloseNotifier | ||
}{rw, hijacker, closeNotifier} | ||
} else if canFlush { | ||
return struct { | ||
http.ResponseWriter | ||
http.Flusher | ||
}{rw, flusher} | ||
} else if canHijack { | ||
return struct { | ||
http.ResponseWriter | ||
http.Hijacker | ||
}{rw, hijacker} | ||
} else if canNotify { | ||
return struct { | ||
http.ResponseWriter | ||
http.CloseNotifier | ||
}{rw, closeNotifier} | ||
} | ||
|
||
return rw | ||
} |
Oops, something went wrong.