Update MethodMap to match new MiddlewareInterface
This commit is contained in:
parent
a0e4ace6a5
commit
d8352e71d9
|
|
@ -53,26 +53,29 @@ class MethodMap implements MiddlewareInterface, MethodMapInterface
|
||||||
// ------------------------------------------------------------------------
|
// ------------------------------------------------------------------------
|
||||||
// MiddlewareInterface
|
// MiddlewareInterface
|
||||||
|
|
||||||
public function dispatch(ServerRequestInterface $request, ResponseInterface &$response)
|
/**
|
||||||
|
* @param ServerRequestInterface $request
|
||||||
|
* @param ResponseInterface $response
|
||||||
|
* @param callable $next
|
||||||
|
* @return ResponseInterface
|
||||||
|
*/
|
||||||
|
public function dispatch(ServerRequestInterface $request, ResponseInterface $response, $next)
|
||||||
{
|
{
|
||||||
$method = $request->getMethod();
|
$method = $request->getMethod();
|
||||||
// Dispatch middleware registered with the explicitly matching method.
|
// Dispatch middleware registered with the explicitly matching method.
|
||||||
if (isset($this->map[$method])) {
|
if (isset($this->map[$method])) {
|
||||||
$middleware = $this->map[$method];
|
$middleware = $this->map[$method];
|
||||||
$this->dispatchMiddleware($middleware, $request, $response);
|
return $this->dispatchMiddleware($middleware, $request, $response, $next);
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
// For HEAD, dispatch GET by default.
|
// For HEAD, dispatch GET by default.
|
||||||
if ($method === "HEAD" && isset($this->map["GET"])) {
|
if ($method === "HEAD" && isset($this->map["GET"])) {
|
||||||
$middleware = $this->map["GET"];
|
$middleware = $this->map["GET"];
|
||||||
$this->dispatchMiddleware($middleware, $request, $response);
|
return $this->dispatchMiddleware($middleware, $request, $response, $next);
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
// Dispatch * middleware, if registered.
|
// Dispatch * middleware, if registered.
|
||||||
if (isset($this->map["*"])) {
|
if (isset($this->map["*"])) {
|
||||||
$middleware = $this->map["*"];
|
$middleware = $this->map["*"];
|
||||||
$this->dispatchMiddleware($middleware, $request, $response);
|
return $this->dispatchMiddleware($middleware, $request, $response, $next);
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
// Respond describing the allowed methods, either as a 405 response or
|
// Respond describing the allowed methods, either as a 405 response or
|
||||||
// in response to an OPTIONS request.
|
// in response to an OPTIONS request.
|
||||||
|
|
@ -81,15 +84,15 @@ class MethodMap implements MiddlewareInterface, MethodMapInterface
|
||||||
} else {
|
} else {
|
||||||
$response = $response->withStatus(405);
|
$response = $response->withStatus(405);
|
||||||
}
|
}
|
||||||
$this->addAllowHeader($response);
|
return $this->addAllowHeader($response);
|
||||||
}
|
}
|
||||||
|
|
||||||
// ------------------------------------------------------------------------
|
// ------------------------------------------------------------------------
|
||||||
|
|
||||||
protected function addAllowHeader(ResponseInterface &$response)
|
protected function addAllowHeader(ResponseInterface $response)
|
||||||
{
|
{
|
||||||
$methods = join(",", $this->getAllowedMethods());
|
$methods = join(",", $this->getAllowedMethods());
|
||||||
$response = $response->withHeader("Allow", $methods);
|
return $response->withHeader("Allow", $methods);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function getAllowedMethods()
|
protected function getAllowedMethods()
|
||||||
|
|
@ -117,9 +120,16 @@ class MethodMap implements MiddlewareInterface, MethodMapInterface
|
||||||
return new Dispatcher();
|
return new Dispatcher();
|
||||||
}
|
}
|
||||||
|
|
||||||
private function dispatchMiddleware($middleware, ServerRequestInterface $request, ResponseInterface &$response)
|
/**
|
||||||
|
* @param $middleware
|
||||||
|
* @param ServerRequestInterface $request
|
||||||
|
* @param ResponseInterface $response
|
||||||
|
* @param $next
|
||||||
|
* @return ResponseInterface
|
||||||
|
*/
|
||||||
|
private function dispatchMiddleware($middleware, ServerRequestInterface $request, ResponseInterface &$response, $next)
|
||||||
{
|
{
|
||||||
$dispatcher = $this->getDispatcher();
|
$dispatcher = $this->getDispatcher();
|
||||||
$dispatcher->dispatch($middleware, $request, $response);
|
return $dispatcher->dispatch($middleware, $request, $response, $next);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -14,13 +14,20 @@ class MethodMapTest extends \PHPUnit_Framework_TestCase
|
||||||
{
|
{
|
||||||
private $request;
|
private $request;
|
||||||
private $response;
|
private $response;
|
||||||
|
private $next;
|
||||||
|
private $middleware;
|
||||||
|
|
||||||
public function setUp()
|
public function setUp()
|
||||||
{
|
{
|
||||||
$this->request = $this->prophesize("\\Psr\\Http\\Message\\ServerRequestInterface");
|
$this->request = $this->prophesize('Psr\Http\Message\ServerRequestInterface');
|
||||||
$this->response = $this->prophesize("\\Psr\\Http\\Message\\ResponseInterface");
|
$this->response = $this->prophesize('Psr\Http\Message\ResponseInterface');
|
||||||
$this->response->withStatus(Argument::any())->willReturn($this->response->reveal());
|
$this->response->withStatus(Argument::any())->willReturn($this->response->reveal());
|
||||||
$this->response->withHeader(Argument::cetera())->willReturn($this->response->reveal());
|
$this->response->withHeader(Argument::cetera())->willReturn($this->response->reveal());
|
||||||
|
$this->next = function ($request, $response) {
|
||||||
|
return $response;
|
||||||
|
};
|
||||||
|
$this->middleware = $this->prophesize('WellRESTed\Routing\MiddlewareInterface');
|
||||||
|
$this->middleware->dispatch(Argument::cetera())->willReturn();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -42,17 +49,11 @@ class MethodMapTest extends \PHPUnit_Framework_TestCase
|
||||||
{
|
{
|
||||||
$this->request->getMethod()->willReturn("GET");
|
$this->request->getMethod()->willReturn("GET");
|
||||||
|
|
||||||
$middleware = $this->prophesize('WellRESTed\Routing\MiddlewareInterface');
|
|
||||||
$middleware->dispatch(Argument::cetera())->willReturn();
|
|
||||||
|
|
||||||
$map = new MethodMap();
|
$map = new MethodMap();
|
||||||
$map->setMethod("GET", $middleware->reveal());
|
$map->setMethod("GET", $this->middleware->reveal());
|
||||||
|
$map->dispatch($this->request->reveal(), $this->response->reveal(), $this->next);
|
||||||
|
|
||||||
$request = $this->request->reveal();
|
$this->middleware->dispatch($this->request->reveal(), $this->response->reveal(), $this->next)->shouldHaveBeenCalled();
|
||||||
$response = $this->response->reveal();
|
|
||||||
$map->dispatch($request, $response);
|
|
||||||
|
|
||||||
$middleware->dispatch($request, $response)->shouldHaveBeenCalled();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -71,12 +72,9 @@ class MethodMapTest extends \PHPUnit_Framework_TestCase
|
||||||
$map = new MethodMap();
|
$map = new MethodMap();
|
||||||
$map->setMethod("GET", $middlewareUpper->reveal());
|
$map->setMethod("GET", $middlewareUpper->reveal());
|
||||||
$map->setMethod("get", $middlewareLower->reveal());
|
$map->setMethod("get", $middlewareLower->reveal());
|
||||||
|
$map->dispatch($this->request->reveal(), $this->response->reveal(), $this->next);
|
||||||
|
|
||||||
$request = $this->request->reveal();
|
$middlewareLower->dispatch($this->request->reveal(), $this->response->reveal(), $this->next)->shouldHaveBeenCalled();
|
||||||
$response = $this->response->reveal();
|
|
||||||
$map->dispatch($request, $response);
|
|
||||||
|
|
||||||
$middlewareLower->dispatch($request, $response)->shouldHaveBeenCalled();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -89,17 +87,11 @@ class MethodMapTest extends \PHPUnit_Framework_TestCase
|
||||||
{
|
{
|
||||||
$this->request->getMethod()->willReturn("GET");
|
$this->request->getMethod()->willReturn("GET");
|
||||||
|
|
||||||
$middleware = $this->prophesize('WellRESTed\Routing\MiddlewareInterface');
|
|
||||||
$middleware->dispatch(Argument::cetera())->willReturn();
|
|
||||||
|
|
||||||
$map = new MethodMap();
|
$map = new MethodMap();
|
||||||
$map->setMethod("*", $middleware->reveal());
|
$map->setMethod("*", $this->middleware->reveal());
|
||||||
|
$map->dispatch($this->request->reveal(), $this->response->reveal(), $this->next);
|
||||||
|
|
||||||
$request = $this->request->reveal();
|
$this->middleware->dispatch($this->request->reveal(), $this->response->reveal(), $this->next)->shouldHaveBeenCalled();
|
||||||
$response = $this->response->reveal();
|
|
||||||
$map->dispatch($request, $response);
|
|
||||||
|
|
||||||
$middleware->dispatch($request, $response)->shouldHaveBeenCalled();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -109,17 +101,11 @@ class MethodMapTest extends \PHPUnit_Framework_TestCase
|
||||||
{
|
{
|
||||||
$this->request->getMethod()->willReturn("HEAD");
|
$this->request->getMethod()->willReturn("HEAD");
|
||||||
|
|
||||||
$middleware = $this->prophesize('WellRESTed\Routing\MiddlewareInterface');
|
|
||||||
$middleware->dispatch(Argument::cetera())->willReturn();
|
|
||||||
|
|
||||||
$map = new MethodMap();
|
$map = new MethodMap();
|
||||||
$map->setMethod("GET", $middleware->reveal());
|
$map->setMethod("GET", $this->middleware->reveal());
|
||||||
|
$map->dispatch($this->request->reveal(), $this->response->reveal(), $this->next);
|
||||||
|
|
||||||
$request = $this->request->reveal();
|
$this->middleware->dispatch($this->request->reveal(), $this->response->reveal(), $this->next)->shouldHaveBeenCalled();
|
||||||
$response = $this->response->reveal();
|
|
||||||
$map->dispatch($request, $response);
|
|
||||||
|
|
||||||
$middleware->dispatch($request, $response)->shouldHaveBeenCalled();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
@ -127,37 +113,26 @@ class MethodMapTest extends \PHPUnit_Framework_TestCase
|
||||||
*/
|
*/
|
||||||
public function testRegistersMiddlewareForMultipleMethods()
|
public function testRegistersMiddlewareForMultipleMethods()
|
||||||
{
|
{
|
||||||
$middleware = $this->prophesize('WellRESTed\Routing\MiddlewareInterface');
|
|
||||||
$middleware->dispatch(Argument::cetera())->willReturn();
|
|
||||||
|
|
||||||
$map = new MethodMap();
|
$map = new MethodMap();
|
||||||
$map->setMethod("GET,POST", $middleware->reveal());
|
$map->setMethod("GET,POST", $this->middleware->reveal());
|
||||||
|
|
||||||
$request = $this->request->reveal();
|
|
||||||
$response = $this->response->reveal();
|
|
||||||
|
|
||||||
$this->request->getMethod()->willReturn("GET");
|
$this->request->getMethod()->willReturn("GET");
|
||||||
$map->dispatch($request, $response);
|
$map->dispatch($this->request->reveal(), $this->response->reveal(), $this->next);
|
||||||
|
|
||||||
$this->request->getMethod()->willReturn("POST");
|
$this->request->getMethod()->willReturn("POST");
|
||||||
$map->dispatch($request, $response);
|
$map->dispatch($this->request->reveal(), $this->response->reveal(), $this->next);
|
||||||
|
|
||||||
$middleware->dispatch($request, $response)->shouldHaveBeenCalledTimes(2);
|
$this->middleware->dispatch($this->request->reveal(), $this->response->reveal(), $this->next)->shouldHaveBeenCalledTimes(2);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testSettingNullUnregistersMiddleware()
|
public function testSettingNullUnregistersMiddleware()
|
||||||
{
|
{
|
||||||
$this->request->getMethod()->willReturn("POST");
|
$this->request->getMethod()->willReturn("POST");
|
||||||
|
|
||||||
$middleware = $this->prophesize('WellRESTed\Routing\MiddlewareInterface');
|
|
||||||
|
|
||||||
$map = new MethodMap();
|
$map = new MethodMap();
|
||||||
$map->setMethod("POST", $middleware->reveal());
|
$map->setMethod("POST", $this->middleware->reveal());
|
||||||
$map->setMethod("POST", null);
|
$map->setMethod("POST", null);
|
||||||
|
$map->dispatch($this->request->reveal(), $this->response->reveal(), $this->next);
|
||||||
$request = $this->request->reveal();
|
|
||||||
$response = $this->response->reveal();
|
|
||||||
$map->dispatch($request, $response);
|
|
||||||
|
|
||||||
$this->response->withStatus(405)->shouldHaveBeenCalled();
|
$this->response->withStatus(405)->shouldHaveBeenCalled();
|
||||||
}
|
}
|
||||||
|
|
@ -171,14 +146,9 @@ class MethodMapTest extends \PHPUnit_Framework_TestCase
|
||||||
{
|
{
|
||||||
$this->request->getMethod()->willReturn("OPTIONS");
|
$this->request->getMethod()->willReturn("OPTIONS");
|
||||||
|
|
||||||
$middleware = $this->prophesize('WellRESTed\Routing\MiddlewareInterface');
|
|
||||||
|
|
||||||
$map = new MethodMap();
|
$map = new MethodMap();
|
||||||
$map->setMethod("GET", $middleware->reveal());
|
$map->setMethod("GET", $this->middleware->reveal());
|
||||||
|
$map->dispatch($this->request->reveal(), $this->response->reveal(), $this->next);
|
||||||
$request = $this->request->reveal();
|
|
||||||
$response = $this->response->reveal();
|
|
||||||
$map->dispatch($request, $response);
|
|
||||||
|
|
||||||
$this->response->withStatus(200)->shouldHaveBeenCalled();
|
$this->response->withStatus(200)->shouldHaveBeenCalled();
|
||||||
}
|
}
|
||||||
|
|
@ -193,16 +163,11 @@ class MethodMapTest extends \PHPUnit_Framework_TestCase
|
||||||
{
|
{
|
||||||
$this->request->getMethod()->willReturn("OPTIONS");
|
$this->request->getMethod()->willReturn("OPTIONS");
|
||||||
|
|
||||||
$middleware = $this->prophesize('WellRESTed\Routing\MiddlewareInterface');
|
|
||||||
|
|
||||||
$map = new MethodMap();
|
$map = new MethodMap();
|
||||||
foreach ($methodsDeclared as $method) {
|
foreach ($methodsDeclared as $method) {
|
||||||
$map->setMethod($method, $middleware->reveal());
|
$map->setMethod($method, $this->middleware->reveal());
|
||||||
}
|
}
|
||||||
|
$map->dispatch($this->request->reveal(), $this->response->reveal(), $this->next);
|
||||||
$request = $this->request->reveal();
|
|
||||||
$response = $this->response->reveal();
|
|
||||||
$map->dispatch($request, $response);
|
|
||||||
|
|
||||||
$containsAllMethods = function ($headerValue) use ($methodsAllowed) {
|
$containsAllMethods = function ($headerValue) use ($methodsAllowed) {
|
||||||
foreach ($methodsAllowed as $method) {
|
foreach ($methodsAllowed as $method) {
|
||||||
|
|
@ -225,14 +190,9 @@ class MethodMapTest extends \PHPUnit_Framework_TestCase
|
||||||
{
|
{
|
||||||
$this->request->getMethod()->willReturn("POST");
|
$this->request->getMethod()->willReturn("POST");
|
||||||
|
|
||||||
$middleware = $this->prophesize('WellRESTed\Routing\MiddlewareInterface');
|
|
||||||
|
|
||||||
$map = new MethodMap();
|
$map = new MethodMap();
|
||||||
$map->setMethod("GET", $middleware->reveal());
|
$map->setMethod("GET", $this->middleware->reveal());
|
||||||
|
$map->dispatch($this->request->reveal(), $this->response->reveal(), $this->next);
|
||||||
$request = $this->request->reveal();
|
|
||||||
$response = $this->response->reveal();
|
|
||||||
$map->dispatch($request, $response);
|
|
||||||
|
|
||||||
$this->response->withStatus(405)->shouldHaveBeenCalled();
|
$this->response->withStatus(405)->shouldHaveBeenCalled();
|
||||||
}
|
}
|
||||||
|
|
@ -247,16 +207,11 @@ class MethodMapTest extends \PHPUnit_Framework_TestCase
|
||||||
{
|
{
|
||||||
$this->request->getMethod()->willReturn("BAD");
|
$this->request->getMethod()->willReturn("BAD");
|
||||||
|
|
||||||
$middleware = $this->prophesize('WellRESTed\Routing\MiddlewareInterface');
|
|
||||||
|
|
||||||
$map = new MethodMap();
|
$map = new MethodMap();
|
||||||
foreach ($methodsDeclared as $method) {
|
foreach ($methodsDeclared as $method) {
|
||||||
$map->setMethod($method, $middleware->reveal());
|
$map->setMethod($method, $this->middleware->reveal());
|
||||||
}
|
}
|
||||||
|
$map->dispatch($this->request->reveal(), $this->response->reveal(), $this->next);
|
||||||
$request = $this->request->reveal();
|
|
||||||
$response = $this->response->reveal();
|
|
||||||
$map->dispatch($request, $response);
|
|
||||||
|
|
||||||
$containsAllMethods = function ($headerValue) use ($methodsAllowed) {
|
$containsAllMethods = function ($headerValue) use ($methodsAllowed) {
|
||||||
foreach ($methodsAllowed as $method) {
|
foreach ($methodsAllowed as $method) {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue