PageRenderTime 27ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/RestAPI/vendor/symfony/security/Http/Tests/HttpUtilsTest.php

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