Skip to content

Commit

Permalink
Force writing headers when inspecting response/error status codes
Browse files Browse the repository at this point in the history
See gin-gonic/gin#1120 for more info
  • Loading branch information
dnsge committed May 3, 2023
1 parent fdddb33 commit f84b819
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
8 changes: 6 additions & 2 deletions helpers_test.go
Expand Up @@ -7,7 +7,9 @@ import (
)

func TestGetResStatus(t *testing.T) {
res := Status(http.StatusCreated)
res := RawStatus(http.StatusCreated)
assert.Equal(t, http.StatusCreated, GetResStatus(res))
res = Status(http.StatusCreated)
assert.Equal(t, http.StatusCreated, GetResStatus(res))
}

Expand All @@ -17,7 +19,9 @@ func TestGetResBody(t *testing.T) {
}

func TestGetErrStatus(t *testing.T) {
err := ErrorStatus(http.StatusInternalServerError)
err := RawErrorStatus(http.StatusInternalServerError)
assert.Equal(t, http.StatusInternalServerError, GetErrStatus(err))
err = ErrorStatus(http.StatusInternalServerError)
assert.Equal(t, http.StatusInternalServerError, GetErrStatus(err))
}

Expand Down
4 changes: 4 additions & 0 deletions test_helpers.go
Expand Up @@ -10,13 +10,17 @@ func execRes(res Res) *httptest.ResponseRecorder {
recorder := httptest.NewRecorder()
c, _ := gin.CreateTestContext(recorder)
res.WriteResponse(c)
// ref: https://github.com/gin-gonic/gin/issues/1120
c.Writer.WriteHeaderNow()
return recorder
}

func execErr(err Err) *httptest.ResponseRecorder {
recorder := httptest.NewRecorder()
c, _ := gin.CreateTestContext(recorder)
err.WriteError(c)
// ref: https://github.com/gin-gonic/gin/issues/1120
c.Writer.WriteHeaderNow()
return recorder
}

Expand Down

0 comments on commit f84b819

Please sign in to comment.