forked from strava/go.strava
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathratelimit_test.go
164 lines (140 loc) · 5.95 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
package strava
import (
"net/http"
"testing"
"time"
)
func TestRateLimitUpdating(t *testing.T) {
var resp http.Response
resp.StatusCode = 200
resp.Header = http.Header{"Date": []string{"Tue, 10 Oct 2013 20:11:05 GMT"}, "X-Ratelimit-Limit": []string{"600,30000"}, "X-Ratelimit-Usage": []string{"300,10000"}}
RateLimiting.updateRateLimits(&resp)
if !RateLimiting.NextRequestTime.IsZero() {
t.Errorf("rate limiting didn't set zero time", RateLimiting.NextRequestTime) // non-zero is set only when rate limit is reached
}
if RateLimiting.FractionReached() != 0.5 {
t.Errorf("fraction of rate limit computed incorrectly: %v != %v", RateLimiting.FractionReached(), 0.5)
}
resp.Header = http.Header{"Date": []string{"Tue, 10 Oct 2013 20:11:05 GMT"}, "X-Ratelimit-Limit": []string{"600,30000"}, "X-Ratelimit-Usage": []string{"300,27000"}}
RateLimiting.updateRateLimits(&resp)
if !RateLimiting.NextRequestTime.IsZero() {
t.Errorf("rate limiting didn't set zero time", RateLimiting.NextRequestTime) // non-zero is set only when rate limit is reached
}
if RateLimiting.FractionReached() != 0.9 {
t.Errorf("fraction of rate limit computed incorrectly: %v != %v", RateLimiting.FractionReached(), 0.9)
}
// we'll feed it nonsense
resp.Header = http.Header{"Date": []string{"Tue, 10 Oct 2013 20:11:05 GMT"}, "X-Ratelimit-Limit": []string{"xxx"}, "X-Ratelimit-Usage": []string{"zzz"}}
RateLimiting.updateRateLimits(&resp)
if !RateLimiting.NextRequestTime.IsZero() {
t.Errorf("nonsense in rate limiting fields should set next reset to zero")
}
// rate limit reached - short
resp.Header = http.Header{"Date": []string{"Tue, 10 Oct 2013 20:11:05 GMT"}, "X-Ratelimit-Limit": []string{"600,30000"}, "X-Ratelimit-Usage": []string{"650,20000"}}
RateLimiting.updateRateLimits(&resp)
if RateLimiting.NextRequestTime.IsZero() {
t.Errorf("reaching rate limit should set non-zero value", RateLimiting.NextRequestTime)
}
// rate limit reached - long
resp.Header = http.Header{"Date": []string{"Tue, 10 Oct 2013 20:11:05 GMT"}, "X-Ratelimit-Limit": []string{"600,30000"}, "X-Ratelimit-Usage": []string{"550,40000"}}
RateLimiting.updateRateLimits(&resp)
if RateLimiting.NextRequestTime.IsZero() {
t.Errorf("reaching rate limit should set non-zero value", RateLimiting.NextRequestTime)
}
}
func TestNextRateLimit(t *testing.T) {
// SHORT
// normal time
currentTime, err := time.Parse(http.TimeFormat, "Tue, 10 Oct 2013 20:11:05 GMT")
if err != nil {
t.Errorf("error parsing date")
}
expectedTime, err := time.Parse(http.TimeFormat, "Tue, 10 Oct 2013 20:15:00 GMT")
if err != nil {
t.Errorf("error parsing date")
}
RateLimiting.NextRequestTime = getNextRateLimitShort(currentTime, currentTime)
if expectedTime != RateLimiting.NextRequestTime {
t.Errorf("didn't set correct next request time\n%v\n%v", expectedTime, RateLimiting.NextRequestTime)
}
// lowest time
currentTime, err = time.Parse(http.TimeFormat, "Tue, 10 Oct 2013 00:00:01 GMT")
if err != nil {
t.Errorf("error parsing date")
}
expectedTime, err = time.Parse(http.TimeFormat, "Tue, 10 Oct 2013 00:15:00 GMT")
if err != nil {
t.Errorf("error parsing date")
}
RateLimiting.NextRequestTime = getNextRateLimitShort(currentTime, currentTime)
if expectedTime != RateLimiting.NextRequestTime {
t.Errorf("didn't set correct next request time\n%v\n%v", expectedTime, RateLimiting.NextRequestTime)
}
// highest time
currentTime, err = time.Parse(http.TimeFormat, "Tue, 10 Oct 2013 23:59:59 GMT")
if err != nil {
t.Errorf("error parsing date")
}
expectedTime, err = time.Parse(http.TimeFormat, "Tue, 11 Oct 2013 00:00:00 GMT")
if err != nil {
t.Errorf("error parsing date")
}
RateLimiting.NextRequestTime = getNextRateLimitShort(currentTime, currentTime)
if expectedTime != RateLimiting.NextRequestTime {
t.Errorf("didn't set correct next request time\n%v\n%v", expectedTime, RateLimiting.NextRequestTime)
}
// LONG
// normal time
currentTime, err = time.Parse(http.TimeFormat, "Tue, 10 Oct 2013 20:11:05 GMT")
if err != nil {
t.Errorf("error parsing date")
}
expectedTime, err = time.Parse(http.TimeFormat, "Tue, 11 Oct 2013 00:00:00 GMT")
if err != nil {
t.Errorf("error parsing date")
}
RateLimiting.NextRequestTime = getNextRateLimitLong(currentTime, currentTime)
if expectedTime != RateLimiting.NextRequestTime {
t.Errorf("didn't set correct next request time\n%v\n%v", expectedTime, RateLimiting.NextRequestTime)
}
// lowest time
currentTime, err = time.Parse(http.TimeFormat, "Tue, 10 Oct 2013 00:00:01 GMT")
if err != nil {
t.Errorf("error parsing date")
}
expectedTime, err = time.Parse(http.TimeFormat, "Tue, 11 Oct 2013 00:00:00 GMT")
if err != nil {
t.Errorf("error parsing date")
}
RateLimiting.NextRequestTime = getNextRateLimitLong(currentTime, currentTime)
if expectedTime != RateLimiting.NextRequestTime {
t.Errorf("didn't set correct next request time\n%v\n%v", expectedTime, RateLimiting.NextRequestTime)
}
// highest time
currentTime, err = time.Parse(http.TimeFormat, "Tue, 10 Oct 2013 23:59:59 GMT")
if err != nil {
t.Errorf("error parsing date")
}
expectedTime, err = time.Parse(http.TimeFormat, "Tue, 11 Oct 2013 00:00:00 GMT")
if err != nil {
t.Errorf("error parsing date")
}
RateLimiting.NextRequestTime = getNextRateLimitLong(currentTime, currentTime)
if expectedTime != RateLimiting.NextRequestTime {
t.Errorf("didn't set correct next request time\n%v\n%v", expectedTime, RateLimiting.NextRequestTime)
}
}
func TestRateLimitChecking(t *testing.T) {
RateLimiting.NextRequestTime = time.Time{}
if RateLimiting.Exceeded() == true {
t.Errorf("rate limiting didn't allow request but should have")
}
RateLimiting.NextRequestTime = time.Now().Add(time.Duration(-5 * time.Second))
if RateLimiting.Exceeded() == true {
t.Errorf("rate limiting didn't allow request but should have")
}
RateLimiting.NextRequestTime = time.Now().Add(time.Duration(5 * time.Second))
if RateLimiting.Exceeded() == false {
t.Errorf("rate limiting did allow request but shouldn't have")
}
}