@@ -62,11 +62,11 @@ func TestAdd(t *testing.T) {
62
62
router .add ("/" , nil , []string {http .MethodGet }, nil )
63
63
})
64
64
65
- assert .PanicsWithValue (t , "plus path parameters can only be the last part of a path" , func () {
65
+ assert .PanicsWithValue (t , "plus/star path parameters can only be the last part of a path" , func () {
66
66
router .add ("/path/{+extraPath}/asd" , testHandlerFunc , []string {http .MethodGet }, nil )
67
67
})
68
68
69
- assert .PanicsWithValue (t , "plus path parameters can only be the last part of a path" , func () {
69
+ assert .PanicsWithValue (t , "plus/star path parameters can only be the last part of a path" , func () {
70
70
router .add ("/path/{+extraPath}/test/test2" , testHandlerFunc , []string {http .MethodGet }, nil )
71
71
})
72
72
@@ -147,11 +147,13 @@ func TestMatch(t *testing.T) {
147
147
148
148
router .add ("/" , testHandlerFunc , []string {http .MethodGet }, nil )
149
149
router .add ("/test/{var}/get" , testHandlerFunc , []string {http .MethodGet , http .MethodPost }, nil )
150
- router .add ("/test/{var}/get/{+extraPath}" , testHandlerFunc , []string {http .MethodPut }, nil )
150
+ router .add ("/test/{var}/get/{+plusPath}" , testHandlerFunc , []string {http .MethodPut }, nil )
151
+ router .add ("/test/{var}/path/{*starPath}" , testHandlerFunc , []string {http .MethodGet }, nil )
151
152
152
153
firstRoute := router .routes [0 ]
153
154
secondRoute := router .routes [1 ]
154
155
plusRoute := router .routes [2 ]
156
+ starRoute := router .routes [3 ]
155
157
156
158
// Don't need to add starting slash in route's match method as they are skipped in router's find method.
157
159
// For matching we should match relative paths, not abosulute paths.
@@ -218,18 +220,47 @@ func TestMatch(t *testing.T) {
218
220
// Testing plus route.
219
221
params , err = plusRoute .match ("test/123/get/extra/path" , http .MethodPut )
220
222
assert .NoError (t , err )
221
- assert .Equal (t , Params {"var" : "123" , "extraPath" : "extra/path" }, params )
223
+ assert .Equal (t , Params {"var" : "123" , "plusPath" : "extra/path" }, params )
224
+
225
+ params , err = plusRoute .match ("test/123/get/extra" , http .MethodPut )
226
+ assert .NoError (t , err )
227
+ assert .Equal (t , Params {"var" : "123" , "plusPath" : "extra" }, params )
222
228
223
229
params , err = plusRoute .match ("test/123/get/extra/path" , http .MethodGet )
224
230
assert .ErrorIs (t , err , errMethodNotAllowed )
225
231
assert .Nil (t , params )
226
232
227
- params , err = plusRoute .match ("test//get/extra/path" , http .MethodGet )
233
+ params , err = plusRoute .match ("test//get/extra/path" , http .MethodPut )
228
234
assert .ErrorIs (t , err , errNotFound )
229
235
assert .Nil (t , params )
230
236
231
237
// At least one extra path is required
232
- params , err = plusRoute .match ("test/123/get/" , http .MethodGet )
238
+ params , err = plusRoute .match ("test/123/get/" , http .MethodPut )
239
+ assert .ErrorIs (t , err , errNotFound )
240
+ assert .Nil (t , params )
241
+
242
+ // Testing star route.
243
+ params , err = starRoute .match ("test/123/path/star/path" , http .MethodGet )
244
+ assert .NoError (t , err )
245
+ assert .Equal (t , Params {"var" : "123" , "starPath" : "star/path" }, params )
246
+
247
+ params , err = starRoute .match ("test/123/path/star" , http .MethodGet )
248
+ assert .NoError (t , err )
249
+ assert .Equal (t , Params {"var" : "123" , "starPath" : "star" }, params )
250
+
251
+ params , err = starRoute .match ("test/123/path/" , http .MethodGet )
252
+ assert .NoError (t , err )
253
+ assert .Equal (t , Params {"var" : "123" , "starPath" : "" }, params )
254
+
255
+ params , err = starRoute .match ("test/123/path" , http .MethodGet )
256
+ assert .NoError (t , err )
257
+ assert .Equal (t , Params {"var" : "123" , "starPath" : "" }, params )
258
+
259
+ params , err = starRoute .match ("test/123/path/star/path" , http .MethodPost )
260
+ assert .ErrorIs (t , err , errMethodNotAllowed )
261
+ assert .Nil (t , params )
262
+
263
+ params , err = starRoute .match ("test//path/star/path" , http .MethodGet )
233
264
assert .ErrorIs (t , err , errNotFound )
234
265
assert .Nil (t , params )
235
266
}
@@ -317,3 +348,13 @@ func TestFind(t *testing.T) {
317
348
assert .True (t , funcsAreEqual (testHandlerFunc , route .handler ))
318
349
assert .True (t , funcsAreEqual (testMiddlewareFunc , route .middlewares [0 ]))
319
350
}
351
+
352
+ func TestRouterIsPlus (t * testing.T ) {
353
+ router := newRouter ()
354
+
355
+ isPlus := router .isPlus ("{+param}" )
356
+ assert .True (t , isPlus )
357
+
358
+ isPlus = router .isPlus ("{*param}" )
359
+ assert .False (t , isPlus )
360
+ }
0 commit comments