-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
route_test.go
56 lines (50 loc) · 1.37 KB
/
route_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
package zhttp
import (
"fmt"
"net/http"
"net/http/httptest"
"testing"
)
func TestHostRoute(t *testing.T) {
handler := HostRoute(map[string]http.Handler{
"example.com": http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "example.com")
}),
"test.example.com": http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "test.example.com")
}),
"*.example.com": http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "*.example.com")
}),
"rdr.example.com": RedirectHost("example.org"),
"*": http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "default")
}),
})
tests := []struct {
host string
want string
}{
{"example.com", "example.com"},
{"test.example.com", "test.example.com"},
{"asdasd.example.com", "*.example.com"},
{"qweqweqw.qweqasdas.asdasd.example.com", "*.example.com"},
{"asdasdas.com", "default"},
{"asd.asd.zxc.xc.asdasdas.com", "default"},
}
for _, tt := range tests {
t.Run("", func(t *testing.T) {
r, _ := http.NewRequest("GET", "/", nil)
r.Host = tt.host
rr := httptest.NewRecorder()
handler.ServeHTTP(rr, r)
if rr.Code != 200 {
t.Fatalf("wrong response code: %d; wanted 200", rr.Code)
}
got := rr.Body.String()
if got != tt.want {
t.Errorf("\ngot: %q\nwant: %q", got, tt.want)
}
})
}
}