@@ -50,8 +50,8 @@ func TestTrieCompression(t *testing.T) {
50
50
if trie .root .Children ["/adc" ] == nil {
51
51
t .Errorf ("%+v" , trie .root )
52
52
}
53
-
54
53
}
54
+
55
55
func TestParamInsert (t * testing.T ) {
56
56
trie := New ()
57
57
@@ -78,6 +78,18 @@ func TestParamInsert(t *testing.T) {
78
78
}
79
79
}
80
80
81
+ func TestRelaxedInsert (t * testing.T ) {
82
+ trie := New ()
83
+
84
+ trie .AddRoute ("GET" , "/#id/" , "" )
85
+ if trie .root .Children ["/" ].RelaxedChild .Children ["/" ] == nil {
86
+ t .Error ()
87
+ }
88
+ if trie .root .Children ["/" ].RelaxedName != "id" {
89
+ t .Error ()
90
+ }
91
+ }
92
+
81
93
func TestSplatInsert (t * testing.T ) {
82
94
trie := New ()
83
95
trie .AddRoute ("GET" , "/*splat" , "" )
@@ -115,6 +127,7 @@ func TestFindRoute(t *testing.T) {
115
127
trie .AddRoute ("GET" , "/r/:id" , "resource" )
116
128
trie .AddRoute ("GET" , "/r/:id/property" , "property" )
117
129
trie .AddRoute ("GET" , "/r/:id/property.*format" , "property_format" )
130
+ trie .AddRoute ("GET" , "/user/#username/property" , "user_property" )
118
131
119
132
trie .Compress ()
120
133
@@ -166,6 +179,17 @@ func TestFindRoute(t *testing.T) {
166
179
if matches [0 ].Params ["format" ] != "json" {
167
180
t .Error ()
168
181
}
182
+
183
+ matches = trie .FindRoutes ("GET" , "/user/antoine.imbert/property" )
184
+ if len (matches ) != 1 {
185
+ t .Errorf ("expected one route, got %d" , len (matches ))
186
+ }
187
+ if ! isInMatches ("user_property" , matches ) {
188
+ t .Error ("expected 'user_property'" )
189
+ }
190
+ if matches [0 ].Params ["username" ] != "antoine.imbert" {
191
+ t .Error ()
192
+ }
169
193
}
170
194
171
195
func TestFindRouteMultipleMatches (t * testing.T ) {
@@ -177,6 +201,7 @@ func TestFindRouteMultipleMatches(t *testing.T) {
177
201
trie .AddRoute ("GET" , "/r/:id" , "resource_generic" )
178
202
trie .AddRoute ("GET" , "/s/*rest" , "special_all" )
179
203
trie .AddRoute ("GET" , "/s/:param" , "special_generic" )
204
+ trie .AddRoute ("GET" , "/s/#param" , "special_relaxed" )
180
205
trie .AddRoute ("GET" , "/" , "root" )
181
206
182
207
trie .Compress ()
@@ -193,7 +218,7 @@ func TestFindRouteMultipleMatches(t *testing.T) {
193
218
}
194
219
195
220
matches = trie .FindRoutes ("GET" , "/s/1" )
196
- if len (matches ) != 2 {
221
+ if len (matches ) != 3 {
197
222
t .Errorf ("expected two matches, got %d" , len (matches ))
198
223
}
199
224
if ! isInMatches ("special_all" , matches ) {
@@ -202,16 +227,28 @@ func TestFindRouteMultipleMatches(t *testing.T) {
202
227
if ! isInMatches ("special_generic" , matches ) {
203
228
t .Error ()
204
229
}
230
+ if ! isInMatches ("special_relaxed" , matches ) {
231
+ t .Error ()
232
+ }
205
233
}
206
234
207
235
func TestConsistentPlaceholderName (t * testing.T ) {
208
236
209
237
trie := New ()
238
+
210
239
trie .AddRoute ("GET" , "/r/:id" , "oneph" )
211
240
err := trie .AddRoute ("GET" , "/r/:rid/other" , "twoph" )
212
241
if err == nil {
213
242
t .Error ("Should have died on adding second route" )
214
243
}
244
+
245
+ trie .AddRoute ("GET" , "/r/#id" , "oneph" )
246
+ err = trie .AddRoute ("GET" , "/r/#rid/other" , "twoph" )
247
+ if err == nil {
248
+ t .Error ("Should have died on adding second route" )
249
+ }
250
+
251
+ // TODO *param
215
252
}
216
253
217
254
func TestDuplicateName (t * testing.T ) {
@@ -227,4 +264,9 @@ func TestDuplicateName(t *testing.T) {
227
264
if err == nil {
228
265
t .Error ("Should have died, this route has two placeholder named `id`" )
229
266
}
267
+
268
+ err = trie .AddRoute ("GET" , "/r/:id/o/#id" , "two" )
269
+ if err == nil {
270
+ t .Error ("Should have died, this route has two placeholder named `id`" )
271
+ }
230
272
}
0 commit comments