forked from rcrowley/go-tigertonic
/
trie_serve_mux_test.go
164 lines (152 loc) · 4.43 KB
/
trie_serve_mux_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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package tigertonic
import (
"net/http"
"testing"
)
func TestNotFound(t *testing.T) {
mux := NewTrieServeMux()
w := &testResponseWriter{}
r, _ := http.NewRequest("GET", "http://example.com/", nil)
mux.ServeHTTP(w, r)
if http.StatusNotFound != w.StatusCode {
t.Fatal(w.StatusCode)
}
}
func TestMethodNotAllowed(t *testing.T) {
mux := NewTrieServeMux()
mux.HandleFunc("POST", "/", func(w http.ResponseWriter, r *http.Request) {})
w := &testResponseWriter{}
r, _ := http.NewRequest("GET", "http://example.com/", nil)
mux.ServeHTTP(w, r)
if http.StatusMethodNotAllowed != w.StatusCode {
t.Fatal(w.StatusCode)
}
}
func TestOPTIONS(t *testing.T) {
mux := NewTrieServeMux()
mux.HandleFunc("GET", "/foo", func(w http.ResponseWriter, r *http.Request) {})
mux.HandleFunc("POST", "/bar", func(w http.ResponseWriter, r *http.Request) {})
w := &testResponseWriter{}
r, _ := http.NewRequest("OPTIONS", "http://example.com/foo", nil)
r.Header.Set(CORSRequestMethod, "GET")
mux.ServeHTTP(w, r)
if http.StatusOK != w.StatusCode {
t.Fatal(w.StatusCode)
}
if "GET, HEAD, OPTIONS" != w.Header().Get("Allow") {
t.Fatal(w.Header().Get("Allow"))
}
if "GET, HEAD, OPTIONS" != w.Header().Get(CORSAllowMethods) {
t.Fatal(w.Header().Get("Allow"))
}
w = &testResponseWriter{}
r, _ = http.NewRequest("OPTIONS", "http://example.com/bar", nil)
mux.ServeHTTP(w, r)
if http.StatusOK != w.StatusCode {
t.Fatal(w.StatusCode)
}
if "OPTIONS, POST" != w.Header().Get("Allow") {
t.Fatal(w.Header().Get("Allow"))
}
}
func TestRoot(t *testing.T) {
mux := NewTrieServeMux()
mux.HandleFunc("GET", "/", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNoContent)
})
w := &testResponseWriter{}
r, _ := http.NewRequest("GET", "http://example.com/", nil)
mux.ServeHTTP(w, r)
if http.StatusNoContent != w.StatusCode {
t.Fatal(w.StatusCode)
}
}
func TestRecurse(t *testing.T) {
mux := NewTrieServeMux()
mux.HandleFunc("GET", "/foo/bar/baz", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNoContent)
})
w := &testResponseWriter{}
r, _ := http.NewRequest("GET", "http://example.com/foo/bar/baz", nil)
mux.ServeHTTP(w, r)
if http.StatusNoContent != w.StatusCode {
t.Fatal(w.StatusCode)
}
}
func TestParams(t *testing.T) {
mux := NewTrieServeMux()
mux.HandleFunc("GET", "/{foo}/{bar}", func(w http.ResponseWriter, r *http.Request) {
q := r.URL.Query()
if "bar" != q.Get("foo") || "foo" != q.Get("bar") {
t.Fatal(q.Get("foo"), q.Get("bar"))
}
if "bar" != q.Get("{foo}") || "foo" != q.Get("{bar}") {
t.Fatal(q.Get("{foo}"), q.Get("{bar}"))
}
if "quux" != q.Get("baz") {
t.Fatal(q.Get("quux"))
}
w.WriteHeader(http.StatusNoContent)
})
w := &testResponseWriter{}
r, _ := http.NewRequest("GET", "http://example.com/bar/foo?baz=quux", nil)
mux.ServeHTTP(w, r)
if http.StatusNoContent != w.StatusCode {
t.Fatal(w.StatusCode)
}
}
func TestNamespace(t *testing.T) {
mux := NewTrieServeMux()
mux.HandleFunc("GET", "/bar", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte(r.URL.Path))
})
nsMux := NewTrieServeMux()
nsMux.HandleNamespace("", NotFoundHandler{}) // Test longest match wins.
nsMux.HandleNamespace("/foo", mux)
w := &testResponseWriter{}
r, _ := http.NewRequest("GET", "http://example.com/foo/bar", nil)
nsMux.ServeHTTP(w, r)
if http.StatusOK != w.StatusCode {
t.Fatal(w.StatusCode)
}
if "/bar" != w.Body.String() {
t.Fatal(w.Body.String())
}
}
func TestNamespaceParam(t *testing.T) {
mux := NewTrieServeMux()
mux.HandleFunc("GET", "/bar", func(w http.ResponseWriter, r *http.Request) {
q := r.URL.Query()
if "bar" != q.Get("foo") {
t.Fatal(q.Get("foo"))
}
if "bar" != q.Get("{foo}") {
t.Fatal(q.Get("{foo}"))
}
w.WriteHeader(http.StatusOK)
w.Write([]byte(r.URL.Path))
})
nsMux := NewTrieServeMux()
nsMux.HandleNamespace("/{foo}", mux)
w := &testResponseWriter{}
r, _ := http.NewRequest("GET", "http://example.com/bar/bar", nil)
nsMux.ServeHTTP(w, r)
if http.StatusOK != w.StatusCode {
t.Fatal(w.StatusCode)
}
if "/bar" != w.Body.String() {
t.Fatal(w.Body.String())
}
}
func TestHandler(t *testing.T) {
mux := NewTrieServeMux()
mux.HandleFunc("GET", "/foo/{bar}/baz", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNoContent)
})
r, _ := http.NewRequest("GET", "http://example.com/foo/bar/baz", nil)
_, pattern := mux.Handler(r)
if "/foo/{bar}/baz" != pattern {
t.Fatal(pattern)
}
}