-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrouter_test.go
268 lines (208 loc) · 8.56 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
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
package router
import (
"testing"
. "github.com/smartystreets/goconvey/convey"
)
func TestRouter(t *testing.T) {
Convey("Given non-regex routes", t, func() {
router := New()
router.Get("read", "/articles/:name")
router.Compile()
route, params := router.Dispatch("GET", "/articles/article-title")
_, params2 := router.Dispatch("GET", "/articles/10")
route3, params3 := router.Dispatch("GET", "/photos/10")
Convey("It should return named parameters", func() {
So(params, ShouldNotBeNil)
So(params2, ShouldNotBeNil)
So(params["name"], ShouldEqual, "article-title")
So(params2["name"], ShouldEqual, 10)
})
Convey("It should return the route name", func() {
So(route.Name, ShouldEqual, "read")
})
Convey("It should fail when the route doesn't match", func() {
So(route3, ShouldBeNil)
So(params3, ShouldBeNil)
})
Convey("It should return the extension in the params", func() {
So(params["ext"], ShouldNotBeNil)
So(params["ext"], ShouldEqual, "")
})
})
Convey("Given regex routes", t, func() {
router := New()
router.Get("read", "/articles/:id([0-9]+)")
router.Get("title", "/articles/bla/:title([a-z0-9-]+)")
router.Get("users", "/users/:id([0-9]+)")
router.Get("readable_int", "/readable/:id(int)")
router.Get("readable_alpha", "/readable/a/:name(alpha)")
router.Get("readable_slug", "/readable/s/:name(slug)")
router.Get("readable_alphanumeric", "/readable/an/:name(alphanumeric)")
router.Get("readable_md5", "/readable/md5/:hash(md5)")
router.Get("readable_mongo", "/readable/mongo/:id(mongo)")
router.Compile()
Convey("It should match the correct route", func() {
route, _ := router.Dispatch("GET", "/articles/bla/my-title")
So(route.Name, ShouldEqual, "title")
})
Convey("It should return named parameters", func() {
route, params := router.Dispatch("GET", "/articles/bla/my-title")
So(route.Name, ShouldEqual, "title")
// So(params, ShouldEqual, "")
So(params["title"], ShouldEqual, "my-title")
})
Convey("It should accept and match human-readable route-definitions", func() {
route, params := router.Dispatch("GET", "/readable/10")
So(route.Name, ShouldEqual, "readable_int")
So(params["id"], ShouldEqual, 10)
route, params = router.Dispatch("GET", "/readable/a/abc")
So(route.Name, ShouldEqual, "readable_alpha")
So(params["name"], ShouldEqual, "abc")
route, _ = router.Dispatch("GET", "/readable/a/a-b-c")
So(route, ShouldBeNil)
route, _ = router.Dispatch("GET", "/readable/a/a123")
So(route, ShouldBeNil)
route, params = router.Dispatch("GET", "/readable/s/a-b-c")
So(route.Name, ShouldEqual, "readable_slug")
So(params["name"], ShouldEqual, "a-b-c")
route, params = router.Dispatch("GET", "/readable/s/abc")
So(route.Name, ShouldEqual, "readable_slug")
So(params["name"], ShouldEqual, "abc")
route, params = router.Dispatch("GET", "/readable/an/abc123")
So(route.Name, ShouldEqual, "readable_alphanumeric")
So(params["name"], ShouldEqual, "abc123")
route, params = router.Dispatch("GET", "/readable/an/abc")
So(route.Name, ShouldEqual, "readable_alphanumeric")
So(params["name"], ShouldEqual, "abc")
route, params = router.Dispatch("GET", "/readable/an/123")
So(route.Name, ShouldEqual, "readable_alphanumeric")
So(params["name"], ShouldEqual, 123)
route, _ = router.Dispatch("GET", "/readable/an/abc-123")
So(route, ShouldBeNil)
route, params = router.Dispatch("GET", "/readable/md5/49f68a5c8493ec2c0bf489821c21fc3b")
So(route.Name, ShouldEqual, "readable_md5")
So(params["hash"], ShouldEqual, "49f68a5c8493ec2c0bf489821c21fc3b")
route, params = router.Dispatch("GET", "/readable/mongo/52ae7a391db36bc0ff000001")
So(route.Name, ShouldEqual, "readable_mongo")
So(params["id"], ShouldEqual, "52ae7a391db36bc0ff000001")
})
Convey("It should fail when the route doesn't match", func() {
route, params := router.Dispatch("GET", "/users/john")
So(route, ShouldBeNil)
So(params, ShouldBeNil)
})
})
Convey("Given route definitions", t, func() {
router := New()
router.Get("read", "/articles/:id([0-9]+)")
router.Compile()
Convey("It should convert numeric parameters to int types", func() {
_, params := router.Dispatch("GET", "/articles/25")
So(params["id"], ShouldEqual, 25)
So(params["id"], ShouldHaveSameTypeAs, 0)
})
Convey("It should return the route object matched", func() {
route, _ := router.Dispatch("GET", "/articles/25")
So(route, ShouldNotBeNil)
So(route, ShouldHaveSameTypeAs, router.Get("bla_name", "/test/bla"))
})
})
Convey("Given a route name", t, func() {
router := New()
router.Get("read", "/articles/:section/:id/:slug")
router.Compile()
Convey("It should generate route URL", func() {
url := router.URL("read", "section", "entertainment", "id", 10, "slug", "a-b-c")
So(url, ShouldEqual, "/articles/entertainment/10/a-b-c")
})
})
Convey("Given identical routes defined for GET, POST, PUT, and DELETE method types", t, func() {
router := New()
router.Get("find_all", "/articles")
router.Get("read", "/articles/:id(int)")
router.Post("create", "/articles")
router.Put("update", "/articles/:id(int)")
router.Delete("delete", "/articles/:id(int)")
router.Compile()
Convey("It should match routes that match the request method", func() {
route, _ := router.Dispatch("GET", "/articles/10")
So(route.Name, ShouldEqual, "read")
route, _ = router.Dispatch("POST", "/articles")
So(route.Name, ShouldEqual, "create")
route, _ = router.Dispatch("PUT", "/articles/10")
So(route.Name, ShouldEqual, "update")
route, _ = router.Dispatch("DELETE", "/articles/10")
So(route.Name, ShouldEqual, "delete")
route, _ = router.Dispatch("GET", "/articles")
So(route.Name, ShouldEqual, "find_all")
})
})
Convey("Given a router with valid extensions of none, json, and csv", t, func() {
router := New()
router.ValidExtensions("", "json", "csv")
router.Get("read", "/articles/:id(int)")
router.Compile()
Convey("It should only accept requests matching those extensions", func() {
route, params := router.Dispatch("GET", "/articles/10.json")
So(route, ShouldNotBeNil)
So(route.Name, ShouldEqual, "read")
So(params["id"], ShouldEqual, 10)
route, params = router.Dispatch("GET", "/articles/10.csv")
So(route, ShouldNotBeNil)
So(route.Name, ShouldEqual, "read")
So(params["id"], ShouldEqual, 10)
route, params = router.Dispatch("GET", "/articles/10")
So(route, ShouldNotBeNil)
So(route.Name, ShouldEqual, "read")
So(params["id"], ShouldEqual, 10)
route, _ = router.Dispatch("GET", "/articles/10.xml")
So(route, ShouldBeNil)
})
Convey("It should return the extension in the params", func() {
_, params := router.Dispatch("GET", "/articles/10.json")
So(params["ext"], ShouldEqual, "json")
_, params = router.Dispatch("GET", "/articles/10.csv")
So(params["ext"], ShouldEqual, "csv")
_, params = router.Dispatch("GET", "/articles/10")
So(params["ext"], ShouldEqual, "")
})
})
Convey("Given a group definition", t, func() {
router := New()
router.Group("/articles", "articles", func(r *Router) {
r.Get("read", "/:id(int)")
r.Get("section", "/:section(slug)/:id(int)")
r.Get("mongo", "/:id(mongo)")
})
router.Compile()
Convey("It should return matched route name and named parameters", func() {
route, params := router.Dispatch("GET", "/articles/10")
So(route.Name, ShouldEqual, "articles_read")
So(params["id"], ShouldEqual, 10)
route, params = router.Dispatch("GET", "/articles/entertainment/10")
So(route.Name, ShouldEqual, "articles_section")
So(params["section"], ShouldEqual, "entertainment")
So(params["id"], ShouldEqual, 10)
})
Convey("It should accept and match human-readable route-definitions", func() {
route, params := router.Dispatch("GET", "/articles/52ae7a391db36bc0ff000001")
So(route.Name, ShouldEqual, "articles_mongo")
So(params["id"], ShouldEqual, "52ae7a391db36bc0ff000001")
})
Convey("It should fail when the route doesn't match", func() {
route, _ := router.Dispatch("GET", "/articles")
So(route, ShouldBeNil)
})
Convey("It should not match group routes missing the group prefix", func() {
route, _ := router.Dispatch("GET", "/52ae7a391db36bc0ff000001")
So(route, ShouldBeNil)
})
Convey("It should generate route URL", func() {
So(router.URL("articles_section", "section", "technology", "id", 20), ShouldEqual, "/articles/technology/20")
})
Convey("It should only match routes that match the request method", func() {
route, _ := router.Dispatch("POST", "/articles/10")
So(route, ShouldBeNil)
})
})
}