-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfifteen_test.go
76 lines (65 loc) · 2.32 KB
/
fifteen_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 traefik_plugins
import (
"context"
"net/http"
"net/http/httptest"
"testing"
)
func TestDemo(t *testing.T) {
cfg := CreateConfig()
cfg.JwtHeaderName = "X-ApiKey"
cfg.JwtField = "customer_id"
cfg.ValueHeaderName = "X-UserId-RateLimit"
cfg.Fallbacks = []Fallback{
{Type: FallbackIp},
}
cfg.Debug = false
ctx := context.Background()
next := http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {})
handler, err := New(ctx, next, cfg, "traefik-plugins")
if err != nil {
t.Fatal(err)
}
recorder := httptest.NewRecorder()
// No JWT, fallback to IP
req, err := http.NewRequestWithContext(ctx, http.MethodGet, "http://localhost", nil)
if err != nil {
t.Fatal(err)
}
req.RemoteAddr = "1.2.3.4"
handler.ServeHTTP(recorder, req)
assertHeader(t, req, "X-UserId-RateLimit", "1.2.3.4")
// Invalid JWT, fallback to IP
req, err = http.NewRequestWithContext(ctx, http.MethodGet, "http://localhost", nil)
if err != nil {
t.Fatal(err)
}
req.RemoteAddr = "1.2.3.4"
req.Header.Set(cfg.JwtHeaderName, "invalid")
handler.ServeHTTP(recorder, req)
assertHeader(t, req, "X-UserId-RateLimit", "1.2.3.4")
// Valid JWT, field value missing, fallback to IP
req, err = http.NewRequestWithContext(ctx, http.MethodGet, "http://localhost", nil)
if err != nil {
t.Fatal(err)
}
req.RemoteAddr = "1.2.3.4"
req.Header.Set(cfg.JwtHeaderName, "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJUZXN0IGlzc3VlciIsImlhdCI6MTY5NDA5ODQ3NiwiZXhwIjoxNzI1NjM0NDc2LCJhdWQiOiIiLCJzdWIiOiIifQ.898seJ3c8Quryhtwwt_66m_iJQwRVCtt216l1KOhBp8")
handler.ServeHTTP(recorder, req)
assertHeader(t, req, "X-UserId-RateLimit", "1.2.3.4")
// Valid JWT, field value as header
req, err = http.NewRequestWithContext(ctx, http.MethodGet, "http://localhost", nil)
if err != nil {
t.Fatal(err)
}
req.RemoteAddr = "1.2.3.4"
req.Header.Set(cfg.JwtHeaderName, "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJUZXN0IGlzc3VlciIsImlhdCI6MTY5NDA5NzQzMywiZXhwIjoxNzI1NjMzNDMzLCJhdWQiOiIiLCJzdWIiOiIiLCJjdXN0b21lcl9pZCI6InNvbWVfdXNlcl9pZCJ9.MuJhmnrPeEsDqcnz3PnTGnY5Z5Zu2nna9FjQF0Me9qU")
handler.ServeHTTP(recorder, req)
assertHeader(t, req, "X-UserId-RateLimit", "some_user_id")
}
func assertHeader(t *testing.T, req *http.Request, key, expected string) {
t.Helper()
if req.Header.Get(key) != expected {
t.Errorf("invalid header value: %s", req.Header.Get(key))
}
}