|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace ShlinkioTest\Shlink\Common\Response; |
|
5
|
|
|
|
|
6
|
|
|
use PHPUnit\Framework\TestCase; |
|
7
|
|
|
use Shlinkio\Shlink\Common\Response\ResponseUtilsTrait; |
|
8
|
|
|
|
|
9
|
|
|
class ResponseUtilsTraitTest extends TestCase |
|
10
|
|
|
{ |
|
11
|
|
|
use ResponseUtilsTrait; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* @test |
|
15
|
|
|
* @dataProvider provideFiles |
|
16
|
|
|
*/ |
|
17
|
|
|
public function expectedBinaryResponsesAreGenerated( |
|
18
|
|
|
string $expectedType, |
|
19
|
|
|
string $expectedLength, |
|
20
|
|
|
string $path |
|
21
|
|
|
): void { |
|
22
|
|
|
$this->assertExpectedResponseForMethod('generateBinaryResponse', $expectedType, $expectedLength, $path); |
|
23
|
|
|
$this->assertExpectedResponseForMethod('generateImageResponse', $expectedType, $expectedLength, $path); |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
public function provideFiles(): iterable |
|
27
|
|
|
{ |
|
28
|
|
|
yield ['image/png', '2433', __DIR__ . '/../../test-resources/shlink-logo.png']; |
|
29
|
|
|
yield ['text/plain', '20', __DIR__ . '/../../test-resources/text-file.txt']; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
private function assertExpectedResponseForMethod( |
|
33
|
|
|
string $method, |
|
34
|
|
|
string $expectedType, |
|
35
|
|
|
string $expectedLength, |
|
36
|
|
|
string $path |
|
37
|
|
|
): void { |
|
38
|
|
|
$resp = $this->{$method}($path); |
|
39
|
|
|
|
|
40
|
|
|
$this->assertStringContainsString($expectedType, $resp->getHeaderLine('Content-Type')); |
|
41
|
|
|
$this->assertStringContainsString($expectedLength, $resp->getHeaderLine('Content-Length')); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** @test */ |
|
45
|
|
|
public function binaryResponsesIncludeExtraHeaders(): void |
|
46
|
|
|
{ |
|
47
|
|
|
$resp = $this->generateBinaryResponse(__DIR__ . '/../../test-resources/shlink-logo.png', [ |
|
48
|
|
|
'foo' => 'bar', |
|
49
|
|
|
'baz' => 'foo', |
|
50
|
|
|
]); |
|
51
|
|
|
$headers = $resp->getHeaders(); |
|
52
|
|
|
|
|
53
|
|
|
$this->assertArrayHasKey('foo', $headers); |
|
54
|
|
|
$this->assertArrayHasKey('baz', $headers); |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
|