Skip to content

Commit

Permalink
fix problem with external redirects (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-schranz authored and wachterjohannes committed May 12, 2017
1 parent 014d7d8 commit 753ec52
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 11 deletions.
8 changes: 5 additions & 3 deletions Controller/WebsiteRedirectController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
51 changes: 43 additions & 8 deletions Tests/Unit/Controller/RedirectControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,6 @@ class RedirectControllerTest extends \PHPUnit_Framework_TestCase
*/
private $redirectRoute;

/**
* @var string
*/
protected $schemeAndHost = 'http://sulu.io';

/**
* {@inheritdoc}
*/
Expand All @@ -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();
}

Expand All @@ -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());
}

Expand All @@ -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());
Expand Down

0 comments on commit 753ec52

Please sign in to comment.