Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Use direct HTTP status codes instead of constants
  • Loading branch information
ZipoKing committed Feb 18, 2018
commit 103c1227949112ab45183f151f0449a8096fa30a
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function __construct(UrlGeneratorInterface $router = null, int $httpPort
public function redirectAction(Request $request, string $route, bool $permanent = false, $ignoreAttributes = false, bool $keepRequestMethod = false): Response
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for other reviewers: the class is final, so this is not a BC break

{
if ('' == $route) {
throw new HttpException($permanent ? Response::HTTP_GONE : Response::HTTP_NOT_FOUND);
throw new HttpException($permanent ? 410 : 404);
}

$attributes = array();
Expand All @@ -70,9 +70,9 @@ public function redirectAction(Request $request, string $route, bool $permanent
}

if ($keepRequestMethod) {
$statusCode = $permanent ? Response::HTTP_PERMANENTLY_REDIRECT : Response::HTTP_TEMPORARY_REDIRECT;
$statusCode = $permanent ? 308 : 307;
} else {
$statusCode = $permanent ? Response::HTTP_MOVED_PERMANENTLY : Response::HTTP_FOUND;
$statusCode = $permanent ? 301 : 302;
}

return new RedirectResponse($this->router->generate($route, $attributes, UrlGeneratorInterface::ABSOLUTE_URL), $statusCode);
Expand Down Expand Up @@ -100,13 +100,13 @@ public function redirectAction(Request $request, string $route, bool $permanent
public function urlRedirectAction(Request $request, string $path, bool $permanent = false, string $scheme = null, int $httpPort = null, int $httpsPort = null, bool $keepRequestMethod = false): Response
{
if ('' == $path) {
throw new HttpException($permanent ? Response::HTTP_GONE : Response::HTTP_NOT_FOUND);
throw new HttpException($permanent ? 410 : 404);
}

if ($keepRequestMethod) {
$statusCode = $permanent ? Response::HTTP_PERMANENTLY_REDIRECT : Response::HTTP_TEMPORARY_REDIRECT;
$statusCode = $permanent ? 308 : 307;
} else {
$statusCode = $permanent ? Response::HTTP_MOVED_PERMANENTLY : Response::HTTP_FOUND;
$statusCode = $permanent ? 301 : 302;
}

// redirect if the path is a full URL
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ public function testEmptyRoute()
$controller->redirectAction($request, '', true);
$this->fail('Expected Symfony\Component\HttpKernel\Exception\HttpException to be thrown');
} catch (HttpException $e) {
$this->assertSame(Response::HTTP_GONE, $e->getStatusCode());
$this->assertSame(410, $e->getStatusCode());
}

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

Expand Down Expand Up @@ -86,14 +86,14 @@ public function testRoute($permanent, $keepRequestMethod, $ignoreAttributes, $ex
public function provider()
{
return array(
array(true, false, false, Response::HTTP_MOVED_PERMANENTLY, array('additional-parameter' => 'value')),
array(false, false, false, Response::HTTP_FOUND, array('additional-parameter' => 'value')),
array(false, false, true, Response::HTTP_FOUND, array()),
array(false, false, array('additional-parameter'), Response::HTTP_FOUND, array()),
array(true, true, false, Response::HTTP_PERMANENTLY_REDIRECT, array('additional-parameter' => 'value')),
array(false, true, false, Response::HTTP_TEMPORARY_REDIRECT, array('additional-parameter' => 'value')),
array(false, true, true, Response::HTTP_TEMPORARY_REDIRECT, array()),
array(false, true, array('additional-parameter'), Response::HTTP_TEMPORARY_REDIRECT, array()),
array(true, false, false, 301, array('additional-parameter' => 'value')),
array(false, false, false, 302, array('additional-parameter' => 'value')),
array(false, false, true, 302, array()),
array(false, false, array('additional-parameter'), 302, array()),
array(true, true, false, 308, array('additional-parameter' => 'value')),
array(false, true, false, 307, array('additional-parameter' => 'value')),
array(false, true, true, 307, array()),
array(false, true, array('additional-parameter'), 307, array()),
);
}

Expand All @@ -106,14 +106,14 @@ public function testEmptyPath()
$controller->urlRedirectAction($request, '', true);
$this->fail('Expected Symfony\Component\HttpKernel\Exception\HttpException to be thrown');
} catch (HttpException $e) {
$this->assertSame(Response::HTTP_GONE, $e->getStatusCode());
$this->assertSame(410, $e->getStatusCode());
}

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

Expand All @@ -124,7 +124,7 @@ public function testFullURL()
$returnResponse = $controller->urlRedirectAction($request, 'http://foo.bar/');

$this->assertRedirectUrl($returnResponse, 'http://foo.bar/');
$this->assertEquals(Response::HTTP_FOUND, $returnResponse->getStatusCode());
$this->assertEquals(302, $returnResponse->getStatusCode());
}

public function testFullURLWithMethodKeep()
Expand All @@ -134,7 +134,7 @@ public function testFullURLWithMethodKeep()
$returnResponse = $controller->urlRedirectAction($request, 'http://foo.bar/', false, null, null, null, true);

$this->assertRedirectUrl($returnResponse, 'http://foo.bar/');
$this->assertEquals(Response::HTTP_TEMPORARY_REDIRECT, $returnResponse->getStatusCode());
$this->assertEquals(307, $returnResponse->getStatusCode());
}

public function testUrlRedirectDefaultPorts()
Expand Down