New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Wrong status code returned when using httptest.NewRecorder #1120
Comments
Because testing handler didn't through package main
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/gin-gonic/gin"
)
func TestFailStatus(t *testing.T) {
gin.SetMode(gin.TestMode)
router := gin.New()
res:=performRequest("GET","/fail1",router)
if res.Code != http.StatusNotFound { // Should be 404
t.Fatalf("Expecting status to be 404 got %v", res.Code)
}
}
func TestFailJson(t *testing.T) {
gin.SetMode(gin.TestMode)
router := gin.New()
res:=performRequest("GET","/fail2",router)
if res.Code != http.StatusNotFound { // Should be 404
t.Fatalf("Expecting status to be 404 got %v", res.Code)
}
}
func performRequest(method, target string, router *gin.Engine) *httptest.ResponseRecorder {
r := httptest.NewRequest(method, target, nil)
w := httptest.NewRecorder()
router.ServeHTTP(w, r)
return w
} |
Hi @easonlin404, thanks for your answer. Correct me if I'm wrong, but in this case I would be testing the handler and the engine and not only the handler. In a real project I would have a struct that it's instantiated by injecting dependencies (when testing mocks) and then the a Handler that is called. What I want to test is the logic of my handler, and not the routing/engine/middlewares/handler combination (that would be another kind of test I think). Either way, the thing that I consider a bug is that |
I got the same issue error in testing context.
|
Bumping as error is still reproducible and discussion has been silent. 😄 Any update on the behavior of |
This bug is still present. Setting c.Status(204) does not result in the responserecorder's code being updated. My workaround at the moment is just getting the status from c.Writer.Status() in tests. |
This, I think, is the best workaround available. |
Another option is to "force" a flush in the tests with a
|
Do we have a plan to solve this ? w.Result().StatusCode will always return 200. Thank you for workaround propose above to use c.Writer.Status() |
After 5 years since the issue opened, the same issue still occurs. Using workaround mentioned above. (c.Writer.Status()) |
Still having this problem... did a workaround in the tests like this: if ctx.Writer.Status() != res.Code {
ctx.Writer.WriteHeaderNow()
} |
Issue still happing in 2024... |
I think I found the [solution] (https://stackoverflow.com/questions/77951149/error-on-testing-gingko-and-gomega-for-gingonic-w-code-not-working/77957924#77957924) It seems that the This seemed to fix the issue: req, err := http.NewRequest(http.MethodPut, "/v1/create-user", bytes.NewReader(body))
Expect(err).NotTo(HaveOccurred())
req.Header.Set("Content-Type", "application/json")
w := httptest.NewRecorder() // <- creates a new response recorder
router.ServeHTTP(w, req)
Ω(w.Code).Should(Equal(http.StatusBadGateway)) This way the test runs successfully. Note that i am NOT using |
When testing a gin handler using a
httptest.NewRecorder
, when using (inside the handler)c.Status(404)
to set the status code, and then bailing out of the request, then the status from the recorder is wrongly returned as200
.On the other hand if we use
c.JSON(404, gin.H{})
then the status code inside the recorder is correct.The expected behavior is of course to always receive in the recorder the status that was set inside the handler.
Example code reproducing the bug bellow, both tests are expected to PASS but only the one using JSON does.
Having a
main.go
with the following code:And a
main_test.go
:The text was updated successfully, but these errors were encountered: