Open baytan0720 opened 1 year ago
I think I have a good understanding of what caused the error. Normally, in the gin.go file at line 621, Gin would call c.Writer.WriteHeaderNow()
once. When calling methods like c.JSON
, c.String
and so on, which all call c.Writer.Write()
, they would first call c.Writer.WriteHeaderNow()
. In this case, there were at least two calls.This is why only c.Status()
did not work.
This is my first time participating in an open-source project, and I'm very eager to contribute to gin. Doing so would give me the confidence to continue working on open-source projects. If possible, could this issue be assigned to me?
I believe that the handler is functioning as intended. Instead retrieving the value of rec.Code
, which hasn't been updated in the call to your handler function, you should get c.Writer.Status()
, which will return 204
, the expected status code.
With that considerations, the updated function:
func TestHandler() {
rec := httptest.NewRecorder()
c, _ := gin.CreateTestContext(rec)
handler(c)
fmt.Println(c.Writer.Status()) // updated line
}
Outputs 204
.
Note that your handler function does not update the object httptest.NewRecorder()
with the new status code, only the context.
Description
I am testing the correctness of my code. There is a handler called
func XXXHandler(c *gin.Context)
with simple internal logic that callsc.Status(204)
at the end. When I usegin.CreateTestContext(httptest.NewRecorder())
to create a ctx and pass it into the handler, the value ofrecorder.Code
after the call does not meet my expectations. I then proceeded to test other status codes and confirmed that thec.Status()
function is not working correctly in this environment.How to reproduce
Expectations
204
Actual result
200
Environment