Refractor Transmitter test
This commit is contained in:
parent
b3dc82e744
commit
4fb7bf6050
|
|
@ -3,16 +3,16 @@
|
||||||
namespace WellRESTed\Test\Unit\Transmission;
|
namespace WellRESTed\Test\Unit\Transmission;
|
||||||
|
|
||||||
use Prophecy\Argument;
|
use Prophecy\Argument;
|
||||||
|
use Psr\Http\Message\StreamInterface;
|
||||||
|
use WellRESTed\Message\Response;
|
||||||
|
use WellRESTed\Message\ServerRequest;
|
||||||
use WellRESTed\Transmission\HeaderStack;
|
use WellRESTed\Transmission\HeaderStack;
|
||||||
use WellRESTed\Transmission\Transmitter;
|
use WellRESTed\Transmission\Transmitter;
|
||||||
|
|
||||||
require_once __DIR__ . "/../../../src/HeaderStack.php";
|
require_once __DIR__ . "/../../../src/HeaderStack.php";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @coversDefaultClass WellRESTed\Transmission\Transmitter
|
* @covers WellRESTed\Transmission\Transmitter
|
||||||
* @uses WellRESTed\Transmission\Transmitter
|
|
||||||
* @uses WellRESTed\Dispatching\Dispatcher
|
|
||||||
* @uses WellRESTed\Dispatching\DispatchStack
|
|
||||||
* @group transmission
|
* @group transmission
|
||||||
*/
|
*/
|
||||||
class TransmitterTest extends \PHPUnit_Framework_TestCase
|
class TransmitterTest extends \PHPUnit_Framework_TestCase
|
||||||
|
|
@ -24,73 +24,52 @@ class TransmitterTest extends \PHPUnit_Framework_TestCase
|
||||||
public function setUp()
|
public function setUp()
|
||||||
{
|
{
|
||||||
HeaderStack::reset();
|
HeaderStack::reset();
|
||||||
|
|
||||||
|
$this->request = (new ServerRequest())
|
||||||
|
->withMethod("HEAD");
|
||||||
|
|
||||||
$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->body->getSize()->willReturn(1024);
|
$this->body->getSize()->willReturn(1024);
|
||||||
$this->request = $this->prophesize('\Psr\Http\Message\ServerRequestInterface');
|
/** @var StreamInterface $stream */
|
||||||
$this->request->getMethod()->willReturn("HEAD");
|
$stream = $this->body->reveal();
|
||||||
$this->response = $this->prophesize('\Psr\Http\Message\ResponseInterface');
|
|
||||||
$this->response->getHeaders()->willReturn([]);
|
$this->response = (new Response())
|
||||||
$this->response->hasHeader("Content-length")->willReturn(true);
|
->withStatus(200)
|
||||||
$this->response->getHeaderLine("Transfer-encoding")->willReturn("");
|
->withBody($stream);
|
||||||
$this->response->getProtocolVersion()->willReturn("1.1");
|
|
||||||
$this->response->getStatusCode()->willReturn("200");
|
|
||||||
$this->response->getReasonPhrase()->willReturn("Ok");
|
|
||||||
$this->response->getBody()->willReturn($this->body->reveal());
|
|
||||||
$this->response->withHeader(Argument::cetera())->willReturn($this->response->reveal());
|
|
||||||
$this->response->withBody(Argument::any())->willReturn($this->response->reveal());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @covers ::__construct
|
|
||||||
*/
|
|
||||||
public function testCreatesInstance()
|
public function testCreatesInstance()
|
||||||
{
|
{
|
||||||
$transmitter = new Transmitter();
|
$transmitter = new Transmitter();
|
||||||
$this->assertNotNull($transmitter);
|
$this->assertNotNull($transmitter);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @covers ::transmit
|
|
||||||
* @covers ::getStatusLine
|
|
||||||
*/
|
|
||||||
public function testSendStatusCodeWithReasonPhrase()
|
public function testSendStatusCodeWithReasonPhrase()
|
||||||
{
|
{
|
||||||
$this->response->getStatusCode()->willReturn("200");
|
|
||||||
$this->response->getReasonPhrase()->willReturn("Ok");
|
|
||||||
|
|
||||||
$transmitter = new Transmitter();
|
$transmitter = new Transmitter();
|
||||||
$transmitter->transmit($this->request->reveal(), $this->response->reveal());
|
$transmitter->transmit($this->request, $this->response);
|
||||||
$this->assertContains("HTTP/1.1 200 Ok", HeaderStack::getHeaders());
|
$this->assertContains("HTTP/1.1 200 OK", HeaderStack::getHeaders());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @covers ::transmit
|
|
||||||
* @covers ::getStatusLine
|
|
||||||
*/
|
|
||||||
public function testSendStatusCodeWithoutReasonPhrase()
|
public function testSendStatusCodeWithoutReasonPhrase()
|
||||||
{
|
{
|
||||||
$this->response->getStatusCode()->willReturn("999");
|
$this->response = $this->response->withStatus(999);
|
||||||
$this->response->getReasonPhrase()->willReturn(null);
|
|
||||||
|
|
||||||
$transmitter = new Transmitter();
|
$transmitter = new Transmitter();
|
||||||
$transmitter->transmit($this->request->reveal(), $this->response->reveal());
|
$transmitter->transmit($this->request, $this->response);
|
||||||
$this->assertContains("HTTP/1.1 999", HeaderStack::getHeaders());
|
$this->assertContains("HTTP/1.1 999", HeaderStack::getHeaders());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/** @dataProvider headerProvider */
|
||||||
* @covers ::transmit
|
|
||||||
* @dataProvider headerProvider
|
|
||||||
*/
|
|
||||||
public function testSendsHeaders($header)
|
public function testSendsHeaders($header)
|
||||||
{
|
{
|
||||||
$this->response->getHeaders()->willReturn([
|
$this->response = $this->response
|
||||||
"Content-length" => ["2048"],
|
->withHeader("Content-length", ["2048"])
|
||||||
"X-foo" => ["bar", "baz"],
|
->withHeader("X-foo", ["bar", "baz"]);
|
||||||
]);
|
|
||||||
|
|
||||||
$transmitter = new Transmitter();
|
$transmitter = new Transmitter();
|
||||||
$transmitter->transmit($this->request->reveal(), $this->response->reveal());
|
$transmitter->transmit($this->request, $this->response);
|
||||||
$this->assertContains($header, HeaderStack::getHeaders());
|
$this->assertContains($header, HeaderStack::getHeaders());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -103,10 +82,6 @@ class TransmitterTest extends \PHPUnit_Framework_TestCase
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @covers ::transmit
|
|
||||||
* @covers ::outputBody
|
|
||||||
*/
|
|
||||||
public function testOutputsBody()
|
public function testOutputsBody()
|
||||||
{
|
{
|
||||||
$content = "Hello, world!";
|
$content = "Hello, world!";
|
||||||
|
|
@ -117,18 +92,13 @@ class TransmitterTest extends \PHPUnit_Framework_TestCase
|
||||||
$transmitter = new Transmitter();
|
$transmitter = new Transmitter();
|
||||||
|
|
||||||
ob_start();
|
ob_start();
|
||||||
$transmitter->transmit($this->request->reveal(), $this->response->reveal());
|
$transmitter->transmit($this->request, $this->response);
|
||||||
$captured = ob_get_contents();
|
$captured = ob_get_contents();
|
||||||
ob_end_clean();
|
ob_end_clean();
|
||||||
|
|
||||||
$this->assertEquals($content, $captured);
|
$this->assertEquals($content, $captured);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @covers ::transmit
|
|
||||||
* @covers ::setChunkSize
|
|
||||||
* @covers ::outputBody
|
|
||||||
*/
|
|
||||||
public function testOutputsBodyInChunks()
|
public function testOutputsBodyInChunks()
|
||||||
{
|
{
|
||||||
$content = "Hello, world!";
|
$content = "Hello, world!";
|
||||||
|
|
@ -155,18 +125,13 @@ class TransmitterTest extends \PHPUnit_Framework_TestCase
|
||||||
$transmitter->setChunkSize($chunkSize);
|
$transmitter->setChunkSize($chunkSize);
|
||||||
|
|
||||||
ob_start();
|
ob_start();
|
||||||
$transmitter->transmit($this->request->reveal(), $this->response->reveal());
|
$transmitter->transmit($this->request, $this->response);
|
||||||
$captured = ob_get_contents();
|
$captured = ob_get_contents();
|
||||||
ob_end_clean();
|
ob_end_clean();
|
||||||
|
|
||||||
$this->assertEquals($content, $captured);
|
$this->assertEquals($content, $captured);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @covers ::transmit
|
|
||||||
* @covers ::setChunkSize
|
|
||||||
* @covers ::outputBody
|
|
||||||
*/
|
|
||||||
public function testOutputsUnseekableStreamInChunks()
|
public function testOutputsUnseekableStreamInChunks()
|
||||||
{
|
{
|
||||||
$content = "Hello, world!";
|
$content = "Hello, world!";
|
||||||
|
|
@ -193,85 +158,83 @@ class TransmitterTest extends \PHPUnit_Framework_TestCase
|
||||||
$transmitter->setChunkSize($chunkSize);
|
$transmitter->setChunkSize($chunkSize);
|
||||||
|
|
||||||
ob_start();
|
ob_start();
|
||||||
$transmitter->transmit($this->request->reveal(), $this->response->reveal());
|
$transmitter->transmit($this->request, $this->response);
|
||||||
$captured = ob_get_contents();
|
$captured = ob_get_contents();
|
||||||
ob_end_clean();
|
ob_end_clean();
|
||||||
|
|
||||||
$this->assertEquals($content, $captured);
|
$this->assertEquals($content, $captured);
|
||||||
}
|
}
|
||||||
|
|
||||||
// ------------------------------------------------------------------------
|
// ------------------------------------------------------------------------
|
||||||
// Preparation
|
// Preparation
|
||||||
|
|
||||||
/**
|
|
||||||
* @covers ::prepareResponse
|
|
||||||
*/
|
|
||||||
public function testAddContentLengthHeader()
|
public function testAddContentLengthHeader()
|
||||||
{
|
{
|
||||||
$bodySize = 1024;
|
$bodySize = 1024;
|
||||||
$this->response->getStatusCode()->willReturn("200");
|
|
||||||
$this->response->getReasonPhrase()->willReturn("Ok");
|
|
||||||
$this->response->hasHeader("Content-length")->willReturn(false);
|
|
||||||
$this->body->isReadable()->willReturn(true);
|
$this->body->isReadable()->willReturn(true);
|
||||||
$this->body->__toString()->willReturn("");
|
$this->body->__toString()->willReturn("");
|
||||||
$this->body->getSize()->willReturn($bodySize);
|
$this->body->getSize()->willReturn($bodySize);
|
||||||
|
|
||||||
$transmitter = new Transmitter();
|
$transmitter = new Transmitter();
|
||||||
$transmitter->transmit($this->request->reveal(), $this->response->reveal());
|
$transmitter->transmit($this->request, $this->response);
|
||||||
$this->response->withHeader("Content-length", $bodySize)->shouldHaveBeenCalled();
|
|
||||||
|
$this->assertContains("Content-length: $bodySize", HeaderStack::getHeaders());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @covers ::prepareResponse
|
|
||||||
*/
|
|
||||||
public function testDoesNotReplaceContentLengthHeaderWhenContentLenghtIsAlreadySet()
|
public function testDoesNotReplaceContentLengthHeaderWhenContentLenghtIsAlreadySet()
|
||||||
{
|
{
|
||||||
$bodySize = 1024;
|
$streamSize = 1024;
|
||||||
$this->response->getStatusCode()->willReturn("200");
|
$headerSize = 2048;
|
||||||
$this->response->getReasonPhrase()->willReturn("Ok");
|
|
||||||
$this->response->hasHeader("Content-length")->willReturn(true);
|
$this->response = $this->response->withHeader("Content-length", $headerSize);
|
||||||
|
|
||||||
$this->body->isReadable()->willReturn(true);
|
$this->body->isReadable()->willReturn(true);
|
||||||
$this->body->__toString()->willReturn("");
|
$this->body->__toString()->willReturn("");
|
||||||
$this->body->getSize()->willReturn($bodySize);
|
$this->body->getSize()->willReturn($streamSize);
|
||||||
|
|
||||||
$transmitter = new Transmitter();
|
$transmitter = new Transmitter();
|
||||||
$transmitter->transmit($this->request->reveal(), $this->response->reveal());
|
$transmitter->transmit($this->request, $this->response);
|
||||||
$this->response->withHeader("Content-length", $bodySize)->shouldNotHaveBeenCalled();
|
|
||||||
|
$this->assertContains("Content-length: $headerSize", HeaderStack::getHeaders());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @covers ::prepareResponse
|
|
||||||
*/
|
|
||||||
public function testDoesNotAddContentLengthHeaderWhenTransferEncodingIsChunked()
|
public function testDoesNotAddContentLengthHeaderWhenTransferEncodingIsChunked()
|
||||||
{
|
{
|
||||||
$bodySize = 1024;
|
$bodySize = 1024;
|
||||||
$this->response->getStatusCode()->willReturn("200");
|
|
||||||
$this->response->getReasonPhrase()->willReturn("Ok");
|
$this->response = $this->response->withHeader("Transfer-encoding", "CHUNKED");
|
||||||
$this->response->hasHeader("Content-length")->willReturn(false);
|
|
||||||
$this->response->getHeaderLine("Transfer-encoding")->willReturn("CHUNKED");
|
|
||||||
$this->body->isReadable()->willReturn(true);
|
$this->body->isReadable()->willReturn(true);
|
||||||
$this->body->__toString()->willReturn("");
|
$this->body->__toString()->willReturn("");
|
||||||
$this->body->getSize()->willReturn($bodySize);
|
$this->body->getSize()->willReturn($bodySize);
|
||||||
|
|
||||||
$transmitter = new Transmitter();
|
$transmitter = new Transmitter();
|
||||||
$transmitter->transmit($this->request->reveal(), $this->response->reveal());
|
$transmitter->transmit($this->request, $this->response);
|
||||||
$this->response->withHeader("Content-length", $bodySize)->shouldNotHaveBeenCalled();
|
|
||||||
|
$this->assertArrayDoesNotContainValueWithPrefix(HeaderStack::getHeaders(), "Content-length:");
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @covers ::prepareResponse
|
|
||||||
*/
|
|
||||||
public function testDoesNotAddContentLengthHeaderWhenBodySizeIsNull()
|
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->isReadable()->willReturn(true);
|
||||||
$this->body->__toString()->willReturn("");
|
$this->body->__toString()->willReturn("");
|
||||||
$this->body->getSize()->willReturn(null);
|
$this->body->getSize()->willReturn(null);
|
||||||
|
|
||||||
$transmitter = new Transmitter();
|
$transmitter = new Transmitter();
|
||||||
$transmitter->transmit($this->request->reveal(), $this->response->reveal());
|
$transmitter->transmit($this->request, $this->response);
|
||||||
$this->response->withHeader("Content-length", Argument::any())->shouldNotHaveBeenCalled();
|
|
||||||
|
$this->assertArrayDoesNotContainValueWithPrefix(HeaderStack::getHeaders(), "Content-length:");
|
||||||
|
}
|
||||||
|
|
||||||
|
private function assertArrayDoesNotContainValueWithPrefix($arr, $prefix)
|
||||||
|
{
|
||||||
|
$normalPrefix = strtolower($prefix);
|
||||||
|
foreach ($arr as $item) {
|
||||||
|
$normalItem = strtolower($item);
|
||||||
|
if (substr($normalItem, 0, strlen($normalPrefix)) === $normalPrefix) {
|
||||||
|
$this->assertTrue(false, "Array should not contain value beginning with '$prefix' but contained '$item'");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
$this->assertTrue(true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue