HttpUtilsTest.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. class HttpUtilsTest extends \PHPUnit_Framework_TestCase
  15. {
  16. public function testCreateRedirectResponse()
  17. {
  18. $utils = new HttpUtils($this->getRouter());
  19. // absolute path
  20. $response = $utils->createRedirectResponse($this->getRequest(), '/foobar');
  21. $this->assertTrue($response->isRedirect('http://localhost/foobar'));
  22. $this->assertEquals(302, $response->getStatusCode());
  23. // absolute URL
  24. $response = $utils->createRedirectResponse($this->getRequest(), 'http://symfony.com/');
  25. $this->assertTrue($response->isRedirect('http://symfony.com/'));
  26. // route name
  27. $utils = new HttpUtils($router = $this->getMockBuilder('Symfony\Component\Routing\Router')->disableOriginalConstructor()->getMock());
  28. $router
  29. ->expects($this->any())
  30. ->method('generate')
  31. ->with('foobar', array(), true)
  32. ->will($this->returnValue('http://localhost/foo/bar'))
  33. ;
  34. $router
  35. ->expects($this->any())
  36. ->method('getContext')
  37. ->will($this->returnValue($this->getMock('Symfony\Component\Routing\RequestContext')))
  38. ;
  39. $response = $utils->createRedirectResponse($this->getRequest(), 'foobar');
  40. $this->assertTrue($response->isRedirect('http://localhost/foo/bar'));
  41. }
  42. public function testCreateRequest()
  43. {
  44. $utils = new HttpUtils($this->getRouter());
  45. // absolute path
  46. $request = $this->getRequest();
  47. $request->server->set('Foo', 'bar');
  48. $subRequest = $utils->createRequest($request, '/foobar');
  49. $this->assertEquals('GET', $subRequest->getMethod());
  50. $this->assertEquals('/foobar', $subRequest->getPathInfo());
  51. $this->assertEquals('bar', $subRequest->server->get('Foo'));
  52. // route name
  53. $utils = new HttpUtils($router = $this->getMockBuilder('Symfony\Component\Routing\Router')->disableOriginalConstructor()->getMock());
  54. $router
  55. ->expects($this->once())
  56. ->method('generate')
  57. ->will($this->returnValue('/foo/bar'))
  58. ;
  59. $router
  60. ->expects($this->any())
  61. ->method('getContext')
  62. ->will($this->returnValue($this->getMock('Symfony\Component\Routing\RequestContext')))
  63. ;
  64. $subRequest = $utils->createRequest($this->getRequest(), 'foobar');
  65. $this->assertEquals('/foo/bar', $subRequest->getPathInfo());
  66. // absolute URL
  67. $subRequest = $utils->createRequest($this->getRequest(), 'http://symfony.com/');
  68. $this->assertEquals('/', $subRequest->getPathInfo());
  69. }
  70. public function testCheckRequestPath()
  71. {
  72. $utils = new HttpUtils($this->getRouter());
  73. $this->assertTrue($utils->checkRequestPath($this->getRequest(), '/'));
  74. $this->assertFalse($utils->checkRequestPath($this->getRequest(), '/foo'));
  75. $router = $this->getMock('Symfony\Component\Routing\RouterInterface');
  76. $router
  77. ->expects($this->any())
  78. ->method('match')
  79. ->will($this->returnValue(array()))
  80. ;
  81. $utils = new HttpUtils($router);
  82. $this->assertFalse($utils->checkRequestPath($this->getRequest(), 'foobar'));
  83. $router = $this->getMock('Symfony\Component\Routing\RouterInterface');
  84. $router
  85. ->expects($this->any())
  86. ->method('match')
  87. ->will($this->returnValue(array('_route' => 'foobar')))
  88. ;
  89. $utils = new HttpUtils($router);
  90. $this->assertTrue($utils->checkRequestPath($this->getRequest('/foo/bar'), 'foobar'));
  91. }
  92. private function getRouter()
  93. {
  94. $router = $this->getMock('Symfony\Component\Routing\RouterInterface');
  95. $router
  96. ->expects($this->any())
  97. ->method('generate')
  98. ->will($this->returnValue('/foo/bar'))
  99. ;
  100. return $router;
  101. }
  102. private function getRequest($path = '/')
  103. {
  104. return Request::create($path, 'get');
  105. }
  106. }