-
Notifications
You must be signed in to change notification settings - Fork 20
/
ratelimit_test.go
281 lines (268 loc) · 6.06 KB
/
ratelimit_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
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
package ratelimit
import (
"context"
"net/http"
"net/http/httptest"
"reflect"
"testing"
"github.com/caddyserver/caddy/v2"
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
"go.uber.org/zap"
)
func TestRateLimit_ServeHTTP(t *testing.T) {
next := caddyhttp.HandlerFunc(func(w http.ResponseWriter, r *http.Request) error {
w.WriteHeader(http.StatusOK)
return nil
})
cases := []struct {
inRL *RateLimit
inReq *http.Request
wantStatusCodes []int
}{
{
inRL: &RateLimit{
Key: "{query.id}",
Rate: "2r/m",
logger: zap.NewNop(),
},
inReq: httptest.NewRequest(http.MethodGet, "/foo?id=1", nil),
wantStatusCodes: []int{
http.StatusOK,
http.StatusOK,
http.StatusTooManyRequests,
},
},
}
for _, c := range cases {
_ = c.inRL.provision()
//c.inRL.Cleanup()
var gotStatusCodes []int
for i := 0; i < len(c.wantStatusCodes); i++ {
// Build the request object.
repl := caddyhttp.NewTestReplacer(c.inReq)
ctx := context.WithValue(c.inReq.Context(), caddy.ReplacerCtxKey, repl)
req := c.inReq.WithContext(ctx)
// Build the response object.
w := httptest.NewRecorder()
_ = c.inRL.ServeHTTP(w, req, next)
// Collect the response status code.
resp := w.Result()
gotStatusCodes = append(gotStatusCodes, resp.StatusCode)
}
if !reflect.DeepEqual(gotStatusCodes, c.wantStatusCodes) {
t.Fatalf("StatusCodes: got (%#v), want (%#v)", gotStatusCodes, c.wantStatusCodes)
}
}
}
func TestParseVar(t *testing.T) {
cases := []struct {
in string
want *Var
wantErrStr string
}{
{
in: "{path.id}",
want: &Var{
Raw: "{path.id}",
Name: "{http.request.uri.path.id}",
},
},
{
in: "{query.id}",
want: &Var{
Raw: "{query.id}",
Name: "{http.request.uri.query.id}",
},
},
{
in: "{header.id}",
want: &Var{
Raw: "{header.id}",
Name: "{http.request.header.id}",
},
},
{
in: "{cookie.id}",
want: &Var{
Raw: "{cookie.id}",
Name: "{http.request.cookie.id}",
},
},
{
in: "{remote.host}",
want: &Var{
Raw: "{remote.host}",
Name: "{http.request.remote.host}",
},
},
{
in: "{remote.port}",
want: &Var{
Raw: "{remote.port}",
Name: "{http.request.remote.port}",
},
},
{
in: "{remote.ip}",
want: &Var{
Raw: "{remote.ip}",
Name: "{http.request.remote.ip}",
},
},
{
in: "{remote.host_prefix.24}",
want: &Var{
Raw: "{remote.host_prefix.24}",
Name: "{http.request.remote.host_prefix}",
Bits: 24,
},
},
{
in: "{remote.ip_prefix.64}",
want: &Var{
Raw: "{remote.ip_prefix.64}",
Name: "{http.request.remote.ip_prefix}",
Bits: 64,
},
},
{
in: "{remote.host_prefix}",
wantErrStr: `invalid key variable: "{remote.host_prefix}"`,
},
{
in: "{remote.ip_prefix.xx}",
wantErrStr: `invalid key variable: "{remote.ip_prefix.xx}"`,
},
{
in: "{http.request.uri.path.id}",
want: &Var{
Raw: "{http.request.uri.path.id}",
Name: "{http.request.uri.path.id}",
},
},
{
in: "{unknown.id}",
wantErrStr: `unrecognized key variable: "{unknown.id}"`,
},
{
in: "{unknown}",
wantErrStr: `invalid key variable: "{unknown}"`,
},
}
for _, c := range cases {
t.Run("", func(t *testing.T) {
v, err := ParseVar(c.in)
if err != nil && err.Error() != c.wantErrStr {
t.Fatalf("ErrStr: got (%#v), want (%#v)", err.Error(), c.wantErrStr)
}
if !reflect.DeepEqual(v, c.want) {
t.Fatalf("Out: got (%#v), want (%#v)", v, c.want)
}
})
}
}
func TestVar_Evaluate(t *testing.T) {
cases := []struct {
name string
inVar *Var
inReq func() *http.Request
wantValue string
wantErrStr string
}{
{
name: "query",
inVar: &Var{
Name: "{http.request.uri.query.id}",
},
inReq: func() *http.Request {
return httptest.NewRequest(http.MethodGet, "/foo?id=1", nil)
},
wantValue: "1",
},
{
name: "peer ip",
inVar: &Var{
Name: "{http.request.remote.host}",
},
inReq: func() *http.Request {
req := httptest.NewRequest(http.MethodGet, "/", nil)
req.Header.Set("X-Forwarded-For", "192.168.0.1, 192.168.0.2")
return req
},
wantValue: "192.0.2.1",
},
{
name: "peer port",
inVar: &Var{
Name: "{http.request.remote.port}",
},
inReq: func() *http.Request {
return httptest.NewRequest(http.MethodGet, "/", nil)
},
wantValue: "1234",
},
{
name: "forwarded ip",
inVar: &Var{
Name: "{http.request.remote.ip}",
},
inReq: func() *http.Request {
req := httptest.NewRequest(http.MethodGet, "/", nil)
req.Header.Set("X-Forwarded-For", "192.168.0.1, 192.168.0.2")
return req
},
wantValue: "192.168.0.1",
},
{
name: "ipv4 prefix",
inVar: &Var{
Name: "{http.request.remote.ip_prefix}",
Bits: 24,
},
inReq: func() *http.Request {
return httptest.NewRequest(http.MethodGet, "/", nil)
},
wantValue: "192.0.2.0/24",
},
{
name: "ipv6 prefix",
inVar: &Var{
Name: "{http.request.remote.ip_prefix}",
Bits: 64,
},
inReq: func() *http.Request {
req := httptest.NewRequest(http.MethodGet, "/", nil)
req.RemoteAddr = "[2001:db8:85a3:8d3:1319:8a2e:370:7348]:443"
return req
},
wantValue: "2001:db8:85a3:8d3::/64",
},
{
name: "bad ipv4 prefix",
inVar: &Var{
Name: "{http.request.remote.ip_prefix}",
Bits: 64,
},
inReq: func() *http.Request {
return httptest.NewRequest(http.MethodGet, "/", nil)
},
wantErrStr: "prefix length 64 too large for IPv4",
},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
// Build the request object.
r := c.inReq()
repl := caddyhttp.NewTestReplacer(r)
ctx := context.WithValue(r.Context(), caddy.ReplacerCtxKey, repl)
req := r.WithContext(ctx)
value, err := c.inVar.Evaluate(req)
if err != nil && err.Error() != c.wantErrStr {
t.Fatalf("ErrStr: got (%#v), want (%#v)", err.Error(), c.wantErrStr)
}
if value != c.wantValue {
t.Fatalf("Value: got (%#v), want (%#v)", value, c.wantValue)
}
})
}
}