Added support to define differnet not-found redirects per domain

This commit is contained in:
Alejandro Celaya
2021-07-21 09:28:21 +02:00
parent 2054784a4a
commit 4d48482d1e
14 changed files with 398 additions and 107 deletions

View File

@@ -6,21 +6,18 @@ namespace ShlinkioTest\Shlink\Core\ErrorHandler;
use Laminas\Diactoros\Response;
use Laminas\Diactoros\ServerRequestFactory;
use Laminas\Diactoros\Uri;
use Mezzio\Router\Route;
use Mezzio\Router\RouteResult;
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Prophecy\PhpUnit\ProphecyTrait;
use Prophecy\Prophecy\ObjectProphecy;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Shlinkio\Shlink\Core\Action\RedirectAction;
use Shlinkio\Shlink\Core\Config\NotFoundRedirectResolverInterface;
use Shlinkio\Shlink\Core\Domain\DomainServiceInterface;
use Shlinkio\Shlink\Core\Entity\Domain;
use Shlinkio\Shlink\Core\ErrorHandler\Model\NotFoundType;
use Shlinkio\Shlink\Core\ErrorHandler\NotFoundRedirectHandler;
use Shlinkio\Shlink\Core\Options\NotFoundRedirectOptions;
use Shlinkio\Shlink\Core\Util\RedirectResponseHelperInterface;
class NotFoundRedirectHandlerTest extends TestCase
{
@@ -28,93 +25,103 @@ class NotFoundRedirectHandlerTest extends TestCase
private NotFoundRedirectHandler $middleware;
private NotFoundRedirectOptions $redirectOptions;
private ObjectProphecy $helper;
private ObjectProphecy $resolver;
private ObjectProphecy $domainService;
private ObjectProphecy $next;
private ServerRequestInterface $req;
public function setUp(): void
{
$this->redirectOptions = new NotFoundRedirectOptions();
$this->helper = $this->prophesize(RedirectResponseHelperInterface::class);
$this->middleware = new NotFoundRedirectHandler($this->redirectOptions, $this->helper->reveal());
$this->resolver = $this->prophesize(NotFoundRedirectResolverInterface::class);
$this->domainService = $this->prophesize(DomainServiceInterface::class);
$this->middleware = new NotFoundRedirectHandler(
$this->redirectOptions,
$this->resolver->reveal(),
$this->domainService->reveal(),
);
$this->next = $this->prophesize(RequestHandlerInterface::class);
$this->req = ServerRequestFactory::fromGlobals()->withAttribute(
NotFoundType::class,
$this->prophesize(NotFoundType::class)->reveal(),
);
}
/**
* @test
* @dataProvider provideRedirects
* @dataProvider provideNonRedirectScenarios
*/
public function expectedRedirectionIsReturnedDependingOnTheCase(
ServerRequestInterface $request,
string $expectedRedirectTo,
): void {
$this->redirectOptions->invalidShortUrl = 'invalidShortUrl';
$this->redirectOptions->regular404 = 'regular404';
$this->redirectOptions->baseUrl = 'baseUrl';
public function nextIsCalledWhenNoRedirectIsResolved(callable $setUp): void
{
$expectedResp = new Response();
$buildResp = $this->helper->buildRedirectResponse($expectedRedirectTo)->willReturn($expectedResp);
$next = $this->prophesize(RequestHandlerInterface::class);
$handle = $next->handle($request)->willReturn(new Response());
$setUp($this->domainService, $this->resolver);
$handle = $this->next->handle($this->req)->willReturn($expectedResp);
$resp = $this->middleware->process($request, $next->reveal());
$result = $this->middleware->process($this->req, $this->next->reveal());
self::assertSame($expectedResp, $resp);
$buildResp->shouldHaveBeenCalledOnce();
$handle->shouldNotHaveBeenCalled();
}
public function provideRedirects(): iterable
{
yield 'base URL with trailing slash' => [
$this->withNotFoundType(ServerRequestFactory::fromGlobals()->withUri(new Uri('/'))),
'baseUrl',
];
yield 'base URL without trailing slash' => [
$this->withNotFoundType(ServerRequestFactory::fromGlobals()->withUri(new Uri(''))),
'baseUrl',
];
yield 'regular 404' => [
$this->withNotFoundType(ServerRequestFactory::fromGlobals()->withUri(new Uri('/foo/bar'))),
'regular404',
];
yield 'invalid short URL' => [
$this->withNotFoundType(ServerRequestFactory::fromGlobals()
->withAttribute(
RouteResult::class,
RouteResult::fromRoute(
new Route(
'',
$this->prophesize(MiddlewareInterface::class)->reveal(),
['GET'],
RedirectAction::class,
),
),
)
->withUri(new Uri('/abc123'))),
'invalidShortUrl',
];
}
/** @test */
public function nextMiddlewareIsInvokedWhenNotRedirectNeedsToOccur(): void
{
$req = $this->withNotFoundType(ServerRequestFactory::fromGlobals());
$resp = new Response();
$buildResp = $this->helper->buildRedirectResponse(Argument::cetera());
$next = $this->prophesize(RequestHandlerInterface::class);
$handle = $next->handle($req)->willReturn($resp);
$result = $this->middleware->process($req, $next->reveal());
self::assertSame($resp, $result);
$buildResp->shouldNotHaveBeenCalled();
self::assertSame($expectedResp, $result);
$handle->shouldHaveBeenCalledOnce();
}
private function withNotFoundType(ServerRequestInterface $req): ServerRequestInterface
public function provideNonRedirectScenarios(): iterable
{
$type = NotFoundType::fromRequest($req, '');
return $req->withAttribute(NotFoundType::class, $type);
yield 'no domain' => [function (ObjectProphecy $domainService, ObjectProphecy $resolver): void {
$domainService->findByAuthority(Argument::cetera())
->willReturn(null)
->shouldBeCalledOnce();
$resolver->resolveRedirectResponse(Argument::cetera())
->willReturn(null)
->shouldBeCalledOnce();
}];
yield 'non-redirecting domain' => [function (ObjectProphecy $domainService, ObjectProphecy $resolver): void {
$domainService->findByAuthority(Argument::cetera())
->willReturn(new Domain(''))
->shouldBeCalledOnce();
$resolver->resolveRedirectResponse(Argument::cetera())
->willReturn(null)
->shouldBeCalledTimes(2);
}];
}
/** @test */
public function globalRedirectIsUsedIfDomainRedirectIsNotFound(): void
{
$expectedResp = new Response();
$findDomain = $this->domainService->findByAuthority(Argument::cetera())->willReturn(null);
$resolveRedirect = $this->resolver->resolveRedirectResponse(
Argument::type(NotFoundType::class),
$this->redirectOptions,
)->willReturn($expectedResp);
$result = $this->middleware->process($this->req, $this->next->reveal());
self::assertSame($expectedResp, $result);
$findDomain->shouldHaveBeenCalledOnce();
$resolveRedirect->shouldHaveBeenCalledOnce();
$this->next->handle(Argument::cetera())->shouldNotHaveBeenCalled();
}
/** @test */
public function domainRedirectIsUsedIfFound(): void
{
$expectedResp = new Response();
$domain = new Domain('');
$findDomain = $this->domainService->findByAuthority(Argument::cetera())->willReturn($domain);
$resolveRedirect = $this->resolver->resolveRedirectResponse(
Argument::type(NotFoundType::class),
$domain,
)->willReturn($expectedResp);
$result = $this->middleware->process($this->req, $this->next->reveal());
self::assertSame($expectedResp, $result);
$findDomain->shouldHaveBeenCalledOnce();
$resolveRedirect->shouldHaveBeenCalledOnce();
$this->next->handle(Argument::cetera())->shouldNotHaveBeenCalled();
}
}