Open cardillomarcelo opened 1 month ago
c.Status(204)
do not change the recorder.status
but change responseWriter.status
It seems that ctx.Status()
In the function changeResponseCode
changes the Writer
in ctx
but doesn't change the recorder
. That is to say, you can fetch the status code by c.Writer.Status()
func TestName(t *testing.T) {
recorder := httptest.NewRecorder()
c, _ := gin.CreateTestContext(recorder)
changeResponseCode(c)
fmt.Println(c.Writer.Status()) // 204
fmt.Println(recorder.Code) // 200
}
Besides, I found a related unit test in response_writer_test.go
func TestResponseWriterWriteHeader(t *testing.T) {
testWriter := httptest.NewRecorder()
writer := &responseWriter{}
writer.reset(testWriter)
w := ResponseWriter(writer)
w.WriteHeader(http.StatusMultipleChoices)
assert.False(t, w.Written())
assert.Equal(t, http.StatusMultipleChoices, w.Status())
assert.NotEqual(t, http.StatusMultipleChoices, testWriter.Code) // ← WHY ?
w.WriteHeader(-1)
assert.Equal(t, http.StatusMultipleChoices, w.Status())
}
I'm not sure why it was designed so that the original testWriter
does not change along with it.
Description
ctx.Status() does not affect httptest.ResponseRecorder while ctx.JSON() does
How to reproduce
Expectations
Actual result
Environment