Router stops propagating on 404, 405, and OPTIONS

This commit is contained in:
PJ Dietz 2015-05-19 19:12:12 -04:00
parent a825654336
commit ac2ed4a24a
5 changed files with 26 additions and 8 deletions

View File

@ -87,7 +87,7 @@ class MethodMap implements MethodMapInterface
} else { } else {
$response = $response->withStatus(405); $response = $response->withStatus(405);
} }
return $next($request, $this->addAllowHeader($response)); return $this->addAllowHeader($response);
} }
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------

View File

@ -20,7 +20,7 @@ interface MethodMapInterface extends MiddlewareInterface
* implementing MiddlewareInterface * implementing MiddlewareInterface
* - A callable that returns an instance implementing MiddleInterface * - A callable that returns an instance implementing MiddleInterface
* - A callable maching the signature of MiddlewareInteraface::dispatch * - 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 may also be null, in which case any previously set
* middleware for that method or methods will be unset. * middleware for that method or methods will be unset.

View File

@ -99,8 +99,9 @@ class Router implements RouterInterface
} }
} }
// If no route exists, set the status code of the response to 404. // If no route exists, set the status code of the response to 404 and
return $next($request, $response->withStatus(404)); // return the response without propagating.
return $response->withStatus(404);
} }
/** /**

View File

@ -180,6 +180,23 @@ class MethodMapTest extends \PHPUnit_Framework_TestCase
$this->response->withStatus(200)->shouldHaveBeenCalled(); $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 ::__invoke
* @covers ::addAllowHeader * @covers ::addAllowHeader
@ -228,7 +245,7 @@ class MethodMapTest extends \PHPUnit_Framework_TestCase
* @coversNothing * @coversNothing
* @dataProvider allowedMethodProvider * @dataProvider allowedMethodProvider
*/ */
public function testCallsNextForBadMethod() public function testStopsPropagatingAfterBadMethod()
{ {
$calledNext = false; $calledNext = false;
$next = function ($request, $response) use (&$calledNext) { $next = function ($request, $response) use (&$calledNext) {
@ -240,7 +257,7 @@ class MethodMapTest extends \PHPUnit_Framework_TestCase
$map = new MethodMap($this->dispatcher->reveal()); $map = new MethodMap($this->dispatcher->reveal());
$map->register("GET", $this->middleware->reveal()); $map->register("GET", $this->middleware->reveal());
$map($this->request->reveal(), $this->response->reveal(), $next); $map($this->request->reveal(), $this->response->reveal(), $next);
$this->assertTrue($calledNext); $this->assertFalse($calledNext);
} }
/** /**

View File

@ -403,7 +403,7 @@ class RouterTest extends \PHPUnit_Framework_TestCase
* @covers ::getStaticRoute * @covers ::getStaticRoute
* @covers ::getPrefixRoute * @covers ::getPrefixRoute
*/ */
public function testCallsNextWhenNoRouteMatches() public function testStopsPropagatingWhenNoRouteMatches()
{ {
$calledNext = false; $calledNext = false;
$next = function ($request, $response) use (&$calledNext) { $next = function ($request, $response) use (&$calledNext) {
@ -414,7 +414,7 @@ class RouterTest extends \PHPUnit_Framework_TestCase
$this->request->getRequestTarget()->willReturn("/no/match"); $this->request->getRequestTarget()->willReturn("/no/match");
$this->response->withStatus(Argument::any())->willReturn($this->response->reveal()); $this->response->withStatus(Argument::any())->willReturn($this->response->reveal());
$this->router->__invoke($this->request->reveal(), $this->response->reveal(), $next); $this->router->__invoke($this->request->reveal(), $this->response->reveal(), $next);
$this->assertTrue($calledNext); $this->assertFalse($calledNext);
} }
public function testRegisterIsFluid() public function testRegisterIsFluid()