Skip to content

Commit 0552c3b

Browse files
congcongkeappleboy
authored andcommittedAug 7, 2018
flush operation will overwrite the origin status code (#1460)
The status of responseWriter will be overwrite if flush was called. This is caused by the Flush of http.response.Flush().
1 parent 9b7e7bd commit 0552c3b

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed
 

‎response_writer.go

+1
Original file line numberDiff line numberDiff line change
@@ -110,5 +110,6 @@ func (w *responseWriter) CloseNotify() <-chan bool {
110110

111111
// Flush implements the http.Flush interface.
112112
func (w *responseWriter) Flush() {
113+
w.WriteHeaderNow()
113114
w.ResponseWriter.(http.Flusher).Flush()
114115
}

‎response_writer_test.go

+16
Original file line numberDiff line numberDiff line change
@@ -113,3 +113,19 @@ func TestResponseWriterHijack(t *testing.T) {
113113

114114
w.Flush()
115115
}
116+
117+
func TestResponseWriterFlush(t *testing.T) {
118+
testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
119+
writer := &responseWriter{}
120+
writer.reset(w)
121+
122+
writer.WriteHeader(http.StatusInternalServerError)
123+
writer.Flush()
124+
}))
125+
defer testServer.Close()
126+
127+
// should return 500
128+
resp, err := http.Get(testServer.URL)
129+
assert.NoError(t, err)
130+
assert.Equal(t, http.StatusInternalServerError, resp.StatusCode)
131+
}

0 commit comments

Comments
 (0)
Please sign in to comment.