Compare commits

...

3 Commits

Author SHA1 Message Date
David Desmarais-Michaud c1d06e3d08
add supprt for go1.20 http.rwUnwrapper to gin.responseWriter (#3489) 2023-02-12 13:01:43 +08:00
mstmdev bd82c9e351
chore(go): Add support go 1.20 (#3484)
* chore(go): Add support go 1.20

* Surround the go version parameters with single quotes

* chore(deps): bump github.com/bytedance/sonic from v1.7.0 to v1.7.1
2023-02-12 13:01:05 +08:00
Vladislav Dmitriyev 0c96a20209
Stop useless panicking in context and render (#2150)
Co-authored-by: Bo-Yi Wu <appleboy.tw@gmail.com>
2023-02-12 10:01:33 +08:00
9 changed files with 30 additions and 23 deletions

View File

@ -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

View File

@ -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()
}
}

View File

@ -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
View File

@ -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
View File

@ -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=

View File

@ -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.

View File

@ -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) {

View File

@ -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

View File

@ -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{}