Tests for download headers

This commit is contained in:
Milos Stojanovic
2020-03-08 17:20:54 +01:00
parent 2cfa7d7615
commit 1fa1c38212
2 changed files with 32 additions and 4 deletions

View File

@@ -10,8 +10,8 @@
namespace Tests\Feature; namespace Tests\Feature;
use Tests\TestCase;
use Exception; use Exception;
use Tests\TestCase;
/** /**
* @internal * @internal
@@ -148,7 +148,7 @@ class FilesTest extends TestCase
$this->assertOk(); $this->assertOk();
} }
public function testDownloadFile() public function testDownloadFileHeaders()
{ {
$username = 'john@example.com'; $username = 'john@example.com';
$this->signIn($username, 'john123'); $this->signIn($username, 'john123');
@@ -159,6 +159,30 @@ class FilesTest extends TestCase
$path_encoded = base64_encode('john.txt'); $path_encoded = base64_encode('john.txt');
$this->sendRequest('GET', '/download&path='.$path_encoded); $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(); $this->assertOk();
} }
@@ -597,7 +621,7 @@ class FilesTest extends TestCase
$this->sendRequest('POST', '/savecontent', [ $this->sendRequest('POST', '/savecontent', [
'name' => 'john.txt', 'name' => 'john.txt',
'content' => 'lorem ipsum new' 'content' => 'lorem ipsum new',
]); ]);
$this->assertOk(); $this->assertOk();
@@ -622,7 +646,7 @@ class FilesTest extends TestCase
$this->sendRequest('POST', '/savecontent', [ $this->sendRequest('POST', '/savecontent', [
'name' => 'john.txt', 'name' => 'john.txt',
'content' => 'lorem ipsum new' 'content' => 'lorem ipsum new',
]); ]);
$this->assertOk(); $this->assertOk();

View File

@@ -15,6 +15,7 @@ use Filegator\Config\Config;
use Filegator\Container\Container; use Filegator\Container\Container;
use Filegator\Kernel\Request; use Filegator\Kernel\Request;
use Filegator\Kernel\Response; use Filegator\Kernel\Response;
use Filegator\Kernel\StreamedResponse;
use Filegator\Services\Session\Session; use Filegator\Services\Session\Session;
use PHPUnit\Framework\TestCase as BaseTestCase; use PHPUnit\Framework\TestCase as BaseTestCase;
use Symfony\Component\HttpFoundation\Session\Storage\MockFileSessionStorage; use Symfony\Component\HttpFoundation\Session\Storage\MockFileSessionStorage;
@@ -37,6 +38,8 @@ class TestCase extends BaseTestCase
public $response; public $response;
public $streamedResponse;
public $previous_session = false; public $previous_session = false;
protected $auth = false; protected $auth = false;
@@ -74,6 +77,7 @@ class TestCase extends BaseTestCase
$app = $this->bootFreshApp(null, $fakeRequest, null, true); $app = $this->bootFreshApp(null, $fakeRequest, null, true);
$this->response = $app->resolve(Response::class); $this->response = $app->resolve(Response::class);
$this->streamedResponse = $app->resolve(StreamedResponse::class);
return $app; return $app;
} }