@@ -22,7 +22,7 @@ func panicHandler(w http.ResponseWriter, r *http.Request, params map[string]stri
2222
2323func newRequest (method , path string , body io.Reader ) (* http.Request , error ) {
2424 r , _ := http .NewRequest (method , path , body )
25- u , _ := url .Parse (path )
25+ u , _ := url .ParseRequestURI (path )
2626 r .URL = u
2727 r .RequestURI = path
2828 return r , nil
@@ -428,7 +428,7 @@ func testRedirect(t *testing.T, defaultBehavior, getBehavior, postBehavior Redir
428428 t .Errorf ("/noslash/ expected code %d, saw %d" , expectedCode , w .Code )
429429 }
430430 if expectedCode != http .StatusNoContent && w .Header ().Get ("Location" ) != "/noslash" {
431- t .Errorf ("/noslash/ was not redirected to /noslash" )
431+ t .Errorf ("/noslash/ was redirected to `%s` instead of /noslash" , w . Header (). Get ( "Location" ) )
432432 }
433433
434434 r , _ = newRequest (method , "//noslash/" , nil )
@@ -1043,6 +1043,36 @@ func TestLookup(t *testing.T) {
10431043 tryLookup ("POST" , "/user/dimfeld/" , true , http .StatusTemporaryRedirect )
10441044}
10451045
1046+ func TestRedirectEscapedPath (t * testing.T ) {
1047+ router := New ()
1048+
1049+ testHandler := func (w http.ResponseWriter , r * http.Request , params map [string ]string ) {}
1050+
1051+ router .GET ("/:escaped/" , testHandler )
1052+
1053+ w := httptest .NewRecorder ()
1054+ u , err := url .Parse ("/Test P@th" )
1055+ if err != nil {
1056+ t .Error (err )
1057+ return
1058+ }
1059+
1060+ r , _ := newRequest ("GET" , u .String (), nil )
1061+
1062+ router .ServeHTTP (w , r )
1063+
1064+ if w .Code != http .StatusMovedPermanently {
1065+ t .Errorf ("Expected status 301 but saw %d" , w .Code )
1066+ }
1067+
1068+ path := w .Header ().Get ("Location" )
1069+ expected := "/Test%20P@th/"
1070+ if path != expected {
1071+ t .Errorf ("Given path wasn't escaped correctly.\n " +
1072+ "Expected: %q\n But got: %q" , expected , path )
1073+ }
1074+ }
1075+
10461076func BenchmarkRouterSimple (b * testing.B ) {
10471077 router := New ()
10481078
0 commit comments