mirror of https://github.com/gin-gonic/gin.git
Compare commits
3 Commits
153b229fcc
...
c1d06e3d08
Author | SHA1 | Date |
---|---|---|
David Desmarais-Michaud | c1d06e3d08 | |
mstmdev | bd82c9e351 | |
Vladislav Dmitriyev | 0c96a20209 |
|
@ -31,7 +31,7 @@ jobs:
|
|||
strategy:
|
||||
matrix:
|
||||
os: [ubuntu-latest, macos-latest]
|
||||
go: [1.16, 1.17, 1.18, 1.19]
|
||||
go: ['1.16', '1.17', '1.18', '1.19', '1.20']
|
||||
test-tags: ['', '-tags nomsgpack', '-tags "sonic avx"', '-tags go_json']
|
||||
include:
|
||||
- os: ubuntu-latest
|
||||
|
|
|
@ -924,7 +924,9 @@ func (c *Context) Render(code int, r render.Render) {
|
|||
}
|
||||
|
||||
if err := r.Render(c.Writer); err != nil {
|
||||
panic(err)
|
||||
// Pushing error to c.Errors
|
||||
_ = c.Error(err)
|
||||
c.Abort()
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -32,6 +32,8 @@ import (
|
|||
|
||||
var _ context.Context = (*Context)(nil)
|
||||
|
||||
var errTestRender = errors.New("TestRender")
|
||||
|
||||
// Unit tests TODO
|
||||
// func (c *Context) File(filepath string) {
|
||||
// func (c *Context) Negotiate(code int, config Negotiate) {
|
||||
|
@ -643,25 +645,21 @@ func TestContextBodyAllowedForStatus(t *testing.T) {
|
|||
assert.True(t, true, bodyAllowedForStatus(http.StatusInternalServerError))
|
||||
}
|
||||
|
||||
type TestPanicRender struct{}
|
||||
type TestRender struct{}
|
||||
|
||||
func (*TestPanicRender) Render(http.ResponseWriter) error {
|
||||
return errors.New("TestPanicRender")
|
||||
func (*TestRender) Render(http.ResponseWriter) error {
|
||||
return errTestRender
|
||||
}
|
||||
|
||||
func (*TestPanicRender) WriteContentType(http.ResponseWriter) {}
|
||||
func (*TestRender) WriteContentType(http.ResponseWriter) {}
|
||||
|
||||
func TestContextRenderPanicIfErr(t *testing.T) {
|
||||
defer func() {
|
||||
r := recover()
|
||||
assert.Equal(t, "TestPanicRender", fmt.Sprint(r))
|
||||
}()
|
||||
func TestContextRenderIfErr(t *testing.T) {
|
||||
w := httptest.NewRecorder()
|
||||
c, _ := CreateTestContext(w)
|
||||
|
||||
c.Render(http.StatusOK, &TestPanicRender{})
|
||||
c.Render(http.StatusOK, &TestRender{})
|
||||
|
||||
assert.Fail(t, "Panic not detected")
|
||||
assert.Equal(t, errorMsgs{&Error{Err: errTestRender, Type: 1}}, c.Errors)
|
||||
}
|
||||
|
||||
// Tests that the response is serialized as JSON
|
||||
|
|
2
go.mod
2
go.mod
|
@ -3,7 +3,7 @@ module github.com/gin-gonic/gin
|
|||
go 1.18
|
||||
|
||||
require (
|
||||
github.com/bytedance/sonic v1.7.0
|
||||
github.com/bytedance/sonic v1.7.1
|
||||
github.com/gin-contrib/sse v0.1.0
|
||||
github.com/go-playground/validator/v10 v10.11.2
|
||||
github.com/goccy/go-json v0.10.0
|
||||
|
|
4
go.sum
4
go.sum
|
@ -1,6 +1,6 @@
|
|||
github.com/bytedance/sonic v1.5.0/go.mod h1:ED5hyg4y6t3/9Ku1R6dU/4KyJ48DZ4jPhfY1O2AihPM=
|
||||
github.com/bytedance/sonic v1.7.0 h1:P7DyGrkLbVDzcuqagPsSFnAwwljjhmB3qVF5wzmHOxE=
|
||||
github.com/bytedance/sonic v1.7.0/go.mod h1:i736AoUSYt75HyZLoJW9ERYxcy6eaN6h4BZXU064P/U=
|
||||
github.com/bytedance/sonic v1.7.1 h1:UYWEKUHQDye89c2U6zvrvuxWdGCI/wCrZITFQmKGtGc=
|
||||
github.com/bytedance/sonic v1.7.1/go.mod h1:i736AoUSYt75HyZLoJW9ERYxcy6eaN6h4BZXU064P/U=
|
||||
github.com/chenzhuoyu/base64x v0.0.0-20211019084208-fb5309c8db06/go.mod h1:DH46F32mSOjUmXrMHnKwZdA8wcEefY7UVqBKYGjpdQY=
|
||||
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 h1:qSGYFH7+jGhDF8vLC+iwCD4WpbV1EBDSzWkJODFLams=
|
||||
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311/go.mod h1:b583jCggY9gE99b6G5LEC39OIiVsWj+R97kbl5odCEk=
|
||||
|
|
|
@ -53,11 +53,8 @@ var (
|
|||
)
|
||||
|
||||
// Render (JSON) writes data with custom ContentType.
|
||||
func (r JSON) Render(w http.ResponseWriter) (err error) {
|
||||
if err = WriteJSON(w, r.Data); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return
|
||||
func (r JSON) Render(w http.ResponseWriter) error {
|
||||
return WriteJSON(w, r.Data)
|
||||
}
|
||||
|
||||
// WriteContentType (JSON) writes JSON ContentType.
|
||||
|
|
|
@ -40,12 +40,12 @@ func TestRenderJSON(t *testing.T) {
|
|||
assert.Equal(t, "application/json; charset=utf-8", w.Header().Get("Content-Type"))
|
||||
}
|
||||
|
||||
func TestRenderJSONPanics(t *testing.T) {
|
||||
func TestRenderJSONError(t *testing.T) {
|
||||
w := httptest.NewRecorder()
|
||||
data := make(chan int)
|
||||
|
||||
// json: unsupported type: chan int
|
||||
assert.Panics(t, func() { assert.NoError(t, (JSON{data}).Render(w)) })
|
||||
assert.Error(t, (JSON{data}).Render(w))
|
||||
}
|
||||
|
||||
func TestRenderIndentedJSON(t *testing.T) {
|
||||
|
|
|
@ -51,6 +51,10 @@ type responseWriter struct {
|
|||
|
||||
var _ ResponseWriter = (*responseWriter)(nil)
|
||||
|
||||
func (w *responseWriter) Unwrap() http.ResponseWriter {
|
||||
return w.ResponseWriter
|
||||
}
|
||||
|
||||
func (w *responseWriter) reset(writer http.ResponseWriter) {
|
||||
w.ResponseWriter = writer
|
||||
w.size = noWritten
|
||||
|
|
|
@ -30,6 +30,12 @@ func init() {
|
|||
SetMode(TestMode)
|
||||
}
|
||||
|
||||
func TestResponseWriterUnwrap(t *testing.T) {
|
||||
testWriter := httptest.NewRecorder()
|
||||
writer := &responseWriter{ResponseWriter: testWriter}
|
||||
assert.Same(t, testWriter, writer.Unwrap())
|
||||
}
|
||||
|
||||
func TestResponseWriterReset(t *testing.T) {
|
||||
testWriter := httptest.NewRecorder()
|
||||
writer := &responseWriter{}
|
||||
|
|
Loading…
Reference in New Issue