[HttpFoundation] RedirectResponse: add the ability to retrieve the target URL, add unit tests

This commit is contained in:
Victor Berchet 2012-02-06 10:53:46 +01:00
parent 50c85aef24
commit e3cf37fe84
3 changed files with 58 additions and 0 deletions

View File

@ -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 File

@ -20,6 +20,8 @@ namespace Symfony\Component\HttpFoundation;
*/
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 @@ class RedirectResponse extends Response
throw new \InvalidArgumentException('Cannot redirect to an empty URL.');
}
$this->targetUrl = $url;
parent::__construct(
sprintf('<!DOCTYPE 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));
}
}
/**
* Returns the target URL.
*
* @return string target URL
*/
public function getTargetUrl()
{
return $this->targetUrl;
}
}

View File

@ -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());
}
}