forked from didip/tollbooth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtollbooth_test.go
302 lines (258 loc) · 9.99 KB
/
tollbooth_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
package tollbooth
import (
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
"github.com/syreclabs/tollbooth/config"
)
func TestLimitByKeys(t *testing.T) {
limiter := NewLimiter(1, time.Second) // Only 1 request per second is allowed.
httperror := LimitByKeys(limiter, []string{"127.0.0.1", "/"})
if httperror != nil {
t.Errorf("First time count should not return error. Error: %v", httperror.Error())
}
httperror = LimitByKeys(limiter, []string{"127.0.0.1", "/"})
if httperror == nil {
t.Errorf("Second time count should return error because it exceeds 1 request per second.")
}
<-time.After(1 * time.Second)
httperror = LimitByKeys(limiter, []string{"127.0.0.1", "/"})
if httperror != nil {
t.Errorf("Third time count should not return error because the 1 second window has passed.")
}
}
func TestDefaultBuildKeys(t *testing.T) {
limiter := NewLimiter(1, time.Second)
limiter.IPLookups = []string{"X-Forwarded-For", "X-Real-IP", "RemoteAddr"}
request, err := http.NewRequest("GET", "/", strings.NewReader("Hello, world!"))
if err != nil {
t.Errorf("Unable to create new HTTP request. Error: %v", err)
}
request.Header.Set("X-Real-IP", "2601:7:1c82:4097:59a0:a80b:2841:b8c8")
sliceKeys := BuildKeys(limiter, request)
if len(sliceKeys) == 0 {
t.Error("Length of sliceKeys should never be empty.")
}
for _, keys := range sliceKeys {
for i, keyChunk := range keys {
if i == 0 && keyChunk != request.Header.Get("X-Real-IP") {
t.Errorf("The first chunk should be remote IP. KeyChunk: %v", keyChunk)
}
if i == 1 && keyChunk != request.URL.Path {
t.Errorf("The second chunk should be request path. KeyChunk: %v", keyChunk)
}
}
}
}
func TestBasicAuthBuildKeys(t *testing.T) {
limiter := NewLimiter(1, time.Second)
limiter.BasicAuthUsers = []string{"bro"}
request, err := http.NewRequest("GET", "/", strings.NewReader("Hello, world!"))
if err != nil {
t.Errorf("Unable to create new HTTP request. Error: %v", err)
}
request.Header.Set("X-Real-IP", "2601:7:1c82:4097:59a0:a80b:2841:b8c8")
request.SetBasicAuth("bro", "tato")
for _, keys := range BuildKeys(limiter, request) {
if len(keys) != 3 {
t.Error("Keys should be made of 3 parts.")
}
for i, keyChunk := range keys {
if i == 0 && keyChunk != request.Header.Get("X-Real-IP") {
t.Errorf("The (%v) chunk should be remote IP. KeyChunk: %v", i+1, keyChunk)
}
if i == 1 && keyChunk != request.URL.Path {
t.Errorf("The (%v) chunk should be request path. KeyChunk: %v", i+1, keyChunk)
}
if i == 2 && keyChunk != "bro" {
t.Errorf("The (%v) chunk should be request username. KeyChunk: %v", i+1, keyChunk)
}
}
}
}
func TestCustomHeadersBuildKeys(t *testing.T) {
limiter := NewLimiter(1, time.Second)
limiter.Headers = make(map[string][]string)
limiter.Headers["X-Auth-Token"] = []string{"totally-top-secret", "another-secret"}
request, err := http.NewRequest("GET", "/", strings.NewReader("Hello, world!"))
if err != nil {
t.Errorf("Unable to create new HTTP request. Error: %v", err)
}
request.Header.Set("X-Real-IP", "2601:7:1c82:4097:59a0:a80b:2841:b8c8")
request.Header.Set("X-Auth-Token", "totally-top-secret")
for _, keys := range BuildKeys(limiter, request) {
if len(keys) != 4 {
t.Errorf("Keys should be made of 4 parts. Keys: %v", keys)
}
for i, keyChunk := range keys {
if i == 0 && keyChunk != request.Header.Get("X-Real-IP") {
t.Errorf("The (%v) chunk should be remote IP. KeyChunk: %v", i+1, keyChunk)
}
if i == 1 && keyChunk != request.URL.Path {
t.Errorf("The (%v) chunk should be request path. KeyChunk: %v", i+1, keyChunk)
}
if i == 2 && keyChunk != "X-Auth-Token" {
t.Errorf("The (%v) chunk should be request header. KeyChunk: %v", i+1, keyChunk)
}
if i == 3 && (keyChunk != "totally-top-secret" && keyChunk != "another-secret") {
t.Errorf("The (%v) chunk should be request path. KeyChunk: %v", i+1, keyChunk)
}
}
}
}
func TestRequestMethodBuildKeys(t *testing.T) {
limiter := NewLimiter(1, time.Second)
limiter.Methods = []string{"GET"}
request, err := http.NewRequest("GET", "/", strings.NewReader("Hello, world!"))
if err != nil {
t.Errorf("Unable to create new HTTP request. Error: %v", err)
}
request.Header.Set("X-Real-IP", "2601:7:1c82:4097:59a0:a80b:2841:b8c8")
for _, keys := range BuildKeys(limiter, request) {
if len(keys) != 3 {
t.Errorf("Keys should be made of 3 parts. Keys: %v", keys)
}
for i, keyChunk := range keys {
if i == 0 && keyChunk != request.Header.Get("X-Real-IP") {
t.Errorf("The (%v) chunk should be remote IP. KeyChunk: %v", i+1, keyChunk)
}
if i == 1 && keyChunk != request.URL.Path {
t.Errorf("The (%v) chunk should be request path. KeyChunk: %v", i+1, keyChunk)
}
if i == 2 && keyChunk != "GET" {
t.Errorf("The (%v) chunk should be request method. KeyChunk: %v", i+1, keyChunk)
}
}
}
}
func TestRequestMethodAndCustomHeadersBuildKeys(t *testing.T) {
limiter := NewLimiter(1, time.Second)
limiter.Methods = []string{"GET"}
limiter.Headers = make(map[string][]string)
limiter.Headers["X-Auth-Token"] = []string{"totally-top-secret", "another-secret"}
request, err := http.NewRequest("GET", "/", strings.NewReader("Hello, world!"))
if err != nil {
t.Errorf("Unable to create new HTTP request. Error: %v", err)
}
request.Header.Set("X-Real-IP", "2601:7:1c82:4097:59a0:a80b:2841:b8c8")
request.Header.Set("X-Auth-Token", "totally-top-secret")
for _, keys := range BuildKeys(limiter, request) {
if len(keys) != 5 {
t.Errorf("Keys should be made of 4 parts. Keys: %v", keys)
}
for i, keyChunk := range keys {
if i == 0 && keyChunk != request.Header.Get("X-Real-IP") {
t.Errorf("The (%v) chunk should be remote IP. KeyChunk: %v", i+1, keyChunk)
}
if i == 1 && keyChunk != request.URL.Path {
t.Errorf("The (%v) chunk should be request path. KeyChunk: %v", i+1, keyChunk)
}
if i == 2 && keyChunk != "GET" {
t.Errorf("The (%v) chunk should be request method. KeyChunk: %v", i+1, keyChunk)
}
if i == 3 && keyChunk != "X-Auth-Token" {
t.Errorf("The (%v) chunk should be request header. KeyChunk: %v", i+1, keyChunk)
}
if i == 4 && (keyChunk != "totally-top-secret" && keyChunk != "another-secret") {
t.Errorf("The (%v) chunk should be request path. KeyChunk: %v", i+1, keyChunk)
}
}
}
}
func TestRequestMethodAndBasicAuthUsersBuildKeys(t *testing.T) {
limiter := NewLimiter(1, time.Second)
limiter.Methods = []string{"GET"}
limiter.BasicAuthUsers = []string{"bro"}
request, err := http.NewRequest("GET", "/", strings.NewReader("Hello, world!"))
if err != nil {
t.Errorf("Unable to create new HTTP request. Error: %v", err)
}
request.Header.Set("X-Real-IP", "2601:7:1c82:4097:59a0:a80b:2841:b8c8")
request.SetBasicAuth("bro", "tato")
for _, keys := range BuildKeys(limiter, request) {
if len(keys) != 4 {
t.Errorf("Keys should be made of 4 parts. Keys: %v", keys)
}
for i, keyChunk := range keys {
if i == 0 && keyChunk != request.Header.Get("X-Real-IP") {
t.Errorf("The (%v) chunk should be remote IP. KeyChunk: %v", i+1, keyChunk)
}
if i == 1 && keyChunk != request.URL.Path {
t.Errorf("The (%v) chunk should be request path. KeyChunk: %v", i+1, keyChunk)
}
if i == 2 && keyChunk != "GET" {
t.Errorf("The (%v) chunk should be request method. KeyChunk: %v", i+1, keyChunk)
}
if i == 3 && keyChunk != "bro" {
t.Errorf("The (%v) chunk should be basic auth user. KeyChunk: %v", i+1, keyChunk)
}
}
}
}
func TestRequestMethodCustomHeadersAndBasicAuthUsersBuildKeys(t *testing.T) {
limiter := NewLimiter(1, time.Second)
limiter.Methods = []string{"GET"}
limiter.Headers = make(map[string][]string)
limiter.Headers["X-Auth-Token"] = []string{"totally-top-secret", "another-secret"}
limiter.BasicAuthUsers = []string{"bro"}
request, err := http.NewRequest("GET", "/", strings.NewReader("Hello, world!"))
if err != nil {
t.Errorf("Unable to create new HTTP request. Error: %v", err)
}
request.Header.Set("X-Real-IP", "2601:7:1c82:4097:59a0:a80b:2841:b8c8")
request.Header.Set("X-Auth-Token", "totally-top-secret")
request.SetBasicAuth("bro", "tato")
for _, keys := range BuildKeys(limiter, request) {
if len(keys) != 6 {
t.Errorf("Keys should be made of 4 parts. Keys: %v", keys)
}
for i, keyChunk := range keys {
if i == 0 && keyChunk != request.Header.Get("X-Real-IP") {
t.Errorf("The (%v) chunk should be remote IP. KeyChunk: %v", i+1, keyChunk)
}
if i == 1 && keyChunk != request.URL.Path {
t.Errorf("The (%v) chunk should be request path. KeyChunk: %v", i+1, keyChunk)
}
if i == 2 && keyChunk != "GET" {
t.Errorf("The (%v) chunk should be request method. KeyChunk: %v", i+1, keyChunk)
}
if i == 3 && keyChunk != "X-Auth-Token" {
t.Errorf("The (%v) chunk should be request header. KeyChunk: %v", i+1, keyChunk)
}
if i == 4 && (keyChunk != "totally-top-secret" && keyChunk != "another-secret") {
t.Errorf("The (%v) chunk should be request path. KeyChunk: %v", i+1, keyChunk)
}
if i == 5 && keyChunk != "bro" {
t.Errorf("The (%v) chunk should be basic auth user. KeyChunk: %v", i+1, keyChunk)
}
}
}
}
func TestLimitHandler(t *testing.T) {
limiter := config.NewLimiter(1, time.Second)
limiter.IPLookups = []string{"X-Real-IP", "RemoteAddr", "X-Forwarded-For"}
limiter.Methods = []string{"POST"}
handler := LimitHandler(limiter, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`hello world`))
}))
req, err := http.NewRequest("POST", "/doesntmatter", nil)
if err != nil {
t.Fatal(err)
}
req.Header.Set("X-Real-IP", "2601:7:1c82:4097:59a0:a80b:2841:b8c8")
rr := httptest.NewRecorder()
go func() {
handler.ServeHTTP(rr, req)
// Should not be limited
if status := rr.Code; status != http.StatusOK {
t.Errorf("handler returned wrong status code: got %v want %v", status, http.StatusOK)
}
}()
handler.ServeHTTP(rr, req)
//Should be limited
if status := rr.Code; status != http.StatusTooManyRequests {
t.Errorf("handler returned wrong status code: got %v want %v", status, http.StatusTooManyRequests)
}
}