-
Notifications
You must be signed in to change notification settings - Fork 1
/
http_test.go
145 lines (128 loc) · 3.83 KB
/
http_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
package redis_rate_limiter
import (
"github.com/alicebob/miniredis/v2"
"github.com/go-redis/redis/v8"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"io"
"net/http"
"net/http/httptest"
"testing"
"time"
)
const (
forwardedFor = "X-Forwarded-For"
)
var (
_ http.Handler = &handleFuncWrapper{}
)
type handleFuncWrapper struct {
handleFunc http.HandlerFunc
}
func (h *handleFuncWrapper) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
h.handleFunc(writer, request)
}
func TestNewHTTPHeadersExtractor(t *testing.T) {
tt := []struct {
name string
builder func(r *http.Request)
totalRequests int
config func(client *redis.Client, now func() time.Time) *RateLimiterConfig
advance time.Duration
lastResponseStatus int
matchedHeaders map[string]string
}{
{
name: "a request that is not rate limited",
builder: func(r *http.Request) {
r.Header.Set(forwardedFor, "10.10.10.10")
},
totalRequests: 10,
lastResponseStatus: http.StatusOK,
advance: time.Second,
matchedHeaders: map[string]string{
rateLimitingState: "Allow",
rateLimitingTotalRequests: "10",
},
config: func(client *redis.Client, now func() time.Time) *RateLimiterConfig {
return &RateLimiterConfig{
Extractor: NewHTTPHeadersExtractor(forwardedFor),
Strategy: NewCounterStrategy(client, now),
Expiration: time.Minute,
MaxRequests: 50,
}
},
},
{
name: "a request that is rate limited",
builder: func(r *http.Request) {
r.Header.Set(forwardedFor, "10.10.10.10")
},
totalRequests: 55,
lastResponseStatus: http.StatusTooManyRequests,
advance: time.Second,
matchedHeaders: map[string]string{
rateLimitingState: "Deny",
rateLimitingTotalRequests: "50",
},
config: func(client *redis.Client, now func() time.Time) *RateLimiterConfig {
return &RateLimiterConfig{
Extractor: NewHTTPHeadersExtractor(forwardedFor),
Strategy: NewSortedSetCounterStrategy(client, now),
Expiration: time.Minute,
MaxRequests: 50,
}
},
},
{
name: "a request that fails because of missing headers",
builder: func(r *http.Request) {
r.Header.Set("User-Agent", "Netscape Navigator")
},
totalRequests: 1,
lastResponseStatus: http.StatusBadRequest,
advance: time.Second,
config: func(client *redis.Client, now func() time.Time) *RateLimiterConfig {
return &RateLimiterConfig{
Extractor: NewHTTPHeadersExtractor(forwardedFor),
Strategy: NewSortedSetCounterStrategy(client, now),
Expiration: time.Minute,
MaxRequests: 50,
}
},
},
}
for _, ts := range tt {
t.Run(ts.name, func(t *testing.T) {
server, err := miniredis.Run()
require.NoError(t, err)
defer server.Close()
client := redis.NewClient(&redis.Options{
Addr: server.Addr(),
})
defer client.Close()
now := time.Now()
nowGenerator := func() time.Time {
return now
}
handler := func(w http.ResponseWriter, r *http.Request) {
io.WriteString(w, "<html><body>Request received!</body></html>")
}
wrapper := NewHTTPRateLimiterHandler(&handleFuncWrapper{handleFunc: handler}, ts.config(client, nowGenerator))
var lastResponse *http.Response
for x := 0; x < ts.totalRequests; x++ {
req := httptest.NewRequest(http.MethodGet, "http://example.com/foo", nil)
ts.builder(req)
w := httptest.NewRecorder()
wrapper.ServeHTTP(w, req)
lastResponse = w.Result()
now = now.Add(ts.advance)
}
assert.Equal(t, ts.lastResponseStatus, lastResponse.StatusCode)
for key, value := range ts.matchedHeaders {
got := lastResponse.Header.Get(key)
assert.Equalf(t, value, got, "expected header %v to have value %v but was %v", key, value, got)
}
})
}
}