RequestMatcherTest.php 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 testIpv4Provider
  19. */
  20. public function testIpv4($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 testIpv4Provider()
  28. {
  29. return array(
  30. array(true, '192.168.1.1', '192.168.1.1'),
  31. array(true, '192.168.1.1', '192.168.1.1/1'),
  32. array(true, '192.168.1.1', '192.168.1.0/24'),
  33. array(false, '192.168.1.1', '1.2.3.4/1'),
  34. array(false, '192.168.1.1', '192.168.1/33'),
  35. );
  36. }
  37. /**
  38. * @dataProvider testIpv6Provider
  39. */
  40. public function testIpv6($matches, $remoteAddr, $cidr)
  41. {
  42. if (!defined('AF_INET6')) {
  43. $this->markTestSkipped('Only works when PHP is compiled without the option "disable-ipv6".');
  44. }
  45. $request = Request::create('', 'get', array(), array(), array(), array('REMOTE_ADDR' => $remoteAddr));
  46. $matcher = new RequestMatcher();
  47. $matcher->matchIp($cidr);
  48. $this->assertEquals($matches, $matcher->matches($request));
  49. }
  50. public function testIpv6Provider()
  51. {
  52. return array(
  53. array(true, '2a01:198:603:0:396e:4789:8e99:890f', '2a01:198:603:0::/65'),
  54. array(false, '2a00:198:603:0:396e:4789:8e99:890f', '2a01:198:603:0::/65'),
  55. );
  56. }
  57. public function testAnIpv6WithOptionDisabledIpv6()
  58. {
  59. if (defined('AF_INET6')) {
  60. $this->markTestSkipped('Only works when PHP is compiled with the option "disable-ipv6".');
  61. }
  62. $request = Request::create('', 'get', array(), array(), array(), array('REMOTE_ADDR' => '2a01:198:603:0:396e:4789:8e99:890f'));
  63. $matcher = new RequestMatcher();
  64. $matcher->matchIp('2a01:198:603:0::/65');
  65. try {
  66. $matcher->matches($request);
  67. $this->fail('An expected RuntimeException has not been raised.');
  68. } catch (\Exception $e) {
  69. $this->assertInstanceOf('\RuntimeException', $e);
  70. }
  71. }
  72. public function testMethod()
  73. {
  74. $matcher = new RequestMatcher();
  75. $matcher->matchMethod('get');
  76. $request = Request::create('', 'get');
  77. $this->assertTrue($matcher->matches($request));
  78. $matcher->matchMethod('post');
  79. $this->assertFalse($matcher->matches($request));
  80. $matcher->matchMethod(array('get', 'post'));
  81. $this->assertTrue($matcher->matches($request));
  82. }
  83. public function testHost()
  84. {
  85. $matcher = new RequestMatcher();
  86. $request = Request::create('', 'get', array(), array(), array(), array('HTTP_HOST' => 'foo.example.com'));
  87. $matcher->matchHost('.*\.example\.com');
  88. $this->assertTrue($matcher->matches($request));
  89. $matcher->matchHost('\.example\.com$');
  90. $this->assertTrue($matcher->matches($request));
  91. $matcher->matchHost('^.*\.example\.com$');
  92. $this->assertTrue($matcher->matches($request));
  93. $matcher->matchMethod('.*\.sensio\.com');
  94. $this->assertFalse($matcher->matches($request));
  95. }
  96. public function testPath()
  97. {
  98. $matcher = new RequestMatcher();
  99. $request = Request::create('/admin/foo');
  100. $matcher->matchPath('/admin/.*');
  101. $this->assertTrue($matcher->matches($request));
  102. $matcher->matchPath('/admin');
  103. $this->assertTrue($matcher->matches($request));
  104. $matcher->matchPath('^/admin/.*$');
  105. $this->assertTrue($matcher->matches($request));
  106. $matcher->matchMethod('/blog/.*');
  107. $this->assertFalse($matcher->matches($request));
  108. }
  109. public function testPathWithLocaleIsNotSupported()
  110. {
  111. $matcher = new RequestMatcher();
  112. $request = Request::create('/en/login');
  113. $session = new Session(new ArraySessionStorage());
  114. $session->setLocale('en');
  115. $request->setSession($session);
  116. $matcher->matchPath('^/{_locale}/login$');
  117. $this->assertFalse($matcher->matches($request));
  118. }
  119. public function testAttributes()
  120. {
  121. $matcher = new RequestMatcher();
  122. $request = Request::create('/admin/foo');
  123. $request->attributes->set('foo', 'foo_bar');
  124. $matcher->matchAttribute('foo', 'foo_.*');
  125. $this->assertTrue($matcher->matches($request));
  126. $matcher->matchAttribute('foo', 'foo');
  127. $this->assertTrue($matcher->matches($request));
  128. $matcher->matchAttribute('foo', '^foo_bar$');
  129. $this->assertTrue($matcher->matches($request));
  130. $matcher->matchAttribute('foo', 'babar');
  131. $this->assertFalse($matcher->matches($request));
  132. }
  133. }