-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathtree_test.go
295 lines (266 loc) · 7.6 KB
/
tree_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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
package baa
import (
"fmt"
"net/http"
"net/http/httptest"
"testing"
. "github.com/smartystreets/goconvey/convey"
)
// print the route map
func (t *Tree) print(prefix string, root *leaf) {
if root == nil {
for m := range t.nodes {
fmt.Println(m)
t.print("", t.nodes[m])
}
return
}
prefix = fmt.Sprintf("%s -> %s", prefix, root.pattern)
fmt.Println(prefix)
root.children = append(root.children, root.paramChild)
root.children = append(root.children, root.wideChild)
for i := range root.children {
if root.children[i] != nil {
t.print(prefix, root.children[i])
}
}
}
func TestTreeRouteAdd1(t *testing.T) {
Convey("add static route", t, func() {
r.Add("GET", "/", []HandlerFunc{f})
r.Add("GET", "/bcd", []HandlerFunc{f})
r.Add("GET", "/abcd", []HandlerFunc{f})
r.Add("GET", "/abc", []HandlerFunc{f})
r.Add("GET", "/abd", []HandlerFunc{f})
r.Add("GET", "/abcdef", []HandlerFunc{f})
r.Add("GET", "/bcdefg", []HandlerFunc{f})
r.Add("GET", "/abc/123", []HandlerFunc{f})
r.Add("GET", "/abc/234", []HandlerFunc{f})
r.Add("GET", "/abc/125", []HandlerFunc{f})
r.Add("GET", "/abc/235", []HandlerFunc{f})
r.Add("GET", "/cbd/123", []HandlerFunc{f})
r.Add("GET", "/cbd/234", []HandlerFunc{f})
r.Add("GET", "/cbd/345", []HandlerFunc{f})
r.Add("GET", "/cbd/456", []HandlerFunc{f})
r.Add("GET", "/cbd/346", []HandlerFunc{f})
})
}
func TestTreeRouteAdd2(t *testing.T) {
Convey("add param route", t, func() {
r.Add("GET", "/a/:id/id", []HandlerFunc{f})
r.Add("GET", "/a/:id/name", []HandlerFunc{f})
r.Add("GET", "/a", []HandlerFunc{f})
r.Add("GET", "/a/:id/", []HandlerFunc{f})
r.Add("GET", "/a/", []HandlerFunc{f})
r.Add("GET", "/a/*/xxx", []HandlerFunc{f})
r.Add("GET", "/p/:project/file/:fileName", []HandlerFunc{f})
r.Add("GET", "/cbd/:id", []HandlerFunc{f})
defer func() {
e := recover()
So(e, ShouldNotBeNil)
}()
r.Add("GET", "/p/:/a", []HandlerFunc{f})
})
}
func TestTreeRouteAdd3(t *testing.T) {
Convey("add param route with two different param", t, func() {
defer func() {
e := recover()
So(e, ShouldNotBeNil)
}()
r.Add("GET", "/a/:id", []HandlerFunc{f})
r.Add("GET", "/a/:name", []HandlerFunc{f})
})
}
func TestTreeRouteAdd4(t *testing.T) {
Convey("add route by group", t, func() {
b.Group("/user", func() {
b.Get("/info", f)
b.Get("/info2", f)
b.Group("/group", func() {
b.Get("/info", f)
b.Get("/info2", f)
})
})
b.Group("/user", func() {
b.Get("/", f)
b.Get("/pass", f)
b.Get("/pass2", f)
}, f)
})
}
func TestTreeRouteAdd5(t *testing.T) {
Convey("add route then set name, URLFor", t, func() {
b.Get("/article/:id/show", f).Name("articleShow")
b.Get("/article/:id/detail", f).Name("")
url := b.URLFor("articleShow", 123)
So(url, ShouldEqual, "/article/123/show")
url = b.URLFor("", nil)
So(url, ShouldEqual, "")
url = b.URLFor("not exits", "no")
So(url, ShouldEqual, "")
ru, name := r.Match("GET", "/article/123/show", c)
So(ru, ShouldNotBeNil)
So(name, ShouldEqual, "articleShow")
})
}
func TestTreeRouteAdd6(t *testing.T) {
Convey("add route with not support method", t, func() {
defer func() {
e := recover()
So(e, ShouldNotBeNil)
}()
r.Add("TRACE", "/", []HandlerFunc{f})
})
}
func TestTreeRouteAdd7(t *testing.T) {
Convey("add route with empty pattern", t, func() {
defer func() {
e := recover()
So(e, ShouldNotBeNil)
}()
r.Add("GET", "", []HandlerFunc{f})
})
}
func TestTreeRouteAdd8(t *testing.T) {
Convey("add route with pattern that not begin with /", t, func() {
defer func() {
e := recover()
So(e, ShouldNotBeNil)
}()
r.Add("GET", "abc", []HandlerFunc{f})
})
}
func TestTreeRouteAdd9(t *testing.T) {
Convey("other route method", t, func() {
b2 := New()
Convey("set auto head route", func() {
b2.SetAutoHead(true)
b2.Get("/head", func(c *Context) {
So(c.Req.Method, ShouldEqual, "HEAD")
})
req, _ := http.NewRequest("HEAD", "/head", nil)
w := httptest.NewRecorder()
b2.ServeHTTP(w, req)
So(w.Code, ShouldEqual, http.StatusOK)
})
Convey("set auto training slash", func() {
b2.SetAutoTrailingSlash(true)
b2.Get("/slash", func(c *Context) {})
b2.Group("/slash2", func() {
b2.Get("/", func(c *Context) {})
b2.Get("/exist", func(c *Context) {})
})
req, _ := http.NewRequest("GET", "/slash", nil)
w := httptest.NewRecorder()
b2.ServeHTTP(w, req)
So(w.Code, ShouldEqual, http.StatusOK)
req, _ = http.NewRequest("GET", "/slash/", nil)
w = httptest.NewRecorder()
b2.ServeHTTP(w, req)
So(w.Code, ShouldEqual, http.StatusOK)
req, _ = http.NewRequest("GET", "/slash2", nil)
w = httptest.NewRecorder()
b2.ServeHTTP(w, req)
So(w.Code, ShouldEqual, http.StatusOK)
req, _ = http.NewRequest("GET", "/slash2/", nil)
w = httptest.NewRecorder()
b2.ServeHTTP(w, req)
So(w.Code, ShouldEqual, http.StatusOK)
req, _ = http.NewRequest("GET", "/slash2/exist/", nil)
w = httptest.NewRecorder()
b2.ServeHTTP(w, req)
So(w.Code, ShouldEqual, http.StatusOK)
})
Convey("set multi method", func() {
b2.Route("/mul1", "*", func(c *Context) {
c.String(200, "mul")
})
b2.Route("/mul2", "GET,HEAD,POST", func(c *Context) {
c.String(200, "mul")
})
req, _ := http.NewRequest("HEAD", "/mul2", nil)
w := httptest.NewRecorder()
b2.ServeHTTP(w, req)
So(w.Code, ShouldEqual, http.StatusOK)
req, _ = http.NewRequest("GET", "/mul2", nil)
w = httptest.NewRecorder()
b2.ServeHTTP(w, req)
So(w.Code, ShouldEqual, http.StatusOK)
req, _ = http.NewRequest("POST", "/mul2", nil)
w = httptest.NewRecorder()
b2.ServeHTTP(w, req)
So(w.Code, ShouldEqual, http.StatusOK)
})
Convey("methods", func() {
b2.Get("/methods", f)
b2.Patch("/methods", f)
b2.Post("/methods", f)
b2.Put("/methods", f)
b2.Delete("/methods", f)
b2.Options("/methods", f)
b2.Head("/methods", f)
b2.Any("/any", f)
b2.SetNotFound(func(c *Context) {
c.String(404, "baa not found")
})
})
})
}
func TestTreeRouteMatch1(t *testing.T) {
Convey("match route", t, func() {
ru, _ := r.Match("GET", "/", c)
So(ru, ShouldNotBeNil)
ru, _ = r.Match("GET", "/abc/1234", c)
So(ru, ShouldBeNil)
ru, _ = r.Match("GET", "xxx", c)
So(ru, ShouldBeNil)
ru, _ = r.Match("GET", "/a/123/id", c)
So(ru, ShouldNotBeNil)
ru, _ = r.Match("GET", "/p/yst/file/a.jpg", c)
So(ru, ShouldNotBeNil)
ru, _ = r.Match("GET", "/user/info", c)
So(ru, ShouldNotBeNil)
ru, _ = r.Match("GET", "/user/pass", c)
So(ru, ShouldNotBeNil)
ru, _ = r.Match("GET", "/user/pass32", c)
So(ru, ShouldBeNil)
ru, _ = r.Match("GET", "/user/xxx", c)
So(ru, ShouldBeNil)
ru, _ = r.Match("GET", "/xxxx", c)
So(ru, ShouldBeNil)
b.Get("/notifications/threads/:id", f)
b.Get("/notifications/threads/:id/subscription", f)
b.Get("/notifications/threads/:id/subc", f)
b.Put("/notifications/threads/:id/subscription", f)
b.Delete("/notifications/threads/:id/subscription", f)
ru, _ = r.Match("GET", "/notifications/threads/:id", c)
So(ru, ShouldNotBeNil)
ru, _ = r.Match("GET", "/notifications/threads/:id/sub", c)
So(ru, ShouldBeNil)
})
}
func TestTreeRoutePrint1(t *testing.T) {
Convey("print route table", t, func() {
r.(*Tree).print("", nil)
})
}
func TestTreeRoutePrint2(t *testing.T) {
Convey("print routes", t, func() {
fmt.Println("")
for method, routes := range r.Routes() {
fmt.Println("Method: ", method)
for i, route := range routes {
fmt.Printf(" %3d %s\n", i, route)
}
}
})
}
func TestTreeRoutePrint3(t *testing.T) {
Convey("print named routes", t, func() {
fmt.Println("")
for name, route := range r.NamedRoutes() {
fmt.Printf("%20s \t %s\n", name, route)
}
})
}