forked from vulcand/oxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtokenlimiter_test.go
358 lines (275 loc) · 9.59 KB
/
tokenlimiter_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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
package ratelimit
import (
"fmt"
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/vulcand/oxy/internal/holsterv4/clock"
"github.com/vulcand/oxy/testutils"
"github.com/vulcand/oxy/utils"
)
func TestRateSetAdd(t *testing.T) {
rs := NewRateSet()
// Invalid period
err := rs.Add(0, 1, 1)
require.Error(t, err)
// Invalid Average
err = rs.Add(clock.Second, 0, 1)
require.Error(t, err)
// Invalid Burst
err = rs.Add(clock.Second, 1, 0)
require.Error(t, err)
err = rs.Add(clock.Second, 1, 1)
require.NoError(t, err)
assert.Equal(t, rs.String(), "map[1s:rate(1/1s, burst=1)]")
}
// We've hit the limit and were able to proceed on the next time run.
func TestHitLimit(t *testing.T) {
handler := http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
_, _ = w.Write([]byte("hello"))
})
rates := NewRateSet()
err := rates.Add(clock.Second, 1, 1)
require.NoError(t, err)
done := testutils.FreezeTime()
defer done()
l, err := New(handler, headerLimit, rates)
require.NoError(t, err)
srv := httptest.NewServer(l)
defer srv.Close()
re, _, err := testutils.Get(srv.URL, testutils.Header("Source", "a"))
require.NoError(t, err)
assert.Equal(t, http.StatusOK, re.StatusCode)
// Next request from the same source hits rate limit
re, _, err = testutils.Get(srv.URL, testutils.Header("Source", "a"))
require.NoError(t, err)
assert.Equal(t, 429, re.StatusCode)
// Second later, the request from this ip will succeed
clock.Advance(clock.Second)
re, _, err = testutils.Get(srv.URL, testutils.Header("Source", "a"))
require.NoError(t, err)
assert.Equal(t, http.StatusOK, re.StatusCode)
}
// We've failed to extract client ip.
func TestFailure(t *testing.T) {
handler := http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
_, _ = w.Write([]byte("hello"))
})
rates := NewRateSet()
err := rates.Add(clock.Second, 1, 1)
require.NoError(t, err)
done := testutils.FreezeTime()
defer done()
l, err := New(handler, faultyExtract, rates)
require.NoError(t, err)
srv := httptest.NewServer(l)
defer srv.Close()
re, _, err := testutils.Get(srv.URL, testutils.Header("Source", "a"))
require.NoError(t, err)
assert.Equal(t, http.StatusInternalServerError, re.StatusCode)
}
// Make sure rates from different ips are controlled separately.
func TestIsolation(t *testing.T) {
handler := http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
_, _ = w.Write([]byte("hello"))
})
rates := NewRateSet()
err := rates.Add(clock.Second, 1, 1)
require.NoError(t, err)
done := testutils.FreezeTime()
defer done()
l, err := New(handler, headerLimit, rates)
require.NoError(t, err)
srv := httptest.NewServer(l)
defer srv.Close()
re, _, err := testutils.Get(srv.URL, testutils.Header("Source", "a"))
require.NoError(t, err)
assert.Equal(t, http.StatusOK, re.StatusCode)
// Next request from the same source hits rate limit
re, _, err = testutils.Get(srv.URL, testutils.Header("Source", "a"))
require.NoError(t, err)
assert.Equal(t, 429, re.StatusCode)
// The request from other source can proceed
re, _, err = testutils.Get(srv.URL, testutils.Header("Source", "b"))
require.NoError(t, err)
assert.Equal(t, http.StatusOK, re.StatusCode)
}
// Make sure that expiration works (Expiration is triggered after significant amount of time passes).
func TestExpiration(t *testing.T) {
handler := http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
_, _ = w.Write([]byte("hello"))
})
rates := NewRateSet()
err := rates.Add(clock.Second, 1, 1)
require.NoError(t, err)
done := testutils.FreezeTime()
defer done()
l, err := New(handler, headerLimit, rates)
require.NoError(t, err)
srv := httptest.NewServer(l)
defer srv.Close()
re, _, err := testutils.Get(srv.URL, testutils.Header("Source", "a"))
require.NoError(t, err)
assert.Equal(t, http.StatusOK, re.StatusCode)
// Next request from the same source hits rate limit
re, _, err = testutils.Get(srv.URL, testutils.Header("Source", "a"))
require.NoError(t, err)
assert.Equal(t, 429, re.StatusCode)
// 24 hours later, the request from this ip will succeed
clock.Advance(24 * clock.Hour)
re, _, err = testutils.Get(srv.URL, testutils.Header("Source", "a"))
require.NoError(t, err)
assert.Equal(t, http.StatusOK, re.StatusCode)
}
// If rate limiting configuration is valid, then it is applied.
func TestExtractRates(t *testing.T) {
// Given
extractRates := func(*http.Request) (*RateSet, error) {
rates := NewRateSet()
err := rates.Add(clock.Second, 2, 2)
if err != nil {
return nil, err
}
err = rates.Add(60*clock.Second, 10, 10)
if err != nil {
return nil, err
}
return rates, nil
}
rates := NewRateSet()
err := rates.Add(clock.Second, 1, 1)
require.NoError(t, err)
handler := http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
_, _ = w.Write([]byte("hello"))
})
done := testutils.FreezeTime()
defer done()
tl, err := New(handler, headerLimit, rates, ExtractRates(RateExtractorFunc(extractRates)))
require.NoError(t, err)
srv := httptest.NewServer(tl)
defer srv.Close()
// When/Then: The configured rate is applied, which 2 req/second
re, _, err := testutils.Get(srv.URL, testutils.Header("Source", "a"))
require.NoError(t, err)
assert.Equal(t, http.StatusOK, re.StatusCode)
re, _, err = testutils.Get(srv.URL, testutils.Header("Source", "a"))
require.NoError(t, err)
assert.Equal(t, http.StatusOK, re.StatusCode)
re, _, err = testutils.Get(srv.URL, testutils.Header("Source", "a"))
require.NoError(t, err)
assert.Equal(t, 429, re.StatusCode)
clock.Advance(clock.Second)
re, _, err = testutils.Get(srv.URL, testutils.Header("Source", "a"))
require.NoError(t, err)
assert.Equal(t, http.StatusOK, re.StatusCode)
}
// If configMapper returns error, then the default rate is applied.
func TestBadRateExtractor(t *testing.T) {
// Given
extractor := func(*http.Request) (*RateSet, error) {
return nil, fmt.Errorf("boom")
}
rates := NewRateSet()
err := rates.Add(clock.Second, 1, 1)
require.NoError(t, err)
handler := http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
_, _ = w.Write([]byte("hello"))
})
done := testutils.FreezeTime()
defer done()
l, err := New(handler, headerLimit, rates, ExtractRates(RateExtractorFunc(extractor)))
require.NoError(t, err)
srv := httptest.NewServer(l)
defer srv.Close()
// When/Then: The default rate is applied, which 1 req/second
re, _, err := testutils.Get(srv.URL, testutils.Header("Source", "a"))
require.NoError(t, err)
assert.Equal(t, http.StatusOK, re.StatusCode)
re, _, err = testutils.Get(srv.URL, testutils.Header("Source", "a"))
require.NoError(t, err)
assert.Equal(t, 429, re.StatusCode)
clock.Advance(clock.Second)
re, _, err = testutils.Get(srv.URL, testutils.Header("Source", "a"))
require.NoError(t, err)
assert.Equal(t, http.StatusOK, re.StatusCode)
}
// If configMapper returns empty rates, then the default rate is applied.
func TestExtractorEmpty(t *testing.T) {
// Given
extractor := func(*http.Request) (*RateSet, error) {
return NewRateSet(), nil
}
rates := NewRateSet()
err := rates.Add(clock.Second, 1, 1)
require.NoError(t, err)
handler := http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
_, _ = w.Write([]byte("hello"))
})
done := testutils.FreezeTime()
defer done()
l, err := New(handler, headerLimit, rates, ExtractRates(RateExtractorFunc(extractor)))
require.NoError(t, err)
srv := httptest.NewServer(l)
defer srv.Close()
// When/Then: The default rate is applied, which 1 req/second
re, _, err := testutils.Get(srv.URL, testutils.Header("Source", "a"))
require.NoError(t, err)
assert.Equal(t, http.StatusOK, re.StatusCode)
re, _, err = testutils.Get(srv.URL, testutils.Header("Source", "a"))
require.NoError(t, err)
assert.Equal(t, 429, re.StatusCode)
clock.Advance(clock.Second)
re, _, err = testutils.Get(srv.URL, testutils.Header("Source", "a"))
require.NoError(t, err)
assert.Equal(t, http.StatusOK, re.StatusCode)
}
func TestInvalidParams(t *testing.T) {
// Rates are missing
rs := NewRateSet()
err := rs.Add(clock.Second, 1, 1)
require.NoError(t, err)
// Empty
_, err = New(nil, nil, rs)
require.Error(t, err)
// Rates are empty
_, err = New(nil, nil, NewRateSet())
require.Error(t, err)
// Bad capacity
_, err = New(nil, headerLimit, rs, Capacity(-1))
require.Error(t, err)
}
// We've hit the limit and were able to proceed on the next time run.
func TestOptions(t *testing.T) {
handler := http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
_, _ = w.Write([]byte("hello"))
})
rates := NewRateSet()
err := rates.Add(clock.Second, 1, 1)
require.NoError(t, err)
errHandler := utils.ErrorHandlerFunc(func(w http.ResponseWriter, req *http.Request, err error) {
w.WriteHeader(http.StatusTeapot)
_, _ = w.Write([]byte(http.StatusText(http.StatusTeapot)))
})
done := testutils.FreezeTime()
defer done()
l, err := New(handler, headerLimit, rates, ErrorHandler(errHandler))
require.NoError(t, err)
srv := httptest.NewServer(l)
defer srv.Close()
re, _, err := testutils.Get(srv.URL, testutils.Header("Source", "a"))
require.NoError(t, err)
assert.Equal(t, http.StatusOK, re.StatusCode)
re, _, err = testutils.Get(srv.URL, testutils.Header("Source", "a"))
require.NoError(t, err)
assert.Equal(t, http.StatusTeapot, re.StatusCode)
}
func headerLimiter(req *http.Request) (string, int64, error) {
return req.Header.Get("Source"), 1, nil
}
func faultyExtractor(_ *http.Request) (string, int64, error) {
return "", -1, fmt.Errorf("oops")
}
var headerLimit = utils.ExtractorFunc(headerLimiter)
var faultyExtract = utils.ExtractorFunc(faultyExtractor)