diff --git a/src/main/php/web/Routing.class.php b/src/main/php/web/Routing.class.php index fa1f7cbd..1bde5e36 100755 --- a/src/main/php/web/Routing.class.php +++ b/src/main/php/web/Routing.class.php @@ -60,13 +60,15 @@ public function routes() { return $this->routes; } * @return self */ public function matching($definitions, $target) { + static $quote= ['#' => '\\#']; + $handler= $target instanceof Handler ? $target : new Call($target); foreach ((array)$definitions as $definition) { if ('/' === $definition[0]) { - $this->routes['#^[A-Z]+ '.preg_quote(rtrim($definition, '/'), '#').'/#']= $handler; + $this->routes['#^[A-Z]+ '.strtr(rtrim($definition, '/'), $quote).'/#']= $handler; } else { sscanf($definition, "%[A-Z|] %[^\r]", $methods, $path); - $this->routes['#^'.$methods.' '.(null === $path ? '' : preg_quote(rtrim($path, '/'), '#')).'/#']= $handler; + $this->routes['#^'.$methods.' '.(null === $path ? '' : strtr(rtrim($path, '/'), $quote)).'/#']= $handler; } } return $this; diff --git a/src/test/php/web/unittest/RoutingTest.class.php b/src/test/php/web/unittest/RoutingTest.class.php index 918b1053..3215b8d5 100755 --- a/src/test/php/web/unittest/RoutingTest.class.php +++ b/src/test/php/web/unittest/RoutingTest.class.php @@ -131,6 +131,15 @@ public function matching_paths($url, $expected) { ); } + #[Test, Values([['/test', 'specific'], ['/test.html', 'specific'], ['/', 'default']])] + public function matching_pattern($url, $expected) { + Assert::equals($this->handlers[$expected], (new Routing()) + ->matching('/test(.html)?', $this->handlers['specific']) + ->fallbacks($this->handlers['default']) + ->route(new Request(new TestInput('GET', $url))) + ); + } + #[Test, Values(['/api', '//api', '///api', '/test/../api', '/./api', '/../api', '/./../api',])] public function request_canonicalized_before_matching($requested) { Assert::equals($this->handlers['specific'], Routing::cast(['/api' => $this->handlers['specific']])