ctx.Status() does not affect httptest.ResponseRecorder while ctx.JSON() does
Author: cardillomarceloCreated Oct 9, 2024Updated Jun 22, 2026
Description
ctx.Status() does not affect httptest.ResponseRecorder while ctx.JSON() does
How to reproduce
package main
import (
"fmt"
"github.com/gin-gonic/gin"
"net/http"
"net/http/httptest"
"testing"
)
func TestName(t *testing.T) {
recorder := httptest.NewRecorder()
c, _ := gin.CreateTestContext(recorder)
changeResponseCode(c)
fmt.Println(recorder.Code)
}
func changeResponseCode(ctx *gin.Context) {
ctx.Status(http.StatusNoContent)
}
Expectations
Should print 204
Actual result
Prints 200
Environment
- go version: 1.23.2
- gin version (or commit ref): 1.10.0
- operating system: Ubuntu 22.04
Source: gin-gonic/gin