This repository has been archived by the owner on Feb 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 67
/
ratelimit_test.go
76 lines (57 loc) · 2.06 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
package strava
import (
"net/http"
"testing"
)
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.RequestTime.IsZero() {
t.Errorf("rate limiting should set request time")
}
if v := RateLimiting.FractionReached(); v != 0.5 {
t.Errorf("fraction of rate limit computed incorrectly, got %v", v)
}
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.RequestTime.IsZero() {
t.Errorf("rate limiting should set request time")
}
if v := RateLimiting.FractionReached(); v != 0.9 {
t.Errorf("fraction of rate limit computed incorrectly, got %v", v)
}
// 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.RequestTime.IsZero() {
t.Errorf("nonsense in rate limiting fields should set next reset to zero")
}
}
func TestRateLimitExceeded(t *testing.T) {
RateLimiting.LimitLong = 1
RateLimiting.UsageLong = 0
RateLimiting.LimitShort = 100
RateLimiting.UsageShort = 200
if RateLimiting.Exceeded() != true {
t.Errorf("should have exceeded rate limit")
}
RateLimiting.LimitShort = 200
RateLimiting.UsageShort = 100
if RateLimiting.Exceeded() == true {
t.Errorf("should not have exceeded rate limit")
}
RateLimiting.LimitShort = 1
RateLimiting.UsageShort = 0
RateLimiting.LimitLong = 100
RateLimiting.UsageLong = 200
if RateLimiting.Exceeded() != true {
t.Errorf("should have exceeded rate limit")
}
RateLimiting.LimitLong = 200
RateLimiting.UsageLong = 100
if RateLimiting.Exceeded() == true {
t.Errorf("should not have exceeded rate limit")
}
}