orbit/tests/src/Orbit/ResponseTest.php
2020-09-06 02:32:56 -05:00

138 lines
3.5 KiB
PHP

<?php declare(strict_types=1);
namespace Orbit\Tests;
use PHPUnit\Framework\TestCase;
use Orbit\Response;
final class ResponseTest extends TestCase
{
public function testConstruct(): void
{
$response = new Response();
$this->assertInstanceOf(Response::class, $response);
}
public function testGetHeader(): void
{
$response = new Response("20", "text/plain");
$this->assertSame("20 text/plain\r\n", $response->getHeader());
}
public function testSendWithBody(): void
{
$response = new Response("20", "text/plain");
$response->body = 'xyz';
$fp = fopen("php://memory", "w");
$result = $response->send($fp);
$this->assertSame(3, $result);
}
public function testSendWithFile(): void
{
file_put_contents('test.txt', 'hiho');
$response = new Response("20", "text/plain");
$response->filepath = 'test.txt';
$fp = fopen("php://memory", "w");
$result = $response->send($fp);
$this->assertSame(4, $result);
@unlink('test.txt');
}
public function testSendResourceBrokenPipe(): void
{
$this->expectException(\Exception::class);
$this->expectExceptionMessage("Failed to write to client");
$response = new Response("20", "text/plain");
$response->body = "123";
// Cannot write to this read-only stream resource
$fp = fopen("php://memory", "r");
$result = $response->send($fp);
}
public function testSendInvalidResource(): void
{
$this->expectException(\Exception::class);
$this->expectExceptionMessage("Invalid resource to write to");
$response = new Response("20", "text/plain");
$response->body = "123";
$fp = null;
$result = $response->send($fp);
}
public function testSendWithFileNoExists(): void
{
$this->expectException(\Exception::class);
$this->expectExceptionMessage("Error reading file");
$response = new Response("20", "text/plain");
$response->filepath = 'nofile.txt';
$fp = fopen("php://memory", "w");
$result = $response->send($fp);
$this->assertSame(4, $result);
}
public function testSendWithDirectory(): void
{
$this->expectException(\Exception::class);
$this->expectExceptionMessage("Cannot serve directory");
$dir = "test-aloof";
mkdir($dir);
$response = new Response("20", "text/plain");
$response->filepath = $dir;
$fp = fopen("php://memory", "w");
$result = $response->send($fp);
@rmdir($dir);
}
public function testSetBody(): void
{
$response = new Response("20", "text/plain");
$response->setBody('olive juice');
$body = $response->getBody();
$this->assertEquals('olive juice', $body);
}
public function testGetBodyWithFile(): void
{
file_put_contents('elem.gmi', '# Hi there');
$response = new Response("20", "text/plain");
$response->setStaticFile('elem.gmi');
$body = $response->getBody();
$this->assertEquals('# Hi there', $body);
unlink('elem.gmi');
}
public function testSetStatusSetMeta(): void
{
$response = new Response();
$response->setStatus(20);
$response->setMeta('text/gemini');
$header = $response->getHeader();
$this->assertEquals("20 text/gemini\r\n", $header);
}
public function tearDown(): void
{
@rmdir('test-aloof');
}
}