From f84b819055e03b237e75190b4be35ef49bea04ea Mon Sep 17 00:00:00 2001 From: Daniel Sage Date: Wed, 3 May 2023 02:39:30 -0400 Subject: [PATCH] Force writing headers when inspecting response/error status codes See https://github.com/gin-gonic/gin/issues/1120 for more info --- helpers_test.go | 8 ++++++-- test_helpers.go | 4 ++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/helpers_test.go b/helpers_test.go index 0c8bfaa..99b8eaf 100644 --- a/helpers_test.go +++ b/helpers_test.go @@ -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)) } @@ -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)) } diff --git a/test_helpers.go b/test_helpers.go index ee58e0d..67e87a7 100644 --- a/test_helpers.go +++ b/test_helpers.go @@ -10,6 +10,8 @@ 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 } @@ -17,6 +19,8 @@ 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 }