Moved all logic to redirect to specific URLs when a 404 is found to the NotFoundHandler

This commit is contained in:
Alejandro Celaya
2019-11-02 18:33:26 +01:00
parent 24c3a3e84c
commit eeb5306883
5 changed files with 119 additions and 55 deletions
+1 -27
View File
@@ -25,20 +25,16 @@ class RedirectActionTest extends TestCase
private $urlShortener;
/** @var ObjectProphecy */
private $visitTracker;
/** @var Options\NotFoundRedirectOptions */
private $redirectOptions;
public function setUp(): void
{
$this->urlShortener = $this->prophesize(UrlShortener::class);
$this->visitTracker = $this->prophesize(VisitsTracker::class);
$this->redirectOptions = new Options\NotFoundRedirectOptions();
$this->action = new RedirectAction(
$this->urlShortener->reveal(),
$this->visitTracker->reveal(),
new Options\AppOptions(['disableTrackParam' => 'foobar']),
$this->redirectOptions
new Options\AppOptions(['disableTrackParam' => 'foobar'])
);
}
@@ -78,28 +74,6 @@ class RedirectActionTest extends TestCase
$handle->shouldHaveBeenCalledOnce();
}
/** @test */
public function redirectToCustomUrlIsReturnedIfConfiguredSoAndShortUrlIsNotFound(): void
{
$shortCode = 'abc123';
$shortCodeToUrl = $this->urlShortener->shortCodeToUrl($shortCode, '')->willThrow(
EntityDoesNotExistException::class
);
$handler = $this->prophesize(RequestHandlerInterface::class);
$handle = $handler->handle(Argument::any())->willReturn(new Response());
$this->redirectOptions->invalidShortUrl = 'https://shlink.io';
$request = (new ServerRequest())->withAttribute('shortCode', $shortCode);
$resp = $this->action->process($request, $handler->reveal());
$this->assertEquals(302, $resp->getStatusCode());
$this->assertEquals('https://shlink.io', $resp->getHeaderLine('Location'));
$shortCodeToUrl->shouldHaveBeenCalledOnce();
$handle->shouldNotHaveBeenCalled();
}
/** @test */
public function visitIsNotTrackedIfDisableParamIsProvided(): void
{
@@ -7,9 +7,16 @@ namespace ShlinkioTest\Shlink\Core\Response;
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Prophecy\Prophecy\ObjectProphecy;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Shlinkio\Shlink\Core\Options\NotFoundRedirectOptions;
use Shlinkio\Shlink\Core\Response\NotFoundHandler;
use Zend\Diactoros\Response;
use Zend\Diactoros\ServerRequest;
use Zend\Diactoros\ServerRequestFactory;
use Zend\Diactoros\Uri;
use Zend\Expressive\Router\Route;
use Zend\Expressive\Router\RouteResult;
use Zend\Expressive\Template\TemplateRendererInterface;
class NotFoundHandlerTest extends TestCase
@@ -18,11 +25,15 @@ class NotFoundHandlerTest extends TestCase
private $delegate;
/** @var ObjectProphecy */
private $renderer;
/** @var NotFoundRedirectOptions */
private $redirectOptions;
public function setUp(): void
{
$this->renderer = $this->prophesize(TemplateRendererInterface::class);
$this->delegate = new NotFoundHandler($this->renderer->reveal());
$this->redirectOptions = new NotFoundRedirectOptions();
$this->delegate = new NotFoundHandler($this->renderer->reveal(), $this->redirectOptions, '');
}
/**
@@ -47,4 +58,55 @@ class NotFoundHandlerTest extends TestCase
yield 'application/x-json' => [Response\JsonResponse::class, 'application/x-json', 0];
yield 'text/html' => [Response\HtmlResponse::class, 'text/html', 1];
}
/**
* @test
* @dataProvider provideRedirects
*/
public function expectedRedirectionIsReturnedDependingOnTheCase(
ServerRequestInterface $request,
string $expectedRedirectTo
): void {
$this->redirectOptions->invalidShortUrl = 'invalidShortUrl';
$this->redirectOptions->regular404 = 'regular404';
$this->redirectOptions->baseUrl = 'baseUrl';
$resp = $this->delegate->handle($request);
$this->assertInstanceOf(Response\RedirectResponse::class, $resp);
$this->assertEquals($expectedRedirectTo, $resp->getHeaderLine('Location'));
$this->renderer->render(Argument::cetera())->shouldNotHaveBeenCalled();
}
public function provideRedirects(): iterable
{
yield 'base URL with trailing slash' => [
ServerRequestFactory::fromGlobals()->withUri(new Uri('/')),
'baseUrl',
];
yield 'base URL without trailing slash' => [
ServerRequestFactory::fromGlobals()->withUri(new Uri('')),
'baseUrl',
];
yield 'regular 404' => [
ServerRequestFactory::fromGlobals()->withUri(new Uri('/foo/bar')),
'regular404',
];
yield 'invalid short URL' => [
ServerRequestFactory::fromGlobals()
->withAttribute(
RouteResult::class,
RouteResult::fromRoute(
new Route(
'',
$this->prophesize(MiddlewareInterface::class)->reveal(),
['GET'],
'long-url-redirect'
)
)
)
->withUri(new Uri('/abc123')),
'invalidShortUrl',
];
}
}