[HttpFoundation] RedirectResponse: add the ability to retrieve the target URL, add unit tests
This commit is contained in:
parent
50c85aef24
commit
e3cf37fe84
@ -204,6 +204,7 @@ To get the diff between two versions, go to https://github.com/symfony/symfony/c
|
|||||||
|
|
||||||
### HttpFoundation
|
### HttpFoundation
|
||||||
|
|
||||||
|
* added a getTargetUrl method to RedirectResponse
|
||||||
* added support for streamed responses
|
* added support for streamed responses
|
||||||
* made Response::prepare() method the place to enforce HTTP specification
|
* made Response::prepare() method the place to enforce HTTP specification
|
||||||
* [BC BREAK] moved management of the locale from the Session class to the Request class
|
* [BC BREAK] moved management of the locale from the Session class to the Request class
|
||||||
|
@ -20,6 +20,8 @@ namespace Symfony\Component\HttpFoundation;
|
|||||||
*/
|
*/
|
||||||
class RedirectResponse extends Response
|
class RedirectResponse extends Response
|
||||||
{
|
{
|
||||||
|
protected $targetUrl;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a redirect response so that it conforms to the rules defined for a redirect status code.
|
* Creates a redirect response so that it conforms to the rules defined for a redirect status code.
|
||||||
*
|
*
|
||||||
@ -36,6 +38,8 @@ class RedirectResponse extends Response
|
|||||||
throw new \InvalidArgumentException('Cannot redirect to an empty URL.');
|
throw new \InvalidArgumentException('Cannot redirect to an empty URL.');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$this->targetUrl = $url;
|
||||||
|
|
||||||
parent::__construct(
|
parent::__construct(
|
||||||
sprintf('<!DOCTYPE html>
|
sprintf('<!DOCTYPE html>
|
||||||
<html>
|
<html>
|
||||||
@ -57,4 +61,14 @@ class RedirectResponse extends Response
|
|||||||
throw new \InvalidArgumentException(sprintf('The HTTP status code is not a redirect ("%s" given).', $status));
|
throw new \InvalidArgumentException(sprintf('The HTTP status code is not a redirect ("%s" given).', $status));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the target URL.
|
||||||
|
*
|
||||||
|
* @return string target URL
|
||||||
|
*/
|
||||||
|
public function getTargetUrl()
|
||||||
|
{
|
||||||
|
return $this->targetUrl;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,43 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This file is part of the Symfony package.
|
||||||
|
*
|
||||||
|
* (c) Fabien Potencier <fabien@symfony.com>
|
||||||
|
*
|
||||||
|
* For the full copyright and license information, please view the LICENSE
|
||||||
|
* file that was distributed with this source code.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Symfony\Tests\Component\HttpFoundation;
|
||||||
|
|
||||||
|
use \Symfony\Component\HttpFoundation\RedirectResponse;
|
||||||
|
|
||||||
|
class RedirectResponseTest extends \PHPUnit_Framework_TestCase
|
||||||
|
{
|
||||||
|
public function testGenerateMetaRedirect()
|
||||||
|
{
|
||||||
|
$response = new RedirectResponse('foo.bar');
|
||||||
|
|
||||||
|
$this->assertEquals(1, preg_match(
|
||||||
|
'#<meta http-equiv="refresh" content="\d+;url=foo\.bar" />#',
|
||||||
|
preg_replace(array('/\s+/', '/\'/'), array(' ', '"'), $response->getContent())
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testGenerateLocationHeader()
|
||||||
|
{
|
||||||
|
$response = new RedirectResponse('foo.bar');
|
||||||
|
|
||||||
|
$this->assertTrue($response->headers->has('Location'));
|
||||||
|
$this->assertEquals('foo.bar', $response->headers->get('Location'));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testGetTargetUrl()
|
||||||
|
{
|
||||||
|
$response = new RedirectResponse('foo.bar');
|
||||||
|
|
||||||
|
$this->assertEquals('foo.bar', $response->getTargetUrl());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Reference in New Issue
Block a user