generated from traefik/plugindemo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin_test.go
94 lines (80 loc) · 2.12 KB
/
plugin_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
package plugin_test
import (
"context"
"net/http"
"net/http/httptest"
"testing"
plugin "github.com/RiskIdent/traefik-remoteaddr-plugin"
)
func TestInvalidConfig(t *testing.T) {
cfg := plugin.CreateConfig()
next := http.HandlerFunc(func(http.ResponseWriter, *http.Request) {})
_, err := plugin.New(context.Background(), next, cfg, "traefik-remoteaddr-plugin")
if err == nil {
t.Fatal("expected error")
}
}
func TestHeaderAddress(t *testing.T) {
tests := []struct {
name string
cfg plugin.ConfigHeaders
wantHeader string
wantValue string
}{
{
name: "full address",
cfg: plugin.ConfigHeaders{Address: "X-Real-Address"},
wantHeader: "X-Real-Address",
wantValue: "127.0.0.1:1234",
},
{
name: "only ip",
cfg: plugin.ConfigHeaders{IP: "X-Real-IP"},
wantHeader: "X-Real-IP",
wantValue: "127.0.0.1",
},
{
name: "only port",
cfg: plugin.ConfigHeaders{Port: "X-Real-Port"},
wantHeader: "X-Real-Port",
wantValue: "1234",
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
cfg := plugin.CreateConfig()
cfg.Headers = tc.cfg
req := testPlugin(t, cfg)
assertHeader(t, req.Header, tc.wantHeader, tc.wantValue)
})
}
}
func testPlugin(t *testing.T, cfg *plugin.Config) *http.Request {
t.Helper()
ctx := context.Background()
next := http.HandlerFunc(func(http.ResponseWriter, *http.Request) {})
handler, err := plugin.New(ctx, next, cfg, "traefik-remoteaddr-plugin")
if err != nil {
t.Fatal(err)
}
recorder := httptest.NewRecorder()
req, err := http.NewRequestWithContext(ctx, http.MethodGet, "http://localhost", nil)
if err != nil {
t.Fatal(err)
}
req.RemoteAddr = "127.0.0.1:1234"
handler.ServeHTTP(recorder, req)
t.Logf("request headers: %d", len(req.Header))
for k, vals := range req.Header {
for _, v := range vals {
t.Logf(" %s=%q", k, v)
}
}
return req
}
func assertHeader(t *testing.T, header http.Header, key, expected string) {
t.Helper()
if header.Get(key) != expected {
t.Errorf("invalid header value\nwant: %s=%q\ngot: %s=%q", key, expected, key, header.Get(key))
}
}