Skip to content

Commit

Permalink
[HttpFoundation] RedirectResponse: add the ability to retrieve the ta…
Browse files Browse the repository at this point in the history
…rget URL, add unit tests
  • Loading branch information
vicb committed Feb 6, 2012
1 parent 50c85ae commit e3cf37f
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG-2.1.md
Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions src/Symfony/Component/HttpFoundation/RedirectResponse.php
Expand Up @@ -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.
*
Expand All @@ -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>
Expand All @@ -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;
}
}
@@ -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.