(t *testing.T)
| 15 | ) |
| 16 | |
| 17 | func 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 | |
| 71 | func TestBodyLimitAfterDecompressUsesDecodedSize(t *testing.T) { |
| 72 | e := echo.New() |
nothing calls this directly
no test coverage detected
searching dependent graphs…