generated from traefik/plugindemo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_test.go
60 lines (52 loc) · 1.49 KB
/
main_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
package traefikpluginhcindex_test
import (
"context"
"log"
"net/http"
"net/http/httptest"
"strings"
"testing"
traefikplugin_hc_index "github.com/rjop-hccgt/traefikpluginhcindex"
)
func TestHcIndex(t *testing.T) {
cfg := traefikplugin_hc_index.CreateConfig()
ctx := context.Background()
next := http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {})
handler, err := traefikplugin_hc_index.New(ctx, next, cfg, "hc-index-test")
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)
assertIndex(t, req)
req, err = http.NewRequestWithContext(ctx, http.MethodGet, "http://localhost/folder1", nil)
if err != nil {
t.Fatal(err)
}
handler.ServeHTTP(recorder, req)
assertIndex(t, req)
req, err = http.NewRequestWithContext(ctx, http.MethodGet, "http://localhost/folder1/style.css", nil)
if err != nil {
t.Fatal(err)
}
handler.ServeHTTP(recorder, req)
assertNonIndex(t, req)
}
func assertIndex(t *testing.T, req *http.Request) {
t.Helper()
log.Printf("Validating %v", req.URL.Path)
if !strings.HasSuffix(req.URL.Path, "/index.html") {
t.Errorf("invalid path value: %s", req.URL.Path)
}
}
func assertNonIndex(t *testing.T, req *http.Request) {
t.Helper()
log.Printf("Validating %v", req.URL.Path)
if strings.HasSuffix(req.URL.Path, "/index.html") {
t.Errorf("invalid path value: %s", req.URL.Path)
}
}