generated from traefik/plugindemo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathratelimiter_test.go
162 lines (144 loc) · 3.99 KB
/
ratelimiter_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
//nolint:stylecheck,revive
package traefik_ratelimiter_middleware
import (
"context"
"net/http"
"net/http/httptest"
"testing"
)
type TestServeHTTP struct {
haveResponse int
rateLimiterURL string
requestURL string
haveDryRun bool
expectCall bool
expectResponse int
expectBody string
expectHost string
expectPath string
}
func TestBypassIfRatelimiterConfigIsMalformed(t *testing.T) {
RunTestServeHTTP(t, TestServeHTTP{
rateLimiterURL: "https://api.xxx.com/cgi-bin/%%32%65%%32%65/%%32%65%%32%65/%%32%65%%",
expectCall: true,
expectResponse: http.StatusOK,
expectBody: "OK",
})
}
func TestFailedCallingRateLimiter(t *testing.T) {
RunTestServeHTTP(t, TestServeHTTP{
rateLimiterURL: "127.0.0.1",
haveResponse: 0, // cause failure
expectCall: true,
expectResponse: http.StatusOK,
expectBody: "OK",
})
}
func TestDeniedByRateLimiter(t *testing.T) {
RunTestServeHTTP(t, TestServeHTTP{
haveResponse: http.StatusTooManyRequests,
expectCall: false,
expectResponse: http.StatusTooManyRequests,
expectBody: "Too many requests\n",
})
}
func TestDeniedByRateLimiterDryRun(t *testing.T) {
RunTestServeHTTP(t, TestServeHTTP{
haveResponse: http.StatusTooManyRequests,
haveDryRun: true,
expectCall: true,
expectResponse: http.StatusOK,
expectBody: "OK",
})
}
func TestRateLimiterReturnsOk(t *testing.T) {
RunTestServeHTTP(t, TestServeHTTP{
haveResponse: http.StatusOK,
expectCall: true,
expectResponse: http.StatusOK,
expectBody: "OK",
})
}
func TestPathIsSent(t *testing.T) {
RunTestServeHTTP(t, TestServeHTTP{
requestURL: "https://test.test/v1/resource",
haveResponse: http.StatusOK,
expectCall: true,
expectResponse: http.StatusOK,
expectBody: "OK",
expectPath: "/v1/resource",
})
}
func TestDefaultPath(t *testing.T) {
RunTestServeHTTP(t, TestServeHTTP{
requestURL: "https://test.test",
haveResponse: http.StatusOK,
expectCall: true,
expectResponse: http.StatusOK,
expectBody: "OK",
expectPath: "/",
})
}
func TestHostIsSent(t *testing.T) {
RunTestServeHTTP(t, TestServeHTTP{
requestURL: "https://test-host.test",
haveResponse: http.StatusOK,
expectCall: true,
expectResponse: http.StatusOK,
expectBody: "OK",
expectHost: "test-host.test",
})
}
func RunTestServeHTTP(t *testing.T, tt TestServeHTTP) {
t.Helper()
ratelimiterServer := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
user, pass, ok := req.BasicAuth()
if !ok || user != "user" || pass != "pass" {
t.Error("unexpected user/password")
}
if tt.expectPath != "" && tt.expectPath != req.URL.Path {
t.Errorf("got path `%s` but expected `%s`", req.URL.Path, tt.expectPath)
}
if tt.expectHost != "" && tt.expectHost != req.Host {
t.Errorf("got host `%s` but expected `%s`", req.Host, tt.expectHost)
}
rw.WriteHeader(tt.haveResponse)
_, _ = rw.Write([]byte(http.StatusText(tt.haveResponse)))
}))
defer ratelimiterServer.Close()
url := ratelimiterServer.URL
if url == "" {
url = tt.rateLimiterURL
}
called := false
rl, err := New(context.TODO(), http.HandlerFunc(func(rw http.ResponseWriter, _ *http.Request) {
called = true
rw.WriteHeader(http.StatusOK)
_, _ = rw.Write([]byte("OK"))
}), &Config{
URL: url,
DryRun: tt.haveDryRun,
}, "")
if err != nil {
t.Error(err)
}
if tt.requestURL == "" {
tt.requestURL = "https://some.url.com"
}
req, err := http.NewRequest(http.MethodGet, tt.requestURL, nil)
if err != nil {
t.Errorf("couldn't create request: %v", err)
}
req.SetBasicAuth("user", "pass")
rw := httptest.NewRecorder()
rl.ServeHTTP(rw, req)
if tt.expectCall != called {
t.Errorf("expectCall(%t)!=called(%t)", tt.expectCall, called)
}
if tt.expectResponse != rw.Code {
t.Errorf("tt.expectResponse(%d)!=rw.Code(%d)", tt.expectResponse, rw.Code)
}
if tt.expectBody != rw.Body.String() {
t.Errorf("tt.expectBody(%s)!=rw.Body(%s)", tt.expectBody, rw.Body.String())
}
}