diff --git a/src/Routing/ResponsePrep/ContentLengthPrep.php b/src/Routing/Hook/ContentLengthHook.php similarity index 89% rename from src/Routing/ResponsePrep/ContentLengthPrep.php rename to src/Routing/Hook/ContentLengthHook.php index 863fd7a..fc9c554 100644 --- a/src/Routing/ResponsePrep/ContentLengthPrep.php +++ b/src/Routing/Hook/ContentLengthHook.php @@ -1,6 +1,6 @@ request = $this->prophesize('Psr\Http\Message\ServerRequestInterface'); $this->response = $this->prophesize('Psr\Http\Message\ResponseInterface'); $this->response->getBody()->willReturn($this->body->reveal()); - $this->response->withHeader(Argument::cetera())->willReturn($this->response->reveal()); + $this->response->withHeader(Argument::cetera())->will( + function () { + $this->hasHeader("Content-length")->willReturn(true); + return $this; + } + ); } - public function testAddContentLengthHeader() + public function testAddsContentLengthHeader() { $this->response->hasHeader("Content-length")->willReturn(false); $this->response->getHeaderLine("Transfer-encoding")->willReturn(""); $request = $this->request->reveal(); $response = $this->response->reveal(); - $prep = new ContentLengthPrep(); - $prep->dispatch($request, $response); + $hook = new ContentLengthHook(); + $hook->dispatch($request, $response); $this->response->withHeader("Content-length", 1024)->shouldHaveBeenCalled(); } + public function testMultipleDispatchesHaveNoEffect() + { + $this->response->hasHeader("Content-length")->willReturn(false); + $this->response->getHeaderLine("Transfer-encoding")->willReturn(""); + + $request = $this->request->reveal(); + $response = $this->response->reveal(); + $hook = new ContentLengthHook(); + $hook->dispatch($request, $response); + $hook->dispatch($request, $response); + + $this->response->withHeader("Content-length", 1024)->shouldHaveBeenCalledTimes(1); + } + public function testDoesNotAddHeaderWhenContentLenghtIsAlreadySet() { $this->response->hasHeader("Content-length")->willReturn(true); @@ -45,8 +64,8 @@ class ContentLengthPrepTest extends \PHPUnit_Framework_TestCase $request = $this->request->reveal(); $response = $this->response->reveal(); - $prep = new ContentLengthPrep(); - $prep->dispatch($request, $response); + $hook = new ContentLengthHook(); + $hook->dispatch($request, $response); $this->response->withHeader(Argument::cetera())->shouldNotHaveBeenCalled(); } @@ -58,8 +77,8 @@ class ContentLengthPrepTest extends \PHPUnit_Framework_TestCase $request = $this->request->reveal(); $response = $this->response->reveal(); - $prep = new ContentLengthPrep(); - $prep->dispatch($request, $response); + $hook = new ContentLengthHook(); + $hook->dispatch($request, $response); $this->response->withHeader(Argument::cetera())->shouldNotHaveBeenCalled(); } @@ -72,8 +91,8 @@ class ContentLengthPrepTest extends \PHPUnit_Framework_TestCase $request = $this->request->reveal(); $response = $this->response->reveal(); - $prep = new ContentLengthPrep(); - $prep->dispatch($request, $response); + $hook = new ContentLengthHook(); + $hook->dispatch($request, $response); $this->response->withHeader(Argument::cetera())->shouldNotHaveBeenCalled(); } diff --git a/test/tests/unit/Routing/ResponsePrep/HeadPrepTest.php b/test/tests/unit/Routing/Hook/HeadHookTest.php similarity index 63% rename from test/tests/unit/Routing/ResponsePrep/HeadPrepTest.php rename to test/tests/unit/Routing/Hook/HeadHookTest.php index 9946188..6506f74 100644 --- a/test/tests/unit/Routing/ResponsePrep/HeadPrepTest.php +++ b/test/tests/unit/Routing/Hook/HeadHookTest.php @@ -1,15 +1,15 @@ request->getMethod()->willReturn("HEAD"); $request = $this->request->reveal(); $response = $this->response->reveal(); - $prep = new HeadPrep(); - $prep->dispatch($request, $response); + $hook = new HeadHook(); + $hook->dispatch($request, $response); $this->assertSame(0, $response->getBody()->getSize()); } + public function testMultipleDispatchesHaveNoEffect() + { + $this->request->getMethod()->willReturn("HEAD"); + $request = $this->request->reveal(); + $response = $this->response->reveal(); + $hook = new HeadHook(); + $hook->dispatch($request, $response); + $hook->dispatch($request, $response); + $this->response->withBody(Argument::any())->shouldHaveBeenCalledTimes(1); + } + public function testDoesNotReplaceBodyForNonHeadRequests() { $this->request->getMethod()->willReturn("GET"); $request = $this->request->reveal(); $response = $this->response->reveal(); - $prep = new HeadPrep(); - $prep->dispatch($request, $response); + $hook = new HeadHook(); + $hook->dispatch($request, $response); $this->response->withBody(Argument::any())->shouldNotHaveBeenCalled(); } }