From ac2ed4a24ab687c19f5bcdeaaee62822d0b81133 Mon Sep 17 00:00:00 2001 From: PJ Dietz Date: Tue, 19 May 2015 19:12:12 -0400 Subject: [PATCH] Router stops propagating on 404, 405, and OPTIONS --- src/Routing/MethodMap.php | 2 +- src/Routing/MethodMapInterface.php | 2 +- src/Routing/Router.php | 5 +++-- test/tests/unit/Routing/MethodMapTest.php | 21 +++++++++++++++++++-- test/tests/unit/Routing/RouterTest.php | 4 ++-- 5 files changed, 26 insertions(+), 8 deletions(-) diff --git a/src/Routing/MethodMap.php b/src/Routing/MethodMap.php index 2791a23..03ea9ac 100644 --- a/src/Routing/MethodMap.php +++ b/src/Routing/MethodMap.php @@ -87,7 +87,7 @@ class MethodMap implements MethodMapInterface } else { $response = $response->withStatus(405); } - return $next($request, $this->addAllowHeader($response)); + return $this->addAllowHeader($response); } // ------------------------------------------------------------------------ diff --git a/src/Routing/MethodMapInterface.php b/src/Routing/MethodMapInterface.php index f68942e..3daef77 100644 --- a/src/Routing/MethodMapInterface.php +++ b/src/Routing/MethodMapInterface.php @@ -20,7 +20,7 @@ interface MethodMapInterface extends MiddlewareInterface * implementing MiddlewareInterface * - A callable that returns an instance implementing MiddleInterface * - A callable maching the signature of MiddlewareInteraface::dispatch - * @see DispatchedInterface::dispatch + * @see DispatcherInterface::dispatch * * $middleware may also be null, in which case any previously set * middleware for that method or methods will be unset. diff --git a/src/Routing/Router.php b/src/Routing/Router.php index ad316f1..36b8f09 100644 --- a/src/Routing/Router.php +++ b/src/Routing/Router.php @@ -99,8 +99,9 @@ class Router implements RouterInterface } } - // If no route exists, set the status code of the response to 404. - return $next($request, $response->withStatus(404)); + // If no route exists, set the status code of the response to 404 and + // return the response without propagating. + return $response->withStatus(404); } /** diff --git a/test/tests/unit/Routing/MethodMapTest.php b/test/tests/unit/Routing/MethodMapTest.php index fc6a44c..db5a422 100644 --- a/test/tests/unit/Routing/MethodMapTest.php +++ b/test/tests/unit/Routing/MethodMapTest.php @@ -180,6 +180,23 @@ class MethodMapTest extends \PHPUnit_Framework_TestCase $this->response->withStatus(200)->shouldHaveBeenCalled(); } + public function testStopsPropagatingAfterOptions() + { + $calledNext = false; + $next = function ($request, $response) use (&$calledNext) { + $calledNext = true; + return $response; + }; + + $this->request->getMethod()->willReturn("OPTIONS"); + + $map = new MethodMap($this->dispatcher->reveal()); + $map->register("GET", $this->middleware->reveal()); + $map($this->request->reveal(), $this->response->reveal(), $next); + + $this->assertFalse($calledNext); + } + /** * @covers ::__invoke * @covers ::addAllowHeader @@ -228,7 +245,7 @@ class MethodMapTest extends \PHPUnit_Framework_TestCase * @coversNothing * @dataProvider allowedMethodProvider */ - public function testCallsNextForBadMethod() + public function testStopsPropagatingAfterBadMethod() { $calledNext = false; $next = function ($request, $response) use (&$calledNext) { @@ -240,7 +257,7 @@ class MethodMapTest extends \PHPUnit_Framework_TestCase $map = new MethodMap($this->dispatcher->reveal()); $map->register("GET", $this->middleware->reveal()); $map($this->request->reveal(), $this->response->reveal(), $next); - $this->assertTrue($calledNext); + $this->assertFalse($calledNext); } /** diff --git a/test/tests/unit/Routing/RouterTest.php b/test/tests/unit/Routing/RouterTest.php index 9d2a56b..1e27718 100644 --- a/test/tests/unit/Routing/RouterTest.php +++ b/test/tests/unit/Routing/RouterTest.php @@ -403,7 +403,7 @@ class RouterTest extends \PHPUnit_Framework_TestCase * @covers ::getStaticRoute * @covers ::getPrefixRoute */ - public function testCallsNextWhenNoRouteMatches() + public function testStopsPropagatingWhenNoRouteMatches() { $calledNext = false; $next = function ($request, $response) use (&$calledNext) { @@ -414,7 +414,7 @@ class RouterTest extends \PHPUnit_Framework_TestCase $this->request->getRequestTarget()->willReturn("/no/match"); $this->response->withStatus(Argument::any())->willReturn($this->response->reveal()); $this->router->__invoke($this->request->reveal(), $this->response->reveal(), $next); - $this->assertTrue($calledNext); + $this->assertFalse($calledNext); } public function testRegisterIsFluid()