1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace ShlinkioTest\Shlink\Core\Util; |
6
|
|
|
|
7
|
|
|
use Laminas\Diactoros\Response\RedirectResponse; |
8
|
|
|
use PHPUnit\Framework\TestCase; |
9
|
|
|
use Shlinkio\Shlink\Core\Options\UrlShortenerOptions; |
10
|
|
|
use Shlinkio\Shlink\Core\Util\RedirectResponseHelper; |
11
|
|
|
|
12
|
|
|
class RedirectResponseHelperTest extends TestCase |
13
|
|
|
{ |
14
|
|
|
private RedirectResponseHelper $helper; |
15
|
|
|
private UrlShortenerOptions $shortenerOpts; |
16
|
|
|
|
17
|
|
|
protected function setUp(): void |
18
|
|
|
{ |
19
|
|
|
$this->shortenerOpts = new UrlShortenerOptions(); |
20
|
|
|
$this->helper = new RedirectResponseHelper($this->shortenerOpts); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @test |
25
|
|
|
* @dataProvider provideRedirectConfigs |
26
|
|
|
*/ |
27
|
|
|
public function expectedStatusCodeAndCacheIsReturnedBasedOnConfig( |
28
|
|
|
int $configuredStatus, |
29
|
|
|
int $configuredLifetime, |
30
|
|
|
int $expectedStatus, |
31
|
|
|
?string $expectedCacheControl |
32
|
|
|
): void { |
33
|
|
|
$this->shortenerOpts->redirectStatusCode = $configuredStatus; |
|
|
|
|
34
|
|
|
$this->shortenerOpts->redirectCacheLifetime = $configuredLifetime; |
|
|
|
|
35
|
|
|
|
36
|
|
|
$response = $this->helper->buildRedirectResponse('destination'); |
37
|
|
|
|
38
|
|
|
self::assertInstanceOf(RedirectResponse::class, $response); |
39
|
|
|
self::assertEquals($expectedStatus, $response->getStatusCode()); |
40
|
|
|
self::assertTrue($response->hasHeader('Location')); |
41
|
|
|
self::assertEquals('destination', $response->getHeaderLine('Location')); |
42
|
|
|
self::assertEquals($expectedCacheControl !== null, $response->hasHeader('Cache-Control')); |
43
|
|
|
self::assertEquals($expectedCacheControl ?? '', $response->getHeaderLine('Cache-Control')); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function provideRedirectConfigs(): iterable |
47
|
|
|
{ |
48
|
|
|
yield 'status 302' => [302, 20, 302, null]; |
49
|
|
|
yield 'status over 302' => [400, 20, 302, null]; |
50
|
|
|
yield 'status below 301' => [201, 20, 302, null]; |
51
|
|
|
yield 'status 301 with valid expiration' => [301, 20, 301, 'private,max-age=20']; |
52
|
|
|
yield 'status 301 with zero expiration' => [301, 0, 301, 'private,max-age=30']; |
53
|
|
|
yield 'status 301 with negative expiration' => [301, -20, 301, 'private,max-age=30']; |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|