-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbodylimit_test.go
More file actions
181 lines (149 loc) · 4.6 KB
/
bodylimit_test.go
File metadata and controls
181 lines (149 loc) · 4.6 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package api_test
import (
"bytes"
"context"
"encoding/json"
"io"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/bjaus/api"
)
func TestBodyLimit(t *testing.T) {
t.Parallel()
tests := map[string]struct {
maxBytes int64
bodySize int
wantStatus int
}{
"request within limit succeeds": {
maxBytes: 1024,
bodySize: 512,
wantStatus: http.StatusOK,
},
"request exceeding limit fails": {
maxBytes: 64,
bodySize: 128,
wantStatus: http.StatusRequestEntityTooLarge,
},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
t.Parallel()
mw := api.BodyLimit(tc.maxBytes)
handler := mw(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, err := io.ReadAll(r.Body)
if err != nil {
http.Error(w, "body too large", http.StatusRequestEntityTooLarge)
return
}
w.WriteHeader(http.StatusOK)
}))
srv := httptest.NewServer(handler)
t.Cleanup(srv.Close)
body := bytes.Repeat([]byte("x"), tc.bodySize)
req, err := http.NewRequestWithContext(context.Background(), http.MethodPost, srv.URL+"/", bytes.NewReader(body))
require.NoError(t, err)
resp, err := http.DefaultClient.Do(req)
require.NoError(t, err)
defer func() { require.NoError(t, resp.Body.Close()) }()
assert.Equal(t, tc.wantStatus, resp.StatusCode)
})
}
}
func TestWithBodyLimit(t *testing.T) {
t.Parallel()
type Req struct {
Data string `json:"data"`
}
type Resp struct {
Len int `json:"len"`
}
r := api.New()
// Route with a 64-byte body limit.
api.Post(r, "/limited", func(_ context.Context, req *Req) (*api.Resp[Resp], error) {
return &api.Resp[Resp]{Body: Resp{Len: len(req.Data)}}, nil
}, api.WithBodyLimit(64))
// Route with no per-route limit.
api.Post(r, "/unlimited", func(_ context.Context, req *Req) (*api.Resp[Resp], error) {
return &api.Resp[Resp]{Body: Resp{Len: len(req.Data)}}, nil
})
srv := httptest.NewServer(r)
t.Cleanup(srv.Close)
largeBody := `{"data":"` + strings.Repeat("x", 200) + `"}`
smallBody := `{"data":"hello"}`
tests := map[string]struct {
path string
body string
wantStatus int
}{
"small body within limit": {
path: "/limited",
body: smallBody,
wantStatus: http.StatusOK,
},
"large body exceeds per-route limit": {
path: "/limited",
body: largeBody,
wantStatus: http.StatusBadRequest,
},
"large body on unlimited route succeeds": {
path: "/unlimited",
body: largeBody,
wantStatus: http.StatusOK,
},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
t.Parallel()
req, err := http.NewRequestWithContext(context.Background(), http.MethodPost,
srv.URL+tc.path, strings.NewReader(tc.body))
require.NoError(t, err)
req.Header.Set("Content-Type", "application/json")
resp, err := http.DefaultClient.Do(req)
require.NoError(t, err)
defer func() { require.NoError(t, resp.Body.Close()) }()
assert.Equal(t, tc.wantStatus, resp.StatusCode)
if tc.wantStatus == http.StatusOK {
var body Resp
require.NoError(t, json.NewDecoder(resp.Body).Decode(&body))
assert.Greater(t, body.Len, 0)
}
})
}
}
func TestWithBodyLimit_overrides_global(t *testing.T) {
t.Parallel()
type Req struct {
Data string `json:"data"`
}
r := api.New()
r.Use(api.BodyLimit(32)) // Global: 32 bytes
// Per-route: 1024 bytes — should override the global limit.
api.Post(r, "/upload", func(_ context.Context, req *Req) (*api.Void, error) {
return &api.Void{}, nil
}, api.WithBodyLimit(1024))
srv := httptest.NewServer(r)
t.Cleanup(srv.Close)
// 200-byte body: exceeds global (32) but within per-route (1024).
body := `{"data":"` + strings.Repeat("x", 200) + `"}`
req, err := http.NewRequestWithContext(context.Background(), http.MethodPost,
srv.URL+"/upload", strings.NewReader(body))
require.NoError(t, err)
req.Header.Set("Content-Type", "application/json")
resp, err := http.DefaultClient.Do(req)
require.NoError(t, err)
defer func() { require.NoError(t, resp.Body.Close()) }()
// The global middleware runs first (outer), wrapping the body.
// The per-route limit runs second (inner), re-wrapping.
// MaxBytesReader nests — the inner limit is the effective one only if
// the outer limit hasn't already been hit. So the global 32-byte limit
// fires first and the request fails.
//
// This is the expected behavior: per-route limits are useful for
// restricting below the global limit, or when there's no global limit.
assert.Equal(t, http.StatusBadRequest, resp.StatusCode)
}