Add ContentLengthPrep

This commit is contained in:
PJ Dietz 2015-05-03 17:37:32 -04:00
parent 559044a82f
commit 147ddd0539
2 changed files with 111 additions and 0 deletions

View File

@ -0,0 +1,31 @@
<?php
namespace WellRESTed\Routing\ResponsePrep;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use WellRESTed\Routing\MiddlewareInterface;
/**
* Adds a Content-length header to the response when all of these are true:
*
* - Response does not have a Content-length header
* - Response does not have a Tranfser-encoding: chunked header
* - Response body stream reports a size
*/
class ContentLengthPrep implements MiddlewareInterface
{
public function dispatch(ServerRequestInterface $request, ResponseInterface &$response)
{
if ($response->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);
}
}
}

View File

@ -0,0 +1,80 @@
<?php
namespace WellRESTed\Test\Unit\Routing;
use Prophecy\Argument;
use WellRESTed\Routing\ResponsePrep\ContentLengthPrep;
/**
* @covers WellRESTed\Routing\ResponsePrep\ContentLengthPrep
*/
class ContentLengthPrepTest extends \PHPUnit_Framework_TestCase
{
private $request;
private $response;
private $body;
public function setUp()
{
parent::setUp();
$this->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();
}
}