mirror of
https://github.com/shlinkio/shlink.git
synced 2025-03-14 04:00:57 +03:00
Added test covering how redirects config works
This commit is contained in:
parent
83cc11030d
commit
0bea843e7f
3 changed files with 49 additions and 5 deletions
|
@ -13,6 +13,7 @@ use Psr\Log\LoggerInterface;
|
|||
use Shlinkio\Shlink\Core\Options;
|
||||
use Shlinkio\Shlink\Core\Service\ShortUrl\ShortUrlResolverInterface;
|
||||
use Shlinkio\Shlink\Core\Service\VisitsTrackerInterface;
|
||||
|
||||
use function sprintf;
|
||||
|
||||
class RedirectAction extends AbstractTrackingAction implements StatusCodeInterface
|
||||
|
|
|
@ -8,6 +8,7 @@ use Laminas\Stdlib\AbstractOptions;
|
|||
|
||||
use function Functional\contains;
|
||||
|
||||
use const Shlinkio\Shlink\Core\DEFAULT_REDIRECT_CACHE_LIFETIME;
|
||||
use const Shlinkio\Shlink\Core\DEFAULT_REDIRECT_STATUS_CODE;
|
||||
|
||||
class UrlShortenerOptions extends AbstractOptions
|
||||
|
@ -16,7 +17,7 @@ class UrlShortenerOptions extends AbstractOptions
|
|||
|
||||
private bool $validateUrl = true;
|
||||
private int $redirectStatusCode = DEFAULT_REDIRECT_STATUS_CODE;
|
||||
private int $redirectCacheLifetime = 30;
|
||||
private int $redirectCacheLifetime = DEFAULT_REDIRECT_CACHE_LIFETIME;
|
||||
|
||||
public function isUrlValidationEnabled(): bool
|
||||
{
|
||||
|
@ -40,7 +41,7 @@ class UrlShortenerOptions extends AbstractOptions
|
|||
|
||||
private function normalizeRedirectStatusCode(int $statusCode): int
|
||||
{
|
||||
return contains([301, 302], $statusCode) ? $statusCode : 302;
|
||||
return contains([301, 302], $statusCode) ? $statusCode : DEFAULT_REDIRECT_STATUS_CODE;
|
||||
}
|
||||
|
||||
public function redirectCacheLifetime(): int
|
||||
|
@ -50,6 +51,8 @@ class UrlShortenerOptions extends AbstractOptions
|
|||
|
||||
protected function setRedirectCacheLifetime(int $redirectCacheLifetime): void
|
||||
{
|
||||
$this->redirectCacheLifetime = $redirectCacheLifetime;
|
||||
$this->redirectCacheLifetime = $redirectCacheLifetime > 0
|
||||
? $redirectCacheLifetime
|
||||
: DEFAULT_REDIRECT_CACHE_LIFETIME;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -27,16 +27,19 @@ class RedirectActionTest extends TestCase
|
|||
private RedirectAction $action;
|
||||
private ObjectProphecy $urlResolver;
|
||||
private ObjectProphecy $visitTracker;
|
||||
private Options\UrlShortenerOptions $shortenerOpts;
|
||||
|
||||
public function setUp(): void
|
||||
{
|
||||
$this->urlResolver = $this->prophesize(ShortUrlResolverInterface::class);
|
||||
$this->visitTracker = $this->prophesize(VisitsTrackerInterface::class);
|
||||
$this->shortenerOpts = new Options\UrlShortenerOptions();
|
||||
|
||||
$this->action = new RedirectAction(
|
||||
$this->urlResolver->reveal(),
|
||||
$this->visitTracker->reveal(),
|
||||
new Options\AppOptions(['disableTrackParam' => 'foobar']),
|
||||
$this->shortenerOpts,
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -48,8 +51,9 @@ class RedirectActionTest extends TestCase
|
|||
{
|
||||
$shortCode = 'abc123';
|
||||
$shortUrl = new ShortUrl('http://domain.com/foo/bar?some=thing');
|
||||
$shortCodeToUrl = $this->urlResolver->resolveEnabledShortUrl(new ShortUrlIdentifier($shortCode, ''))
|
||||
->willReturn($shortUrl);
|
||||
$shortCodeToUrl = $this->urlResolver->resolveEnabledShortUrl(
|
||||
new ShortUrlIdentifier($shortCode, ''),
|
||||
)->willReturn($shortUrl);
|
||||
$track = $this->visitTracker->track(Argument::cetera())->will(function (): void {
|
||||
});
|
||||
|
||||
|
@ -110,4 +114,40 @@ class RedirectActionTest extends TestCase
|
|||
|
||||
$track->shouldNotHaveBeenCalled();
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
* @dataProvider provideRedirectConfigs
|
||||
*/
|
||||
public function expectedStatusCodeAndCacheIsReturnedBasedOnConfig(
|
||||
int $configuredStatus,
|
||||
int $configuredLifetime,
|
||||
int $expectedStatus,
|
||||
?string $expectedCacheControl
|
||||
): void {
|
||||
$this->shortenerOpts->redirectStatusCode = $configuredStatus;
|
||||
$this->shortenerOpts->redirectCacheLifetime = $configuredLifetime;
|
||||
|
||||
$shortUrl = new ShortUrl('http://domain.com/foo/bar');
|
||||
$shortCode = $shortUrl->getShortCode();
|
||||
$this->urlResolver->resolveEnabledShortUrl(Argument::cetera())->willReturn($shortUrl);
|
||||
|
||||
$request = (new ServerRequest())->withAttribute('shortCode', $shortCode);
|
||||
$response = $this->action->process($request, $this->prophesize(RequestHandlerInterface::class)->reveal());
|
||||
|
||||
$this->assertInstanceOf(Response\RedirectResponse::class, $response);
|
||||
$this->assertEquals($expectedStatus, $response->getStatusCode());
|
||||
$this->assertEquals($response->hasHeader('Cache-Control'), $expectedCacheControl !== null);
|
||||
$this->assertEquals($response->getHeaderLine('Cache-Control'), $expectedCacheControl ?? '');
|
||||
}
|
||||
|
||||
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'];
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue