-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathhostnameswitch_test.go
76 lines (66 loc) · 2.26 KB
/
hostnameswitch_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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package httptools
import (
"net/http"
"net/http/httptest"
"reflect"
"testing"
)
func TestHostnameSwitch_NoDefault(t *testing.T) {
h := HostnameSwitch{
"www.google.com": http.HandlerFunc(handlerA),
"www.google.de": http.HandlerFunc(handlerB),
}
rr := httptest.NewRecorder()
h.ServeHTTP(rr, MustRequest(http.NewRequest("GET", "http://www.google.com", nil)))
expected := []string{"a"}
got := rr.HeaderMap["Handler"]
if !reflect.DeepEqual(got, expected) {
t.Fatalf("Header list wrong. Expected %#v, got %#v", expected, got)
}
rr = httptest.NewRecorder()
h.ServeHTTP(rr, MustRequest(http.NewRequest("GET", "http://www.google.de", nil)))
expected = []string{"b"}
got = rr.HeaderMap["Handler"]
if !reflect.DeepEqual(got, expected) {
t.Fatalf("Header list wrong. Expected %#v, got %#v", expected, got)
}
rr = httptest.NewRecorder()
h.ServeHTTP(rr, MustRequest(http.NewRequest("GET", "http://www.google.it", nil)))
expectedCode := http.StatusNotFound
if rr.Code != expectedCode {
t.Fatalf("Unexpected error code. Expected %d, got %d", expectedCode, rr.Code)
}
}
func TestHostnameSwitch_Default(t *testing.T) {
h := HostnameSwitch{
"www.google.com": http.HandlerFunc(handlerA),
"_": http.HandlerFunc(handlerB),
}
rr := httptest.NewRecorder()
h.ServeHTTP(rr, MustRequest(http.NewRequest("GET", "http://www.google.com", nil)))
expected := []string{"a"}
got := rr.HeaderMap["Handler"]
if !reflect.DeepEqual(got, expected) {
t.Fatalf("Header list wrong. Expected %#v, got %#v", expected, got)
}
rr = httptest.NewRecorder()
h.ServeHTTP(rr, MustRequest(http.NewRequest("GET", "http://www.google.de", nil)))
expected = []string{"b"}
got = rr.HeaderMap["Handler"]
if !reflect.DeepEqual(got, expected) {
t.Fatalf("Header list wrong. Expected %#v, got %#v", expected, got)
}
}
func TestHostnameSwitch_IgnorePortNumbers(t *testing.T) {
h := HostnameSwitch{
"www.google.com": http.HandlerFunc(handlerA),
"_": http.HandlerFunc(handlerB),
}
rr := httptest.NewRecorder()
h.ServeHTTP(rr, MustRequest(http.NewRequest("GET", "http://www.google.com:8080/Test/123", nil)))
expected := []string{"a"}
got := rr.HeaderMap["Handler"]
if !reflect.DeepEqual(got, expected) {
t.Fatalf("Header list wrong. Expected %#v, got %#v", expected, got)
}
}