Skip to content

Commit

Permalink
[FrameworkBundle] Thrown an HttpException instead returning a Respons…
Browse files Browse the repository at this point in the history
…e in order to show an error page when path is not defined.
  • Loading branch information
jakzal committed Jan 14, 2014
1 parent 704f982 commit 3097ec6
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 14 deletions.
Expand Up @@ -15,6 +15,7 @@
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;

/**
Expand All @@ -39,11 +40,13 @@ class RedirectController extends ContainerAware
* @param Boolean|array $ignoreAttributes Whether to ignore attributes or an array of attributes to ignore
*
* @return Response A Response instance
*
* @throws HttpException In case the route name is empty
*/
public function redirectAction(Request $request, $route, $permanent = false, $ignoreAttributes = false)
{
if ('' == $route) {
return new Response(null, $permanent ? 410 : 404);
throw new HttpException($permanent ? 410 : 404);
}

$attributes = array();
Expand Down Expand Up @@ -75,11 +78,13 @@ public function redirectAction(Request $request, $route, $permanent = false, $ig
* @param integer|null $httpsPort The HTTPS port (null to keep the current one for the same scheme or the configured port in the container)
*
* @return Response A Response instance
*
* @throws HttpException In case the path is empty
*/
public function urlRedirectAction(Request $request, $path, $permanent = false, $scheme = null, $httpPort = null, $httpsPort = null)
{
if ('' == $path) {
return new Response(null, $permanent ? 410 : 404);
throw new HttpException($permanent ? 410 : 404);
}

$statusCode = $permanent ? 301 : 302;
Expand Down
Expand Up @@ -14,6 +14,7 @@
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\ParameterBag;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Symfony\Bundle\FrameworkBundle\Controller\RedirectController;
use Symfony\Bundle\FrameworkBundle\Tests\TestCase;

Expand All @@ -27,13 +28,19 @@ public function testEmptyRoute()
$request = new Request();
$controller = new RedirectController();

$returnResponse = $controller->redirectAction($request, '', true);
$this->assertInstanceOf('\Symfony\Component\HttpFoundation\Response', $returnResponse);
$this->assertEquals(410, $returnResponse->getStatusCode());
try {
$controller->redirectAction($request, '', true);
$this->fail('Expected Symfony\Component\HttpKernel\Exception\HttpException to be thrown');
} catch (HttpException $e) {
$this->assertSame(410, $e->getStatusCode());
}

$returnResponse = $controller->redirectAction($request, '', false);
$this->assertInstanceOf('\Symfony\Component\HttpFoundation\Response', $returnResponse);
$this->assertEquals(404, $returnResponse->getStatusCode());
try {
$controller->redirectAction($request, '', false);
$this->fail('Expected Symfony\Component\HttpKernel\Exception\HttpException to be thrown');
} catch (HttpException $e) {
$this->assertSame(404, $e->getStatusCode());
}
}

/**
Expand Down Expand Up @@ -98,13 +105,19 @@ public function testEmptyPath()
$request = new Request();
$controller = new RedirectController();

$returnResponse = $controller->urlRedirectAction($request, '', true);
$this->assertInstanceOf('\Symfony\Component\HttpFoundation\Response', $returnResponse);
$this->assertEquals(410, $returnResponse->getStatusCode());
try {
$controller->urlRedirectAction($request, '', true);
$this->fail('Expected Symfony\Component\HttpKernel\Exception\HttpException to be thrown');
} catch (HttpException $e) {
$this->assertSame(410, $e->getStatusCode());
}

$returnResponse = $controller->urlRedirectAction($request, '', false);
$this->assertInstanceOf('\Symfony\Component\HttpFoundation\Response', $returnResponse);
$this->assertEquals(404, $returnResponse->getStatusCode());
try {
$controller->urlRedirectAction($request, '', false);
$this->fail('Expected Symfony\Component\HttpKernel\Exception\HttpException to be thrown');
} catch (HttpException $e) {
$this->assertSame(404, $e->getStatusCode());
}
}

public function testFullURL()
Expand Down

0 comments on commit 3097ec6

Please sign in to comment.