Skip to content

Commit

Permalink
console logger HTTP status bug fixed and the corresponding unit test …
Browse files Browse the repository at this point in the history
…added (#3453)
  • Loading branch information
alirezaeftekhari authored Jan 2, 2023
1 parent 82e1c53 commit 41f2669
Show file tree
Hide file tree
Showing 2 changed files with 19 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 @@ -61,6 +61,7 @@ func (w *responseWriter) WriteHeader(code int) {
if code > 0 && w.status != code {
if w.Written() {
debugPrint("[WARNING] Headers were already written. Wanted to override status code %d with %d", w.status, code)
return
}
w.status = code
}
Expand Down
18 changes: 18 additions & 0 deletions response_writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,3 +132,21 @@ func TestResponseWriterFlush(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, http.StatusInternalServerError, resp.StatusCode)
}

func TestResponseWriterStatusCode(t *testing.T) {
testWriter := httptest.NewRecorder()
writer := &responseWriter{}
writer.reset(testWriter)
w := ResponseWriter(writer)

w.WriteHeader(http.StatusOK)
w.WriteHeaderNow()

assert.Equal(t, http.StatusOK, w.Status())
assert.True(t, w.Written())

w.WriteHeader(http.StatusUnauthorized)

// status must be 200 although we tried to change it
assert.Equal(t, http.StatusOK, w.Status())
}

0 comments on commit 41f2669

Please sign in to comment.