HttpUtilsTest.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. /*
  3. * This file is part of the Symfony framework.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * This source file is subject to the MIT license that is bundled
  8. * with this source code in the file LICENSE.
  9. */
  10. namespace Symfony\Tests\Component\Security\Http;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\HttpFoundation\Response;
  13. use Symfony\Component\Security\Http\HttpUtils;
  14. use Symfony\Component\Routing\Exception\ResourceNotFoundException;
  15. class HttpUtilsTest extends \PHPUnit_Framework_TestCase
  16. {
  17. public function testCreateRedirectResponse()
  18. {
  19. $utils = new HttpUtils($this->getRouter());
  20. // absolute path
  21. $response = $utils->createRedirectResponse($this->getRequest(), '/foobar');
  22. $this->assertTrue($response->isRedirect('http://localhost/foobar'));
  23. $this->assertEquals(302, $response->getStatusCode());
  24. // absolute URL
  25. $response = $utils->createRedirectResponse($this->getRequest(), 'http://symfony.com/');
  26. $this->assertTrue($response->isRedirect('http://symfony.com/'));
  27. // route name
  28. $utils = new HttpUtils($router = $this->getMockBuilder('Symfony\Component\Routing\Router')->disableOriginalConstructor()->getMock());
  29. $router
  30. ->expects($this->any())
  31. ->method('generate')
  32. ->with('foobar', array(), true)
  33. ->will($this->returnValue('http://localhost/foo/bar'))
  34. ;
  35. $router
  36. ->expects($this->any())
  37. ->method('getContext')
  38. ->will($this->returnValue($this->getMock('Symfony\Component\Routing\RequestContext')))
  39. ;
  40. $response = $utils->createRedirectResponse($this->getRequest(), 'foobar');
  41. $this->assertTrue($response->isRedirect('http://localhost/foo/bar'));
  42. }
  43. public function testCreateRequest()
  44. {
  45. $utils = new HttpUtils($this->getRouter());
  46. // absolute path
  47. $request = $this->getRequest();
  48. $request->server->set('Foo', 'bar');
  49. $subRequest = $utils->createRequest($request, '/foobar');
  50. $this->assertEquals('GET', $subRequest->getMethod());
  51. $this->assertEquals('/foobar', $subRequest->getPathInfo());
  52. $this->assertEquals('bar', $subRequest->server->get('Foo'));
  53. // route name
  54. $utils = new HttpUtils($router = $this->getMockBuilder('Symfony\Component\Routing\Router')->disableOriginalConstructor()->getMock());
  55. $router
  56. ->expects($this->once())
  57. ->method('generate')
  58. ->will($this->returnValue('/foo/bar'))
  59. ;
  60. $router
  61. ->expects($this->any())
  62. ->method('getContext')
  63. ->will($this->returnValue($this->getMock('Symfony\Component\Routing\RequestContext')))
  64. ;
  65. $subRequest = $utils->createRequest($this->getRequest(), 'foobar');
  66. $this->assertEquals('/foo/bar', $subRequest->getPathInfo());
  67. // absolute URL
  68. $subRequest = $utils->createRequest($this->getRequest(), 'http://symfony.com/');
  69. $this->assertEquals('/', $subRequest->getPathInfo());
  70. }
  71. public function testCheckRequestPath()
  72. {
  73. $utils = new HttpUtils($this->getRouter());
  74. $this->assertTrue($utils->checkRequestPath($this->getRequest(), '/'));
  75. $this->assertFalse($utils->checkRequestPath($this->getRequest(), '/foo'));
  76. $router = $this->getMock('Symfony\Component\Routing\RouterInterface');
  77. $router
  78. ->expects($this->any())
  79. ->method('match')
  80. ->will($this->throwException(new ResourceNotFoundException()))
  81. ;
  82. $utils = new HttpUtils($router);
  83. $this->assertFalse($utils->checkRequestPath($this->getRequest(), 'foobar'));
  84. $router = $this->getMock('Symfony\Component\Routing\RouterInterface');
  85. $router
  86. ->expects($this->any())
  87. ->method('match')
  88. ->will($this->returnValue(array('_route' => 'foobar')))
  89. ;
  90. $utils = new HttpUtils($router);
  91. $this->assertTrue($utils->checkRequestPath($this->getRequest('/foo/bar'), 'foobar'));
  92. }
  93. /**
  94. * @expectedException \RuntimeException
  95. */
  96. public function testCheckRequestPathWithRouterLoadingException()
  97. {
  98. $router = $this->getMock('Symfony\Component\Routing\RouterInterface');
  99. $router
  100. ->expects($this->any())
  101. ->method('match')
  102. ->will($this->throwException(new \RuntimeException()))
  103. ;
  104. $utils = new HttpUtils($router);
  105. $utils->checkRequestPath($this->getRequest(), 'foobar');
  106. }
  107. private function getRouter()
  108. {
  109. $router = $this->getMock('Symfony\Component\Routing\RouterInterface');
  110. $router
  111. ->expects($this->any())
  112. ->method('generate')
  113. ->will($this->returnValue('/foo/bar'))
  114. ;
  115. return $router;
  116. }
  117. private function getRequest($path = '/')
  118. {
  119. return Request::create($path, 'get');
  120. }
  121. }