orbit/tests/src/Orbit/Module/StaticsTest.php
2020-09-07 17:05:38 -05:00

192 lines
6.4 KiB
PHP

<?php declare(strict_types=1);
namespace Orbit\Tests;
use PHPUnit\Framework\TestCase;
use Monolog\Logger;
use Orbit\Config;
use Orbit\Module\Statics;
use Orbit\Request;
use Orbit\Response;
final class StaticsTest extends TestCase
{
public function makeObject($config_params = []): Statics
{
$config = new Config();
$config->readFromArray($config_params);
$statics = new Statics($config, new Logger('test-orbit'));
return $statics;
}
public function testConstruct(): void
{
$statics = new Statics(new Config(), new Logger('test-orbit'));
$this->assertInstanceOf(Statics::class, $statics);
}
public function testHandle(): void
{
$statics = $this->makeObject();
$request = new Request('lmnop');
[$success, $response] = $statics->handle($request);
$this->assertSame(Response::STATUS_NOT_FOUND, $response->getStatus());
$this->assertTrue($success);
}
public function testHandleFileAttemptAboveRoot(): void
{
$statics = $this->makeObject();
$request = new Request('/../README.md');
[$success, $response] = $statics->handle($request);
$this->assertSame(Response::STATUS_PERMANENT_FAILURE, $response->getStatus());
$this->assertFalse($success);
}
public function testHandleDirectoryWithRedirect(): void
{
@mkdir('dir1');
$statics = $this->makeObject();
$request = new Request('/dir1');
[$success, $response] = $statics->handle($request);
$this->assertSame(Response::STATUS_REDIRECT_PERMANENT, $response->getStatus());
$this->assertSame('gemini:///dir1/', $response->getMeta());
$this->assertFalse($success);
@rmdir('dir1');
}
public function testHandleDirectoryWithIndexFile(): void
{
@mkdir('dir1');
file_put_contents('dir1/index.gmi', '# Sunlit lands');
$statics = $this->makeObject();
$request = new Request('/dir1/');
[$success, $response] = $statics->handle($request);
$this->assertSame(Response::STATUS_SUCCESS, $response->getStatus());
$this->assertSame('# Sunlit lands', $response->getBody());
$this->assertTrue($success);
unlink('dir1/index.gmi');
@rmdir('dir1');
}
public function testHandleNoDirectoryIndex(): void
{
@mkdir('dir1');
$statics = $this->makeObject(['enable_directory_index' => false]);
$request = new Request('/dir1/');
[$success, $response] = $statics->handle($request);
$this->assertSame(Response::STATUS_NOT_FOUND, $response->getStatus());
$this->assertSame('Path not available', $response->getMeta());
$this->assertFalse($success);
@rmdir('dir1');
}
public function testHandleCustomDirectoryIndex(): void
{
@mkdir('dir1');
file_put_contents('dir1/INDEX', '# Welcome to index');
$statics = $this->makeObject(['index_file' => 'INDEX']);
$request = new Request('/dir1/');
[$success, $response] = $statics->handle($request);
$this->assertSame(Response::STATUS_SUCCESS, $response->getStatus());
$this->assertStringContainsString('text/plain', $response->getMeta());
$this->assertStringContainsString('# Welcome to index', $response->getBody());
$this->assertTrue($success);
@unlink('dir1/INDEX');
@rmdir('dir1');
}
public function testHandleMakeDirectoryIndex(): void
{
@mkdir('dir1');
file_put_contents('dir1/abc.txt', 'ABCDEF');
file_put_contents('dir1/def.txt', 'XFFFF');
$statics = $this->makeObject(['enable_directory_index' => true]);
$request = new Request('/dir1/');
[$success, $response] = $statics->handle($request);
$this->assertSame(Response::STATUS_SUCCESS, $response->getStatus());
$this->assertSame('text/gemini', $response->getMeta());
$this->assertStringContainsString('abc.txt', $response->getBody());
$this->assertTrue($success);
@unlink('dir1/abc.txt');
@unlink('dir1/def.txt');
@rmdir('dir1');
}
public function testHandleFileNotFound(): void
{
$statics = $this->makeObject();
$request = new Request('foobar.txt');
[$success, $response] = $statics->handle($request);
$this->assertSame(Response::STATUS_NOT_FOUND, $response->getStatus());
$this->assertSame('Not found!', $response->getMeta());
$this->assertTrue($success);
}
public function testCustomMimeTypes(): void
{
file_put_contents('xyz.gmi', '# Make it great');
$statics = $this->makeObject();
$request = new Request('/xyz.gmi');
[$success, $response] = $statics->handle($request);
$this->assertSame(Response::STATUS_SUCCESS, $response->getStatus());
$this->assertSame('text/gemini', $response->getMeta());
$this->assertTrue($success);
unlink('xyz.gmi');
}
public function testGetCustomMimeFromFileExtension(): void
{
$statics = $this->makeObject();
$this->assertEquals('text/gemini', $statics->getCustomMimeFromFileExtension('gmi'));
$this->assertEquals('text/gemini', $statics->getCustomMimeFromFileExtension('gemini'));
$this->assertEquals('text/gemini', $statics->getCustomMimeFromFileExtension('md'));
$this->assertEquals('text/gemini', $statics->getCustomMimeFromFileExtension('markdown'));
$this->assertEquals('text/x-ansi', $statics->getCustomMimeFromFileExtension('ans'));
$this->assertEquals('text/x-ansi', $statics->getCustomMimeFromFileExtension('ansi'));
$this->assertEquals('', $statics->getCustomMimeFromFileExtension('hoo-haw'));
}
public function testMakeDirectoryIndexWithSubdirs(): void
{
mkdir('dir1');
mkdir('dir1/sub1');
file_put_contents('dir1/foo.txt', 'foo1');
file_put_contents('dir1/sub1/bar.txt', 'bar1');
$statics = $this->makeObject();
$results = $statics->makeDirectoryIndex('', realpath('./'));
$this->assertStringContainsString('# Directory listing', $results);
$this->assertStringContainsString('=> / ..', $results);
$this->assertStringContainsString('=> dir1/ dir1/', $results);
unlink('dir1/foo.txt');
unlink('dir1/sub1/bar.txt');
rmdir('dir1/sub1');
rmdir('dir1');
}
}