Test that the response is 400 for sonic and go-json

This commit is contained in:
ItalyPaleAle 2025-05-23 07:56:48 -07:00
parent bff43ed527
commit 4de0ae4795
No known key found for this signature in database
2 changed files with 10 additions and 5 deletions

View File

@ -772,6 +772,10 @@ func (c *Context) MustBindWith(obj any, b binding.Binding) error {
err := c.ShouldBindWith(obj, b) err := c.ShouldBindWith(obj, b)
if err != nil { if err != nil {
var maxBytesErr *http.MaxBytesError var maxBytesErr *http.MaxBytesError
// Note: When using sonic or go-json as JSON encoder, they do not propagate the http.MaxBytesError error
// https://github.com/goccy/go-json/issues/485
// https://github.com/bytedance/sonic/issues/800
switch { switch {
case errors.As(err, &maxBytesErr): case errors.As(err, &maxBytesErr):
c.AbortWithError(http.StatusRequestEntityTooLarge, err).SetType(ErrorTypeBind) //nolint: errcheck c.AbortWithError(http.StatusRequestEntityTooLarge, err).SetType(ErrorTypeBind) //nolint: errcheck

View File

@ -1850,15 +1850,16 @@ func TestContextContentType(t *testing.T) {
} }
func TestContextBindRequestTooLarge(t *testing.T) { func TestContextBindRequestTooLarge(t *testing.T) {
// Disabling this test when using sonic or go-json as JSON encoder because it doesn't cascade the error correctly // When using sonic or 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 // https://github.com/goccy/go-json/issues/485
// https://github.com/bytedance/sonic/issues/800 // https://github.com/bytedance/sonic/issues/800
var expectedCode int
switch json.Package { switch json.Package {
case "github.com/goccy/go-json", "github.com/bytedance/sonic": case "github.com/goccy/go-json", "github.com/bytedance/sonic":
t.Skip() expectedCode = http.StatusBadRequest
return
default: default:
// We can test expectedCode = http.StatusRequestEntityTooLarge
} }
w := httptest.NewRecorder() w := httptest.NewRecorder()
@ -1876,7 +1877,7 @@ func TestContextBindRequestTooLarge(t *testing.T) {
assert.Empty(t, obj.Bar) assert.Empty(t, obj.Bar)
assert.Empty(t, obj.Foo) assert.Empty(t, obj.Foo)
assert.Equal(t, http.StatusRequestEntityTooLarge, w.Code) assert.Equal(t, expectedCode, w.Code)
assert.True(t, c.IsAborted()) assert.True(t, c.IsAborted())
} }