2016-05-01 05:08:06 +02:00
|
|
|
package middleware
|
|
|
|
|
|
|
|
import (
|
2016-05-23 20:23:15 +02:00
|
|
|
"bytes"
|
2016-05-01 05:08:06 +02:00
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
2016-09-23 07:53:44 +02:00
|
|
|
"net/http/httptest"
|
2016-05-01 05:08:06 +02:00
|
|
|
"testing"
|
|
|
|
|
2019-01-30 12:56:56 +02:00
|
|
|
"github.com/labstack/echo/v4"
|
2016-05-01 05:08:06 +02:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestBodyLimit(t *testing.T) {
|
|
|
|
e := echo.New()
|
2016-05-23 20:23:15 +02:00
|
|
|
hw := []byte("Hello, World!")
|
2018-10-14 17:16:58 +02:00
|
|
|
req := httptest.NewRequest(http.MethodPost, "/", bytes.NewReader(hw))
|
2016-09-23 07:53:44 +02:00
|
|
|
rec := httptest.NewRecorder()
|
2016-05-01 05:08:06 +02:00
|
|
|
c := e.NewContext(req, rec)
|
|
|
|
h := func(c echo.Context) error {
|
2016-09-23 07:53:44 +02:00
|
|
|
body, err := ioutil.ReadAll(c.Request().Body)
|
2016-05-01 06:56:35 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-05-01 05:08:06 +02:00
|
|
|
return c.String(http.StatusOK, string(body))
|
|
|
|
}
|
|
|
|
|
2018-10-14 09:18:44 +02:00
|
|
|
assert := assert.New(t)
|
|
|
|
|
2016-05-23 20:23:15 +02:00
|
|
|
// Based on content length (within limit)
|
2018-10-14 09:18:44 +02:00
|
|
|
if assert.NoError(BodyLimit("2M")(h)(c)) {
|
|
|
|
assert.Equal(http.StatusOK, rec.Code)
|
|
|
|
assert.Equal(hw, rec.Body.Bytes())
|
2016-05-23 20:23:15 +02:00
|
|
|
}
|
|
|
|
|
2022-03-21 17:45:06 +02:00
|
|
|
// Based on content length (overlimit)
|
2016-05-23 20:23:15 +02:00
|
|
|
he := BodyLimit("2B")(h)(c).(*echo.HTTPError)
|
2018-10-14 09:18:44 +02:00
|
|
|
assert.Equal(http.StatusRequestEntityTooLarge, he.Code)
|
2016-05-23 20:23:15 +02:00
|
|
|
|
|
|
|
// Based on content read (within limit)
|
2018-10-14 17:16:58 +02:00
|
|
|
req = httptest.NewRequest(http.MethodPost, "/", bytes.NewReader(hw))
|
2022-03-21 17:45:06 +02:00
|
|
|
req.ContentLength = -1
|
2016-09-23 07:53:44 +02:00
|
|
|
rec = httptest.NewRecorder()
|
2016-05-23 20:23:15 +02:00
|
|
|
c = e.NewContext(req, rec)
|
2018-10-14 09:18:44 +02:00
|
|
|
if assert.NoError(BodyLimit("2M")(h)(c)) {
|
|
|
|
assert.Equal(http.StatusOK, rec.Code)
|
|
|
|
assert.Equal("Hello, World!", rec.Body.String())
|
2016-05-23 20:23:15 +02:00
|
|
|
}
|
2016-05-01 05:08:06 +02:00
|
|
|
|
2016-05-23 20:23:15 +02:00
|
|
|
// Based on content read (overlimit)
|
2018-10-14 17:16:58 +02:00
|
|
|
req = httptest.NewRequest(http.MethodPost, "/", bytes.NewReader(hw))
|
2022-03-21 17:45:06 +02:00
|
|
|
req.ContentLength = -1
|
2016-09-23 07:53:44 +02:00
|
|
|
rec = httptest.NewRecorder()
|
2016-05-01 06:56:35 +02:00
|
|
|
c = e.NewContext(req, rec)
|
2016-05-23 20:23:15 +02:00
|
|
|
he = BodyLimit("2B")(h)(c).(*echo.HTTPError)
|
2018-10-14 09:18:44 +02:00
|
|
|
assert.Equal(http.StatusRequestEntityTooLarge, he.Code)
|
2016-05-01 05:08:06 +02:00
|
|
|
}
|
2018-03-15 15:28:25 +02:00
|
|
|
|
|
|
|
func TestBodyLimitReader(t *testing.T) {
|
|
|
|
hw := []byte("Hello, World!")
|
|
|
|
e := echo.New()
|
2018-10-14 17:16:58 +02:00
|
|
|
req := httptest.NewRequest(http.MethodPost, "/", bytes.NewReader(hw))
|
2018-03-15 15:28:25 +02:00
|
|
|
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)
|
|
|
|
}
|