RequestMatcherTest.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien.potencier@symfony-project.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. public function testIp()
  16. {
  17. $matcher = new RequestMatcher();
  18. $matcher->matchIp('192.168.1.1/1');
  19. $request = Request::create('', 'get', array(), array(), array(), array('REMOTE_ADDR' => '192.168.1.1'));
  20. $this->assertTrue($matcher->matches($request));
  21. $matcher->matchIp('192.168.1.0/24');
  22. $this->assertTrue($matcher->matches($request));
  23. $matcher->matchIp('1.2.3.4/1');
  24. $this->assertFalse($matcher->matches($request));
  25. }
  26. public function testMethod()
  27. {
  28. $matcher = new RequestMatcher();
  29. $matcher->matchMethod('get');
  30. $request = Request::create('', 'get');
  31. $this->assertTrue($matcher->matches($request));
  32. $matcher->matchMethod('post');
  33. $this->assertFalse($matcher->matches($request));
  34. $matcher->matchMethod(array('get', 'post'));
  35. $this->assertTrue($matcher->matches($request));
  36. }
  37. public function testHost()
  38. {
  39. $matcher = new RequestMatcher();
  40. $matcher->matchHost('.*\.example\.com');
  41. $request = Request::create('', 'get', array(), array(), array(), array('HTTP_HOST' => 'foo.example.com'));
  42. $this->assertTrue($matcher->matches($request));
  43. $matcher->matchMethod('.*\.sensio\.com');
  44. $this->assertFalse($matcher->matches($request));
  45. }
  46. public function testPath()
  47. {
  48. $matcher = new RequestMatcher();
  49. $matcher->matchPath('/admin/.*');
  50. $request = Request::create('/admin/foo');
  51. $this->assertTrue($matcher->matches($request));
  52. $matcher->matchMethod('/blog/.*');
  53. $this->assertFalse($matcher->matches($request));
  54. }
  55. }