generated from traefik/plugindemo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin_test.go
58 lines (50 loc) · 1.27 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
package plugin
import (
"context"
"io"
"net/http"
"net/http/httptest"
"testing"
)
func TestInvalidConfig(t *testing.T) {
cfg := CreateConfig()
next := http.HandlerFunc(func(http.ResponseWriter, *http.Request) {})
_, err := New(context.Background(), next, cfg, "traefik-tls-headers-plugin")
if err == nil {
t.Fatal("expected error")
}
}
func TestTLSCipher(t *testing.T) {
cfg := CreateConfig()
cfg.Headers.Cipher = "X-Tls-Cipher"
next := http.HandlerFunc(func(_ http.ResponseWriter, r *http.Request) {
assertHeader(t, r.Header, "X-Tls-Cipher", "TLS_AES_128_GCM_SHA256")
})
handler, err := New(context.Background(), next, cfg, "traefik-tls-headers-plugin")
if err != nil {
t.Fatal(err)
}
server := httptest.NewTLSServer(handler)
defer server.Close()
req, err := http.NewRequest(http.MethodGet, server.URL, nil)
if err != nil {
t.Fatal(err)
}
client := server.Client()
resp, err := client.Do(req)
if err != nil {
t.Fatal(err)
}
defer func(Body io.ReadCloser) {
err := Body.Close()
if err != nil {
t.Fatal(err)
}
}(resp.Body)
}
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))
}
}