HttpUtilsTest.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Security\Tests\Http;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\Routing\Exception\MethodNotAllowedException;
  13. use Symfony\Component\Routing\Exception\ResourceNotFoundException;
  14. use Symfony\Component\Security\Core\SecurityContextInterface;
  15. use Symfony\Component\Security\Http\HttpUtils;
  16. class HttpUtilsTest extends \PHPUnit_Framework_TestCase
  17. {
  18. protected function setUp()
  19. {
  20. if (!class_exists('Symfony\Component\HttpFoundation\Request')) {
  21. $this->markTestSkipped('The "HttpFoundation" component is not available');
  22. }
  23. if (!class_exists('Symfony\Component\Routing\Router')) {
  24. $this->markTestSkipped('The "Routing" component is not available');
  25. }
  26. }
  27. public function testCreateRedirectResponseWithPath()
  28. {
  29. $utils = new HttpUtils($this->getUrlGenerator());
  30. $response = $utils->createRedirectResponse($this->getRequest(), '/foobar');
  31. $this->assertTrue($response->isRedirect('http://localhost/foobar'));
  32. $this->assertEquals(302, $response->getStatusCode());
  33. }
  34. public function testCreateRedirectResponseWithAbsoluteUrl()
  35. {
  36. $utils = new HttpUtils($this->getUrlGenerator());
  37. $response = $utils->createRedirectResponse($this->getRequest(), 'http://symfony.com/');
  38. $this->assertTrue($response->isRedirect('http://symfony.com/'));
  39. }
  40. public function testCreateRedirectResponseWithRouteName()
  41. {
  42. $utils = new HttpUtils($urlGenerator = $this->getMock('Symfony\Component\Routing\Generator\UrlGeneratorInterface'));
  43. $urlGenerator
  44. ->expects($this->any())
  45. ->method('generate')
  46. ->with('foobar', array(), true)
  47. ->will($this->returnValue('http://localhost/foo/bar'))
  48. ;
  49. $urlGenerator
  50. ->expects($this->any())
  51. ->method('getContext')
  52. ->will($this->returnValue($this->getMock('Symfony\Component\Routing\RequestContext')))
  53. ;
  54. $response = $utils->createRedirectResponse($this->getRequest(), 'foobar');
  55. $this->assertTrue($response->isRedirect('http://localhost/foo/bar'));
  56. }
  57. public function testCreateRequestWithPath()
  58. {
  59. $request = $this->getRequest();
  60. $request->server->set('Foo', 'bar');
  61. $utils = new HttpUtils($this->getUrlGenerator());
  62. $subRequest = $utils->createRequest($request, '/foobar');
  63. $this->assertEquals('GET', $subRequest->getMethod());
  64. $this->assertEquals('/foobar', $subRequest->getPathInfo());
  65. $this->assertEquals('bar', $subRequest->server->get('Foo'));
  66. }
  67. public function testCreateRequestWithRouteName()
  68. {
  69. $utils = new HttpUtils($urlGenerator = $this->getMock('Symfony\Component\Routing\Generator\UrlGeneratorInterface'));
  70. $urlGenerator
  71. ->expects($this->once())
  72. ->method('generate')
  73. ->will($this->returnValue('/foo/bar'))
  74. ;
  75. $urlGenerator
  76. ->expects($this->any())
  77. ->method('getContext')
  78. ->will($this->returnValue($this->getMock('Symfony\Component\Routing\RequestContext')))
  79. ;
  80. $subRequest = $utils->createRequest($this->getRequest(), 'foobar');
  81. $this->assertEquals('/foo/bar', $subRequest->getPathInfo());
  82. }
  83. public function testCreateRequestWithAbsoluteUrl()
  84. {
  85. $utils = new HttpUtils($this->getMock('Symfony\Component\Routing\Generator\UrlGeneratorInterface'));
  86. $subRequest = $utils->createRequest($this->getRequest(), 'http://symfony.com/');
  87. $this->assertEquals('/', $subRequest->getPathInfo());
  88. }
  89. public function testCreateRequestPassesSessionToTheNewRequest()
  90. {
  91. $request = $this->getRequest();
  92. $request->setSession($session = $this->getMock('Symfony\Component\HttpFoundation\Session\SessionInterface'));
  93. $utils = new HttpUtils($this->getUrlGenerator());
  94. $subRequest = $utils->createRequest($request, '/foobar');
  95. $this->assertSame($session, $subRequest->getSession());
  96. }
  97. /**
  98. * @dataProvider provideSecurityContextAttributes
  99. */
  100. public function testCreateRequestPassesSecurityContextAttributesToTheNewRequest($attribute)
  101. {
  102. $request = $this->getRequest();
  103. $request->attributes->set($attribute, 'foo');
  104. $utils = new HttpUtils($this->getUrlGenerator());
  105. $subRequest = $utils->createRequest($request, '/foobar');
  106. $this->assertSame('foo', $subRequest->attributes->get($attribute));
  107. }
  108. public function provideSecurityContextAttributes()
  109. {
  110. return array(
  111. array(SecurityContextInterface::AUTHENTICATION_ERROR),
  112. array(SecurityContextInterface::ACCESS_DENIED_ERROR),
  113. array(SecurityContextInterface::LAST_USERNAME)
  114. );
  115. }
  116. public function testCheckRequestPath()
  117. {
  118. $utils = new HttpUtils($this->getUrlGenerator());
  119. $this->assertTrue($utils->checkRequestPath($this->getRequest(), '/'));
  120. $this->assertFalse($utils->checkRequestPath($this->getRequest(), '/foo'));
  121. $this->assertTrue($utils->checkRequestPath($this->getRequest('/foo%20bar'), '/foo bar'));
  122. // Plus must not decoded to space
  123. $this->assertTrue($utils->checkRequestPath($this->getRequest('/foo+bar'), '/foo+bar'));
  124. // Checking unicode
  125. $this->assertTrue($utils->checkRequestPath($this->getRequest(urlencode('/вход')), '/вход'));
  126. }
  127. public function testCheckRequestPathWithUrlMatcherAndResourceNotFound()
  128. {
  129. $urlMatcher = $this->getMock('Symfony\Component\Routing\Matcher\UrlMatcherInterface');
  130. $urlMatcher
  131. ->expects($this->any())
  132. ->method('match')
  133. ->with('/')
  134. ->will($this->throwException(new ResourceNotFoundException()))
  135. ;
  136. $utils = new HttpUtils(null, $urlMatcher);
  137. $this->assertFalse($utils->checkRequestPath($this->getRequest(), 'foobar'));
  138. }
  139. public function testCheckRequestPathWithUrlMatcherAndMethodNotAllowed()
  140. {
  141. $request = $this->getRequest();
  142. $urlMatcher = $this->getMock('Symfony\Component\Routing\Matcher\RequestMatcherInterface');
  143. $urlMatcher
  144. ->expects($this->any())
  145. ->method('matchRequest')
  146. ->with($request)
  147. ->will($this->throwException(new MethodNotAllowedException(array())))
  148. ;
  149. $utils = new HttpUtils(null, $urlMatcher);
  150. $this->assertFalse($utils->checkRequestPath($request, 'foobar'));
  151. }
  152. public function testCheckRequestPathWithUrlMatcherAndResourceFoundByUrl()
  153. {
  154. $urlMatcher = $this->getMock('Symfony\Component\Routing\Matcher\UrlMatcherInterface');
  155. $urlMatcher
  156. ->expects($this->any())
  157. ->method('match')
  158. ->with('/foo/bar')
  159. ->will($this->returnValue(array('_route' => 'foobar')))
  160. ;
  161. $utils = new HttpUtils(null, $urlMatcher);
  162. $this->assertTrue($utils->checkRequestPath($this->getRequest('/foo/bar'), 'foobar'));
  163. }
  164. public function testCheckRequestPathWithUrlMatcherAndResourceFoundByRequest()
  165. {
  166. $request = $this->getRequest();
  167. $urlMatcher = $this->getMock('Symfony\Component\Routing\Matcher\RequestMatcherInterface');
  168. $urlMatcher
  169. ->expects($this->any())
  170. ->method('matchRequest')
  171. ->with($request)
  172. ->will($this->returnValue(array('_route' => 'foobar')))
  173. ;
  174. $utils = new HttpUtils(null, $urlMatcher);
  175. $this->assertTrue($utils->checkRequestPath($request, 'foobar'));
  176. }
  177. /**
  178. * @expectedException \RuntimeException
  179. */
  180. public function testCheckRequestPathWithUrlMatcherLoadingException()
  181. {
  182. $urlMatcher = $this->getMock('Symfony\Component\Routing\Matcher\UrlMatcherInterface');
  183. $urlMatcher
  184. ->expects($this->any())
  185. ->method('match')
  186. ->will($this->throwException(new \RuntimeException()))
  187. ;
  188. $utils = new HttpUtils(null, $urlMatcher);
  189. $utils->checkRequestPath($this->getRequest(), 'foobar');
  190. }
  191. /**
  192. * @expectedException \InvalidArgumentException
  193. * @expectedExceptionMessage Matcher must either implement UrlMatcherInterface or RequestMatcherInterface
  194. */
  195. public function testUrlMatcher()
  196. {
  197. new HttpUtils($this->getUrlGenerator(), new \stdClass());
  198. }
  199. public function testGenerateUriRemovesQueryString()
  200. {
  201. $utils = new HttpUtils($this->getUrlGenerator('/foo/bar'));
  202. $this->assertEquals('/foo/bar', $utils->generateUri(new Request(), 'route_name'));
  203. $utils = new HttpUtils($this->getUrlGenerator('/foo/bar?param=value'));
  204. $this->assertEquals('/foo/bar', $utils->generateUri(new Request(), 'route_name'));
  205. }
  206. /**
  207. * @expectedException \LogicException
  208. * @expectedExceptionMessage You must provide a UrlGeneratorInterface instance to be able to use routes.
  209. */
  210. public function testUrlGeneratorIsRequiredToGenerateUrl()
  211. {
  212. $utils = new HttpUtils();
  213. $utils->generateUri(new Request(), 'route_name');
  214. }
  215. private function getUrlGenerator($generatedUrl = '/foo/bar')
  216. {
  217. $urlGenerator = $this->getMock('Symfony\Component\Routing\Generator\UrlGeneratorInterface');
  218. $urlGenerator
  219. ->expects($this->any())
  220. ->method('generate')
  221. ->will($this->returnValue($generatedUrl))
  222. ;
  223. return $urlGenerator;
  224. }
  225. private function getRequest($path = '/')
  226. {
  227. return Request::create($path, 'get');
  228. }
  229. }