Skip to content

Commit

Permalink
flush operation will overwrite the origin status code
Browse files Browse the repository at this point in the history
  • Loading branch information
congcongke committed Aug 7, 2018
1 parent 647535c commit 4a6eea7
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
1 change: 1 addition & 0 deletions response_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
16 changes: 16 additions & 0 deletions response_writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

0 comments on commit 4a6eea7

Please sign in to comment.