This repository has been archived by the owner on Jan 31, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathratelimit_test.go
181 lines (147 loc) · 5.41 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package ratelimit
import (
"sync"
"testing"
"time"
"github.com/caddyserver/caddy/v2"
)
func Test_rateLimitOptions_refreshWindows(t *testing.T) {
t.Run("Should refresh", func(t *testing.T) {
RateLimit := RateLimit{
WindowLength: 15 * 60,
currentWindow: &RequestCountTracker{
requestCount: map[string]int64{},
startTime: time.Now().Add(-35 * time.Minute),
endTime: time.Now().Add(-20 * time.Minute),
_mutex: &sync.RWMutex{},
},
}
if didRefresh := RateLimit.refreshWindows(); !didRefresh {
t.Errorf("Should have refreshed, but did not: %+v", RateLimit)
}
})
t.Run("Should not refresh", func(t *testing.T) {
RateLimit := RateLimit{
WindowLength: 60,
currentWindow: &RequestCountTracker{
requestCount: map[string]int64{},
startTime: time.Now().Add(-30 * time.Minute),
endTime: time.Now().Add(30 * time.Minute),
_mutex: &sync.RWMutex{},
},
}
if didRefresh := RateLimit.refreshWindows(); didRefresh {
t.Errorf("Should not have refreshed, but did: %+v", RateLimit)
}
})
}
func Test_rateLimitOptions_blockingAndRequestCounting(t *testing.T) {
// in this we test the case described in the documentation for
// getInterpolatedRequestCount()
hostName := "10.0.0.127"
rl := RateLimit{
ByHeader: "",
WindowLength: caddy.Duration(20 * time.Minute),
MaxRequests: 200,
}
rl.Provision(caddy.Context{})
rl.currentWindow.requestCount[hostName] = 100
rl.currentWindow.startTime = rl.currentWindow.startTime.Add(-10 * time.Minute)
rl.currentWindow.endTime = rl.currentWindow.endTime.Add(-10 * time.Minute)
// start/end time doesn't really matter for previous window
rl.previousWindow = &RequestCountTracker{
requestCount: map[string]int64{hostName: 50},
_mutex: &sync.RWMutex{},
}
t.Run("50-50 split should interpolate to 75 requests", func(t *testing.T) {
// expected result is (100+50) / 2
if requestCount := rl.getInterpolatedRequestCount(hostName); requestCount != 75 {
t.Errorf("Expected requestCount of 75 for 50-50 split, got %v", requestCount)
}
})
t.Run("50-50 split should not block as 76 < 100", func(t *testing.T) {
if shouldBlock := rl.requestShouldBlock(hostName); shouldBlock {
t.Errorf("Well clear of max request count, should not block, got %+v", rl)
}
})
// test whether blocking works
rl.MaxRequests = 50
t.Run("50-50 split should block with now reduced maxRequest as 77 > 50", func(t *testing.T) {
if shouldBlock := rl.requestShouldBlock(hostName); !shouldBlock {
t.Errorf("Should have blocked with reduced maxRequests, did not, got %+v", rl)
}
})
}
func Test_rateLimitOptions_blockingAndRequestCountingByHeader(t *testing.T) {
header := "Authorization"
rl := RateLimit{
ByHeader: "",
WindowLength: caddy.Duration(20 * time.Minute),
MaxRequests: 200,
}
rl.Provision(caddy.Context{})
rl.currentWindow.requestCount[header] = 100
rl.currentWindow.startTime = rl.currentWindow.startTime.Add(-10 * time.Minute)
rl.currentWindow.endTime = rl.currentWindow.endTime.Add(-10 * time.Minute)
// start/end time doesn't really matter for previous window
rl.previousWindow = &RequestCountTracker{
requestCount: map[string]int64{header: 50},
_mutex: &sync.RWMutex{},
}
t.Run("50-50 split should interpolate to 75 requests", func(t *testing.T) {
// expected result is (100+50) / 2
if requestCount := rl.getInterpolatedRequestCount(header); requestCount != 75 {
t.Errorf("Expected requestCount of 75 for 50-50 split, got %v", requestCount)
}
})
t.Run("50-50 split should not block as 76 < 100", func(t *testing.T) {
if shouldBlock := rl.requestShouldBlock(header); shouldBlock {
t.Errorf("Well clear of max request count, should not block, got %+v", rl)
}
})
// test whether blocking works
rl.MaxRequests = 50
t.Run("50-50 split should block with now reduced maxRequest as 77 > 50", func(t *testing.T) {
if shouldBlock := rl.requestShouldBlock(header); !shouldBlock {
t.Errorf("Should have blocked with reduced maxRequests, did not, got %+v", rl)
}
})
}
func Test_rateLimitOptions_setupRateLimit(t *testing.T) {
t.Run("Should initialise properly", func(t *testing.T) {
rl := RateLimit{
ByHeader: "",
WindowLength: caddy.Duration(60 * time.Minute),
MaxRequests: 200,
}
rl.Provision(caddy.Context{})
hostName := "localhost"
if count := rl.currentWindow.getRequestCountFor(hostName); count != 0 {
t.Errorf("Unexpected request count - expected 0, got %v", count)
}
if count := rl.previousWindow.getRequestCountFor(hostName); count != 0 {
t.Errorf("Unexpected request count - expected 0, got %v", count)
}
})
// value of window kept very low for testing, minimum value should be 5 minutes
t.Run("Should shuffle windows after one second", func(t *testing.T) {
rl := RateLimit{
ByHeader: "",
WindowLength: caddy.Duration(1 * time.Second),
MaxRequests: 1000,
}
rl.Provision(caddy.Context{})
hostName := "localhost"
rl.requestShouldBlock(hostName)
if count := rl.currentWindow.getRequestCountFor(hostName); count != 1 {
t.Errorf("Unexpected request count - expected 1, got %v", count)
}
time.Sleep(1100 * time.Millisecond)
if count := rl.currentWindow.getRequestCountFor(hostName); count != 0 {
t.Errorf("Unexpected request count - expected 0, got %v", count)
}
if count := rl.previousWindow.getRequestCountFor(hostName); count != 1 {
t.Errorf("Unexpected request count - expected 1, got %v", count)
}
})
}