@@ -469,7 +469,7 @@ public function testUnauthenticatedRedirectWithQuery()
469469
470470 $ response = $ middleware ($ request , $ response , $ next );
471471 $ this ->assertSame (302 , $ response ->getStatusCode ());
472- $ this ->assertSame ('/users/login?redirect=http%3A%2F%2Flocalhost %2Ftestpath ' , $ response ->getHeaderLine ('Location ' ));
472+ $ this ->assertSame ('/users/login?redirect=%2Ftestpath ' , $ response ->getHeaderLine ('Location ' ));
473473 $ this ->assertSame ('' , $ response ->getBody () . '' );
474474 }
475475
@@ -498,7 +498,7 @@ public function testUnauthenticatedRedirectWithExistingQuery()
498498
499499 $ response = $ middleware ($ request , $ response , $ next );
500500 $ this ->assertSame (302 , $ response ->getStatusCode ());
501- $ this ->assertSame ('/users/login?hello=world&redirect=http%3A%2F%2Flocalhost %2Ftestpath ' , $ response ->getHeaderLine ('Location ' ));
501+ $ this ->assertSame ('/users/login?hello=world&redirect=%2Ftestpath ' , $ response ->getHeaderLine ('Location ' ));
502502 $ this ->assertSame ('' , $ response ->getBody () . '' );
503503 }
504504
@@ -528,7 +528,7 @@ public function testUnauthenticatedRedirectWithFragment()
528528 $ response = $ middleware ($ request , $ response , $ next );
529529 $ this ->assertSame (302 , $ response ->getStatusCode ());
530530 $ this ->assertSame (
531- '/users/login?hello=world&redirect=http%3A%2F%2Flocalhost %2Ftestpath#frag ' ,
531+ '/users/login?hello=world&redirect=%2Ftestpath#frag ' ,
532532 $ response ->getHeaderLine ('Location ' )
533533 );
534534 $ this ->assertSame ('' , $ response ->getBody () . '' );
@@ -563,7 +563,36 @@ public function testUnauthenticatedRedirectWithBase()
563563
564564 $ response = $ middleware ($ request , $ response , $ next );
565565 $ this ->assertSame (302 , $ response ->getStatusCode ());
566- $ this ->assertSame ('/users/login?redirect=http%3A%2F%2Flocalhost%2Fbase%2Ftestpath ' , $ response ->getHeaderLine ('Location ' ));
566+ $ this ->assertSame ('/users/login?redirect=%2Fbase%2Ftestpath ' , $ response ->getHeaderLine ('Location ' ));
567+ $ this ->assertSame ('' , $ response ->getBody () . '' );
568+ }
569+
570+ /**
571+ * test unauthenticated redirects preserving path and query
572+ *
573+ * @return void
574+ */
575+ public function testUnauthenticatedRedirectWithQueryStringData ()
576+ {
577+ $ request = ServerRequestFactory::fromGlobals (
578+ ['REQUEST_URI ' => '/testpath ' , 'QUERY_STRING ' => 'a=b&c=d ' ],
579+ [],
580+ ['username ' => 'mariano ' , 'password ' => 'password ' ]
581+ );
582+ $ response = new Response ();
583+
584+ $ middleware = new AuthenticationMiddleware ($ this ->service , [
585+ 'unauthenticatedRedirect ' => '/users/login ' ,
586+ 'queryParam ' => 'redirect ' ,
587+ ]);
588+
589+ $ next = function ($ request , $ response ) {
590+ throw new UnauthenticatedException ();
591+ };
592+
593+ $ response = $ middleware ($ request , $ response , $ next );
594+ $ this ->assertSame (302 , $ response ->getStatusCode ());
595+ $ this ->assertSame ('/users/login?redirect=%2Ftestpath%3Fa%3Db%26c%3Dd ' , $ response ->getHeaderLine ('Location ' ));
567596 $ this ->assertSame ('' , $ response ->getBody () . '' );
568597 }
569598
0 commit comments