-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugin_test.go
More file actions
52 lines (42 loc) · 1.33 KB
/
Copy pathplugin_test.go
File metadata and controls
52 lines (42 loc) · 1.33 KB
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
package traefikplugin_test
import (
"context"
"net/http"
"net/http/httptest"
"testing"
"github.com/tmarshall98/traefikplugin"
)
func TestDemo(t *testing.T) {
cfg := traefikplugin.CreateConfig()
cfg.Headers["X-Host"] = "[[.Host]]"
cfg.Headers["X-Method"] = "[[.Method]]"
cfg.Headers["X-URL"] = "[[.URL]]"
cfg.Headers["X-URL"] = "[[.URL]]"
cfg.Headers["X-Test-FromHeader"] = `[[ .Header.Get "Cf-Connecting-Ip"]]`
cfg.Headers["X-Plugin"] = "test"
ctx := context.Background()
next := http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {})
handler, err := traefikplugin.New(ctx, next, cfg, "demo-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.Header.Set("Cf-Connecting-Ip", "TEST")
handler.ServeHTTP(recorder, req)
assertHeader(t, req, "X-Host", "localhost")
assertHeader(t, req, "X-URL", "http://localhost")
assertHeader(t, req, "X-Method", "GET")
assertHeader(t, req, "X-Plugin", "test")
assertHeader(t, req, "X-Test-FromHeader", "TEST")
}
func assertHeader(t *testing.T, req *http.Request, key, expected string) {
t.Helper()
got := req.Header.Get(key)
if got != expected {
t.Errorf("invalid header value: got=%s, expected=%s", got, req.Header.Get(key))
}
}