mirror of
https://github.com/shlinkio/shlink.git
synced 2025-02-25 18:45:27 -06:00
#867 Ensured status code config is honored when doing not-found redirects
This commit is contained in:
55
module/Core/test/Util/RedirectResponseHelperTest.php
Normal file
55
module/Core/test/Util/RedirectResponseHelperTest.php
Normal file
@@ -0,0 +1,55 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace ShlinkioTest\Shlink\Core\Util;
|
||||
|
||||
use Laminas\Diactoros\Response\RedirectResponse;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Shlinkio\Shlink\Core\Options\UrlShortenerOptions;
|
||||
use Shlinkio\Shlink\Core\Util\RedirectResponseHelper;
|
||||
|
||||
class RedirectResponseHelperTest extends TestCase
|
||||
{
|
||||
private RedirectResponseHelper $helper;
|
||||
private UrlShortenerOptions $shortenerOpts;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->shortenerOpts = new UrlShortenerOptions();
|
||||
$this->helper = new RedirectResponseHelper($this->shortenerOpts);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
* @dataProvider provideRedirectConfigs
|
||||
*/
|
||||
public function expectedStatusCodeAndCacheIsReturnedBasedOnConfig(
|
||||
int $configuredStatus,
|
||||
int $configuredLifetime,
|
||||
int $expectedStatus,
|
||||
?string $expectedCacheControl
|
||||
): void {
|
||||
$this->shortenerOpts->redirectStatusCode = $configuredStatus;
|
||||
$this->shortenerOpts->redirectCacheLifetime = $configuredLifetime;
|
||||
|
||||
$response = $this->helper->buildRedirectResponse('destination');
|
||||
|
||||
self::assertInstanceOf(RedirectResponse::class, $response);
|
||||
self::assertEquals($expectedStatus, $response->getStatusCode());
|
||||
self::assertTrue($response->hasHeader('Location'));
|
||||
self::assertEquals('destination', $response->getHeaderLine('Location'));
|
||||
self::assertEquals($expectedCacheControl !== null, $response->hasHeader('Cache-Control'));
|
||||
self::assertEquals($expectedCacheControl ?? '', $response->getHeaderLine('Cache-Control'));
|
||||
}
|
||||
|
||||
public function provideRedirectConfigs(): iterable
|
||||
{
|
||||
yield 'status 302' => [302, 20, 302, null];
|
||||
yield 'status over 302' => [400, 20, 302, null];
|
||||
yield 'status below 301' => [201, 20, 302, null];
|
||||
yield 'status 301 with valid expiration' => [301, 20, 301, 'private,max-age=20'];
|
||||
yield 'status 301 with zero expiration' => [301, 0, 301, 'private,max-age=30'];
|
||||
yield 'status 301 with negative expiration' => [301, -20, 301, 'private,max-age=30'];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user