@@ -608,7 +608,6 @@ func TestRouterMatchAny(t *testing.T) {
608
608
return nil
609
609
})
610
610
c := e .NewContext (nil , nil ).(* context )
611
-
612
611
r .Find (http .MethodGet , "/" , c )
613
612
assert .Equal (t , "" , c .Param ("*" ))
614
613
@@ -619,6 +618,78 @@ func TestRouterMatchAny(t *testing.T) {
619
618
assert .Equal (t , "joe" , c .Param ("*" ))
620
619
}
621
620
621
+ // TestRouterMatchAnySlash shall verify finding the best route
622
+ // for any routes with trailing slash requests
623
+ func TestRouterMatchAnySlash (t * testing.T ) {
624
+ e := New ()
625
+ r := e .router
626
+
627
+ handler := func (c Context ) error {
628
+ c .Set ("path" , c .Path ())
629
+ return nil
630
+ }
631
+
632
+ // Routes
633
+ r .Add (http .MethodGet , "/users" , handler )
634
+ r .Add (http .MethodGet , "/users/*" , handler )
635
+ r .Add (http .MethodGet , "/img/*" , handler )
636
+ r .Add (http .MethodGet , "/img/load" , handler )
637
+ r .Add (http .MethodGet , "/img/load/*" , handler )
638
+ r .Add (http .MethodGet , "/assets/*" , handler )
639
+
640
+ c := e .NewContext (nil , nil ).(* context )
641
+ r .Find (http .MethodGet , "/" , c )
642
+ assert .Equal (t , "" , c .Param ("*" ))
643
+
644
+ // Test trailing slash request for simple any route (see #1526)
645
+ c = e .NewContext (nil , nil ).(* context )
646
+ r .Find (http .MethodGet , "/users/" , c )
647
+ c .handler (c )
648
+ assert .Equal (t , "/users/*" , c .Get ("path" ))
649
+ assert .Equal (t , "" , c .Param ("*" ))
650
+
651
+ c = e .NewContext (nil , nil ).(* context )
652
+ r .Find (http .MethodGet , "/users/joe" , c )
653
+ c .handler (c )
654
+ assert .Equal (t , "/users/*" , c .Get ("path" ))
655
+ assert .Equal (t , "joe" , c .Param ("*" ))
656
+
657
+ // Test trailing slash request for nested any route (see #1526)
658
+ c = e .NewContext (nil , nil ).(* context )
659
+ r .Find (http .MethodGet , "/img/load" , c )
660
+ c .handler (c )
661
+ assert .Equal (t , "/img/load" , c .Get ("path" ))
662
+ assert .Equal (t , "" , c .Param ("*" ))
663
+
664
+ c = e .NewContext (nil , nil ).(* context )
665
+ r .Find (http .MethodGet , "/img/load/" , c )
666
+ c .handler (c )
667
+ assert .Equal (t , "/img/load/*" , c .Get ("path" ))
668
+ assert .Equal (t , "" , c .Param ("*" ))
669
+
670
+ c = e .NewContext (nil , nil ).(* context )
671
+ r .Find (http .MethodGet , "/img/load/ben" , c )
672
+ c .handler (c )
673
+ assert .Equal (t , "/img/load/*" , c .Get ("path" ))
674
+ assert .Equal (t , "ben" , c .Param ("*" ))
675
+
676
+ // Test /assets/* any route
677
+ // ... without trailing slash must not match
678
+ c = e .NewContext (nil , nil ).(* context )
679
+ r .Find (http .MethodGet , "/assets" , c )
680
+ c .handler (c )
681
+ assert .Equal (t , nil , c .Get ("path" ))
682
+ assert .Equal (t , "" , c .Param ("*" ))
683
+
684
+ // ... with trailing slash must match
685
+ c = e .NewContext (nil , nil ).(* context )
686
+ r .Find (http .MethodGet , "/assets/" , c )
687
+ c .handler (c )
688
+ assert .Equal (t , "/assets/*" , c .Get ("path" ))
689
+ assert .Equal (t , "" , c .Param ("*" ))
690
+
691
+ }
692
+
622
693
func TestRouterMatchAnyMultiLevel (t * testing.T ) {
623
694
e := New ()
624
695
r := e .router
0 commit comments