Permalink
Browse files

[HttpFoundation] RedirectResponse: add the ability to retrieve the ta…

…rget URL, add unit tests
  • Loading branch information...
1 parent 50c85ae commit e3cf37fe84df6a785208d7bd50b24481f32440ba @vicb vicb committed Feb 6, 2012
View
1 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
View
14 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('<!DOCTYPE html>
<html>
@@ -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;
+ }
}
View
43 tests/Symfony/Tests/Component/HttpFoundation/RedirectResponseTest.php
@@ -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());
+ }
+
+}

0 comments on commit e3cf37f

Please sign in to comment.