mirror of
https://github.com/gin-gonic/gin.git
synced 2026-09-04 14:49:27 +08:00
bugfix #1019 - fixed testcases for different json libs.
This commit is contained in:
parent
ba32475d00
commit
4ef53ff26d
@ -18,14 +18,26 @@ import (
|
||||
"go.mongodb.org/mongo-driver/v2/bson"
|
||||
)
|
||||
|
||||
// maxBytesErrorPropagates reports whether the active JSON backend propagates
|
||||
// *http.MaxBytesError through Decode()/Unmarshal(). goccy/go-json normalizes
|
||||
// a MaxBytesReader read failure into a generic EOF, discarding the original
|
||||
// error (see the caveat in context.go's MustBindWith and
|
||||
// https://github.com/goccy/go-json/issues/485). bytedance/sonic was reported
|
||||
// to have the same issue (https://github.com/bytedance/sonic/issues/800), but
|
||||
// that no longer reproduces with the sonic version currently pinned in
|
||||
// go.mod — verified locally via `go test -tags sonic`.
|
||||
func maxBytesErrorPropagates() bool {
|
||||
return json.Package != "github.com/goccy/go-json"
|
||||
}
|
||||
|
||||
// expectedTooLargeStatus mirrors the go-json caveat documented in
|
||||
// TestContextBindRequestTooLarge: go-json does not propagate
|
||||
// *http.MaxBytesError, so the response falls back to a generic 400.
|
||||
func expectedTooLargeStatus() int {
|
||||
if json.Package == "github.com/goccy/go-json" {
|
||||
return http.StatusBadRequest
|
||||
if maxBytesErrorPropagates() {
|
||||
return http.StatusRequestEntityTooLarge
|
||||
}
|
||||
return http.StatusRequestEntityTooLarge
|
||||
return http.StatusBadRequest
|
||||
}
|
||||
|
||||
// --- unit-level: limitRequestBody branch coverage ---
|
||||
@ -112,7 +124,7 @@ func TestBindExceedsBodyLimit(t *testing.T) {
|
||||
require.Error(t, err)
|
||||
c.Writer.WriteHeaderNow()
|
||||
|
||||
if json.Package != "github.com/goccy/go-json" {
|
||||
if maxBytesErrorPropagates() {
|
||||
var maxBytesErr *http.MaxBytesError
|
||||
require.ErrorAs(t, err, &maxBytesErr)
|
||||
}
|
||||
@ -200,8 +212,11 @@ func TestBindBodyLimitAcrossFormats(t *testing.T) {
|
||||
|
||||
err := c.ShouldBindWith(tt.target(), tt.b)
|
||||
require.Error(t, err)
|
||||
var maxBytesErr *http.MaxBytesError
|
||||
require.ErrorAs(t, err, &maxBytesErr)
|
||||
|
||||
if tt.name != "json" || maxBytesErrorPropagates() {
|
||||
var maxBytesErr *http.MaxBytesError
|
||||
require.ErrorAs(t, err, &maxBytesErr)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@ -29,7 +29,6 @@ import (
|
||||
|
||||
"github.com/gin-contrib/sse"
|
||||
"github.com/gin-gonic/gin/binding"
|
||||
"github.com/gin-gonic/gin/codec/json"
|
||||
testdata "github.com/gin-gonic/gin/testdata/protoexample"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
@ -2221,15 +2220,12 @@ func TestContextContentType(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestContextBindRequestTooLarge(t *testing.T) {
|
||||
// When using go-json as JSON encoder, they do not propagate the http.MaxBytesError error
|
||||
// The response will fail with a generic 400 instead of 413
|
||||
// https://github.com/goccy/go-json/issues/485
|
||||
var expectedCode int
|
||||
switch json.Package {
|
||||
case "github.com/goccy/go-json":
|
||||
// When using go-json as JSON encoder, it does not propagate the
|
||||
// http.MaxBytesError error, so the response falls back to a generic 400
|
||||
// instead of 413. See maxBytesErrorPropagates in body_limit_test.go.
|
||||
expectedCode := http.StatusRequestEntityTooLarge
|
||||
if !maxBytesErrorPropagates() {
|
||||
expectedCode = http.StatusBadRequest
|
||||
default:
|
||||
expectedCode = http.StatusRequestEntityTooLarge
|
||||
}
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user