forked from labstack/echo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbody_limit_test.go
39 lines (33 loc) · 935 Bytes
/
body_limit_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package middleware
import (
"io/ioutil"
"net/http"
"testing"
"bytes"
"github.com/labstack/echo"
"github.com/labstack/echo/test"
"github.com/stretchr/testify/assert"
)
func TestBodyLimit(t *testing.T) {
e := echo.New()
req := test.NewRequest(echo.POST, "/", bytes.NewReader([]byte("Hello, World!")))
rec := test.NewResponseRecorder()
c := e.NewContext(req, rec)
h := func(c echo.Context) error {
body, err := ioutil.ReadAll(c.Request().Body())
if err != nil {
return err
}
return c.String(http.StatusOK, string(body))
}
// Within limit
BodyLimit("2M")(h)(c)
assert.Equal(t, http.StatusOK, rec.Status())
assert.Equal(t, "Hello, World!", rec.Body.String())
// Overlimit
req = test.NewRequest(echo.POST, "/", bytes.NewReader([]byte("Hello, World!")))
rec = test.NewResponseRecorder()
c = e.NewContext(req, rec)
BodyLimit("2B")(h)(c)
assert.Equal(t, http.StatusRequestEntityTooLarge, rec.Status())
}