From e3cf37fe84df6a785208d7bd50b24481f32440ba Mon Sep 17 00:00:00 2001 From: Victor Berchet Date: Mon, 6 Feb 2012 10:53:46 +0100 Subject: [PATCH] [HttpFoundation] RedirectResponse: add the ability to retrieve the target URL, add unit tests --- CHANGELOG-2.1.md | 1 + .../HttpFoundation/RedirectResponse.php | 14 ++++++ .../HttpFoundation/RedirectResponseTest.php | 43 +++++++++++++++++++ 3 files changed, 58 insertions(+) create mode 100644 tests/Symfony/Tests/Component/HttpFoundation/RedirectResponseTest.php diff --git a/CHANGELOG-2.1.md b/CHANGELOG-2.1.md index d72a19677971..9d2f2eaf0c94 100644 --- a/CHANGELOG-2.1.md +++ b/CHANGELOG-2.1.md @@ -204,6 +204,7 @@ To get the diff between two versions, go to https://github.com/symfony/symfony/c ### HttpFoundation + * added a getTargetUrl method to RedirectResponse * added support for streamed responses * 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 diff --git a/src/Symfony/Component/HttpFoundation/RedirectResponse.php b/src/Symfony/Component/HttpFoundation/RedirectResponse.php index 3318b12bb389..eb0facf20f20 100644 --- a/src/Symfony/Component/HttpFoundation/RedirectResponse.php +++ b/src/Symfony/Component/HttpFoundation/RedirectResponse.php @@ -20,6 +20,8 @@ */ class RedirectResponse extends Response { + protected $targetUrl; + /** * Creates a redirect response so that it conforms to the rules defined for a redirect status code. * @@ -36,6 +38,8 @@ public function __construct($url, $status = 302) throw new \InvalidArgumentException('Cannot redirect to an empty URL.'); } + $this->targetUrl = $url; + parent::__construct( sprintf(' @@ -57,4 +61,14 @@ public function __construct($url, $status = 302) 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; + } } diff --git a/tests/Symfony/Tests/Component/HttpFoundation/RedirectResponseTest.php b/tests/Symfony/Tests/Component/HttpFoundation/RedirectResponseTest.php new file mode 100644 index 000000000000..3b2d18d90034 --- /dev/null +++ b/tests/Symfony/Tests/Component/HttpFoundation/RedirectResponseTest.php @@ -0,0 +1,43 @@ + + * + * 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( + '##', + 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()); + } + +} \ No newline at end of file