RequestMatcherTest.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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\Request;
  13. use Symfony\Component\HttpFoundation\RequestMatcher;
  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 [
  32. ['get', 'get', true],
  33. ['get', ['get', 'post'], true],
  34. ['get', 'post', false],
  35. ['get', 'GET', true],
  36. ['get', ['GET', 'POST'], true],
  37. ['get', 'POST', false],
  38. ];
  39. }
  40. public function testScheme()
  41. {
  42. $httpRequest = $request = $request = Request::create('');
  43. $httpsRequest = $request = $request = Request::create('', 'get', [], [], [], ['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', [], [], [], ['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 testPort()
  68. {
  69. $matcher = new RequestMatcher();
  70. $request = Request::create('', 'get', [], [], [], ['HTTP_HOST' => null, 'SERVER_PORT' => 8000]);
  71. $matcher->matchPort(8000);
  72. $this->assertTrue($matcher->matches($request));
  73. $matcher->matchPort(9000);
  74. $this->assertFalse($matcher->matches($request));
  75. $matcher = new RequestMatcher(null, null, null, null, [], null, 8000);
  76. $this->assertTrue($matcher->matches($request));
  77. }
  78. public function getHostData()
  79. {
  80. return [
  81. ['.*\.example\.com', true],
  82. ['\.example\.com$', true],
  83. ['^.*\.example\.com$', true],
  84. ['.*\.sensio\.com', false],
  85. ['.*\.example\.COM', true],
  86. ['\.example\.COM$', true],
  87. ['^.*\.example\.COM$', true],
  88. ['.*\.sensio\.COM', false],
  89. ];
  90. }
  91. public function testPath()
  92. {
  93. $matcher = new RequestMatcher();
  94. $request = Request::create('/admin/foo');
  95. $matcher->matchPath('/admin/.*');
  96. $this->assertTrue($matcher->matches($request));
  97. $matcher->matchPath('/admin');
  98. $this->assertTrue($matcher->matches($request));
  99. $matcher->matchPath('^/admin/.*$');
  100. $this->assertTrue($matcher->matches($request));
  101. $matcher->matchMethod('/blog/.*');
  102. $this->assertFalse($matcher->matches($request));
  103. }
  104. public function testPathWithLocaleIsNotSupported()
  105. {
  106. $matcher = new RequestMatcher();
  107. $request = Request::create('/en/login');
  108. $request->setLocale('en');
  109. $matcher->matchPath('^/{_locale}/login$');
  110. $this->assertFalse($matcher->matches($request));
  111. }
  112. public function testPathWithEncodedCharacters()
  113. {
  114. $matcher = new RequestMatcher();
  115. $request = Request::create('/admin/fo%20o');
  116. $matcher->matchPath('^/admin/fo o*$');
  117. $this->assertTrue($matcher->matches($request));
  118. }
  119. public function testAttributes()
  120. {
  121. $matcher = new RequestMatcher();
  122. $request = Request::create('/admin/foo');
  123. $request->attributes->set('foo', 'foo_bar');
  124. $matcher->matchAttribute('foo', 'foo_.*');
  125. $this->assertTrue($matcher->matches($request));
  126. $matcher->matchAttribute('foo', 'foo');
  127. $this->assertTrue($matcher->matches($request));
  128. $matcher->matchAttribute('foo', '^foo_bar$');
  129. $this->assertTrue($matcher->matches($request));
  130. $matcher->matchAttribute('foo', 'babar');
  131. $this->assertFalse($matcher->matches($request));
  132. }
  133. }