Update HeadHook

This commit is contained in:
PJ Dietz 2015-05-09 20:11:10 -04:00
parent 06f694154c
commit c1a104af4f
2 changed files with 10 additions and 11 deletions

View File

@ -12,7 +12,7 @@ use WellRESTed\Routing\MiddlewareInterface;
*/ */
class HeadHook implements MiddlewareInterface class HeadHook implements MiddlewareInterface
{ {
public function dispatch(ServerRequestInterface $request, ResponseInterface &$response) public function dispatch(ServerRequestInterface $request, ResponseInterface $response, $next)
{ {
$method = strtoupper($request->getMethod()); $method = strtoupper($request->getMethod());
if ($method === "HEAD") { if ($method === "HEAD") {
@ -20,5 +20,6 @@ class HeadHook implements MiddlewareInterface
$response = $response->withBody(new NullStream()); $response = $response->withBody(new NullStream());
} }
} }
return $next($request, $response);
} }
} }

View File

@ -13,6 +13,7 @@ class HeadHookTest extends \PHPUnit_Framework_TestCase
{ {
private $request; private $request;
private $response; private $response;
private $next;
private $body; private $body;
public function setUp() public function setUp()
@ -27,36 +28,33 @@ class HeadHookTest extends \PHPUnit_Framework_TestCase
$this->getBody()->willReturn($args[0]); $this->getBody()->willReturn($args[0]);
return $this; return $this;
}); });
$this->next = function ($request, $response) {
return $response;
};
} }
public function testReplacesBodyForHeadRequest() public function testReplacesBodyForHeadRequest()
{ {
$this->request->getMethod()->willReturn("HEAD"); $this->request->getMethod()->willReturn("HEAD");
$request = $this->request->reveal();
$response = $this->response->reveal();
$hook = new HeadHook(); $hook = new HeadHook();
$hook->dispatch($request, $response); $response = $hook->dispatch($this->request->reveal(), $this->response->reveal(), $this->next);
$this->assertSame(0, $response->getBody()->getSize()); $this->assertSame(0, $response->getBody()->getSize());
} }
public function testMultipleDispatchesHaveNoEffect() public function testMultipleDispatchesHaveNoEffect()
{ {
$this->request->getMethod()->willReturn("HEAD"); $this->request->getMethod()->willReturn("HEAD");
$request = $this->request->reveal();
$response = $this->response->reveal();
$hook = new HeadHook(); $hook = new HeadHook();
$hook->dispatch($request, $response); $response = $hook->dispatch($this->request->reveal(), $this->response->reveal(), $this->next);
$hook->dispatch($request, $response); $hook->dispatch($this->request->reveal(), $response, $this->next);
$this->response->withBody(Argument::any())->shouldHaveBeenCalledTimes(1); $this->response->withBody(Argument::any())->shouldHaveBeenCalledTimes(1);
} }
public function testDoesNotReplaceBodyForNonHeadRequests() public function testDoesNotReplaceBodyForNonHeadRequests()
{ {
$this->request->getMethod()->willReturn("GET"); $this->request->getMethod()->willReturn("GET");
$request = $this->request->reveal();
$response = $this->response->reveal();
$hook = new HeadHook(); $hook = new HeadHook();
$hook->dispatch($request, $response); $hook->dispatch($this->request->reveal(), $this->response->reveal(), $this->next);
$this->response->withBody(Argument::any())->shouldNotHaveBeenCalled(); $this->response->withBody(Argument::any())->shouldNotHaveBeenCalled();
} }
} }