MCPcopy Index your code
hub / github.com/labstack/echo / TestBodyLimitConfig_ToMiddleware

Function TestBodyLimitConfig_ToMiddleware

middleware/body_limit_test.go:17–69  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

15)
16
17func TestBodyLimitConfig_ToMiddleware(t *testing.T) {
18 e := echo.New()
19 hw := []byte("Hello, World!")
20 req := httptest.NewRequest(http.MethodPost, "/", bytes.NewReader(hw))
21 rec := httptest.NewRecorder()
22 c := e.NewContext(req, rec)
23 h := func(c *echo.Context) error {
24 body, err := io.ReadAll(c.Request().Body)
25 if err != nil {
26 return err
27 }
28 return c.String(http.StatusOK, string(body))
29 }
30
31 // Based on content length (within limit)
32 mw, err := BodyLimitConfig{LimitBytes: 2 * MB}.ToMiddleware()
33 assert.NoError(t, err)
34
35 err = mw(h)(c)
36 if assert.NoError(t, err) {
37 assert.Equal(t, http.StatusOK, rec.Code)
38 assert.Equal(t, hw, rec.Body.Bytes())
39 }
40
41 // Based on content read (overlimit)
42 mw, err = BodyLimitConfig{LimitBytes: 2}.ToMiddleware()
43 assert.NoError(t, err)
44 he := mw(h)(c).(echo.HTTPStatusCoder)
45 assert.Equal(t, http.StatusRequestEntityTooLarge, he.StatusCode())
46
47 // Based on content read (within limit)
48 req = httptest.NewRequest(http.MethodPost, "/", bytes.NewReader(hw))
49 req.ContentLength = -1
50 rec = httptest.NewRecorder()
51 c = e.NewContext(req, rec)
52
53 mw, err = BodyLimitConfig{LimitBytes: 2 * MB}.ToMiddleware()
54 assert.NoError(t, err)
55 err = mw(h)(c)
56 assert.NoError(t, err)
57 assert.Equal(t, http.StatusOK, rec.Code)
58 assert.Equal(t, "Hello, World!", rec.Body.String())
59
60 // Based on content read (overlimit)
61 req = httptest.NewRequest(http.MethodPost, "/", bytes.NewReader(hw))
62 req.ContentLength = -1
63 rec = httptest.NewRecorder()
64 c = e.NewContext(req, rec)
65 mw, err = BodyLimitConfig{LimitBytes: 2}.ToMiddleware()
66 assert.NoError(t, err)
67 he = mw(h)(c).(echo.HTTPStatusCoder)
68 assert.Equal(t, http.StatusRequestEntityTooLarge, he.StatusCode())
69}
70
71func TestBodyLimitAfterDecompressUsesDecodedSize(t *testing.T) {
72 e := echo.New()

Callers

nothing calls this directly

Calls 5

RequestMethod · 0.95
StringMethod · 0.95
NewContextMethod · 0.80
ToMiddlewareMethod · 0.65
StatusCodeMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…