From 1fa1c3821259bb4be1fae7fc6a2db6bb55045f56 Mon Sep 17 00:00:00 2001 From: Milos Stojanovic Date: Sun, 8 Mar 2020 17:20:54 +0100 Subject: [PATCH] Tests for download headers --- tests/backend/Feature/FilesTest.php | 32 +++++++++++++++++++++++++---- tests/backend/TestCase.php | 4 ++++ 2 files changed, 32 insertions(+), 4 deletions(-) diff --git a/tests/backend/Feature/FilesTest.php b/tests/backend/Feature/FilesTest.php index 0a7bada..b17e514 100644 --- a/tests/backend/Feature/FilesTest.php +++ b/tests/backend/Feature/FilesTest.php @@ -10,8 +10,8 @@ namespace Tests\Feature; -use Tests\TestCase; use Exception; +use Tests\TestCase; /** * @internal @@ -148,7 +148,7 @@ class FilesTest extends TestCase $this->assertOk(); } - public function testDownloadFile() + public function testDownloadFileHeaders() { $username = 'john@example.com'; $this->signIn($username, 'john123'); @@ -159,6 +159,30 @@ class FilesTest extends TestCase $path_encoded = base64_encode('john.txt'); $this->sendRequest('GET', '/download&path='.$path_encoded); + $headers = $this->streamedResponse->headers; + $this->assertEquals($headers->get('content-disposition'), "attachment; filename=file; filename*=utf-8''john.txt"); + $this->assertEquals($headers->get('content-type'), 'application/octet-stream'); + $this->assertEquals($headers->get('content-transfer-encoding'), 'binary'); + + $this->assertOk(); + } + + public function testDownloadPDFFileHeaders() + { + $username = 'john@example.com'; + $this->signIn($username, 'john123'); + + mkdir(TEST_REPOSITORY.'/john'); + touch(TEST_REPOSITORY.'/john/john.pdf', $this->timestamp); + + $path_encoded = base64_encode('john.pdf'); + $this->sendRequest('GET', '/download&path='.$path_encoded); + + $headers = $this->streamedResponse->headers; + $this->assertEquals($headers->get('content-disposition'), "inline; filename=file; filename*=utf-8''john.pdf"); + $this->assertEquals($headers->get('content-type'), 'application/pdf'); + $this->assertEquals($headers->get('content-transfer-encoding'), 'binary'); + $this->assertOk(); } @@ -597,7 +621,7 @@ class FilesTest extends TestCase $this->sendRequest('POST', '/savecontent', [ 'name' => 'john.txt', - 'content' => 'lorem ipsum new' + 'content' => 'lorem ipsum new', ]); $this->assertOk(); @@ -622,7 +646,7 @@ class FilesTest extends TestCase $this->sendRequest('POST', '/savecontent', [ 'name' => 'john.txt', - 'content' => 'lorem ipsum new' + 'content' => 'lorem ipsum new', ]); $this->assertOk(); diff --git a/tests/backend/TestCase.php b/tests/backend/TestCase.php index 7ee7a20..6b627e3 100644 --- a/tests/backend/TestCase.php +++ b/tests/backend/TestCase.php @@ -15,6 +15,7 @@ use Filegator\Config\Config; use Filegator\Container\Container; use Filegator\Kernel\Request; use Filegator\Kernel\Response; +use Filegator\Kernel\StreamedResponse; use Filegator\Services\Session\Session; use PHPUnit\Framework\TestCase as BaseTestCase; use Symfony\Component\HttpFoundation\Session\Storage\MockFileSessionStorage; @@ -37,6 +38,8 @@ class TestCase extends BaseTestCase public $response; + public $streamedResponse; + public $previous_session = false; protected $auth = false; @@ -74,6 +77,7 @@ class TestCase extends BaseTestCase $app = $this->bootFreshApp(null, $fakeRequest, null, true); $this->response = $app->resolve(Response::class); + $this->streamedResponse = $app->resolve(StreamedResponse::class); return $app; }