-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlimitterDatastore_test.go
208 lines (181 loc) · 7.03 KB
/
limitterDatastore_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
package limitter
import (
"fmt"
"net/http"
"time"
"cloud.google.com/go/datastore"
"github.com/gin-gonic/gin"
"testing"
log "github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
)
var dsClient *datastore.Client
const DatastoreKindRequestTracker string = "test_request_trackers"
const FieldNameUserId string = "userId"
// go test -timeout 30s -run ^TestLimitter_ValidGETRequest_Correct$ github.com/zeroboo/gin-request-limitter -v
func TestLimitter_ValidGETRequest_Correct(t *testing.T) {
recorder := RecordRequest(http.MethodGet,
"/health",
map[string][]string{},
map[string][]string{},
CreateFakeAuthenticationHandler(FieldNameUserId, "testUser"),
CreateDatastoreBackedLimitterHandler(dsClient,
DatastoreKindRequestTracker,
GetUserIdFromContextByField(FieldNameUserId),
200, 60000, 10, 3600*24),
HandleHealth,
)
assert.Equal(t, http.StatusOK, recorder.Code, "Response success")
assert.Equal(t, "OK", recorder.Body.String(), "Response body OK")
}
var testHandlersSet []gin.HandlerFunc = []gin.HandlerFunc{
CreateFakeAuthenticationHandler(FieldNameUserId, "testUser"),
CreateDatastoreBackedLimitterHandler(dsClient,
DatastoreKindRequestTracker,
GetUserIdFromContextByField("userId"),
200, 60000, 10, 3600*24),
HandleHealth,
}
// go test -timeout 30s -run ^TestLimitter_MultiRequestTooFast_ResponseError$ github.com/zeroboo/gin-request-limitter -v
func TestLimitter_MultiRequestTooFast_ResponseError(t *testing.T) {
userId := "test-too-fast"
var minimumIntervalMilisecs int64 = 1000
url := "/health"
recorder := RecordRequest(http.MethodGet,
url,
map[string][]string{},
map[string][]string{},
CreateFakeAuthenticationHandler(FieldNameUserId, userId),
CreateDatastoreBackedLimitterHandler(dsClient,
DatastoreKindRequestTracker,
GetUserIdFromContextByField("userId"),
minimumIntervalMilisecs, 60000, 10, 3600*24),
HandleHealth,
)
assert.Equal(t, http.StatusOK, recorder.Code, "First response success")
assert.Equal(t, "OK", recorder.Body.String(), "First response body OK")
recorder2 := RecordRequest(http.MethodGet,
url,
map[string][]string{},
map[string][]string{},
CreateFakeAuthenticationHandler(FieldNameUserId, userId),
CreateDatastoreBackedLimitterHandler(dsClient,
DatastoreKindRequestTracker,
GetUserIdFromContextByField("userId"),
minimumIntervalMilisecs, 60000, 10, 3600*24),
HandleHealth,
)
assert.Equal(t, http.StatusTooEarly, recorder2.Code, "Second response's code is too early ")
//Test tracker
key, tracker, err := LoadUserTracker(dsClient, DatastoreKindRequestTracker, url, userId)
assert.Equal(t, err, nil, "Load tracker no error")
assert.Equal(t, "/test_request_trackers,cda5c99c0242bc5b3a0ecf309c672d14b24683f0", fmt.Sprintf("%v", key), "Correct key")
assert.Equal(t, url, tracker.URL, "Correct url")
assert.Equal(t, int64(1), tracker.WindowRequest, "Correct calls")
}
// go test -timeout 30s -run ^TestLimitter_MultiRequestNotTooFast_Success$ github.com/zeroboo/gin-request-limitter -v
func TestLimitter_MultiRequestNotTooFast_Success(t *testing.T) {
userId := fmt.Sprintf("test-too-fast-%v", time.Now().UnixMilli())
interval := int64(200)
recorder := RecordRequest(http.MethodGet,
"/health",
map[string][]string{},
map[string][]string{},
CreateFakeAuthenticationHandler(FieldNameUserId, userId),
CreateDatastoreBackedLimitterHandler(dsClient, DatastoreKindRequestTracker, GetUserIdFromContextByField("userId"),
interval, 0, 0, 3600*24),
HandleHealth,
)
assert.Equal(t, http.StatusOK, recorder.Code, "First response success")
assert.Equal(t, "OK", recorder.Body.String(), "First response body OK")
//Second request that respect interval restriction
time.Sleep(time.Millisecond * time.Duration(interval+100))
recorder2 := RecordRequest(http.MethodGet,
"/health",
map[string][]string{},
map[string][]string{},
CreateFakeAuthenticationHandler(FieldNameUserId, userId),
CreateDatastoreBackedLimitterHandler(dsClient,
DatastoreKindRequestTracker,
GetUserIdFromContextByField("userId"),
interval, 0, 0, 3600*24),
HandleHealth,
)
assert.Equal(t, http.StatusOK, recorder2.Code, "Second response's code is success ")
}
// go test -timeout 30s -run ^TestLimitter_TooFrequentlyRequests_ResponseError$ github.com/zeroboo/gin-request-limitter -v
func TestLimitter_TooFrequentlyRequests_ResponseError(t *testing.T) {
userId := fmt.Sprintf("test-too-fast-%v", time.Now().Unix())
interval := int64(10)
limitter := CreateDatastoreBackedLimitterHandler(dsClient, DatastoreKindRequestTracker,
GetUserIdFromContextByField("userId"),
interval, 10000, 1, 3600*24)
authHandler := CreateFakeAuthenticationHandler(FieldNameUserId, userId)
recorder := RecordRequest(http.MethodGet,
"/health",
map[string][]string{},
map[string][]string{},
authHandler,
limitter,
HandleHealth,
)
assert.Equal(t, http.StatusOK, recorder.Code, "First response success")
assert.Equal(t, "OK", recorder.Body.String(), "First response body OK")
time.Sleep(time.Millisecond * time.Duration(interval+1))
recorder2 := RecordRequest(http.MethodGet,
"/health",
map[string][]string{},
map[string][]string{},
authHandler,
limitter,
HandleHealth,
)
assert.Equal(t, http.StatusTooManyRequests, recorder2.Code, "Second response's code failed cause it too freequently")
}
// go test -timeout 30s -run ^TestLimitterTooFreequently_NewWindow_RequestSuccess$ github.com/zeroboo/gin-request-limitter -v
func TestLimitterTooFreequently_NewWindow_RequestSuccess(t *testing.T) {
userId := fmt.Sprintf("test-too-fast-%v", time.Now().Unix())
interval := int64(10)
windowSize := int64(1000)
limitter := CreateDatastoreBackedLimitterHandler(dsClient, DatastoreKindRequestTracker,
GetUserIdFromContextByField("userId"),
interval, windowSize, 1, 3600*24)
authHandler := CreateFakeAuthenticationHandler(FieldNameUserId, userId)
recorder := RecordRequest(http.MethodGet,
"/health",
map[string][]string{},
map[string][]string{},
authHandler,
limitter,
HandleHealth,
)
assert.Equal(t, http.StatusOK, recorder.Code, "First response success")
assert.Equal(t, "OK", recorder.Body.String(), "First response body OK")
recorder2 := RecordRequest(http.MethodGet,
"/health",
map[string][]string{},
map[string][]string{},
authHandler,
limitter,
HandleHealth,
)
assert.Equal(t, http.StatusTooManyRequests, recorder2.Code, "Second response's code failed cause it too freequently")
//Sleep to next time window
time.Sleep(time.Millisecond * time.Duration(windowSize+100))
recorder3 := RecordRequest(http.MethodGet,
"/health",
map[string][]string{},
map[string][]string{},
authHandler,
limitter,
HandleHealth,
)
assert.Equal(t, http.StatusOK, recorder3.Code, "Third request as next window, must be success")
}
// go test -timeout 30s -run ^TestUpdateTracker_WindowsIncreased$ github.com/zeroboo/gin-request-limitter -v
func TestUpdateTracker_WindowsIncreased(t *testing.T) {
var tracker *RequestTracker = NewRequestTrackerWithExpiration("uid", "url", time.Now().Add(100*time.Second))
config := &LimitterConfig{}
tracker.UpdateRequest(time.Now(), config)
log.Infof("Tracker: %v", tracker)
}