Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions middleware/body_limit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,31 @@ func TestBodyLimitConfig_ToMiddleware(t *testing.T) {
assert.Equal(t, http.StatusRequestEntityTooLarge, he.StatusCode())
}

func TestBodyLimitAfterDecompressUsesDecodedSize(t *testing.T) {
e := echo.New()
body := "ok"
gz, err := gzipString(body)
assert.NoError(t, err)
assert.Greater(t, len(gz), len(body))

req := httptest.NewRequest(http.MethodPost, "/", bytes.NewReader(gz))
req.Header.Set(echo.HeaderContentEncoding, GZIPEncoding)
rec := httptest.NewRecorder()
c := e.NewContext(req, rec)

err = Decompress()(BodyLimit(int64(len(body)))(func(c *echo.Context) error {
body, readErr := io.ReadAll(c.Request().Body)
if readErr != nil {
return readErr
}
return c.String(http.StatusOK, string(body))
}))(c)

assert.NoError(t, err)
assert.Equal(t, http.StatusOK, rec.Code)
assert.Equal(t, body, rec.Body.String())
}

func TestBodyLimitReader(t *testing.T) {
hw := []byte("Hello, World!")

Expand Down
1 change: 1 addition & 0 deletions middleware/decompress.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ func (config DecompressConfig) ToMiddleware() (echo.MiddlewareFunc, error) {
// -1 means explicitly unlimited (not recommended)
c.Request().Body = gr
}
c.Request().ContentLength = -1

return next(c)
}
Expand Down
4 changes: 2 additions & 2 deletions middleware/decompress_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,6 @@ func BenchmarkDecompress(b *testing.B) {
e := echo.New()
body := `{"name": "echo"}`
gz, _ := gzipString(body)
req := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(string(gz)))
req.Header.Set(echo.HeaderContentEncoding, GZIPEncoding)

h := Decompress()(func(c *echo.Context) error {
c.Response().Write([]byte(body)) // For Content-Type sniffing
Expand All @@ -228,6 +226,8 @@ func BenchmarkDecompress(b *testing.B) {

for i := 0; i < b.N; i++ {
// Decompress
req := httptest.NewRequest(http.MethodPost, "/", bytes.NewReader(gz))
req.Header.Set(echo.HeaderContentEncoding, GZIPEncoding)
rec := httptest.NewRecorder()
c := e.NewContext(req, rec)
h(c)
Expand Down
Loading