mirror of
https://github.com/labstack/echo.git
synced 2024-11-24 08:22:21 +02:00
fix: BodyLimit middleware: limitedReader Reset does not reset read counter
This commit is contained in:
parent
ee32e3e7fb
commit
9aafcaf934
@ -105,6 +105,7 @@ func (r *limitedReader) Close() error {
|
||||
func (r *limitedReader) Reset(reader io.ReadCloser, context echo.Context) {
|
||||
r.reader = reader
|
||||
r.context = context
|
||||
r.read = 0
|
||||
}
|
||||
|
||||
func limitedReaderPool(c BodyLimitConfig) sync.Pool {
|
||||
|
@ -51,3 +51,33 @@ func TestBodyLimit(t *testing.T) {
|
||||
he = BodyLimit("2B")(h)(c).(*echo.HTTPError)
|
||||
assert.Equal(t, http.StatusRequestEntityTooLarge, he.Code)
|
||||
}
|
||||
|
||||
func TestBodyLimitReader(t *testing.T) {
|
||||
hw := []byte("Hello, World!")
|
||||
e := echo.New()
|
||||
req := httptest.NewRequest(echo.POST, "/", bytes.NewReader(hw))
|
||||
rec := httptest.NewRecorder()
|
||||
|
||||
config := BodyLimitConfig{
|
||||
Skipper: DefaultSkipper,
|
||||
Limit: "2B",
|
||||
limit: 2,
|
||||
}
|
||||
reader := &limitedReader{
|
||||
BodyLimitConfig: config,
|
||||
reader: ioutil.NopCloser(bytes.NewReader(hw)),
|
||||
context: e.NewContext(req, rec),
|
||||
}
|
||||
|
||||
// read all should return ErrStatusRequestEntityTooLarge
|
||||
_, err := ioutil.ReadAll(reader)
|
||||
he := err.(*echo.HTTPError)
|
||||
assert.Equal(t, http.StatusRequestEntityTooLarge, he.Code)
|
||||
|
||||
// reset reader and read two bytes must succeed
|
||||
bt := make([]byte, 2)
|
||||
reader.Reset(ioutil.NopCloser(bytes.NewReader(hw)), e.NewContext(req, rec))
|
||||
n, err := reader.Read(bt)
|
||||
assert.Equal(t, 2, n)
|
||||
assert.Equal(t, nil, err)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user