flush operation will overwrite the origin status code

This commit is contained in:
congcongke 2018-08-06 19:25:51 +08:00
parent 647535cd9b
commit 4a6eea71b7
2 changed files with 17 additions and 0 deletions

View File

@ -110,5 +110,6 @@ func (w *responseWriter) CloseNotify() <-chan bool {
// Flush implements the http.Flush interface.
func (w *responseWriter) Flush() {
w.WriteHeaderNow()
w.ResponseWriter.(http.Flusher).Flush()
}

View File

@ -113,3 +113,19 @@ func TestResponseWriterHijack(t *testing.T) {
w.Flush()
}
func TestResponseWriterFlush(t *testing.T) {
testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
writer := &responseWriter{}
writer.reset(w)
writer.WriteHeader(http.StatusInternalServerError)
writer.Flush()
}))
defer testServer.Close()
// should return 500
resp, err := http.Get(testServer.URL)
assert.NoError(t, err)
assert.Equal(t, http.StatusInternalServerError, resp.StatusCode)
}