Transmitter provides Content-length header without external class. Transmitter no longer alters the body for HEAD requests.

This commit is contained in:
PJ Dietz 2015-05-14 07:51:28 -04:00
parent 1be4ff7691
commit 3f5e2321d9
7 changed files with 62 additions and 239 deletions

View File

@ -1,32 +0,0 @@
<?php
namespace WellRESTed\Transmission\Middleware;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use WellRESTed\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 ContentLengthHandler implements MiddlewareInterface
{
public function dispatch(ServerRequestInterface $request, ResponseInterface $response, $next)
{
if ($response->hasHeader("Content-length")) {
return $next($request, $response);
}
if (strtolower($response->getHeaderLine("Transfer-encoding")) === "chunked") {
return $next($request, $response);
}
$size = $response->getBody()->getSize();
if ($size !== null) {
$response = $response->withHeader("Content-length", (string) $size);
}
return $next($request, $response);
}
}

View File

@ -1,25 +0,0 @@
<?php
namespace WellRESTed\Transmission\Middleware;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use WellRESTed\Message\NullStream;
use WellRESTed\MiddlewareInterface;
/**
* Removes the body of a response to a HEAD request.
*/
class HeadHandler implements MiddlewareInterface
{
public function dispatch(ServerRequestInterface $request, ResponseInterface $response, $next)
{
$method = strtoupper($request->getMethod());
if ($method === "HEAD") {
if ($response->getBody()->getSize() !== 0) {
$response = $response->withBody(new NullStream());
}
}
return $next($request, $response);
}
}

View File

@ -7,8 +7,6 @@ use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\StreamInterface;
use WellRESTed\Dispatching\Dispatcher;
use WellRESTed\Dispatching\DispatcherInterface;
use WellRESTed\Transmission\Middleware\ContentLengthHandler;
use WellRESTed\Transmission\Middleware\HeadHandler;
class Transmitter implements TransmitterInterface
{
@ -31,8 +29,10 @@ class Transmitter implements TransmitterInterface
*
* This method outputs the status line, headers, and body to the client.
*
* This method will also provide a Content-length header if needed and
* supress the body for HEAD requests.
* This method will also provide a Content-length header if:
* - Response does not have a Content-length header
* - Response does not have a Tranfser-encoding: chunked header
* - Response body stream is readable and reports a non-null size
*
* @param ServerRequestInterface $request
* @param ResponseInterface $response Response to output
@ -69,17 +69,21 @@ class Transmitter implements TransmitterInterface
protected function prepareResponse(ServerRequestInterface $request, ResponseInterface $response)
{
return $this->dispatcher->dispatch(
[
new ContentLengthHandler(),
new HeadHandler()
],
$request,
$response,
function ($request, $response) {
return $response;
// Add 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 is readable and reports a non-null size
//
if (!$response->hasHeader("Content-length")
&& !(strtolower($response->getHeaderLine("Transfer-encoding")) === "chunked")
) {
$size = $response->getBody()->getSize();
if ($size !== null) {
$response = $response->withHeader("Content-length", (string) $size);
}
);
}
return $response;
}
private function getStatusLine(ResponseInterface $response)

View File

@ -12,9 +12,6 @@ interface TransmitterInterface
*
* This method MUST output the status line, headers, and body to the client.
*
* This method MUST NOT alter the response body, unless it is to remove the
* body for a HEAD request.
*
* Implementations MAY add response headers to ensure expected headers are
* presents but MUST NOT alter existing headers.
*

View File

@ -1,98 +0,0 @@
<?php
namespace WellRESTed\Test\Unit\Transmission\Middleware;
use Prophecy\Argument;
use WellRESTed\Transmission\Middleware\ContentLengthHandler;
/**
* @covers WellRESTed\Transmission\Middleware\ContentLengthHandler
* @group transmission
*/
class ContentLengthHandlerTest extends \PHPUnit_Framework_TestCase
{
private $request;
private $response;
private $next;
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())->will(
function ($args) {
$this->hasHeader($args[0])->willReturn(true);
$this->getHeader($args[0])->willReturn([$args[1]]);
$this->getHeaderLine($args[0])->willReturn($args[1]);
return $this;
}
);
$this->next = function ($request, $response) {
return $response;
};
}
public function testAddsContentLengthHeader()
{
$this->response->hasHeader("Content-length")->willReturn(false);
$this->response->getHeaderLine("Transfer-encoding")->willReturn("");
$hook = new ContentLengthHandler();
$response = $hook->dispatch($this->request->reveal(), $this->response->reveal(), $this->next);
$this->assertEquals([1024], $response->getHeader("Content-length"));
}
public function testMultipleDispatchesHaveNoEffect()
{
$this->response->hasHeader("Content-length")->willReturn(false);
$this->response->getHeaderLine("Transfer-encoding")->willReturn("");
$hook = new ContentLengthHandler();
$response = $this->response->reveal();
$response = $hook->dispatch($this->request->reveal(), $response, $this->next);
$hook->dispatch($this->request->reveal(), $response, $this->next);
$this->response->withHeader("Content-length", 1024)->shouldHaveBeenCalledTimes(1);
}
public function testDoesNotAddHeaderWhenContentLenghtIsAlreadySet()
{
$this->response->hasHeader("Content-length")->willReturn(true);
$this->response->getHeaderLine("Transfer-encoding")->willReturn("");
$hook = new ContentLengthHandler();
$hook->dispatch($this->request->reveal(), $this->response->reveal(), $this->next);
$this->response->withHeader(Argument::cetera())->shouldNotHaveBeenCalled();
}
public function testDoesNotAddHeaderWhenTransferEncodingIsChunked()
{
$this->response->hasHeader("Content-length")->willReturn(false);
$this->response->getHeaderLine("Transfer-encoding")->willReturn("CHUNKED");
$hook = new ContentLengthHandler();
$hook->dispatch($this->request->reveal(), $this->response->reveal(), $this->next);
$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);
$hook = new ContentLengthHandler();
$hook->dispatch($this->request->reveal(), $this->response->reveal(), $this->next);
$this->response->withHeader(Argument::cetera())->shouldNotHaveBeenCalled();
}
}

View File

@ -1,63 +0,0 @@
<?php
namespace WellRESTed\Test\Unit\Transmission\Middleware;
use Prophecy\Argument;
use WellRESTed\Transmission\Middleware\HeadHandler;
/**
* @covers WellRESTed\Transmission\Middleware\HeadHandler
* @uses WellRESTed\Message\NullStream
* @group transmission
*/
class HeadHandlerTest extends \PHPUnit_Framework_TestCase
{
private $request;
private $response;
private $next;
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->withBody(Argument::any())->will(
function ($args) {
$this->getBody()->willReturn($args[0]);
return $this;
}
);
$this->next = function ($request, $response) {
return $response;
};
}
public function testReplacesBodyForHeadRequest()
{
$this->request->getMethod()->willReturn("HEAD");
$hook = new HeadHandler();
$response = $hook->dispatch($this->request->reveal(), $this->response->reveal(), $this->next);
$this->assertSame(0, $response->getBody()->getSize());
}
public function testMultipleDispatchesHaveNoEffect()
{
$this->request->getMethod()->willReturn("HEAD");
$hook = new HeadHandler();
$response = $hook->dispatch($this->request->reveal(), $this->response->reveal(), $this->next);
$hook->dispatch($this->request->reveal(), $response, $this->next);
$this->response->withBody(Argument::any())->shouldHaveBeenCalledTimes(1);
}
public function testDoesNotReplaceBodyForNonHeadRequests()
{
$this->request->getMethod()->willReturn("GET");
$hook = new HeadHandler();
$hook->dispatch($this->request->reveal(), $this->response->reveal(), $this->next);
$this->response->withBody(Argument::any())->shouldNotHaveBeenCalled();
}
}

View File

@ -11,8 +11,6 @@ require_once __DIR__ . "/../../../src/HeaderStack.php";
/**
* @coversDefaultClass WellRESTed\Transmission\Transmitter
* @uses WellRESTed\Transmission\Transmitter
* @uses WellRESTed\Transmission\Middleware\ContentLengthHandler
* @uses WellRESTed\Transmission\Middleware\HeadHandler
* @uses WellRESTed\Dispatching\Dispatcher
* @uses WellRESTed\Dispatching\DispatchStack
* @group transmission
@ -163,6 +161,9 @@ class TransmitterTest extends \PHPUnit_Framework_TestCase
$this->assertEquals($content, $captured);
}
// ------------------------------------------------------------------------
// Preparation
/**
* @covers ::prepareResponse
*/
@ -184,16 +185,55 @@ class TransmitterTest extends \PHPUnit_Framework_TestCase
/**
* @covers ::prepareResponse
*/
public function testReplacesBodyForHeadRequeset()
public function testDoesNotReplaceContentLengthHeaderWhenContentLenghtIsAlreadySet()
{
$bodySize = 1024;
$this->response->getStatusCode()->willReturn("200");
$this->response->getReasonPhrase()->willReturn("Ok");
$this->response->hasHeader("Content-length")->willReturn(true);
$this->body->isReadable()->willReturn(true);
$this->body->__toString()->willReturn("");
$this->body->getSize()->willReturn($bodySize);
$transmitter = new Transmitter();
$transmitter->transmit($this->request->reveal(), $this->response->reveal());
$this->response->withHeader("Content-length", $bodySize)->shouldNotHaveBeenCalled();
}
/**
* @covers ::prepareResponse
*/
public function testDoesNotAddContentLengthHeaderWhenTransferEncodingIsChunked()
{
$bodySize = 1024;
$this->response->getStatusCode()->willReturn("200");
$this->response->getReasonPhrase()->willReturn("Ok");
$this->response->hasHeader("Content-length")->willReturn(false);
$this->response->getHeaderLine("Transfer-encoding")->willReturn("CHUNKED");
$this->body->isReadable()->willReturn(true);
$this->body->__toString()->willReturn("");
$this->body->getSize()->willReturn($bodySize);
$transmitter = new Transmitter();
$transmitter->transmit($this->request->reveal(), $this->response->reveal());
$this->response->withHeader("Content-length", $bodySize)->shouldNotHaveBeenCalled();
}
/**
* @covers ::prepareResponse
*/
public function testDoesNotAddContentLengthHeaderWhenBodySizeIsNull()
{
$this->response->getStatusCode()->willReturn("200");
$this->response->getReasonPhrase()->willReturn("Ok");
$this->response->hasHeader("Content-length")->willReturn(false);
$this->response->getHeaderLine("Transfer-encoding")->willReturn("");
$this->body->isReadable()->willReturn(true);
$this->body->__toString()->willReturn("");
$this->body->getSize()->willReturn(null);
$transmitter = new Transmitter();
$transmitter->transmit($this->request->reveal(), $this->response->reveal());
$this->response->withBody(Argument::any())->shouldHaveBeenCalled();
$this->response->withHeader("Content-length", Argument::any())->shouldNotHaveBeenCalled();
}
}