From 4de0ae47955e12892353f04ebc857c4fdc12c4dc Mon Sep 17 00:00:00 2001 From: ItalyPaleAle <43508+ItalyPaleAle@users.noreply.github.com> Date: Fri, 23 May 2025 07:56:48 -0700 Subject: [PATCH] Test that the response is 400 for sonic and go-json --- context.go | 4 ++++ context_test.go | 11 ++++++----- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/context.go b/context.go index dc075711..3ebb3ee4 100644 --- a/context.go +++ b/context.go @@ -772,6 +772,10 @@ func (c *Context) MustBindWith(obj any, b binding.Binding) error { err := c.ShouldBindWith(obj, b) if err != nil { 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 { case errors.As(err, &maxBytesErr): c.AbortWithError(http.StatusRequestEntityTooLarge, err).SetType(ErrorTypeBind) //nolint: errcheck diff --git a/context_test.go b/context_test.go index 650219eb..b64afba3 100644 --- a/context_test.go +++ b/context_test.go @@ -1850,15 +1850,16 @@ func TestContextContentType(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/bytedance/sonic/issues/800 + var expectedCode int switch json.Package { case "github.com/goccy/go-json", "github.com/bytedance/sonic": - t.Skip() - return + expectedCode = http.StatusBadRequest default: - // We can test + expectedCode = http.StatusRequestEntityTooLarge } w := httptest.NewRecorder() @@ -1876,7 +1877,7 @@ func TestContextBindRequestTooLarge(t *testing.T) { assert.Empty(t, obj.Bar) assert.Empty(t, obj.Foo) - assert.Equal(t, http.StatusRequestEntityTooLarge, w.Code) + assert.Equal(t, expectedCode, w.Code) assert.True(t, c.IsAborted()) }