MCPcopy
hub / github.com/gin-gonic/gin / TestContextBindRequestTooLarge

Function TestContextBindRequestTooLarge

context_test.go:2108–2137  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

2106}
2107
2108func TestContextBindRequestTooLarge(t *testing.T) {
2109 // When using go-json as JSON encoder, they do not propagate the http.MaxBytesError error
2110 // The response will fail with a generic 400 instead of 413
2111 // https://github.com/goccy/go-json/issues/485
2112 var expectedCode int
2113 switch json.Package {
2114 case "github.com/goccy/go-json":
2115 expectedCode = http.StatusBadRequest
2116 default:
2117 expectedCode = http.StatusRequestEntityTooLarge
2118 }
2119
2120 w := httptest.NewRecorder()
2121 c, _ := CreateTestContext(w)
2122
2123 c.Request, _ = http.NewRequest(http.MethodPost, "/", strings.NewReader(`{"foo":"bar", "bar":"foo"}`))
2124 c.Request.Body = http.MaxBytesReader(c.Writer, c.Request.Body, 10)
2125
2126 var obj struct {
2127 Foo string `json:"foo"`
2128 Bar string `json:"bar"`
2129 }
2130 require.Error(t, c.BindJSON(&obj))
2131 c.Writer.WriteHeaderNow()
2132
2133 assert.Empty(t, obj.Bar)
2134 assert.Empty(t, obj.Foo)
2135 assert.Equal(t, expectedCode, w.Code)
2136 assert.True(t, c.IsAborted())
2137}
2138
2139func TestContextAutoBindJSON(t *testing.T) {
2140 c, _ := CreateTestContext(httptest.NewRecorder())

Callers

nothing calls this directly

Calls 5

CreateTestContextFunction · 0.85
BindJSONMethod · 0.80
IsAbortedMethod · 0.80
WriteHeaderNowMethod · 0.65
ErrorMethod · 0.45

Tested by

no test coverage detected