Add Responder namespace
Move ContentLength and Head middleware to Resonder\Middleware
This commit is contained in:
parent
7874484c53
commit
b198e83d55
|
|
@ -1,6 +1,6 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace WellRESTed\Routing\Hook;
|
namespace WellRESTed\Responder\Middleware;
|
||||||
|
|
||||||
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\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 ContentLengthHook implements MiddlewareInterface
|
class ContentLengthHandler implements MiddlewareInterface
|
||||||
{
|
{
|
||||||
public function dispatch(ServerRequestInterface $request, ResponseInterface $response, $next)
|
public function dispatch(ServerRequestInterface $request, ResponseInterface $response, $next)
|
||||||
{
|
{
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace WellRESTed\Routing\Hook;
|
namespace WellRESTed\Responder\Middleware;
|
||||||
|
|
||||||
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\MiddlewareInterface;
|
||||||
/**
|
/**
|
||||||
* Removes the body of a response to a HEAD request.
|
* Removes the body of a response to a HEAD request.
|
||||||
*/
|
*/
|
||||||
class HeadHook implements MiddlewareInterface
|
class HeadHandler implements MiddlewareInterface
|
||||||
{
|
{
|
||||||
public function dispatch(ServerRequestInterface $request, ResponseInterface $response, $next)
|
public function dispatch(ServerRequestInterface $request, ResponseInterface $response, $next)
|
||||||
{
|
{
|
||||||
|
|
@ -1,8 +1,9 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace WellRESTed\Routing;
|
namespace WellRESTed\Responder;
|
||||||
|
|
||||||
use Psr\Http\Message\ResponseInterface;
|
use Psr\Http\Message\ResponseInterface;
|
||||||
|
use Psr\Http\Message\ServerRequestInterface;
|
||||||
use Psr\Http\Message\StreamInterface;
|
use Psr\Http\Message\StreamInterface;
|
||||||
|
|
||||||
class Responder implements ResponderInterface
|
class Responder implements ResponderInterface
|
||||||
|
|
@ -12,9 +13,10 @@ class Responder implements ResponderInterface
|
||||||
/**
|
/**
|
||||||
* Outputs a response.
|
* Outputs a response.
|
||||||
*
|
*
|
||||||
|
* @param ServerRequestInterface $request
|
||||||
* @param ResponseInterface $response Response to output
|
* @param ResponseInterface $response Response to output
|
||||||
*/
|
*/
|
||||||
public function respond(ResponseInterface $response)
|
public function respond(ServerRequestInterface $request, ResponseInterface $response)
|
||||||
{
|
{
|
||||||
// Status Line
|
// Status Line
|
||||||
header($this->getStatusLine($response));
|
header($this->getStatusLine($response));
|
||||||
|
|
@ -0,0 +1,17 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace WellRESTed\Responder;
|
||||||
|
|
||||||
|
use Psr\Http\Message\ResponseInterface;
|
||||||
|
use Psr\Http\Message\ServerRequestInterface;
|
||||||
|
|
||||||
|
interface ResponderInterface
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Outputs a response.
|
||||||
|
*
|
||||||
|
* @param ServerRequestInterface $request
|
||||||
|
* @param ResponseInterface $response Response to output
|
||||||
|
*/
|
||||||
|
public function respond(ServerRequestInterface $request, ResponseInterface $response);
|
||||||
|
}
|
||||||
|
|
@ -1,15 +0,0 @@
|
||||||
<?php
|
|
||||||
|
|
||||||
namespace WellRESTed\Routing;
|
|
||||||
|
|
||||||
use Psr\Http\Message\ResponseInterface;
|
|
||||||
|
|
||||||
interface ResponderInterface
|
|
||||||
{
|
|
||||||
/**
|
|
||||||
* Outputs a response.
|
|
||||||
*
|
|
||||||
* @param ResponseInterface $response Response to output
|
|
||||||
*/
|
|
||||||
public function respond(ResponseInterface $response);
|
|
||||||
}
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace WellRESTed\Routing;
|
namespace WellRESTed\Responder;
|
||||||
|
|
||||||
class HeaderStack
|
class HeaderStack
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -3,19 +3,19 @@
|
||||||
namespace WellRESTed\Test\Unit\Routing\Hook;
|
namespace WellRESTed\Test\Unit\Routing\Hook;
|
||||||
|
|
||||||
use Prophecy\Argument;
|
use Prophecy\Argument;
|
||||||
use WellRESTed\Routing\Hook\ContentLengthHook;
|
use WellRESTed\Responder\Middleware\ContentLengthHandler;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @covers WellRESTed\Routing\Hook\ContentLengthHook
|
* @covers WellRESTed\Responder\Middleware\ContentLengthHandler
|
||||||
|
* @group responder
|
||||||
*/
|
*/
|
||||||
class ContentLengthHookTest extends \PHPUnit_Framework_TestCase
|
class ContentLengthHandlerTest extends \PHPUnit_Framework_TestCase
|
||||||
{
|
{
|
||||||
private $request;
|
private $request;
|
||||||
private $response;
|
private $response;
|
||||||
private $next;
|
private $next;
|
||||||
private $body;
|
private $body;
|
||||||
|
|
||||||
|
|
||||||
public function setUp()
|
public function setUp()
|
||||||
{
|
{
|
||||||
parent::setUp();
|
parent::setUp();
|
||||||
|
|
@ -42,7 +42,7 @@ class ContentLengthHookTest extends \PHPUnit_Framework_TestCase
|
||||||
$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("");
|
||||||
|
|
||||||
$hook = new ContentLengthHook();
|
$hook = new ContentLengthHandler();
|
||||||
$response = $hook->dispatch($this->request->reveal(), $this->response->reveal(), $this->next);
|
$response = $hook->dispatch($this->request->reveal(), $this->response->reveal(), $this->next);
|
||||||
|
|
||||||
$this->assertEquals([1024], $response->getHeader("Content-length"));
|
$this->assertEquals([1024], $response->getHeader("Content-length"));
|
||||||
|
|
@ -53,7 +53,7 @@ class ContentLengthHookTest extends \PHPUnit_Framework_TestCase
|
||||||
$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("");
|
||||||
|
|
||||||
$hook = new ContentLengthHook();
|
$hook = new ContentLengthHandler();
|
||||||
|
|
||||||
$response = $this->response->reveal();
|
$response = $this->response->reveal();
|
||||||
$response = $hook->dispatch($this->request->reveal(), $response, $this->next);
|
$response = $hook->dispatch($this->request->reveal(), $response, $this->next);
|
||||||
|
|
@ -67,7 +67,7 @@ class ContentLengthHookTest extends \PHPUnit_Framework_TestCase
|
||||||
$this->response->hasHeader("Content-length")->willReturn(true);
|
$this->response->hasHeader("Content-length")->willReturn(true);
|
||||||
$this->response->getHeaderLine("Transfer-encoding")->willReturn("");
|
$this->response->getHeaderLine("Transfer-encoding")->willReturn("");
|
||||||
|
|
||||||
$hook = new ContentLengthHook();
|
$hook = new ContentLengthHandler();
|
||||||
$hook->dispatch($this->request->reveal(), $this->response->reveal(), $this->next);
|
$hook->dispatch($this->request->reveal(), $this->response->reveal(), $this->next);
|
||||||
|
|
||||||
$this->response->withHeader(Argument::cetera())->shouldNotHaveBeenCalled();
|
$this->response->withHeader(Argument::cetera())->shouldNotHaveBeenCalled();
|
||||||
|
|
@ -78,7 +78,7 @@ class ContentLengthHookTest extends \PHPUnit_Framework_TestCase
|
||||||
$this->response->hasHeader("Content-length")->willReturn(false);
|
$this->response->hasHeader("Content-length")->willReturn(false);
|
||||||
$this->response->getHeaderLine("Transfer-encoding")->willReturn("CHUNKED");
|
$this->response->getHeaderLine("Transfer-encoding")->willReturn("CHUNKED");
|
||||||
|
|
||||||
$hook = new ContentLengthHook();
|
$hook = new ContentLengthHandler();
|
||||||
$hook->dispatch($this->request->reveal(), $this->response->reveal(), $this->next);
|
$hook->dispatch($this->request->reveal(), $this->response->reveal(), $this->next);
|
||||||
|
|
||||||
$this->response->withHeader(Argument::cetera())->shouldNotHaveBeenCalled();
|
$this->response->withHeader(Argument::cetera())->shouldNotHaveBeenCalled();
|
||||||
|
|
@ -90,7 +90,7 @@ class ContentLengthHookTest extends \PHPUnit_Framework_TestCase
|
||||||
$this->response->getHeaderLine("Transfer-encoding")->willReturn("");
|
$this->response->getHeaderLine("Transfer-encoding")->willReturn("");
|
||||||
$this->body->getSize()->willReturn(null);
|
$this->body->getSize()->willReturn(null);
|
||||||
|
|
||||||
$hook = new ContentLengthHook();
|
$hook = new ContentLengthHandler();
|
||||||
$hook->dispatch($this->request->reveal(), $this->response->reveal(), $this->next);
|
$hook->dispatch($this->request->reveal(), $this->response->reveal(), $this->next);
|
||||||
|
|
||||||
$this->response->withHeader(Argument::cetera())->shouldNotHaveBeenCalled();
|
$this->response->withHeader(Argument::cetera())->shouldNotHaveBeenCalled();
|
||||||
|
|
@ -1,15 +1,16 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace WellRESTed\Test\Unit\Routing\Hook;
|
namespace WellRESTed\Test\Unit\Responder\Middleware;
|
||||||
|
|
||||||
use Prophecy\Argument;
|
use Prophecy\Argument;
|
||||||
use WellRESTed\Routing\Hook\HeadHook;
|
use WellRESTed\Responder\Middleware\HeadHandler;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @covers WellRESTed\Routing\Hook\HeadHook
|
* @covers WellRESTed\Responder\Middleware\HeadHandler
|
||||||
* @uses WellRESTed\Message\NullStream
|
* @uses WellRESTed\Message\NullStream
|
||||||
|
* @group responder
|
||||||
*/
|
*/
|
||||||
class HeadHookTest extends \PHPUnit_Framework_TestCase
|
class HeadHandlerTest extends \PHPUnit_Framework_TestCase
|
||||||
{
|
{
|
||||||
private $request;
|
private $request;
|
||||||
private $response;
|
private $response;
|
||||||
|
|
@ -24,10 +25,12 @@ class HeadHookTest 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->withBody(Argument::any())->will(function ($args) {
|
$this->response->withBody(Argument::any())->will(
|
||||||
|
function ($args) {
|
||||||
$this->getBody()->willReturn($args[0]);
|
$this->getBody()->willReturn($args[0]);
|
||||||
return $this;
|
return $this;
|
||||||
});
|
}
|
||||||
|
);
|
||||||
$this->next = function ($request, $response) {
|
$this->next = function ($request, $response) {
|
||||||
return $response;
|
return $response;
|
||||||
};
|
};
|
||||||
|
|
@ -36,7 +39,7 @@ class HeadHookTest extends \PHPUnit_Framework_TestCase
|
||||||
public function testReplacesBodyForHeadRequest()
|
public function testReplacesBodyForHeadRequest()
|
||||||
{
|
{
|
||||||
$this->request->getMethod()->willReturn("HEAD");
|
$this->request->getMethod()->willReturn("HEAD");
|
||||||
$hook = new HeadHook();
|
$hook = new HeadHandler();
|
||||||
$response = $hook->dispatch($this->request->reveal(), $this->response->reveal(), $this->next);
|
$response = $hook->dispatch($this->request->reveal(), $this->response->reveal(), $this->next);
|
||||||
$this->assertSame(0, $response->getBody()->getSize());
|
$this->assertSame(0, $response->getBody()->getSize());
|
||||||
}
|
}
|
||||||
|
|
@ -44,7 +47,7 @@ class HeadHookTest extends \PHPUnit_Framework_TestCase
|
||||||
public function testMultipleDispatchesHaveNoEffect()
|
public function testMultipleDispatchesHaveNoEffect()
|
||||||
{
|
{
|
||||||
$this->request->getMethod()->willReturn("HEAD");
|
$this->request->getMethod()->willReturn("HEAD");
|
||||||
$hook = new HeadHook();
|
$hook = new HeadHandler();
|
||||||
$response = $hook->dispatch($this->request->reveal(), $this->response->reveal(), $this->next);
|
$response = $hook->dispatch($this->request->reveal(), $this->response->reveal(), $this->next);
|
||||||
$hook->dispatch($this->request->reveal(), $response, $this->next);
|
$hook->dispatch($this->request->reveal(), $response, $this->next);
|
||||||
$this->response->withBody(Argument::any())->shouldHaveBeenCalledTimes(1);
|
$this->response->withBody(Argument::any())->shouldHaveBeenCalledTimes(1);
|
||||||
|
|
@ -53,7 +56,7 @@ class HeadHookTest extends \PHPUnit_Framework_TestCase
|
||||||
public function testDoesNotReplaceBodyForNonHeadRequests()
|
public function testDoesNotReplaceBodyForNonHeadRequests()
|
||||||
{
|
{
|
||||||
$this->request->getMethod()->willReturn("GET");
|
$this->request->getMethod()->willReturn("GET");
|
||||||
$hook = new HeadHook();
|
$hook = new HeadHandler();
|
||||||
$hook->dispatch($this->request->reveal(), $this->response->reveal(), $this->next);
|
$hook->dispatch($this->request->reveal(), $this->response->reveal(), $this->next);
|
||||||
$this->response->withBody(Argument::any())->shouldNotHaveBeenCalled();
|
$this->response->withBody(Argument::any())->shouldNotHaveBeenCalled();
|
||||||
}
|
}
|
||||||
|
|
@ -1,18 +1,20 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace WellRESTed\Test\Unit\Routing;
|
namespace WellRESTed\Test\Unit\Responder;
|
||||||
|
|
||||||
use Prophecy\Argument;
|
use Prophecy\Argument;
|
||||||
use WellRESTed\Routing\HeaderStack;
|
use WellRESTed\Responder\HeaderStack;
|
||||||
use WellRESTed\Routing\Responder;
|
use WellRESTed\Responder\Responder;
|
||||||
|
|
||||||
require_once(__DIR__ . "/../../../src/HeaderStack.php");
|
require_once __DIR__ . "/../../../src/HeaderStack.php";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @covers WellRESTed\Routing\Responder
|
* @covers WellRESTed\Responder\Responder
|
||||||
|
* @group responder
|
||||||
*/
|
*/
|
||||||
class ResponderTest extends \PHPUnit_Framework_TestCase
|
class ResponderTest extends \PHPUnit_Framework_TestCase
|
||||||
{
|
{
|
||||||
|
private $request;
|
||||||
private $response;
|
private $response;
|
||||||
private $body;
|
private $body;
|
||||||
|
|
||||||
|
|
@ -21,6 +23,7 @@ class ResponderTest extends \PHPUnit_Framework_TestCase
|
||||||
HeaderStack::reset();
|
HeaderStack::reset();
|
||||||
$this->body = $this->prophesize('\Psr\Http\Message\StreamInterface');
|
$this->body = $this->prophesize('\Psr\Http\Message\StreamInterface');
|
||||||
$this->body->isReadable()->willReturn(false);
|
$this->body->isReadable()->willReturn(false);
|
||||||
|
$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->getHeaders()->willReturn([]);
|
$this->response->getHeaders()->willReturn([]);
|
||||||
$this->response->getProtocolVersion()->willReturn("1.1");
|
$this->response->getProtocolVersion()->willReturn("1.1");
|
||||||
|
|
@ -35,7 +38,7 @@ class ResponderTest extends \PHPUnit_Framework_TestCase
|
||||||
$this->response->getReasonPhrase()->willReturn("Ok");
|
$this->response->getReasonPhrase()->willReturn("Ok");
|
||||||
|
|
||||||
$responder = new Responder();
|
$responder = new Responder();
|
||||||
$responder->respond($this->response->reveal());
|
$responder->respond($this->request->reveal(), $this->response->reveal());
|
||||||
$this->assertContains("HTTP/1.1 200 Ok", HeaderStack::getHeaders());
|
$this->assertContains("HTTP/1.1 200 Ok", HeaderStack::getHeaders());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -45,7 +48,7 @@ class ResponderTest extends \PHPUnit_Framework_TestCase
|
||||||
$this->response->getReasonPhrase()->willReturn(null);
|
$this->response->getReasonPhrase()->willReturn(null);
|
||||||
|
|
||||||
$responder = new Responder();
|
$responder = new Responder();
|
||||||
$responder->respond($this->response->reveal());
|
$responder->respond($this->request->reveal(), $this->response->reveal());
|
||||||
$this->assertContains("HTTP/1.1 999", HeaderStack::getHeaders());
|
$this->assertContains("HTTP/1.1 999", HeaderStack::getHeaders());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -60,7 +63,7 @@ class ResponderTest extends \PHPUnit_Framework_TestCase
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$responder = new Responder();
|
$responder = new Responder();
|
||||||
$responder->respond($this->response->reveal());
|
$responder->respond($this->request->reveal(), $this->response->reveal());
|
||||||
$this->assertContains($header, HeaderStack::getHeaders());
|
$this->assertContains($header, HeaderStack::getHeaders());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -83,7 +86,7 @@ class ResponderTest extends \PHPUnit_Framework_TestCase
|
||||||
$responder = new Responder();
|
$responder = new Responder();
|
||||||
|
|
||||||
ob_start();
|
ob_start();
|
||||||
$responder->respond($this->response->reveal());
|
$responder->respond($this->request->reveal(), $this->response->reveal());
|
||||||
$captured = ob_get_contents();
|
$captured = ob_get_contents();
|
||||||
ob_end_clean();
|
ob_end_clean();
|
||||||
|
|
||||||
|
|
@ -99,7 +102,8 @@ class ResponderTest extends \PHPUnit_Framework_TestCase
|
||||||
$this->body->isReadable()->willReturn(true);
|
$this->body->isReadable()->willReturn(true);
|
||||||
$this->body->rewind()->willReturn(true);
|
$this->body->rewind()->willReturn(true);
|
||||||
$this->body->eof()->willReturn(false);
|
$this->body->eof()->willReturn(false);
|
||||||
$this->body->read(Argument::any())->will(function ($args) use ($content, &$position) {
|
$this->body->read(Argument::any())->will(
|
||||||
|
function ($args) use ($content, &$position) {
|
||||||
$chunkSize = $args[0];
|
$chunkSize = $args[0];
|
||||||
$chunk = substr($content, $position, $chunkSize);
|
$chunk = substr($content, $position, $chunkSize);
|
||||||
$position += $chunkSize;
|
$position += $chunkSize;
|
||||||
|
|
@ -107,13 +111,14 @@ class ResponderTest extends \PHPUnit_Framework_TestCase
|
||||||
$this->eof()->willReturn(true);
|
$this->eof()->willReturn(true);
|
||||||
}
|
}
|
||||||
return $chunk;
|
return $chunk;
|
||||||
});
|
}
|
||||||
|
);
|
||||||
|
|
||||||
$responder = new Responder();
|
$responder = new Responder();
|
||||||
$responder->setChunkSize($chunkSize);
|
$responder->setChunkSize($chunkSize);
|
||||||
|
|
||||||
ob_start();
|
ob_start();
|
||||||
$responder->respond($this->response->reveal(), $chunkSize);
|
$responder->respond($this->request->reveal(), $this->response->reveal(), $chunkSize);
|
||||||
$captured = ob_get_contents();
|
$captured = ob_get_contents();
|
||||||
ob_end_clean();
|
ob_end_clean();
|
||||||
|
|
||||||
Loading…
Reference in New Issue