HttpUtilsTest.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. $subRequest = $utils->createRequest($this->getRequest(), 'foobar');
  54. $this->assertEquals('/foo/bar', $subRequest->getPathInfo());
  55. // absolute URL
  56. $subRequest = $utils->createRequest($this->getRequest(), 'http://symfony.com/');
  57. $this->assertEquals('/', $subRequest->getPathInfo());
  58. }
  59. public function testCheckRequestPath()
  60. {
  61. $utils = new HttpUtils($this->getRouter());
  62. $this->assertTrue($utils->checkRequestPath($this->getRequest(), '/'));
  63. $this->assertFalse($utils->checkRequestPath($this->getRequest(), '/foo'));
  64. $router = $this->getMock('Symfony\Component\Routing\RouterInterface');
  65. $router
  66. ->expects($this->any())
  67. ->method('match')
  68. ->will($this->returnValue(array()))
  69. ;
  70. $utils = new HttpUtils($router);
  71. $this->assertFalse($utils->checkRequestPath($this->getRequest(), 'foobar'));
  72. $router = $this->getMock('Symfony\Component\Routing\RouterInterface');
  73. $router
  74. ->expects($this->any())
  75. ->method('match')
  76. ->will($this->returnValue(array('_route' => 'foobar')))
  77. ;
  78. $utils = new HttpUtils($router);
  79. $this->assertTrue($utils->checkRequestPath($this->getRequest('/foo/bar'), 'foobar'));
  80. }
  81. private function getRouter()
  82. {
  83. $router = $this->getMock('Symfony\Component\Routing\RouterInterface');
  84. $router
  85. ->expects($this->any())
  86. ->method('generate')
  87. ->will($this->returnValue('/foo/bar'))
  88. ;
  89. return $router;
  90. }
  91. private function getRequest($path = '/')
  92. {
  93. return Request::create($path, 'get');
  94. }
  95. }