RequestMatcherTest.php 3.4 KB

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