-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrouter_test.go
193 lines (155 loc) · 5.13 KB
/
router_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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package router
import (
"github.com/nbio/st"
"io"
"net/http"
"net/http/httptest"
"sort"
"strings"
"testing"
)
func TestRouterRemoveRoute(t *testing.T) {
p := New()
p.Get("/foo")
p.All("/bar")
st.Expect(t, len(p.Routes["GET"]), 1)
st.Expect(t, len(p.Routes["*"]), 1)
st.Expect(t, routeExists(p, "*", "/bar"), true)
st.Expect(t, routeExists(p, "GET", "/foo"), true)
st.Expect(t, p.Remove("GET", "/foo"), true)
st.Expect(t, len(p.Routes["GET"]), 0)
st.Expect(t, routeExists(p, "GET", "/foo"), false)
st.Expect(t, p.Remove("*", "/bar"), true)
st.Expect(t, len(p.Routes["*"]), 0)
st.Expect(t, routeExists(p, "*", "/bar"), false)
st.Expect(t, p.Remove("*", "/baz"), false)
}
func TestRoutingHit(t *testing.T) {
p := New()
var ok bool
p.Get("/foo/:name").Handle(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ok = true
st.Expect(t, r.URL.Query().Get(":name"), "keith")
}))
p.HandleHTTP(nil, newRequest("GET", "/foo/keith?a=b", nil), nil)
if !ok {
t.Error("handler not called")
}
}
func TestRoutingMethodNotAllowed(t *testing.T) {
p := New()
p.ForceMethodNotAllowed = true
var ok bool
p.Post("/foo/:name").Handle(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ok = true
}))
p.Put("/foo/:name").Handle(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ok = true
}))
r := httptest.NewRecorder()
var final bool
p.HandleHTTP(r, newRequest("GET", "/foo/keith", nil), http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
final = true
}))
st.Expect(t, ok, false)
st.Expect(t, final, false)
st.Expect(t, r.Code, http.StatusMethodNotAllowed)
got := strings.Split(r.Header().Get("Allow"), ", ")
sort.Strings(got)
want := []string{"POST", "PUT"}
st.Expect(t, got, want)
}
// Check to make sure we don't pollute the Raw Query when we have no parameters
func TestNoParams(t *testing.T) {
p := New()
var ok bool
p.Get("/foo/").Handle(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ok = true
st.Expect(t, r.URL.RawQuery, "")
}))
p.HandleHTTP(nil, newRequest("GET", "/foo/", nil), nil)
st.Expect(t, ok, true)
}
// Check to make sure we don't pollute the Raw Query when there are parameters but no pattern variables
func TestOnlyUserParams(t *testing.T) {
p := New()
var ok bool
p.Get("/foo/").Handle(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ok = true
st.Expect(t, r.URL.RawQuery, "a=b")
}))
p.HandleHTTP(nil, newRequest("GET", "/foo/?a=b", nil), nil)
st.Expect(t, ok, true)
}
func TestImplicitRedirect(t *testing.T) {
p := New()
p.Get("/foo/").Handle(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {}))
res := httptest.NewRecorder()
p.HandleHTTP(res, newRequest("GET", "/foo", nil), nil)
st.Expect(t, res.Code, 200)
st.Expect(t, res.Header().Get("Location"), "")
p = New()
p.Get("/foo").Handle(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {}))
p.Get("/foo/").Handle(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {}))
res = httptest.NewRecorder()
p.HandleHTTP(res, newRequest("GET", "/foo", nil), nil)
st.Expect(t, res.Code, 200)
p = New()
p.Get("/hello/:name/").Handle(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {}))
res = httptest.NewRecorder()
p.HandleHTTP(res, newRequest("GET", "/hello/bob?a=b#f", nil), nil)
st.Expect(t, res.Code, 200)
}
func TestNotFound(t *testing.T) {
p := New()
p.NotFound = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(123)
})
p.Post("/bar").Handle(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {}))
for _, path := range []string{"/foo", "/bar"} {
res := httptest.NewRecorder()
p.HandleHTTP(res, newRequest("GET", path, nil), nil)
st.Expect(t, res.Code, 123)
}
}
func TestMethodPatch(t *testing.T) {
p := New()
p.Patch("/foo/bar").Handle(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {}))
// Test to see if we get a 405 Method Not Allowed errors from trying to
// issue a GET request to a handler that only supports the PATCH method.
res := httptest.NewRecorder()
res.Code = http.StatusMethodNotAllowed
var final bool
p.HandleHTTP(res, newRequest("GET", "/foo/bar", nil), http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
final = true
}))
st.Expect(t, final, true)
st.Expect(t, res.Code, http.StatusMethodNotAllowed)
// Now, test to see if we get a 200 OK from issuing a PATCH request to
// the same handler.
res = httptest.NewRecorder()
p.HandleHTTP(res, newRequest("PATCH", "/foo/bar", nil), nil)
st.Expect(t, res.Code, http.StatusOK)
}
func BenchmarkPatternMatching(b *testing.B) {
p := New()
p.Get("/hello/:name").Handle(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {}))
b.ResetTimer()
for n := 0; n < b.N; n++ {
b.StopTimer()
r := newRequest("GET", "/hello/blake", nil)
b.StartTimer()
p.HandleHTTP(nil, r, nil)
}
}
func newRequest(method, urlStr string, body io.Reader) *http.Request {
req, err := http.NewRequest(method, urlStr, body)
if err != nil {
panic(err)
}
return req
}
func routeExists(r *Router, method, path string) bool {
route, _ := r.FindRoute(method, path)
return route != nil
}