Description
The gin.Context.Redirect
function panics when the gin.Context
uses httptest.ResponseRecorder
as its Writer
.
A code to check the header code(https://github.com/golang/go/commit/c3931ab1b7bceddc56479d7ddbd7517d244bfe17) was added in April 2021, but the gin.Context.Render
passes '-1' (https://github.com/gin-gonic/gin/blob/master/context.go#L976), not the code argument.
How to reproduce
package main
import (
"github.com/gin-gonic/gin"
)
type fakeGinResponseWriter struct {
httptest.ResponseRecorder
http.Hijacker
}
func main() {
ctx = &gin.Context{
Request: &http.Request{
URL: &url.URL{},
Header: http.Header{},
},
Writer: &fakeGinResponseWriter{},
}
ctx.Redirect(302, "http://foo.bar")
}
Environment
- go version: 1.17.6 darwin/amd64
- gin version (or commit ref): 1.7.7
- operating system: MacOS 12.3
Comment From: Fov6363
can change ?
func (c *Context) Redirect(code int, location string) {
c.Render(-1, render.Redirect{
Code: code,
Location: location,
Request: c.Request,
})
}
to
func (c *Context) Redirect(code int, location string) {
c.Render(code, render.Redirect{
Code: code,
Location: location,
Request: c.Request,
})
}