Update ContentLengthHook
This commit is contained in:
parent
5a01d20f8e
commit
06f694154c
|
|
@ -15,17 +15,18 @@ use WellRESTed\Routing\MiddlewareInterface;
|
||||||
*/
|
*/
|
||||||
class ContentLengthHook implements MiddlewareInterface
|
class ContentLengthHook implements MiddlewareInterface
|
||||||
{
|
{
|
||||||
public function dispatch(ServerRequestInterface $request, ResponseInterface &$response)
|
public function dispatch(ServerRequestInterface $request, ResponseInterface $response, $next)
|
||||||
{
|
{
|
||||||
if ($response->hasHeader("Content-length")) {
|
if ($response->hasHeader("Content-length")) {
|
||||||
return;
|
return $next($request, $response);
|
||||||
}
|
}
|
||||||
if (strtolower($response->getHeaderLine("Transfer-encoding")) === "chunked") {
|
if (strtolower($response->getHeaderLine("Transfer-encoding")) === "chunked") {
|
||||||
return;
|
return $next($request, $response);
|
||||||
}
|
}
|
||||||
$size = $response->getBody()->getSize();
|
$size = $response->getBody()->getSize();
|
||||||
if ($size !== null) {
|
if ($size !== null) {
|
||||||
$response = $response->withHeader("Content-length", (string) $size);
|
$response = $response->withHeader("Content-length", (string) $size);
|
||||||
}
|
}
|
||||||
|
return $next($request, $response);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -12,8 +12,10 @@ class ContentLengthHookTest extends \PHPUnit_Framework_TestCase
|
||||||
{
|
{
|
||||||
private $request;
|
private $request;
|
||||||
private $response;
|
private $response;
|
||||||
|
private $next;
|
||||||
private $body;
|
private $body;
|
||||||
|
|
||||||
|
|
||||||
public function setUp()
|
public function setUp()
|
||||||
{
|
{
|
||||||
parent::setUp();
|
parent::setUp();
|
||||||
|
|
@ -23,11 +25,16 @@ class ContentLengthHookTest extends \PHPUnit_Framework_TestCase
|
||||||
$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())->will(
|
$this->response->withHeader(Argument::cetera())->will(
|
||||||
function () {
|
function ($args) {
|
||||||
$this->hasHeader("Content-length")->willReturn(true);
|
$this->hasHeader($args[0])->willReturn(true);
|
||||||
|
$this->getHeader($args[0])->willReturn([$args[1]]);
|
||||||
|
$this->getHeaderLine($args[0])->willReturn($args[1]);
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
$this->next = function ($request, $response) {
|
||||||
|
return $response;
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testAddsContentLengthHeader()
|
public function testAddsContentLengthHeader()
|
||||||
|
|
@ -35,12 +42,10 @@ 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("");
|
||||||
|
|
||||||
$request = $this->request->reveal();
|
|
||||||
$response = $this->response->reveal();
|
|
||||||
$hook = new ContentLengthHook();
|
$hook = new ContentLengthHook();
|
||||||
$hook->dispatch($request, $response);
|
$response = $hook->dispatch($this->request->reveal(), $this->response->reveal(), $this->next);
|
||||||
|
|
||||||
$this->response->withHeader("Content-length", 1024)->shouldHaveBeenCalled();
|
$this->assertEquals([1024], $response->getHeader("Content-length"));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testMultipleDispatchesHaveNoEffect()
|
public function testMultipleDispatchesHaveNoEffect()
|
||||||
|
|
@ -48,11 +53,11 @@ 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("");
|
||||||
|
|
||||||
$request = $this->request->reveal();
|
|
||||||
$response = $this->response->reveal();
|
|
||||||
$hook = new ContentLengthHook();
|
$hook = new ContentLengthHook();
|
||||||
$hook->dispatch($request, $response);
|
|
||||||
$hook->dispatch($request, $response);
|
$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);
|
$this->response->withHeader("Content-length", 1024)->shouldHaveBeenCalledTimes(1);
|
||||||
}
|
}
|
||||||
|
|
@ -62,10 +67,8 @@ 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("");
|
||||||
|
|
||||||
$request = $this->request->reveal();
|
|
||||||
$response = $this->response->reveal();
|
|
||||||
$hook = new ContentLengthHook();
|
$hook = new ContentLengthHook();
|
||||||
$hook->dispatch($request, $response);
|
$hook->dispatch($this->request->reveal(), $this->response->reveal(), $this->next);
|
||||||
|
|
||||||
$this->response->withHeader(Argument::cetera())->shouldNotHaveBeenCalled();
|
$this->response->withHeader(Argument::cetera())->shouldNotHaveBeenCalled();
|
||||||
}
|
}
|
||||||
|
|
@ -75,10 +78,8 @@ 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");
|
||||||
|
|
||||||
$request = $this->request->reveal();
|
|
||||||
$response = $this->response->reveal();
|
|
||||||
$hook = new ContentLengthHook();
|
$hook = new ContentLengthHook();
|
||||||
$hook->dispatch($request, $response);
|
$hook->dispatch($this->request->reveal(), $this->response->reveal(), $this->next);
|
||||||
|
|
||||||
$this->response->withHeader(Argument::cetera())->shouldNotHaveBeenCalled();
|
$this->response->withHeader(Argument::cetera())->shouldNotHaveBeenCalled();
|
||||||
}
|
}
|
||||||
|
|
@ -89,10 +90,8 @@ 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);
|
||||||
|
|
||||||
$request = $this->request->reveal();
|
|
||||||
$response = $this->response->reveal();
|
|
||||||
$hook = new ContentLengthHook();
|
$hook = new ContentLengthHook();
|
||||||
$hook->dispatch($request, $response);
|
$hook->dispatch($this->request->reveal(), $this->response->reveal(), $this->next);
|
||||||
|
|
||||||
$this->response->withHeader(Argument::cetera())->shouldNotHaveBeenCalled();
|
$this->response->withHeader(Argument::cetera())->shouldNotHaveBeenCalled();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue