RequestMatcherTest.php 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. array(false, '2a01:198:603:0:396e:4789:8e99:890f', '::1'),
  56. array(true, '0:0:0:0:0:0:0:1', '::1'),
  57. array(false, '0:0:603:0:396e:4789:8e99:0001', '::1'),
  58. );
  59. }
  60. public function testAnIpv6WithOptionDisabledIpv6()
  61. {
  62. if (defined('AF_INET6')) {
  63. $this->markTestSkipped('Only works when PHP is compiled with the option "disable-ipv6".');
  64. }
  65. $request = Request::create('', 'get', array(), array(), array(), array('REMOTE_ADDR' => '2a01:198:603:0:396e:4789:8e99:890f'));
  66. $matcher = new RequestMatcher();
  67. $matcher->matchIp('2a01:198:603:0::/65');
  68. try {
  69. $matcher->matches($request);
  70. $this->fail('An expected RuntimeException has not been raised.');
  71. } catch (\Exception $e) {
  72. $this->assertInstanceOf('\RuntimeException', $e);
  73. }
  74. }
  75. /**
  76. * @dataProvider testMethodFixtures
  77. */
  78. public function testMethod($requestMethod, $matcherMethod, $isMatch)
  79. {
  80. $matcher = new RequestMatcher();
  81. $matcher->matchMethod($matcherMethod);
  82. $request = Request::create('', $requestMethod);
  83. $this->assertSame($isMatch, $matcher->matches($request));
  84. $matcher = new RequestMatcher(null, null, $matcherMethod);
  85. $request = Request::create('', $requestMethod);
  86. $this->assertSame($isMatch, $matcher->matches($request));
  87. }
  88. public function testMethodFixtures()
  89. {
  90. return array(
  91. array('get', 'get', true),
  92. array('get', array('get', 'post'), true),
  93. array('get', 'post', false),
  94. array('get', 'GET', true),
  95. array('get', array('GET', 'POST'), true),
  96. array('get', 'POST', false),
  97. );
  98. }
  99. /**
  100. * @dataProvider testHostFixture
  101. */
  102. public function testHost($pattern, $isMatch)
  103. {
  104. $matcher = new RequestMatcher();
  105. $request = Request::create('', 'get', array(), array(), array(), array('HTTP_HOST' => 'foo.example.com'));
  106. $matcher->matchHost($pattern);
  107. $this->assertSame($isMatch, $matcher->matches($request));
  108. $matcher= new RequestMatcher(null, $pattern);
  109. $this->assertSame($isMatch, $matcher->matches($request));
  110. }
  111. public function testHostFixture()
  112. {
  113. return array(
  114. array('.*\.example\.com', true),
  115. array('\.example\.com$', true),
  116. array('^.*\.example\.com$', true),
  117. array('.*\.sensio\.com', false),
  118. array('.*\.example\.COM', true),
  119. array('\.example\.COM$', true),
  120. array('^.*\.example\.COM$', true),
  121. array('.*\.sensio\.COM', false), );
  122. }
  123. public function testPath()
  124. {
  125. $matcher = new RequestMatcher();
  126. $request = Request::create('/admin/foo');
  127. $matcher->matchPath('/admin/.*');
  128. $this->assertTrue($matcher->matches($request));
  129. $matcher->matchPath('/admin');
  130. $this->assertTrue($matcher->matches($request));
  131. $matcher->matchPath('^/admin/.*$');
  132. $this->assertTrue($matcher->matches($request));
  133. $matcher->matchMethod('/blog/.*');
  134. $this->assertFalse($matcher->matches($request));
  135. }
  136. public function testPathWithLocaleIsNotSupported()
  137. {
  138. $matcher = new RequestMatcher();
  139. $request = Request::create('/en/login');
  140. $session = new Session(new ArraySessionStorage());
  141. $session->setLocale('en');
  142. $request->setSession($session);
  143. $matcher->matchPath('^/{_locale}/login$');
  144. $this->assertFalse($matcher->matches($request));
  145. }
  146. public function testAttributes()
  147. {
  148. $matcher = new RequestMatcher();
  149. $request = Request::create('/admin/foo');
  150. $request->attributes->set('foo', 'foo_bar');
  151. $matcher->matchAttribute('foo', 'foo_.*');
  152. $this->assertTrue($matcher->matches($request));
  153. $matcher->matchAttribute('foo', 'foo');
  154. $this->assertTrue($matcher->matches($request));
  155. $matcher->matchAttribute('foo', '^foo_bar$');
  156. $this->assertTrue($matcher->matches($request));
  157. $matcher->matchAttribute('foo', 'babar');
  158. $this->assertFalse($matcher->matches($request));
  159. }
  160. }