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 {
$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
* - 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.

View File

@ -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);
}
/**

View File

@ -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);
}
/**

View File

@ -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()