HttpUtilsTest.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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($generator = $this->getUrlGenerator());
  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($generator = $this->getMock('Symfony\Component\Routing\Generator\UrlGeneratorInterface'));
  28. $generator
  29. ->expects($this->any())
  30. ->method('generate')
  31. ->with('foobar', array(), true)
  32. ->will($this->returnValue('http://localhost/foo/bar'))
  33. ;
  34. $response = $utils->createRedirectResponse($this->getRequest(), 'foobar');
  35. }
  36. public function testCreateRequest()
  37. {
  38. $utils = new HttpUtils($this->getUrlGenerator());
  39. // absolute path
  40. $request = $this->getRequest();
  41. $request->server->set('Foo', 'bar');
  42. $subRequest = $utils->createRequest($request, '/foobar');
  43. $this->assertEquals('GET', $subRequest->getMethod());
  44. $this->assertEquals('/foobar', $subRequest->getPathInfo());
  45. $this->assertEquals('bar', $subRequest->server->get('Foo'));
  46. // route name
  47. $subRequest = $utils->createRequest($this->getRequest(), 'foobar');
  48. $this->assertEquals('/foo/bar', $subRequest->getPathInfo());
  49. // absolute URL
  50. $subRequest = $utils->createRequest($this->getRequest(), 'http://symfony.com/');
  51. $this->assertEquals('/', $subRequest->getPathInfo());
  52. }
  53. public function testCheckRequestPath()
  54. {
  55. $utils = new HttpUtils($this->getUrlGenerator());
  56. $this->assertTrue($utils->checkRequestPath($this->getRequest(), '/'));
  57. $this->assertFalse($utils->checkRequestPath($this->getRequest(), '/foo'));
  58. $this->assertFalse($utils->checkRequestPath($this->getRequest(), 'foobar'));
  59. $this->assertTrue($utils->checkRequestPath($this->getRequest('/foo/bar'), 'foobar'));
  60. }
  61. private function getUrlGenerator()
  62. {
  63. $generator = $this->getMock('Symfony\Component\Routing\Generator\UrlGeneratorInterface');
  64. $generator
  65. ->expects($this->any())
  66. ->method('generate')
  67. ->will($this->returnValue('/foo/bar'))
  68. ;
  69. return $generator;
  70. }
  71. private function getRequest($path = '/')
  72. {
  73. return Request::create($path, 'get');
  74. }
  75. }