Update finalization hooks.

This commit is contained in:
PJ Dietz 2015-05-07 18:23:44 -04:00
parent ccbe8bb2e0
commit 9915dffcfc
4 changed files with 56 additions and 26 deletions

View File

@ -1,6 +1,6 @@
<?php <?php
namespace WellRESTed\Routing\ResponsePrep; namespace WellRESTed\Routing\Hook;
use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Message\ServerRequestInterface;
@ -13,7 +13,7 @@ use WellRESTed\Routing\MiddlewareInterface;
* - Response does not have a Tranfser-encoding: chunked header * - Response does not have a Tranfser-encoding: chunked header
* - Response body stream reports a size * - Response body stream reports a size
*/ */
class ContentLengthPrep implements MiddlewareInterface class ContentLengthHook implements MiddlewareInterface
{ {
public function dispatch(ServerRequestInterface $request, ResponseInterface &$response) public function dispatch(ServerRequestInterface $request, ResponseInterface &$response)
{ {

View File

@ -1,6 +1,6 @@
<?php <?php
namespace WellRESTed\Routing\ResponsePrep; namespace WellRESTed\Routing\Hook;
use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Message\ServerRequestInterface;
@ -10,7 +10,7 @@ use WellRESTed\Routing\MiddlewareInterface;
/** /**
* Removes the body of a response to a HEAD request. * Removes the body of a response to a HEAD request.
*/ */
class HeadPrep implements MiddlewareInterface class HeadHook implements MiddlewareInterface
{ {
public function dispatch(ServerRequestInterface $request, ResponseInterface &$response) public function dispatch(ServerRequestInterface $request, ResponseInterface &$response)
{ {

View File

@ -1,14 +1,14 @@
<?php <?php
namespace WellRESTed\Test\Unit\Routing; namespace WellRESTed\Test\Unit\Routing\Hook;
use Prophecy\Argument; use Prophecy\Argument;
use WellRESTed\Routing\ResponsePrep\ContentLengthPrep; use WellRESTed\Routing\Hook\ContentLengthHook;
/** /**
* @covers WellRESTed\Routing\ResponsePrep\ContentLengthPrep * @covers WellRESTed\Routing\Hook\ContentLengthHook
*/ */
class ContentLengthPrepTest extends \PHPUnit_Framework_TestCase class ContentLengthHookTest extends \PHPUnit_Framework_TestCase
{ {
private $request; private $request;
private $response; private $response;
@ -22,22 +22,41 @@ class ContentLengthPrepTest extends \PHPUnit_Framework_TestCase
$this->request = $this->prophesize('Psr\Http\Message\ServerRequestInterface'); $this->request = $this->prophesize('Psr\Http\Message\ServerRequestInterface');
$this->response = $this->prophesize('Psr\Http\Message\ResponseInterface'); $this->response = $this->prophesize('Psr\Http\Message\ResponseInterface');
$this->response->getBody()->willReturn($this->body->reveal()); $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->hasHeader("Content-length")->willReturn(false);
$this->response->getHeaderLine("Transfer-encoding")->willReturn(""); $this->response->getHeaderLine("Transfer-encoding")->willReturn("");
$request = $this->request->reveal(); $request = $this->request->reveal();
$response = $this->response->reveal(); $response = $this->response->reveal();
$prep = new ContentLengthPrep(); $hook = new ContentLengthHook();
$prep->dispatch($request, $response); $hook->dispatch($request, $response);
$this->response->withHeader("Content-length", 1024)->shouldHaveBeenCalled(); $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() public function testDoesNotAddHeaderWhenContentLenghtIsAlreadySet()
{ {
$this->response->hasHeader("Content-length")->willReturn(true); $this->response->hasHeader("Content-length")->willReturn(true);
@ -45,8 +64,8 @@ class ContentLengthPrepTest extends \PHPUnit_Framework_TestCase
$request = $this->request->reveal(); $request = $this->request->reveal();
$response = $this->response->reveal(); $response = $this->response->reveal();
$prep = new ContentLengthPrep(); $hook = new ContentLengthHook();
$prep->dispatch($request, $response); $hook->dispatch($request, $response);
$this->response->withHeader(Argument::cetera())->shouldNotHaveBeenCalled(); $this->response->withHeader(Argument::cetera())->shouldNotHaveBeenCalled();
} }
@ -58,8 +77,8 @@ class ContentLengthPrepTest extends \PHPUnit_Framework_TestCase
$request = $this->request->reveal(); $request = $this->request->reveal();
$response = $this->response->reveal(); $response = $this->response->reveal();
$prep = new ContentLengthPrep(); $hook = new ContentLengthHook();
$prep->dispatch($request, $response); $hook->dispatch($request, $response);
$this->response->withHeader(Argument::cetera())->shouldNotHaveBeenCalled(); $this->response->withHeader(Argument::cetera())->shouldNotHaveBeenCalled();
} }
@ -72,8 +91,8 @@ class ContentLengthPrepTest extends \PHPUnit_Framework_TestCase
$request = $this->request->reveal(); $request = $this->request->reveal();
$response = $this->response->reveal(); $response = $this->response->reveal();
$prep = new ContentLengthPrep(); $hook = new ContentLengthHook();
$prep->dispatch($request, $response); $hook->dispatch($request, $response);
$this->response->withHeader(Argument::cetera())->shouldNotHaveBeenCalled(); $this->response->withHeader(Argument::cetera())->shouldNotHaveBeenCalled();
} }

View File

@ -1,15 +1,15 @@
<?php <?php
namespace WellRESTed\Test\Unit\Routing; namespace WellRESTed\Test\Unit\Routing\Hook;
use Prophecy\Argument; use Prophecy\Argument;
use WellRESTed\Routing\ResponsePrep\HeadPrep; use WellRESTed\Routing\Hook\HeadHook;
/** /**
* @covers WellRESTed\Routing\ResponsePrep\HeadPrep * @covers WellRESTed\Routing\Hook\HeadHook
* @uses WellRESTed\Message\NullStream * @uses WellRESTed\Message\NullStream
*/ */
class HeadPrepTest extends \PHPUnit_Framework_TestCase class HeadHookTest extends \PHPUnit_Framework_TestCase
{ {
private $request; private $request;
private $response; private $response;
@ -34,18 +34,29 @@ class HeadPrepTest extends \PHPUnit_Framework_TestCase
$this->request->getMethod()->willReturn("HEAD"); $this->request->getMethod()->willReturn("HEAD");
$request = $this->request->reveal(); $request = $this->request->reveal();
$response = $this->response->reveal(); $response = $this->response->reveal();
$prep = new HeadPrep(); $hook = new HeadHook();
$prep->dispatch($request, $response); $hook->dispatch($request, $response);
$this->assertSame(0, $response->getBody()->getSize()); $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() public function testDoesNotReplaceBodyForNonHeadRequests()
{ {
$this->request->getMethod()->willReturn("GET"); $this->request->getMethod()->willReturn("GET");
$request = $this->request->reveal(); $request = $this->request->reveal();
$response = $this->response->reveal(); $response = $this->response->reveal();
$prep = new HeadPrep(); $hook = new HeadHook();
$prep->dispatch($request, $response); $hook->dispatch($request, $response);
$this->response->withBody(Argument::any())->shouldNotHaveBeenCalled(); $this->response->withBody(Argument::any())->shouldNotHaveBeenCalled();
} }
} }