2011-06-20 06:08:46 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
2012-03-31 22:00:32 +01:00
|
|
|
* This file is part of the Symfony package.
|
2011-06-20 06:08:46 +01:00
|
|
|
*
|
|
|
|
* (c) Fabien Potencier <fabien@symfony.com>
|
|
|
|
*
|
2012-03-31 22:00:32 +01:00
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
2011-06-20 06:08:46 +01:00
|
|
|
*/
|
|
|
|
|
2012-03-28 14:43:52 +01:00
|
|
|
namespace Symfony\Component\Security\Tests\Http;
|
2011-06-20 06:08:46 +01:00
|
|
|
|
|
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
|
|
use Symfony\Component\Security\Http\HttpUtils;
|
2011-11-14 12:10:32 +00:00
|
|
|
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
|
2011-06-20 06:08:46 +01:00
|
|
|
|
|
|
|
class HttpUtilsTest extends \PHPUnit_Framework_TestCase
|
|
|
|
{
|
2012-03-28 14:43:52 +01:00
|
|
|
protected function setUp()
|
|
|
|
{
|
|
|
|
if (!class_exists('Symfony\Component\HttpFoundation\Request')) {
|
|
|
|
$this->markTestSkipped('The "HttpFoundation" component is not available');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!class_exists('Symfony\Component\Routing\Router')) {
|
|
|
|
$this->markTestSkipped('The "Routing" component is not available');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-06-20 06:08:46 +01:00
|
|
|
public function testCreateRedirectResponse()
|
|
|
|
{
|
2012-06-26 10:17:51 +01:00
|
|
|
$utils = new HttpUtils($this->getUrlGenerator());
|
2011-06-20 06:08:46 +01:00
|
|
|
|
|
|
|
// absolute path
|
|
|
|
$response = $utils->createRedirectResponse($this->getRequest(), '/foobar');
|
|
|
|
$this->assertTrue($response->isRedirect('http://localhost/foobar'));
|
|
|
|
$this->assertEquals(302, $response->getStatusCode());
|
|
|
|
|
|
|
|
// absolute URL
|
|
|
|
$response = $utils->createRedirectResponse($this->getRequest(), 'http://symfony.com/');
|
|
|
|
$this->assertTrue($response->isRedirect('http://symfony.com/'));
|
|
|
|
|
|
|
|
// route name
|
2012-06-26 10:17:51 +01:00
|
|
|
$utils = new HttpUtils($urlGenerator = $this->getMock('Symfony\Component\Routing\Generator\UrlGeneratorInterface'));
|
|
|
|
$urlGenerator
|
2011-06-20 06:08:46 +01:00
|
|
|
->expects($this->any())
|
|
|
|
->method('generate')
|
|
|
|
->with('foobar', array(), true)
|
|
|
|
->will($this->returnValue('http://localhost/foo/bar'))
|
|
|
|
;
|
2012-06-26 10:17:51 +01:00
|
|
|
$urlGenerator
|
2011-07-11 07:28:02 +01:00
|
|
|
->expects($this->any())
|
|
|
|
->method('getContext')
|
|
|
|
->will($this->returnValue($this->getMock('Symfony\Component\Routing\RequestContext')))
|
|
|
|
;
|
2011-06-20 06:08:46 +01:00
|
|
|
$response = $utils->createRedirectResponse($this->getRequest(), 'foobar');
|
2011-07-11 07:28:02 +01:00
|
|
|
$this->assertTrue($response->isRedirect('http://localhost/foo/bar'));
|
2011-06-20 06:08:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testCreateRequest()
|
|
|
|
{
|
2012-06-26 10:17:51 +01:00
|
|
|
$utils = new HttpUtils($this->getUrlGenerator());
|
2011-06-20 06:08:46 +01:00
|
|
|
|
|
|
|
// absolute path
|
|
|
|
$request = $this->getRequest();
|
|
|
|
$request->server->set('Foo', 'bar');
|
|
|
|
$subRequest = $utils->createRequest($request, '/foobar');
|
|
|
|
|
|
|
|
$this->assertEquals('GET', $subRequest->getMethod());
|
|
|
|
$this->assertEquals('/foobar', $subRequest->getPathInfo());
|
|
|
|
$this->assertEquals('bar', $subRequest->server->get('Foo'));
|
|
|
|
|
|
|
|
// route name
|
2012-06-26 10:17:51 +01:00
|
|
|
$utils = new HttpUtils($urlGenerator = $this->getMock('Symfony\Component\Routing\Generator\UrlGeneratorInterface'));
|
|
|
|
$urlGenerator
|
2011-07-19 15:21:58 +01:00
|
|
|
->expects($this->once())
|
|
|
|
->method('generate')
|
|
|
|
->will($this->returnValue('/foo/bar'))
|
|
|
|
;
|
2012-06-26 10:17:51 +01:00
|
|
|
$urlGenerator
|
2011-07-19 15:21:58 +01:00
|
|
|
->expects($this->any())
|
|
|
|
->method('getContext')
|
|
|
|
->will($this->returnValue($this->getMock('Symfony\Component\Routing\RequestContext')))
|
|
|
|
;
|
2011-06-20 06:08:46 +01:00
|
|
|
$subRequest = $utils->createRequest($this->getRequest(), 'foobar');
|
|
|
|
$this->assertEquals('/foo/bar', $subRequest->getPathInfo());
|
|
|
|
|
|
|
|
// absolute URL
|
|
|
|
$subRequest = $utils->createRequest($this->getRequest(), 'http://symfony.com/');
|
|
|
|
$this->assertEquals('/', $subRequest->getPathInfo());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testCheckRequestPath()
|
|
|
|
{
|
2012-06-26 10:17:51 +01:00
|
|
|
$utils = new HttpUtils($this->getUrlGenerator());
|
2011-06-20 06:08:46 +01:00
|
|
|
|
|
|
|
$this->assertTrue($utils->checkRequestPath($this->getRequest(), '/'));
|
|
|
|
$this->assertFalse($utils->checkRequestPath($this->getRequest(), '/foo'));
|
|
|
|
|
2012-06-26 10:17:51 +01:00
|
|
|
$urlMatcher = $this->getMock('Symfony\Component\Routing\Matcher\UrlMatcherInterface');
|
|
|
|
$urlMatcher
|
2011-07-05 10:00:08 +01:00
|
|
|
->expects($this->any())
|
|
|
|
->method('match')
|
2011-11-14 12:10:32 +00:00
|
|
|
->will($this->throwException(new ResourceNotFoundException()))
|
2011-07-05 10:00:08 +01:00
|
|
|
;
|
2012-06-26 10:17:51 +01:00
|
|
|
$utils = new HttpUtils(null, $urlMatcher);
|
2011-06-20 06:08:46 +01:00
|
|
|
$this->assertFalse($utils->checkRequestPath($this->getRequest(), 'foobar'));
|
2011-07-05 10:00:08 +01:00
|
|
|
|
2012-06-26 10:17:51 +01:00
|
|
|
$urlMatcher = $this->getMock('Symfony\Component\Routing\Matcher\UrlMatcherInterface');
|
|
|
|
$urlMatcher
|
2011-07-05 10:00:08 +01:00
|
|
|
->expects($this->any())
|
|
|
|
->method('match')
|
|
|
|
->will($this->returnValue(array('_route' => 'foobar')))
|
|
|
|
;
|
2012-06-26 10:17:51 +01:00
|
|
|
$utils = new HttpUtils(null, $urlMatcher);
|
2011-06-20 06:08:46 +01:00
|
|
|
$this->assertTrue($utils->checkRequestPath($this->getRequest('/foo/bar'), 'foobar'));
|
|
|
|
}
|
|
|
|
|
2011-11-14 12:10:32 +00:00
|
|
|
/**
|
|
|
|
* @expectedException \RuntimeException
|
|
|
|
*/
|
2012-06-26 10:17:51 +01:00
|
|
|
public function testCheckRequestPathWithUrlMatcherLoadingException()
|
2011-11-14 12:10:32 +00:00
|
|
|
{
|
2012-06-26 10:17:51 +01:00
|
|
|
$urlMatcher = $this->getMock('Symfony\Component\Routing\Matcher\UrlMatcherInterface');
|
|
|
|
$urlMatcher
|
2011-11-14 12:10:32 +00:00
|
|
|
->expects($this->any())
|
|
|
|
->method('match')
|
|
|
|
->will($this->throwException(new \RuntimeException()))
|
|
|
|
;
|
2012-06-26 10:17:51 +01:00
|
|
|
$utils = new HttpUtils(null, $urlMatcher);
|
2011-11-14 12:10:32 +00:00
|
|
|
$utils->checkRequestPath($this->getRequest(), 'foobar');
|
|
|
|
}
|
|
|
|
|
2012-06-26 10:17:51 +01:00
|
|
|
private function getUrlGenerator()
|
2011-06-20 06:08:46 +01:00
|
|
|
{
|
2012-06-26 10:17:51 +01:00
|
|
|
$urlGenerator = $this->getMock('Symfony\Component\Routing\Generator\UrlGeneratorInterface');
|
|
|
|
$urlGenerator
|
2011-06-20 06:08:46 +01:00
|
|
|
->expects($this->any())
|
|
|
|
->method('generate')
|
|
|
|
->will($this->returnValue('/foo/bar'))
|
|
|
|
;
|
|
|
|
|
2012-06-26 10:17:51 +01:00
|
|
|
return $urlGenerator;
|
2011-06-20 06:08:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private function getRequest($path = '/')
|
|
|
|
{
|
|
|
|
return Request::create($path, 'get');
|
|
|
|
}
|
|
|
|
}
|