RequestMatcherTest.php 4.7 KB

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