RequestMatcherTest.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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\Component\HttpFoundation\Tests;
  11. use Symfony\Component\HttpFoundation\RequestMatcher;
  12. use Symfony\Component\HttpFoundation\Request;
  13. class RequestMatcherTest extends \PHPUnit_Framework_TestCase
  14. {
  15. /**
  16. * @dataProvider testMethodFixtures
  17. */
  18. public function testMethod($requestMethod, $matcherMethod, $isMatch)
  19. {
  20. $matcher = new RequestMatcher();
  21. $matcher->matchMethod($matcherMethod);
  22. $request = Request::create('', $requestMethod);
  23. $this->assertSame($isMatch, $matcher->matches($request));
  24. $matcher = new RequestMatcher(null, null, $matcherMethod);
  25. $request = Request::create('', $requestMethod);
  26. $this->assertSame($isMatch, $matcher->matches($request));
  27. }
  28. public function testMethodFixtures()
  29. {
  30. return array(
  31. array('get', 'get', true),
  32. array('get', array('get', 'post'), true),
  33. array('get', 'post', false),
  34. array('get', 'GET', true),
  35. array('get', array('GET', 'POST'), true),
  36. array('get', 'POST', false),
  37. );
  38. }
  39. public function testScheme()
  40. {
  41. $httpRequest = $request = $request = Request::create('');
  42. $httpsRequest = $request = $request = Request::create('', 'get', array(), array(), array(), array('HTTPS' => 'on'));
  43. $matcher = new RequestMatcher();
  44. $matcher->matchScheme('https');
  45. $this->assertFalse($matcher->matches($httpRequest));
  46. $this->assertTrue($matcher->matches($httpsRequest));
  47. $matcher->matchScheme('http');
  48. $this->assertFalse($matcher->matches($httpsRequest));
  49. $this->assertTrue($matcher->matches($httpRequest));
  50. $matcher = new RequestMatcher();
  51. $this->assertTrue($matcher->matches($httpsRequest));
  52. $this->assertTrue($matcher->matches($httpRequest));
  53. }
  54. /**
  55. * @dataProvider testHostFixture
  56. */
  57. public function testHost($pattern, $isMatch)
  58. {
  59. $matcher = new RequestMatcher();
  60. $request = Request::create('', 'get', array(), array(), array(), array('HTTP_HOST' => 'foo.example.com'));
  61. $matcher->matchHost($pattern);
  62. $this->assertSame($isMatch, $matcher->matches($request));
  63. $matcher = new RequestMatcher(null, $pattern);
  64. $this->assertSame($isMatch, $matcher->matches($request));
  65. }
  66. public function testHostFixture()
  67. {
  68. return array(
  69. array('.*\.example\.com', true),
  70. array('\.example\.com$', true),
  71. array('^.*\.example\.com$', true),
  72. array('.*\.sensio\.com', false),
  73. array('.*\.example\.COM', true),
  74. array('\.example\.COM$', true),
  75. array('^.*\.example\.COM$', true),
  76. array('.*\.sensio\.COM', false),
  77. );
  78. }
  79. public function testPath()
  80. {
  81. $matcher = new RequestMatcher();
  82. $request = Request::create('/admin/foo');
  83. $matcher->matchPath('/admin/.*');
  84. $this->assertTrue($matcher->matches($request));
  85. $matcher->matchPath('/admin');
  86. $this->assertTrue($matcher->matches($request));
  87. $matcher->matchPath('^/admin/.*$');
  88. $this->assertTrue($matcher->matches($request));
  89. $matcher->matchMethod('/blog/.*');
  90. $this->assertFalse($matcher->matches($request));
  91. }
  92. public function testPathWithLocaleIsNotSupported()
  93. {
  94. $matcher = new RequestMatcher();
  95. $request = Request::create('/en/login');
  96. $request->setLocale('en');
  97. $matcher->matchPath('^/{_locale}/login$');
  98. $this->assertFalse($matcher->matches($request));
  99. }
  100. public function testPathWithEncodedCharacters()
  101. {
  102. $matcher = new RequestMatcher();
  103. $request = Request::create('/admin/fo%20o');
  104. $matcher->matchPath('^/admin/fo o*$');
  105. $this->assertTrue($matcher->matches($request));
  106. }
  107. public function testAttributes()
  108. {
  109. $matcher = new RequestMatcher();
  110. $request = Request::create('/admin/foo');
  111. $request->attributes->set('foo', 'foo_bar');
  112. $matcher->matchAttribute('foo', 'foo_.*');
  113. $this->assertTrue($matcher->matches($request));
  114. $matcher->matchAttribute('foo', 'foo');
  115. $this->assertTrue($matcher->matches($request));
  116. $matcher->matchAttribute('foo', '^foo_bar$');
  117. $this->assertTrue($matcher->matches($request));
  118. $matcher->matchAttribute('foo', 'babar');
  119. $this->assertFalse($matcher->matches($request));
  120. }
  121. }