From 1be4ff76910e4ae36d766993556994d4dfe6d661 Mon Sep 17 00:00:00 2001 From: PJ Dietz Date: Wed, 13 May 2015 21:52:55 -0400 Subject: [PATCH] Router uses only the request's path for routing --- src/Routing/Router.php | 3 ++- test/tests/unit/Routing/RouterTest.php | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/Routing/Router.php b/src/Routing/Router.php index 2743c76..3160709 100644 --- a/src/Routing/Router.php +++ b/src/Routing/Router.php @@ -77,7 +77,8 @@ class Router implements RouterInterface public function dispatch(ServerRequestInterface $request, ResponseInterface $response, $next) { - $requestTarget = $request->getRequestTarget(); + // Use only the path for routing. + $requestTarget = parse_url($request->getRequestTarget(), PHP_URL_PATH); $route = $this->getStaticRoute($requestTarget); if ($route) { diff --git a/test/tests/unit/Routing/RouterTest.php b/test/tests/unit/Routing/RouterTest.php index aea9d81..24ebab4 100644 --- a/test/tests/unit/Routing/RouterTest.php +++ b/test/tests/unit/Routing/RouterTest.php @@ -363,6 +363,25 @@ class RouterTest extends \PHPUnit_Framework_TestCase $this->request->withAttribute("pathVariables", $variables)->shouldHaveBeenCalled(); } + /** + * @covers ::dispatch + * @covers ::registerRouteForTarget + */ + public function testMatchesPathAgainstRouteWithoutQuery() + { + $target = "/my/path?cat=molly&dog=bear"; + + $this->request->getRequestTarget()->willReturn($target); + $this->route->getTarget()->willReturn($target); + $this->route->getType()->willReturn(RouteInterface::TYPE_PATTERN); + $this->route->matchesRequestTarget(Argument::cetera())->willReturn(true); + + $this->router->register("GET", $target, "middleware"); + $this->router->dispatch($this->request->reveal(), $this->response->reveal(), $this->next); + + $this->route->matchesRequestTarget("/my/path")->shouldHaveBeenCalled(); + } + // ------------------------------------------------------------------------ // No Matching Routes