From 753ec520fa2999e167b56d28efa83a04d1867e4a Mon Sep 17 00:00:00 2001 From: L91 Date: Fri, 12 May 2017 07:33:40 +0200 Subject: [PATCH] fix problem with external redirects (#7) --- Controller/WebsiteRedirectController.php | 8 +-- .../Controller/RedirectControllerTest.php | 51 ++++++++++++++++--- 2 files changed, 48 insertions(+), 11 deletions(-) diff --git a/Controller/WebsiteRedirectController.php b/Controller/WebsiteRedirectController.php index 2704157..8f80d66 100644 --- a/Controller/WebsiteRedirectController.php +++ b/Controller/WebsiteRedirectController.php @@ -31,13 +31,15 @@ class WebsiteRedirectController public function redirect(Request $request, RedirectRouteInterface $redirectRoute) { $queryString = http_build_query($request->query->all()); + $url = [ - $request->getSchemeAndHttpHost(), $redirectRoute->getTarget(), - (!empty($queryString) ? '?' : ''), + strpos($redirectRoute->getTarget(), '?') === false ? '?' : '&', $queryString, ]; - return new RedirectResponse(implode($url), $redirectRoute->getStatusCode()); + $url = trim(implode($url), '&? '); + + return new RedirectResponse($url, $redirectRoute->getStatusCode()); } } diff --git a/Tests/Unit/Controller/RedirectControllerTest.php b/Tests/Unit/Controller/RedirectControllerTest.php index dac0878..a53a92f 100644 --- a/Tests/Unit/Controller/RedirectControllerTest.php +++ b/Tests/Unit/Controller/RedirectControllerTest.php @@ -39,11 +39,6 @@ class RedirectControllerTest extends \PHPUnit_Framework_TestCase */ private $redirectRoute; - /** - * @var string - */ - protected $schemeAndHost = 'http://sulu.io'; - /** * {@inheritdoc} */ @@ -55,7 +50,6 @@ protected function setUp() $this->queryBag = $this->prophesize(ParameterBag::class); $this->redirectRoute = $this->prophesize(RedirectRouteInterface::class); - $this->request->getSchemeAndHttpHost()->willReturn($this->schemeAndHost); $this->request->reveal()->query = $this->queryBag->reveal(); } @@ -72,7 +66,7 @@ public function testRedirect() $response = $this->controller->redirect($this->request->reveal(), $this->redirectRoute->reveal()); $this->assertInstanceOf(RedirectResponse::class, $response); - $this->assertEquals($this->schemeAndHost . $target, $response->getTargetUrl()); + $this->assertEquals($target, $response->getTargetUrl()); $this->assertEquals($statusCode, $response->getStatusCode()); } @@ -91,7 +85,48 @@ public function testRedirectWithQuery() $this->assertInstanceOf(RedirectResponse::class, $response); $this->assertEquals( - $this->schemeAndHost . $target . '?' . http_build_query($query), + $target . '?' . http_build_query($query), + $response->getTargetUrl() + ); + $this->assertEquals($statusCode, $response->getStatusCode()); + } + + public function testRedirectExternal() + { + $target = 'http://captain-sulu.io/test'; + $statusCode = 301; + + $this->queryBag->all()->willReturn([]); + + $this->redirectRoute->getTarget()->willReturn($target); + $this->redirectRoute->getStatusCode()->willReturn($statusCode); + + $response = $this->controller->redirect($this->request->reveal(), $this->redirectRoute->reveal()); + + $this->assertInstanceOf(RedirectResponse::class, $response); + $this->assertEquals( + $target, + $response->getTargetUrl() + ); + $this->assertEquals($statusCode, $response->getStatusCode()); + } + + public function testRedirectExternalWithQuery() + { + $target = 'http://captain-sulu.io/test?hello=true'; + $statusCode = 301; + $query = ['test' => 1, 'my-parameter' => 'awesome sulu']; + + $this->queryBag->all()->willReturn($query); + + $this->redirectRoute->getTarget()->willReturn($target); + $this->redirectRoute->getStatusCode()->willReturn($statusCode); + + $response = $this->controller->redirect($this->request->reveal(), $this->redirectRoute->reveal()); + + $this->assertInstanceOf(RedirectResponse::class, $response); + $this->assertEquals( + $target . '&' . http_build_query($query), $response->getTargetUrl() ); $this->assertEquals($statusCode, $response->getStatusCode());