-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
sizelimit_test.go
69 lines (61 loc) · 1.62 KB
/
sizelimit_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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package rest
import (
"fmt"
"io"
"net/http"
"net/http/httptest"
"net/http/httputil"
"strings"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestSizeLimit(t *testing.T) {
tbl := []struct {
method string
body string
code int
}{
{"GET", "", 200},
{"POST", "1234567", 200},
{"POST", "1234567890", 200},
{"POST", "12345678901", 413},
{"POST", "1234567", 200},
}
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
body, err := io.ReadAll(r.Body)
require.NoError(t, err, "body read failed")
_, err = w.Write(body)
require.NoError(t, err, "body write failed")
dump, _ := httputil.DumpRequest(r, true)
t.Log(string(dump))
})
ts := httptest.NewServer(SizeLimit(10)(handler))
defer ts.Close()
for i, tt := range tbl {
i := i
tt := tt
for _, wrap := range []bool{false, true} {
wrap := wrap
t.Run(fmt.Sprintf("test-%d/%v", i, wrap), func(t *testing.T) {
client := http.Client{Timeout: 1 * time.Second}
var reader io.Reader = strings.NewReader(tt.body)
if wrap {
reader = io.NopCloser(reader) // to prevent ContentLength setting up
}
req, err := http.NewRequest(tt.method, fmt.Sprintf("%s/%d/%v", ts.URL, i, wrap), reader)
require.NoError(t, err)
resp, err := client.Do(req)
require.NoError(t, err)
require.Equal(t, tt.code, resp.StatusCode)
if resp.StatusCode != http.StatusRequestEntityTooLarge {
body, err := io.ReadAll(resp.Body)
require.NoError(t, err)
defer resp.Body.Close()
assert.Equal(t, tt.body, string(body), "body match")
}
})
}
}
}