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
|
|
|
*/
|
|
|
|
|
2013-09-16 09:03:00 +01:00
|
|
|
namespace Symfony\Component\Security\Http\Tests;
|
2011-06-20 06:08:46 +01:00
|
|
|
|
|
|
|
use Symfony\Component\HttpFoundation\Request;
|
2013-06-02 23:34:18 +01:00
|
|
|
use Symfony\Component\Routing\Exception\MethodNotAllowedException;
|
2011-11-14 12:10:32 +00:00
|
|
|
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
|
2014-09-29 08:09:34 +01:00
|
|
|
use Symfony\Component\Security\Core\Security;
|
2015-10-18 16:23:56 +01:00
|
|
|
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
|
2013-06-02 23:34:18 +01:00
|
|
|
use Symfony\Component\Security\Http\HttpUtils;
|
2011-06-20 06:08:46 +01:00
|
|
|
|
|
|
|
class HttpUtilsTest extends \PHPUnit_Framework_TestCase
|
|
|
|
{
|
2013-06-02 23:34:18 +01:00
|
|
|
public function testCreateRedirectResponseWithPath()
|
2011-06-20 06:08:46 +01:00
|
|
|
{
|
2012-06-26 10:17:51 +01:00
|
|
|
$utils = new HttpUtils($this->getUrlGenerator());
|
2011-06-20 06:08:46 +01:00
|
|
|
$response = $utils->createRedirectResponse($this->getRequest(), '/foobar');
|
2013-06-02 23:34:18 +01:00
|
|
|
|
2011-06-20 06:08:46 +01:00
|
|
|
$this->assertTrue($response->isRedirect('http://localhost/foobar'));
|
|
|
|
$this->assertEquals(302, $response->getStatusCode());
|
2013-06-02 23:34:18 +01:00
|
|
|
}
|
2011-06-20 06:08:46 +01:00
|
|
|
|
2013-06-02 23:34:18 +01:00
|
|
|
public function testCreateRedirectResponseWithAbsoluteUrl()
|
|
|
|
{
|
|
|
|
$utils = new HttpUtils($this->getUrlGenerator());
|
2011-06-20 06:08:46 +01:00
|
|
|
$response = $utils->createRedirectResponse($this->getRequest(), 'http://symfony.com/');
|
2013-06-02 23:34:18 +01:00
|
|
|
|
2011-06-20 06:08:46 +01:00
|
|
|
$this->assertTrue($response->isRedirect('http://symfony.com/'));
|
2013-06-02 23:34:18 +01:00
|
|
|
}
|
2011-06-20 06:08:46 +01:00
|
|
|
|
2013-06-02 23:34:18 +01:00
|
|
|
public function testCreateRedirectResponseWithRouteName()
|
|
|
|
{
|
2016-12-19 09:02:29 +00:00
|
|
|
$utils = new HttpUtils($urlGenerator = $this->getMockBuilder('Symfony\Component\Routing\Generator\UrlGeneratorInterface')->getMock());
|
2013-06-02 23:34:18 +01:00
|
|
|
|
2012-06-26 10:17:51 +01:00
|
|
|
$urlGenerator
|
2011-06-20 06:08:46 +01:00
|
|
|
->expects($this->any())
|
|
|
|
->method('generate')
|
2015-10-18 16:23:56 +01:00
|
|
|
->with('foobar', array(), UrlGeneratorInterface::ABSOLUTE_URL)
|
2011-06-20 06:08:46 +01:00
|
|
|
->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')
|
2016-12-19 09:02:29 +00:00
|
|
|
->will($this->returnValue($this->getMockBuilder('Symfony\Component\Routing\RequestContext')->getMock()))
|
2011-07-11 07:28:02 +01:00
|
|
|
;
|
2013-06-02 23:34:18 +01:00
|
|
|
|
2011-06-20 06:08:46 +01:00
|
|
|
$response = $utils->createRedirectResponse($this->getRequest(), 'foobar');
|
2013-06-02 23:34:18 +01:00
|
|
|
|
2011-07-11 07:28:02 +01:00
|
|
|
$this->assertTrue($response->isRedirect('http://localhost/foo/bar'));
|
2011-06-20 06:08:46 +01:00
|
|
|
}
|
|
|
|
|
2013-06-02 23:34:18 +01:00
|
|
|
public function testCreateRequestWithPath()
|
2011-06-20 06:08:46 +01:00
|
|
|
{
|
|
|
|
$request = $this->getRequest();
|
|
|
|
$request->server->set('Foo', 'bar');
|
2013-06-02 23:34:18 +01:00
|
|
|
|
|
|
|
$utils = new HttpUtils($this->getUrlGenerator());
|
2011-06-20 06:08:46 +01:00
|
|
|
$subRequest = $utils->createRequest($request, '/foobar');
|
|
|
|
|
|
|
|
$this->assertEquals('GET', $subRequest->getMethod());
|
|
|
|
$this->assertEquals('/foobar', $subRequest->getPathInfo());
|
|
|
|
$this->assertEquals('bar', $subRequest->server->get('Foo'));
|
2013-06-02 23:34:18 +01:00
|
|
|
}
|
2011-06-20 06:08:46 +01:00
|
|
|
|
2013-06-02 23:34:18 +01:00
|
|
|
public function testCreateRequestWithRouteName()
|
|
|
|
{
|
2016-12-19 09:02:29 +00:00
|
|
|
$utils = new HttpUtils($urlGenerator = $this->getMockBuilder('Symfony\Component\Routing\Generator\UrlGeneratorInterface')->getMock());
|
2013-06-02 23:34:18 +01:00
|
|
|
|
2012-06-26 10:17:51 +01:00
|
|
|
$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')
|
2016-12-19 09:02:29 +00:00
|
|
|
->will($this->returnValue($this->getMockBuilder('Symfony\Component\Routing\RequestContext')->getMock()))
|
2011-07-19 15:21:58 +01:00
|
|
|
;
|
2013-06-02 23:34:18 +01:00
|
|
|
|
2011-06-20 06:08:46 +01:00
|
|
|
$subRequest = $utils->createRequest($this->getRequest(), 'foobar');
|
2013-06-02 23:34:18 +01:00
|
|
|
|
2011-06-20 06:08:46 +01:00
|
|
|
$this->assertEquals('/foo/bar', $subRequest->getPathInfo());
|
2013-06-02 23:34:18 +01:00
|
|
|
}
|
2011-06-20 06:08:46 +01:00
|
|
|
|
2013-06-02 23:34:18 +01:00
|
|
|
public function testCreateRequestWithAbsoluteUrl()
|
|
|
|
{
|
2016-12-19 09:02:29 +00:00
|
|
|
$utils = new HttpUtils($this->getMockBuilder('Symfony\Component\Routing\Generator\UrlGeneratorInterface')->getMock());
|
2011-06-20 06:08:46 +01:00
|
|
|
$subRequest = $utils->createRequest($this->getRequest(), 'http://symfony.com/');
|
2013-06-02 23:34:18 +01:00
|
|
|
|
2011-06-20 06:08:46 +01:00
|
|
|
$this->assertEquals('/', $subRequest->getPathInfo());
|
|
|
|
}
|
|
|
|
|
2013-06-02 23:34:18 +01:00
|
|
|
public function testCreateRequestPassesSessionToTheNewRequest()
|
|
|
|
{
|
|
|
|
$request = $this->getRequest();
|
2016-12-19 09:02:29 +00:00
|
|
|
$request->setSession($session = $this->getMockBuilder('Symfony\Component\HttpFoundation\Session\SessionInterface')->getMock());
|
2013-06-02 23:34:18 +01:00
|
|
|
|
|
|
|
$utils = new HttpUtils($this->getUrlGenerator());
|
|
|
|
$subRequest = $utils->createRequest($request, '/foobar');
|
|
|
|
|
|
|
|
$this->assertSame($session, $subRequest->getSession());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider provideSecurityContextAttributes
|
|
|
|
*/
|
|
|
|
public function testCreateRequestPassesSecurityContextAttributesToTheNewRequest($attribute)
|
|
|
|
{
|
|
|
|
$request = $this->getRequest();
|
|
|
|
$request->attributes->set($attribute, 'foo');
|
|
|
|
|
|
|
|
$utils = new HttpUtils($this->getUrlGenerator());
|
|
|
|
$subRequest = $utils->createRequest($request, '/foobar');
|
|
|
|
|
|
|
|
$this->assertSame('foo', $subRequest->attributes->get($attribute));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function provideSecurityContextAttributes()
|
|
|
|
{
|
|
|
|
return array(
|
2014-09-29 08:09:34 +01:00
|
|
|
array(Security::AUTHENTICATION_ERROR),
|
|
|
|
array(Security::ACCESS_DENIED_ERROR),
|
|
|
|
array(Security::LAST_USERNAME),
|
2013-06-02 23:34:18 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2011-06-20 06:08:46 +01:00
|
|
|
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-12-11 09:40:14 +00:00
|
|
|
$this->assertTrue($utils->checkRequestPath($this->getRequest('/foo%20bar'), '/foo bar'));
|
|
|
|
// Plus must not decoded to space
|
|
|
|
$this->assertTrue($utils->checkRequestPath($this->getRequest('/foo+bar'), '/foo+bar'));
|
|
|
|
// Checking unicode
|
|
|
|
$this->assertTrue($utils->checkRequestPath($this->getRequest(urlencode('/вход')), '/вход'));
|
2013-06-02 23:34:18 +01:00
|
|
|
}
|
2011-06-20 06:08:46 +01:00
|
|
|
|
2013-06-02 23:34:18 +01:00
|
|
|
public function testCheckRequestPathWithUrlMatcherAndResourceNotFound()
|
|
|
|
{
|
2016-12-19 09:02:29 +00:00
|
|
|
$urlMatcher = $this->getMockBuilder('Symfony\Component\Routing\Matcher\UrlMatcherInterface')->getMock();
|
2012-06-26 10:17:51 +01:00
|
|
|
$urlMatcher
|
2011-07-05 10:00:08 +01:00
|
|
|
->expects($this->any())
|
|
|
|
->method('match')
|
2013-06-02 23:34:18 +01:00
|
|
|
->with('/')
|
2011-11-14 12:10:32 +00:00
|
|
|
->will($this->throwException(new ResourceNotFoundException()))
|
2011-07-05 10:00:08 +01:00
|
|
|
;
|
2013-06-02 23:34:18 +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'));
|
2013-06-02 23:34:18 +01:00
|
|
|
}
|
2011-07-05 10:00:08 +01:00
|
|
|
|
2013-06-02 23:34:18 +01:00
|
|
|
public function testCheckRequestPathWithUrlMatcherAndMethodNotAllowed()
|
|
|
|
{
|
|
|
|
$request = $this->getRequest();
|
2016-12-19 09:02:29 +00:00
|
|
|
$urlMatcher = $this->getMockBuilder('Symfony\Component\Routing\Matcher\RequestMatcherInterface')->getMock();
|
2013-06-02 23:34:18 +01:00
|
|
|
$urlMatcher
|
|
|
|
->expects($this->any())
|
|
|
|
->method('matchRequest')
|
|
|
|
->with($request)
|
|
|
|
->will($this->throwException(new MethodNotAllowedException(array())))
|
|
|
|
;
|
|
|
|
|
|
|
|
$utils = new HttpUtils(null, $urlMatcher);
|
|
|
|
$this->assertFalse($utils->checkRequestPath($request, 'foobar'));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testCheckRequestPathWithUrlMatcherAndResourceFoundByUrl()
|
|
|
|
{
|
2016-12-19 09:02:29 +00:00
|
|
|
$urlMatcher = $this->getMockBuilder('Symfony\Component\Routing\Matcher\UrlMatcherInterface')->getMock();
|
2012-06-26 10:17:51 +01:00
|
|
|
$urlMatcher
|
2011-07-05 10:00:08 +01:00
|
|
|
->expects($this->any())
|
|
|
|
->method('match')
|
2013-06-02 23:34:18 +01:00
|
|
|
->with('/foo/bar')
|
2011-07-05 10:00:08 +01:00
|
|
|
->will($this->returnValue(array('_route' => 'foobar')))
|
|
|
|
;
|
2013-06-02 23:34:18 +01:00
|
|
|
|
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'));
|
|
|
|
}
|
|
|
|
|
2013-06-02 23:34:18 +01:00
|
|
|
public function testCheckRequestPathWithUrlMatcherAndResourceFoundByRequest()
|
|
|
|
{
|
|
|
|
$request = $this->getRequest();
|
2016-12-19 09:02:29 +00:00
|
|
|
$urlMatcher = $this->getMockBuilder('Symfony\Component\Routing\Matcher\RequestMatcherInterface')->getMock();
|
2013-06-02 23:34:18 +01:00
|
|
|
$urlMatcher
|
|
|
|
->expects($this->any())
|
|
|
|
->method('matchRequest')
|
|
|
|
->with($request)
|
|
|
|
->will($this->returnValue(array('_route' => 'foobar')))
|
|
|
|
;
|
|
|
|
|
|
|
|
$utils = new HttpUtils(null, $urlMatcher);
|
|
|
|
$this->assertTrue($utils->checkRequestPath($request, '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
|
|
|
{
|
2016-12-19 09:02:29 +00:00
|
|
|
$urlMatcher = $this->getMockBuilder('Symfony\Component\Routing\Matcher\UrlMatcherInterface')->getMock();
|
2012-06-26 10:17:51 +01:00
|
|
|
$urlMatcher
|
2011-11-14 12:10:32 +00:00
|
|
|
->expects($this->any())
|
|
|
|
->method('match')
|
|
|
|
->will($this->throwException(new \RuntimeException()))
|
|
|
|
;
|
2013-06-02 23:34:18 +01:00
|
|
|
|
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');
|
|
|
|
}
|
|
|
|
|
2013-06-02 23:34:18 +01:00
|
|
|
/**
|
|
|
|
* @expectedException \InvalidArgumentException
|
|
|
|
* @expectedExceptionMessage Matcher must either implement UrlMatcherInterface or RequestMatcherInterface
|
|
|
|
*/
|
|
|
|
public function testUrlMatcher()
|
2013-03-11 05:59:49 +00:00
|
|
|
{
|
2013-06-02 23:34:18 +01:00
|
|
|
new HttpUtils($this->getUrlGenerator(), new \stdClass());
|
|
|
|
}
|
2013-03-11 05:59:49 +00:00
|
|
|
|
2013-06-02 23:34:18 +01:00
|
|
|
public function testGenerateUriRemovesQueryString()
|
|
|
|
{
|
|
|
|
$utils = new HttpUtils($this->getUrlGenerator('/foo/bar'));
|
|
|
|
$this->assertEquals('/foo/bar', $utils->generateUri(new Request(), 'route_name'));
|
2013-03-11 05:59:49 +00:00
|
|
|
|
|
|
|
$utils = new HttpUtils($this->getUrlGenerator('/foo/bar?param=value'));
|
2013-06-02 23:34:18 +01:00
|
|
|
$this->assertEquals('/foo/bar', $utils->generateUri(new Request(), 'route_name'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \LogicException
|
|
|
|
* @expectedExceptionMessage You must provide a UrlGeneratorInterface instance to be able to use routes.
|
|
|
|
*/
|
|
|
|
public function testUrlGeneratorIsRequiredToGenerateUrl()
|
|
|
|
{
|
|
|
|
$utils = new HttpUtils();
|
|
|
|
$utils->generateUri(new Request(), 'route_name');
|
2013-03-11 05:59:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private function getUrlGenerator($generatedUrl = '/foo/bar')
|
2011-06-20 06:08:46 +01:00
|
|
|
{
|
2016-12-19 09:02:29 +00:00
|
|
|
$urlGenerator = $this->getMockBuilder('Symfony\Component\Routing\Generator\UrlGeneratorInterface')->getMock();
|
2012-06-26 10:17:51 +01:00
|
|
|
$urlGenerator
|
2011-06-20 06:08:46 +01:00
|
|
|
->expects($this->any())
|
|
|
|
->method('generate')
|
2013-03-11 05:59:49 +00:00
|
|
|
->will($this->returnValue($generatedUrl))
|
2011-06-20 06:08:46 +01:00
|
|
|
;
|
|
|
|
|
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');
|
|
|
|
}
|
|
|
|
}
|