diff --git a/src/Routing/ResponsePrep/ContentLengthPrep.php b/src/Routing/ResponsePrep/ContentLengthPrep.php new file mode 100644 index 0000000..863fd7a --- /dev/null +++ b/src/Routing/ResponsePrep/ContentLengthPrep.php @@ -0,0 +1,31 @@ +hasHeader("Content-length")) { + return; + } + if (strtolower($response->getHeaderLine("Transfer-encoding")) === "chunked") { + return; + } + $size = $response->getBody()->getSize(); + if ($size !== null) { + $response = $response->withHeader("Content-length", (string) $size); + } + } +} diff --git a/test/tests/unit/Routing/ResponsePrep/ContentLengthPrepTest.php b/test/tests/unit/Routing/ResponsePrep/ContentLengthPrepTest.php new file mode 100644 index 0000000..91a46c8 --- /dev/null +++ b/test/tests/unit/Routing/ResponsePrep/ContentLengthPrepTest.php @@ -0,0 +1,80 @@ +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->withHeader(Argument::cetera())->willReturn($this->response->reveal()); + } + + public function testAddContentLengthHeader() + { + $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); + + $this->response->withHeader("Content-length", 1024)->shouldHaveBeenCalled(); + } + + public function testDoesNotAddHeaderWhenContentLenghtIsAlreadySet() + { + $this->response->hasHeader("Content-length")->willReturn(true); + $this->response->getHeaderLine("Transfer-encoding")->willReturn(""); + + $request = $this->request->reveal(); + $response = $this->response->reveal(); + $prep = new ContentLengthPrep(); + $prep->dispatch($request, $response); + + $this->response->withHeader(Argument::cetera())->shouldNotHaveBeenCalled(); + } + + public function testDoesNotAddHeaderWhenTransferEncodingIsChunked() + { + $this->response->hasHeader("Content-length")->willReturn(false); + $this->response->getHeaderLine("Transfer-encoding")->willReturn("CHUNKED"); + + $request = $this->request->reveal(); + $response = $this->response->reveal(); + $prep = new ContentLengthPrep(); + $prep->dispatch($request, $response); + + $this->response->withHeader(Argument::cetera())->shouldNotHaveBeenCalled(); + } + + public function testDoesNotAddHeaderWhenBodySizeIsNull() + { + $this->response->hasHeader("Content-length")->willReturn(false); + $this->response->getHeaderLine("Transfer-encoding")->willReturn(""); + $this->body->getSize()->willReturn(null); + + $request = $this->request->reveal(); + $response = $this->response->reveal(); + $prep = new ContentLengthPrep(); + $prep->dispatch($request, $response); + + $this->response->withHeader(Argument::cetera())->shouldNotHaveBeenCalled(); + } +}