@@ -13,13 +13,14 @@ import (
1313)
1414
1515type routeTest struct {
16- title string // title of the test
17- route * Route // the route being tested
18- request * http.Request // a request to test the route
19- vars map [string ]string // the expected vars of the match
20- host string // the expected host of the match
21- path string // the expected path of the match
22- shouldMatch bool // whether the request is expected to match the route at all
16+ title string // title of the test
17+ route * Route // the route being tested
18+ request * http.Request // a request to test the route
19+ vars map [string ]string // the expected vars of the match
20+ host string // the expected host of the match
21+ path string // the expected path of the match
22+ shouldMatch bool // whether the request is expected to match the route at all
23+ shouldRedirect bool // whether the request should result in a redirect
2324}
2425
2526func TestHost (t * testing.T ) {
@@ -151,6 +152,33 @@ func TestPath(t *testing.T) {
151152 path : "/111/222/333" ,
152153 shouldMatch : true ,
153154 },
155+ {
156+ title : "Path route, match with trailing slash in request and path" ,
157+ route : new (Route ).Path ("/111/" ),
158+ request : newRequest ("GET" , "http://localhost/111/" ),
159+ vars : map [string ]string {},
160+ host : "" ,
161+ path : "/111/" ,
162+ shouldMatch : true ,
163+ },
164+ {
165+ title : "Path route, do not match with trailing slash in path" ,
166+ route : new (Route ).Path ("/111/" ),
167+ request : newRequest ("GET" , "http://localhost/111" ),
168+ vars : map [string ]string {},
169+ host : "" ,
170+ path : "/111" ,
171+ shouldMatch : false ,
172+ },
173+ {
174+ title : "Path route, do not match with trailing slash in request" ,
175+ route : new (Route ).Path ("/111" ),
176+ request : newRequest ("GET" , "http://localhost/111/" ),
177+ vars : map [string ]string {},
178+ host : "" ,
179+ path : "/111/" ,
180+ shouldMatch : false ,
181+ },
154182 {
155183 title : "Path route, wrong path in request in request URL" ,
156184 route : new (Route ).Path ("/111/222/333" ),
@@ -214,6 +242,15 @@ func TestPathPrefix(t *testing.T) {
214242 path : "/111" ,
215243 shouldMatch : true ,
216244 },
245+ {
246+ title : "PathPrefix route, match substring" ,
247+ route : new (Route ).PathPrefix ("/1" ),
248+ request : newRequest ("GET" , "http://localhost/111/222/333" ),
249+ vars : map [string ]string {},
250+ host : "" ,
251+ path : "/1" ,
252+ shouldMatch : true ,
253+ },
217254 {
218255 title : "PathPrefix route, URL prefix in request does not match" ,
219256 route : new (Route ).PathPrefix ("/111" ),
@@ -579,26 +616,74 @@ func TestNamedRoutes(t *testing.T) {
579616}
580617
581618func TestStrictSlash (t * testing.T ) {
582- var r * Router
583- var req * http.Request
584- var route * Route
585- var match * RouteMatch
586- var matched bool
587-
588- // StrictSlash should be ignored for path prefix.
589- // So we register a route ending in slash but it doesn't attempt to add
590- // the slash for a path not ending in slash.
591- r = NewRouter ()
619+ r := NewRouter ()
592620 r .StrictSlash (true )
593- route = r .NewRoute ().PathPrefix ("/static/" )
594- req , _ = http .NewRequest ("GET" , "http://localhost/static/logo.png" , nil )
595- match = new (RouteMatch )
596- matched = r .Match (req , match )
597- if ! matched {
598- t .Errorf ("Should match request %q -- %v" , req .URL .Path , getRouteTemplate (route ))
621+
622+ tests := []routeTest {
623+ {
624+ title : "Redirect path without slash" ,
625+ route : r .NewRoute ().Path ("/111/" ),
626+ request : newRequest ("GET" , "http://localhost/111" ),
627+ vars : map [string ]string {},
628+ host : "" ,
629+ path : "/111/" ,
630+ shouldMatch : true ,
631+ shouldRedirect : true ,
632+ },
633+ {
634+ title : "Do not redirect path with slash" ,
635+ route : r .NewRoute ().Path ("/111/" ),
636+ request : newRequest ("GET" , "http://localhost/111/" ),
637+ vars : map [string ]string {},
638+ host : "" ,
639+ path : "/111/" ,
640+ shouldMatch : true ,
641+ shouldRedirect : false ,
642+ },
643+ {
644+ title : "Redirect path with slash" ,
645+ route : r .NewRoute ().Path ("/111" ),
646+ request : newRequest ("GET" , "http://localhost/111/" ),
647+ vars : map [string ]string {},
648+ host : "" ,
649+ path : "/111" ,
650+ shouldMatch : true ,
651+ shouldRedirect : true ,
652+ },
653+ {
654+ title : "Do not redirect path without slash" ,
655+ route : r .NewRoute ().Path ("/111" ),
656+ request : newRequest ("GET" , "http://localhost/111" ),
657+ vars : map [string ]string {},
658+ host : "" ,
659+ path : "/111" ,
660+ shouldMatch : true ,
661+ shouldRedirect : false ,
662+ },
663+ {
664+ title : "Propagate StrictSlash to subrouters" ,
665+ route : r .NewRoute ().PathPrefix ("/static/" ).Subrouter ().Path ("/images/" ),
666+ request : newRequest ("GET" , "http://localhost/static/images" ),
667+ vars : map [string ]string {},
668+ host : "" ,
669+ path : "/static/images/" ,
670+ shouldMatch : true ,
671+ shouldRedirect : true ,
672+ },
673+ {
674+ title : "Ignore StrictSlash for path prefix" ,
675+ route : r .NewRoute ().PathPrefix ("/static/" ),
676+ request : newRequest ("GET" , "http://localhost/static/logo.png" ),
677+ vars : map [string ]string {},
678+ host : "" ,
679+ path : "/static/" ,
680+ shouldMatch : true ,
681+ shouldRedirect : false ,
682+ },
599683 }
600- if match .Handler != nil {
601- t .Errorf ("Should not redirect" )
684+
685+ for _ , test := range tests {
686+ testRoute (t , test )
602687 }
603688}
604689
@@ -627,6 +712,7 @@ func testRoute(t *testing.T, test routeTest) {
627712 host := test .host
628713 path := test .path
629714 url := test .host + test .path
715+ shouldRedirect := test .shouldRedirect
630716
631717 var match RouteMatch
632718 ok := route .Match (request , & match )
@@ -664,6 +750,14 @@ func testRoute(t *testing.T, test routeTest) {
664750 return
665751 }
666752 }
753+ if shouldRedirect && match .Handler == nil {
754+ t .Errorf ("(%v) Did not redirect" , test .title )
755+ return
756+ }
757+ if ! shouldRedirect && match .Handler != nil {
758+ t .Errorf ("(%v) Unexpected redirect" , test .title )
759+ return
760+ }
667761 }
668762}
669763
0 commit comments