-
Notifications
You must be signed in to change notification settings - Fork 1
/
router_test.go
57 lines (44 loc) · 1.49 KB
/
router_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
package traefik_routing_plugin_test
import (
"context"
"net/http"
"net/http/httptest"
"testing"
"github.com/kumina/traefik-routing-plugin"
)
func TestRouter(t *testing.T) {
ctx := context.Background()
next := http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {})
cfg := traefik_routing_plugin.CreateConfig()
cfg.Routes["Route-To-Service"] = "Dummy"
cfg.Routes["Route-To-Service-Test"] = "DummyTest"
cfg.Routes["Route-To-Service-Lambda"] = "Lambda"
handler, err := traefik_routing_plugin.New(ctx, next, cfg, "traefik-routing-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)
}
handler.ServeHTTP(recorder, req)
assertHeader(t, req, "Route-To-Service", "Dummy")
assertHeader(t, req, "Route-To-Service-Test", "DummyTest")
assertHeader(t, req, "Route-To-Service-Lambda", "Lambda")
recorder = httptest.NewRecorder()
req, err = http.NewRequestWithContext(ctx, http.MethodGet, "http://localhost/asd", nil)
if err != nil {
t.Fatal(err)
}
handler.ServeHTTP(recorder, req)
assertHeader(t, req, "Route-To-Service", "Dummy")
assertHeader(t, req, "Route-To-Service-Test", "DummyTest")
assertHeader(t, req, "Route-To-Service-Lambda", "Lambda")
}
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))
}
}