diff --git a/src/Routing/ResponsePrep/HeadPrep.php b/src/Routing/ResponsePrep/HeadPrep.php new file mode 100644 index 0000000..c1a5001 --- /dev/null +++ b/src/Routing/ResponsePrep/HeadPrep.php @@ -0,0 +1,24 @@ +getMethod()); + if ($method === "HEAD") { + if ($response->getBody()->getSize() !== 0) { + $response = $response->withBody(new NullStream()); + } + } + } +} diff --git a/test/tests/unit/Routing/ResponsePrep/HeadPrepTest.php b/test/tests/unit/Routing/ResponsePrep/HeadPrepTest.php new file mode 100644 index 0000000..9946188 --- /dev/null +++ b/test/tests/unit/Routing/ResponsePrep/HeadPrepTest.php @@ -0,0 +1,51 @@ +body = $this->prophesize('Psr\Http\Message\StreamInterface'); + $this->body->getSize()->willReturn(1024); + $this->request = $this->prophesize('Psr\Http\Message\ServerRequestInterface'); + $this->response = $this->prophesize('Psr\Http\Message\ResponseInterface'); + $this->response->getBody()->willReturn($this->body->reveal()); + $this->response->withBody(Argument::any())->will(function ($args) { + $this->getBody()->willReturn($args[0]); + return $this; + }); + } + + public function testReplacesBodyForHeadRequest() + { + $this->request->getMethod()->willReturn("HEAD"); + $request = $this->request->reveal(); + $response = $this->response->reveal(); + $prep = new HeadPrep(); + $prep->dispatch($request, $response); + $this->assertSame(0, $response->getBody()->getSize()); + } + + public function testDoesNotReplaceBodyForNonHeadRequests() + { + $this->request->getMethod()->willReturn("GET"); + $request = $this->request->reveal(); + $response = $this->response->reveal(); + $prep = new HeadPrep(); + $prep->dispatch($request, $response); + $this->response->withBody(Argument::any())->shouldNotHaveBeenCalled(); + } +}