gin-gonic / gin

Gin is a HTTP web framework written in Go (Golang). It features a Martini-like API with much better performance -- up to 40 times faster. If you need smashing performance, get yourself some Gin.
https://gin-gonic.com/
MIT License
78.81k stars 8.02k forks source link

ctx.Status() does not affect httptest.ResponseRecorder while ctx.JSON() does #4071

Open cardillomarcelo opened 3 weeks ago

cardillomarcelo commented 3 weeks ago

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

JimChenWYU commented 3 weeks ago
image image

c.Status(204) do not change the recorder.status but change responseWriter.status

image