forked from TykTechnologies/tyk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mw_api_rate_limit_test.go
214 lines (185 loc) · 5.1 KB
/
mw_api_rate_limit_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
package main
import (
"net/http"
"net/http/httptest"
"net/url"
"testing"
"time"
"github.com/justinas/alice"
"github.com/satori/go.uuid"
"github.com/TykTechnologies/tyk/user"
)
func createRLSession() *user.SessionState {
session := new(user.SessionState)
// essentially non-throttled
session.Rate = 100.0
session.Allowance = session.Rate
session.LastCheck = time.Now().Unix()
session.Per = 1.0
session.QuotaRenewalRate = 300 // 5 minutes
session.QuotaRenews = time.Now().Unix()
session.QuotaRemaining = 10
session.QuotaMax = 10
session.AccessRights = map[string]user.AccessDefinition{"31445455": {APIName: "Tyk Auth Key Test", APIID: "31445455", Versions: []string{"default"}}}
return session
}
func getRLOpenChain(spec *APISpec) http.Handler {
remote, _ := url.Parse(spec.Proxy.TargetURL)
proxy := TykNewSingleHostReverseProxy(remote, spec)
proxyHandler := ProxyHandler(proxy, spec)
baseMid := BaseMiddleware{spec, proxy}
chain := alice.New(mwList(
&IPWhiteListMiddleware{baseMid},
&IPBlackListMiddleware{BaseMiddleware: baseMid},
&VersionCheck{BaseMiddleware: baseMid},
&RateLimitForAPI{BaseMiddleware: baseMid},
)...).Then(proxyHandler)
return chain
}
func getGlobalRLAuthKeyChain(spec *APISpec) http.Handler {
remote, _ := url.Parse(spec.Proxy.TargetURL)
proxy := TykNewSingleHostReverseProxy(remote, spec)
proxyHandler := ProxyHandler(proxy, spec)
baseMid := BaseMiddleware{spec, proxy}
chain := alice.New(mwList(
&IPWhiteListMiddleware{baseMid},
&IPBlackListMiddleware{BaseMiddleware: baseMid},
&AuthKey{baseMid},
&VersionCheck{BaseMiddleware: baseMid},
&KeyExpired{baseMid},
&AccessRightsCheck{baseMid},
&RateLimitForAPI{BaseMiddleware: baseMid},
&RateLimitAndQuotaCheck{baseMid},
)...).Then(proxyHandler)
return chain
}
func TestRLOpen(t *testing.T) {
spec := createSpecTest(t, openRLDefSmall)
req := testReq(t, "GET", "/rl_test/", nil)
DRLManager.CurrentTokenValue = 1
DRLManager.RequestTokenValue = 1
chain := getRLOpenChain(spec)
for a := 0; a <= 10; a++ {
recorder := httptest.NewRecorder()
chain.ServeHTTP(recorder, req)
if a < 3 {
if recorder.Code != 200 {
t.Fatalf("Rate limit kicked in too early, after only %v requests", a)
}
}
if a > 7 {
if recorder.Code != 429 {
t.Fatalf("Rate limit did not activate, code was: %v", recorder.Code)
}
}
}
DRLManager.CurrentTokenValue = 0
DRLManager.RequestTokenValue = 0
}
func TestRLClosed(t *testing.T) {
spec := createSpecTest(t, closedRLDefSmall)
req := testReq(t, "GET", "/rl_closed_test/", nil)
session := createRLSession()
customToken := uuid.NewV4().String()
// AuthKey sessions are stored by {token}
spec.SessionManager.UpdateSession(customToken, session, 60, false)
req.Header.Set("authorization", "Bearer "+customToken)
DRLManager.CurrentTokenValue = 1
DRLManager.RequestTokenValue = 1
chain := getGlobalRLAuthKeyChain(spec)
for a := 0; a <= 10; a++ {
recorder := httptest.NewRecorder()
chain.ServeHTTP(recorder, req)
if a < 3 {
if recorder.Code != 200 {
t.Fatalf("Rate limit kicked in too early, after only %v requests", a)
}
}
if a > 7 {
if recorder.Code != 429 {
t.Fatalf("Rate limit did not activate, code was: %v", recorder.Code)
}
}
}
DRLManager.CurrentTokenValue = 0
DRLManager.RequestTokenValue = 0
}
func TestRLOpenWithReload(t *testing.T) {
spec := createSpecTest(t, openRLDefSmall)
req := testReq(t, "GET", "/rl_test/", nil)
DRLManager.CurrentTokenValue = 1
DRLManager.RequestTokenValue = 1
chain := getRLOpenChain(spec)
for a := 0; a <= 10; a++ {
recorder := httptest.NewRecorder()
chain.ServeHTTP(recorder, req)
if a < 3 {
if recorder.Code != 200 {
t.Fatalf("Rate limit (pre change) kicked in too early, after only %v requests", a)
}
}
if a > 7 {
if recorder.Code != 429 {
t.Fatalf("Rate limit (pre change) did not activate, code was: %v", recorder.Code)
}
}
}
// CHange rate and emulate a reload
spec.GlobalRateLimit.Rate = 20
chain = getRLOpenChain(spec)
for a := 0; a <= 30; a++ {
recorder := httptest.NewRecorder()
chain.ServeHTTP(recorder, req)
if a < 20 {
if recorder.Code != 200 {
t.Fatalf("Rate limit (post change) kicked in too early, after only %v requests", a)
}
}
if a > 23 {
if recorder.Code != 429 {
t.Fatalf("Rate limit (post change) did not activate, code was: %v", recorder.Code)
}
}
}
DRLManager.CurrentTokenValue = 0
DRLManager.RequestTokenValue = 0
}
const openRLDefSmall = `{
"api_id": "313232",
"org_id": "default",
"auth": {"auth_header_name": "authorization"},
"use_keyless": true,
"version_data": {
"not_versioned": true,
"versions": {
"v1": {"name": "v1"}
}
},
"proxy": {
"listen_path": "/rl_test/",
"target_url": "` + testHttpAny + `"
},
"global_rate_limit": {
"rate": 3,
"per": 1
}
}`
const closedRLDefSmall = `{
"api_id": "31445455",
"org_id": "default",
"auth": {"auth_header_name": "authorization"},
"version_data": {
"not_versioned": true,
"versions": {
"v1": {"name": "v1"}
}
},
"proxy": {
"listen_path": "/rl_closed_test/",
"target_url": "` + testHttpAny + `"
},
"global_rate_limit": {
"rate": 3,
"per": 1
}
}`