RequestMatcherTest.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. );
  32. }
  33. public function testMethod()
  34. {
  35. $matcher = new RequestMatcher();
  36. $matcher->matchMethod('get');
  37. $request = Request::create('', 'get');
  38. $this->assertTrue($matcher->matches($request));
  39. $matcher->matchMethod('post');
  40. $this->assertFalse($matcher->matches($request));
  41. $matcher->matchMethod(array('get', 'post'));
  42. $this->assertTrue($matcher->matches($request));
  43. }
  44. public function testHost()
  45. {
  46. $matcher = new RequestMatcher();
  47. $request = Request::create('', 'get', array(), array(), array(), array('HTTP_HOST' => 'foo.example.com'));
  48. $matcher->matchHost('.*\.example\.com');
  49. $this->assertTrue($matcher->matches($request));
  50. $matcher->matchHost('\.example\.com$');
  51. $this->assertTrue($matcher->matches($request));
  52. $matcher->matchHost('^.*\.example\.com$');
  53. $this->assertTrue($matcher->matches($request));
  54. $matcher->matchMethod('.*\.sensio\.com');
  55. $this->assertFalse($matcher->matches($request));
  56. }
  57. public function testPath()
  58. {
  59. $matcher = new RequestMatcher();
  60. $request = Request::create('/admin/foo');
  61. $matcher->matchPath('/admin/.*');
  62. $this->assertTrue($matcher->matches($request));
  63. $matcher->matchPath('/admin');
  64. $this->assertTrue($matcher->matches($request));
  65. $matcher->matchPath('^/admin/.*$');
  66. $this->assertTrue($matcher->matches($request));
  67. $matcher->matchMethod('/blog/.*');
  68. $this->assertFalse($matcher->matches($request));
  69. }
  70. public function testAttributes()
  71. {
  72. $matcher = new RequestMatcher();
  73. $request = Request::create('/admin/foo');
  74. $request->attributes->set('foo', 'foo_bar');
  75. $matcher->matchAttribute('foo', 'foo_.*');
  76. $this->assertTrue($matcher->matches($request));
  77. $matcher->matchAttribute('foo', 'foo');
  78. $this->assertTrue($matcher->matches($request));
  79. $matcher->matchAttribute('foo', '^foo_bar$');
  80. $this->assertTrue($matcher->matches($request));
  81. $matcher->matchAttribute('foo', 'babar');
  82. $this->assertFalse($matcher->matches($request));
  83. }
  84. }