RequestMatcherTest.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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\Tests\Component\HttpFoundation;
  11. use Symfony\Component\HttpFoundation\SessionStorage\ArraySessionStorage;
  12. use Symfony\Component\HttpFoundation\Session;
  13. use Symfony\Component\HttpFoundation\RequestMatcher;
  14. use Symfony\Component\HttpFoundation\Request;
  15. class RequestMatcherTest extends \PHPUnit_Framework_TestCase
  16. {
  17. /**
  18. * @dataProvider testIpProvider
  19. */
  20. public function testIp($matches, $remoteAddr, $cidr)
  21. {
  22. $request = Request::create('', 'get', array(), array(), array(), array('REMOTE_ADDR' => $remoteAddr));
  23. $matcher = new RequestMatcher();
  24. $matcher->matchIp($cidr);
  25. $this->assertEquals($matches, $matcher->matches($request));
  26. }
  27. public function testIpProvider()
  28. {
  29. return array(
  30. array(true, '192.168.1.1', '192.168.1.1/1'),
  31. array(true, '192.168.1.1', '192.168.1.0/24'),
  32. array(false, '192.168.1.1', '1.2.3.4/1'),
  33. array(true, '2a01:198:603:0:396e:4789:8e99:890f', '2a01:198:603:0::/65'),
  34. array(false, '2a00:198:603:0:396e:4789:8e99:890f', '2a01:198:603:0::/65'),
  35. );
  36. }
  37. public function testMethod()
  38. {
  39. $matcher = new RequestMatcher();
  40. $matcher->matchMethod('get');
  41. $request = Request::create('', 'get');
  42. $this->assertTrue($matcher->matches($request));
  43. $matcher->matchMethod('post');
  44. $this->assertFalse($matcher->matches($request));
  45. $matcher->matchMethod(array('get', 'post'));
  46. $this->assertTrue($matcher->matches($request));
  47. }
  48. public function testHost()
  49. {
  50. $matcher = new RequestMatcher();
  51. $request = Request::create('', 'get', array(), array(), array(), array('HTTP_HOST' => 'foo.example.com'));
  52. $matcher->matchHost('.*\.example\.com');
  53. $this->assertTrue($matcher->matches($request));
  54. $matcher->matchHost('\.example\.com$');
  55. $this->assertTrue($matcher->matches($request));
  56. $matcher->matchHost('^.*\.example\.com$');
  57. $this->assertTrue($matcher->matches($request));
  58. $matcher->matchMethod('.*\.sensio\.com');
  59. $this->assertFalse($matcher->matches($request));
  60. }
  61. public function testPath()
  62. {
  63. $matcher = new RequestMatcher();
  64. $request = Request::create('/admin/foo');
  65. $matcher->matchPath('/admin/.*');
  66. $this->assertTrue($matcher->matches($request));
  67. $matcher->matchPath('/admin');
  68. $this->assertTrue($matcher->matches($request));
  69. $matcher->matchPath('^/admin/.*$');
  70. $this->assertTrue($matcher->matches($request));
  71. $matcher->matchMethod('/blog/.*');
  72. $this->assertFalse($matcher->matches($request));
  73. }
  74. public function testPathWithLocale()
  75. {
  76. $matcher = new RequestMatcher();
  77. $request = Request::create('/en/login');
  78. $session = new Session(new ArraySessionStorage());
  79. $session->setLocale('en');
  80. $request->setSession($session);
  81. $matcher->matchPath('^/{_locale}/login$');
  82. $this->assertTrue($matcher->matches($request));
  83. $session->setLocale('de');
  84. $this->assertFalse($matcher->matches($request));
  85. }
  86. public function testAttributes()
  87. {
  88. $matcher = new RequestMatcher();
  89. $request = Request::create('/admin/foo');
  90. $request->attributes->set('foo', 'foo_bar');
  91. $matcher->matchAttribute('foo', 'foo_.*');
  92. $this->assertTrue($matcher->matches($request));
  93. $matcher->matchAttribute('foo', 'foo');
  94. $this->assertTrue($matcher->matches($request));
  95. $matcher->matchAttribute('foo', '^foo_bar$');
  96. $this->assertTrue($matcher->matches($request));
  97. $matcher->matchAttribute('foo', 'babar');
  98. $this->assertFalse($matcher->matches($request));
  99. }
  100. }