From 73269cfd0ebc61dfa49b1c8654bfa270f29766bb Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Wed, 4 Apr 2018 15:19:51 +0200 Subject: [PATCH] [Routing] Fix throwing NoConfigurationException instead of 405 --- .../Routing/Matcher/Dumper/PhpMatcherDumper.php | 4 ++-- .../Component/Routing/Matcher/UrlMatcher.php | 2 +- .../Tests/Fixtures/dumper/url_matcher0.php | 2 +- .../Tests/Fixtures/dumper/url_matcher1.php | 2 +- .../Tests/Fixtures/dumper/url_matcher2.php | 2 +- .../Tests/Fixtures/dumper/url_matcher3.php | 2 +- .../Tests/Fixtures/dumper/url_matcher4.php | 2 +- .../Tests/Fixtures/dumper/url_matcher5.php | 2 +- .../Tests/Fixtures/dumper/url_matcher6.php | 2 +- .../Tests/Fixtures/dumper/url_matcher7.php | 2 +- .../Routing/Tests/Matcher/UrlMatcherTest.php | 15 +++++++++++++++ 11 files changed, 26 insertions(+), 11 deletions(-) diff --git a/src/Symfony/Component/Routing/Matcher/Dumper/PhpMatcherDumper.php b/src/Symfony/Component/Routing/Matcher/Dumper/PhpMatcherDumper.php index d3c931dd3f..40d8df67ed 100644 --- a/src/Symfony/Component/Routing/Matcher/Dumper/PhpMatcherDumper.php +++ b/src/Symfony/Component/Routing/Matcher/Dumper/PhpMatcherDumper.php @@ -154,7 +154,7 @@ EOF; } // used to display the Welcome Page in apps that don't define a homepage - $code .= " if ('/' === \$pathinfo) {\n"; + $code .= " if ('/' === \$pathinfo && !\$allow) {\n"; $code .= " throw new Symfony\Component\Routing\Exception\NoConfigurationException();\n"; $code .= " }\n"; @@ -362,7 +362,7 @@ EOF; EOF; } } elseif ($methods) { - $code .= <<allow) { throw new NoConfigurationException(); } diff --git a/src/Symfony/Component/Routing/Tests/Fixtures/dumper/url_matcher0.php b/src/Symfony/Component/Routing/Tests/Fixtures/dumper/url_matcher0.php index 59253f0749..9e9b9103bf 100644 --- a/src/Symfony/Component/Routing/Tests/Fixtures/dumper/url_matcher0.php +++ b/src/Symfony/Component/Routing/Tests/Fixtures/dumper/url_matcher0.php @@ -28,7 +28,7 @@ class ProjectUrlMatcher extends Symfony\Component\Routing\Matcher\UrlMatcher $canonicalMethod = 'GET'; } - if ('/' === $pathinfo) { + if ('/' === $pathinfo && !$allow) { throw new Symfony\Component\Routing\Exception\NoConfigurationException(); } diff --git a/src/Symfony/Component/Routing/Tests/Fixtures/dumper/url_matcher1.php b/src/Symfony/Component/Routing/Tests/Fixtures/dumper/url_matcher1.php index 4ea0757e71..23a93c1934 100644 --- a/src/Symfony/Component/Routing/Tests/Fixtures/dumper/url_matcher1.php +++ b/src/Symfony/Component/Routing/Tests/Fixtures/dumper/url_matcher1.php @@ -309,7 +309,7 @@ class ProjectUrlMatcher extends Symfony\Component\Routing\Matcher\UrlMatcher } - if ('/' === $pathinfo) { + if ('/' === $pathinfo && !$allow) { throw new Symfony\Component\Routing\Exception\NoConfigurationException(); } diff --git a/src/Symfony/Component/Routing/Tests/Fixtures/dumper/url_matcher2.php b/src/Symfony/Component/Routing/Tests/Fixtures/dumper/url_matcher2.php index 8d1c27d7c1..e430adb1fb 100644 --- a/src/Symfony/Component/Routing/Tests/Fixtures/dumper/url_matcher2.php +++ b/src/Symfony/Component/Routing/Tests/Fixtures/dumper/url_matcher2.php @@ -371,7 +371,7 @@ class ProjectUrlMatcher extends Symfony\Component\Routing\Tests\Fixtures\Redirec } not_nonsecure: - if ('/' === $pathinfo) { + if ('/' === $pathinfo && !$allow) { throw new Symfony\Component\Routing\Exception\NoConfigurationException(); } diff --git a/src/Symfony/Component/Routing/Tests/Fixtures/dumper/url_matcher3.php b/src/Symfony/Component/Routing/Tests/Fixtures/dumper/url_matcher3.php index 375b56cafa..67c4667467 100644 --- a/src/Symfony/Component/Routing/Tests/Fixtures/dumper/url_matcher3.php +++ b/src/Symfony/Component/Routing/Tests/Fixtures/dumper/url_matcher3.php @@ -46,7 +46,7 @@ class ProjectUrlMatcher extends Symfony\Component\Routing\Matcher\UrlMatcher return array('_route' => 'with-condition'); } - if ('/' === $pathinfo) { + if ('/' === $pathinfo && !$allow) { throw new Symfony\Component\Routing\Exception\NoConfigurationException(); } diff --git a/src/Symfony/Component/Routing/Tests/Fixtures/dumper/url_matcher4.php b/src/Symfony/Component/Routing/Tests/Fixtures/dumper/url_matcher4.php index c3ca977544..ed07194a26 100644 --- a/src/Symfony/Component/Routing/Tests/Fixtures/dumper/url_matcher4.php +++ b/src/Symfony/Component/Routing/Tests/Fixtures/dumper/url_matcher4.php @@ -103,7 +103,7 @@ class ProjectUrlMatcher extends Symfony\Component\Routing\Matcher\UrlMatcher } - if ('/' === $pathinfo) { + if ('/' === $pathinfo && !$allow) { throw new Symfony\Component\Routing\Exception\NoConfigurationException(); } diff --git a/src/Symfony/Component/Routing/Tests/Fixtures/dumper/url_matcher5.php b/src/Symfony/Component/Routing/Tests/Fixtures/dumper/url_matcher5.php index 3a6faea20b..2b22513a61 100644 --- a/src/Symfony/Component/Routing/Tests/Fixtures/dumper/url_matcher5.php +++ b/src/Symfony/Component/Routing/Tests/Fixtures/dumper/url_matcher5.php @@ -200,7 +200,7 @@ class ProjectUrlMatcher extends Symfony\Component\Routing\Tests\Fixtures\Redirec } - if ('/' === $pathinfo) { + if ('/' === $pathinfo && !$allow) { throw new Symfony\Component\Routing\Exception\NoConfigurationException(); } diff --git a/src/Symfony/Component/Routing/Tests/Fixtures/dumper/url_matcher6.php b/src/Symfony/Component/Routing/Tests/Fixtures/dumper/url_matcher6.php index 415c328d33..48ecdf81c0 100644 --- a/src/Symfony/Component/Routing/Tests/Fixtures/dumper/url_matcher6.php +++ b/src/Symfony/Component/Routing/Tests/Fixtures/dumper/url_matcher6.php @@ -204,7 +204,7 @@ class ProjectUrlMatcher extends Symfony\Component\Routing\Matcher\UrlMatcher } - if ('/' === $pathinfo) { + if ('/' === $pathinfo && !$allow) { throw new Symfony\Component\Routing\Exception\NoConfigurationException(); } diff --git a/src/Symfony/Component/Routing/Tests/Fixtures/dumper/url_matcher7.php b/src/Symfony/Component/Routing/Tests/Fixtures/dumper/url_matcher7.php index bbf6cf6e0b..81d76ea4a0 100644 --- a/src/Symfony/Component/Routing/Tests/Fixtures/dumper/url_matcher7.php +++ b/src/Symfony/Component/Routing/Tests/Fixtures/dumper/url_matcher7.php @@ -240,7 +240,7 @@ class ProjectUrlMatcher extends Symfony\Component\Routing\Tests\Fixtures\Redirec } - if ('/' === $pathinfo) { + if ('/' === $pathinfo && !$allow) { throw new Symfony\Component\Routing\Exception\NoConfigurationException(); } diff --git a/src/Symfony/Component/Routing/Tests/Matcher/UrlMatcherTest.php b/src/Symfony/Component/Routing/Tests/Matcher/UrlMatcherTest.php index 1bd75fa339..e8d31e2225 100644 --- a/src/Symfony/Component/Routing/Tests/Matcher/UrlMatcherTest.php +++ b/src/Symfony/Component/Routing/Tests/Matcher/UrlMatcherTest.php @@ -45,6 +45,21 @@ class UrlMatcherTest extends TestCase } } + public function testMethodNotAllowedOnRoot() + { + $coll = new RouteCollection(); + $coll->add('foo', new Route('/', array(), array(), array(), '', array(), array('GET'))); + + $matcher = $this->getUrlMatcher($coll, new RequestContext('', 'POST')); + + try { + $matcher->match('/'); + $this->fail(); + } catch (MethodNotAllowedException $e) { + $this->assertEquals(array('GET'), $e->getAllowedMethods()); + } + } + public function testHeadAllowedWhenRequirementContainsGet() { $coll = new RouteCollection();