mirror of
https://github.com/shlinkio/shlink.git
synced 2025-02-17 15:59:56 +03:00
Wrapped logic to track requests to a new RequestTracker service
This commit is contained in:
parent
32f7b4fbf6
commit
050f83e3bb
10 changed files with 194 additions and 163 deletions
|
@ -37,6 +37,7 @@ return [
|
|||
Domain\DomainService::class => ConfigAbstractFactory::class,
|
||||
|
||||
Visit\VisitsTracker::class => ConfigAbstractFactory::class,
|
||||
Visit\RequestTracker::class => ConfigAbstractFactory::class,
|
||||
Visit\VisitLocator::class => ConfigAbstractFactory::class,
|
||||
Visit\VisitsStatsHelper::class => ConfigAbstractFactory::class,
|
||||
Visit\Transformer\OrphanVisitDataTransformer::class => InvokableFactory::class,
|
||||
|
@ -71,7 +72,7 @@ return [
|
|||
|
||||
ConfigAbstractFactory::class => [
|
||||
ErrorHandler\NotFoundTypeResolverMiddleware::class => ['config.router.base_path'],
|
||||
ErrorHandler\NotFoundTrackerMiddleware::class => [Visit\VisitsTracker::class],
|
||||
ErrorHandler\NotFoundTrackerMiddleware::class => [Visit\RequestTracker::class],
|
||||
ErrorHandler\NotFoundRedirectHandler::class => [
|
||||
NotFoundRedirectOptions::class,
|
||||
Util\RedirectResponseHelper::class,
|
||||
|
@ -94,6 +95,7 @@ return [
|
|||
EventDispatcherInterface::class,
|
||||
Options\TrackingOptions::class,
|
||||
],
|
||||
Visit\RequestTracker::class => [Visit\VisitsTracker::class, Options\TrackingOptions::class],
|
||||
Service\ShortUrlService::class => [
|
||||
'em',
|
||||
Service\ShortUrl\ShortUrlResolver::class,
|
||||
|
@ -118,16 +120,11 @@ return [
|
|||
|
||||
Action\RedirectAction::class => [
|
||||
Service\ShortUrl\ShortUrlResolver::class,
|
||||
Visit\VisitsTracker::class,
|
||||
Options\TrackingOptions::class,
|
||||
Visit\RequestTracker::class,
|
||||
ShortUrl\Helper\ShortUrlRedirectionBuilder::class,
|
||||
Util\RedirectResponseHelper::class,
|
||||
],
|
||||
Action\PixelAction::class => [
|
||||
Service\ShortUrl\ShortUrlResolver::class,
|
||||
Visit\VisitsTracker::class,
|
||||
Options\TrackingOptions::class,
|
||||
],
|
||||
Action\PixelAction::class => [Service\ShortUrl\ShortUrlResolver::class, Visit\RequestTracker::class],
|
||||
Action\QrCodeAction::class => [
|
||||
Service\ShortUrl\ShortUrlResolver::class,
|
||||
ShortUrl\Helper\ShortUrlStringifier::class,
|
||||
|
@ -142,7 +139,7 @@ return [
|
|||
ShortUrl\Transformer\ShortUrlDataTransformer::class => [ShortUrl\Helper\ShortUrlStringifier::class],
|
||||
ShortUrl\Middleware\ExtraPathRedirectMiddleware::class => [
|
||||
Service\ShortUrl\ShortUrlResolver::class,
|
||||
Visit\VisitsTracker::class,
|
||||
Visit\RequestTracker::class,
|
||||
ShortUrl\Helper\ShortUrlRedirectionBuilder::class,
|
||||
Util\RedirectResponseHelper::class,
|
||||
Options\UrlShortenerOptions::class,
|
||||
|
|
|
@ -5,7 +5,6 @@ declare(strict_types=1);
|
|||
namespace Shlinkio\Shlink\Core\Action;
|
||||
|
||||
use Fig\Http\Message\RequestMethodInterface;
|
||||
use Mezzio\Router\Middleware\ImplicitHeadMiddleware;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\ResponseInterface as Response;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
|
@ -14,33 +13,24 @@ use Psr\Http\Server\RequestHandlerInterface;
|
|||
use Shlinkio\Shlink\Core\Entity\ShortUrl;
|
||||
use Shlinkio\Shlink\Core\Exception\ShortUrlNotFoundException;
|
||||
use Shlinkio\Shlink\Core\Model\ShortUrlIdentifier;
|
||||
use Shlinkio\Shlink\Core\Model\Visitor;
|
||||
use Shlinkio\Shlink\Core\Options\TrackingOptions;
|
||||
use Shlinkio\Shlink\Core\Service\ShortUrl\ShortUrlResolverInterface;
|
||||
use Shlinkio\Shlink\Core\Visit\VisitsTrackerInterface;
|
||||
|
||||
use function array_key_exists;
|
||||
use Shlinkio\Shlink\Core\Visit\RequestTrackerInterface;
|
||||
|
||||
abstract class AbstractTrackingAction implements MiddlewareInterface, RequestMethodInterface
|
||||
{
|
||||
public function __construct(
|
||||
private ShortUrlResolverInterface $urlResolver,
|
||||
private VisitsTrackerInterface $visitTracker,
|
||||
private TrackingOptions $trackingOptions,
|
||||
private RequestTrackerInterface $requestTracker,
|
||||
) {
|
||||
}
|
||||
|
||||
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
|
||||
{
|
||||
$identifier = ShortUrlIdentifier::fromRedirectRequest($request);
|
||||
$query = $request->getQueryParams();
|
||||
|
||||
try {
|
||||
$shortUrl = $this->urlResolver->resolveEnabledShortUrl($identifier);
|
||||
|
||||
if ($this->shouldTrackRequest($request, $query)) {
|
||||
$this->visitTracker->track($shortUrl, Visitor::fromRequest($request));
|
||||
}
|
||||
$this->requestTracker->trackIfApplicable($shortUrl, $request);
|
||||
|
||||
return $this->createSuccessResp($shortUrl, $request);
|
||||
} catch (ShortUrlNotFoundException) {
|
||||
|
@ -48,17 +38,6 @@ abstract class AbstractTrackingAction implements MiddlewareInterface, RequestMet
|
|||
}
|
||||
}
|
||||
|
||||
private function shouldTrackRequest(ServerRequestInterface $request, array $query): bool
|
||||
{
|
||||
$disableTrackParam = $this->trackingOptions->getDisableTrackParam();
|
||||
$forwardedMethod = $request->getAttribute(ImplicitHeadMiddleware::FORWARDED_HTTP_METHOD_ATTRIBUTE);
|
||||
if ($forwardedMethod === self::METHOD_HEAD) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $disableTrackParam === null || ! array_key_exists($disableTrackParam, $query);
|
||||
}
|
||||
|
||||
abstract protected function createSuccessResp(
|
||||
ShortUrl $shortUrl,
|
||||
ServerRequestInterface $request,
|
||||
|
|
|
@ -8,22 +8,20 @@ use Fig\Http\Message\StatusCodeInterface;
|
|||
use Psr\Http\Message\ResponseInterface as Response;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Shlinkio\Shlink\Core\Entity\ShortUrl;
|
||||
use Shlinkio\Shlink\Core\Options;
|
||||
use Shlinkio\Shlink\Core\Service\ShortUrl\ShortUrlResolverInterface;
|
||||
use Shlinkio\Shlink\Core\ShortUrl\Helper\ShortUrlRedirectionBuilderInterface;
|
||||
use Shlinkio\Shlink\Core\Util\RedirectResponseHelperInterface;
|
||||
use Shlinkio\Shlink\Core\Visit\VisitsTrackerInterface;
|
||||
use Shlinkio\Shlink\Core\Visit\RequestTrackerInterface;
|
||||
|
||||
class RedirectAction extends AbstractTrackingAction implements StatusCodeInterface
|
||||
{
|
||||
public function __construct(
|
||||
ShortUrlResolverInterface $urlResolver,
|
||||
VisitsTrackerInterface $visitTracker,
|
||||
Options\TrackingOptions $trackingOptions,
|
||||
RequestTrackerInterface $requestTracker,
|
||||
private ShortUrlRedirectionBuilderInterface $redirectionBuilder,
|
||||
private RedirectResponseHelperInterface $redirectResponseHelper,
|
||||
) {
|
||||
parent::__construct($urlResolver, $visitTracker, $trackingOptions);
|
||||
parent::__construct($urlResolver, $requestTracker);
|
||||
}
|
||||
|
||||
protected function createSuccessResp(ShortUrl $shortUrl, ServerRequestInterface $request): Response
|
||||
|
|
|
@ -8,30 +8,17 @@ use Psr\Http\Message\ResponseInterface;
|
|||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Psr\Http\Server\MiddlewareInterface;
|
||||
use Psr\Http\Server\RequestHandlerInterface;
|
||||
use Shlinkio\Shlink\Core\ErrorHandler\Model\NotFoundType;
|
||||
use Shlinkio\Shlink\Core\Model\Visitor;
|
||||
use Shlinkio\Shlink\Core\Visit\VisitsTrackerInterface;
|
||||
use Shlinkio\Shlink\Core\Visit\RequestTrackerInterface;
|
||||
|
||||
class NotFoundTrackerMiddleware implements MiddlewareInterface
|
||||
{
|
||||
public function __construct(private VisitsTrackerInterface $visitsTracker)
|
||||
public function __construct(private RequestTrackerInterface $requestTracker)
|
||||
{
|
||||
}
|
||||
|
||||
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
|
||||
{
|
||||
/** @var NotFoundType $notFoundType */
|
||||
$notFoundType = $request->getAttribute(NotFoundType::class);
|
||||
$visitor = Visitor::fromRequest($request);
|
||||
|
||||
if ($notFoundType->isBaseUrl()) {
|
||||
$this->visitsTracker->trackBaseUrlVisit($visitor);
|
||||
} elseif ($notFoundType->isRegularNotFound()) {
|
||||
$this->visitsTracker->trackRegularNotFoundVisit($visitor);
|
||||
} elseif ($notFoundType->isInvalidShortUrl()) {
|
||||
$this->visitsTracker->trackInvalidShortUrlVisit($visitor);
|
||||
}
|
||||
|
||||
$this->requestTracker->trackNotFoundIfApplicable($request);
|
||||
return $handler->handle($request);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -16,7 +16,7 @@ use Shlinkio\Shlink\Core\Options\UrlShortenerOptions;
|
|||
use Shlinkio\Shlink\Core\Service\ShortUrl\ShortUrlResolverInterface;
|
||||
use Shlinkio\Shlink\Core\ShortUrl\Helper\ShortUrlRedirectionBuilderInterface;
|
||||
use Shlinkio\Shlink\Core\Util\RedirectResponseHelperInterface;
|
||||
use Shlinkio\Shlink\Core\Visit\VisitsTrackerInterface;
|
||||
use Shlinkio\Shlink\Core\Visit\RequestTrackerInterface;
|
||||
|
||||
use function array_pad;
|
||||
use function explode;
|
||||
|
@ -27,7 +27,7 @@ class ExtraPathRedirectMiddleware implements MiddlewareInterface
|
|||
{
|
||||
public function __construct(
|
||||
private ShortUrlResolverInterface $resolver,
|
||||
private VisitsTrackerInterface $visitTracker,
|
||||
private RequestTrackerInterface $requestTracker,
|
||||
private ShortUrlRedirectionBuilderInterface $redirectionBuilder,
|
||||
private RedirectResponseHelperInterface $redirectResponseHelper,
|
||||
private UrlShortenerOptions $urlShortenerOptions,
|
||||
|
@ -51,8 +51,7 @@ class ExtraPathRedirectMiddleware implements MiddlewareInterface
|
|||
|
||||
try {
|
||||
$shortUrl = $this->resolver->resolveEnabledShortUrl($identifier);
|
||||
|
||||
// TODO Track visit
|
||||
$this->requestTracker->trackIfApplicable($shortUrl, $request);
|
||||
|
||||
$longUrl = $this->redirectionBuilder->buildShortUrlRedirect($shortUrl, $query, $extraPath);
|
||||
return $this->redirectResponseHelper->buildRedirectResponse($longUrl);
|
||||
|
|
60
module/Core/src/Visit/RequestTracker.php
Normal file
60
module/Core/src/Visit/RequestTracker.php
Normal file
|
@ -0,0 +1,60 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Shlinkio\Shlink\Core\Visit;
|
||||
|
||||
use Fig\Http\Message\RequestMethodInterface;
|
||||
use Mezzio\Router\Middleware\ImplicitHeadMiddleware;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Shlinkio\Shlink\Core\Entity\ShortUrl;
|
||||
use Shlinkio\Shlink\Core\ErrorHandler\Model\NotFoundType;
|
||||
use Shlinkio\Shlink\Core\Model\Visitor;
|
||||
use Shlinkio\Shlink\Core\Options\TrackingOptions;
|
||||
|
||||
use function array_key_exists;
|
||||
|
||||
class RequestTracker implements RequestTrackerInterface, RequestMethodInterface
|
||||
{
|
||||
public function __construct(private VisitsTrackerInterface $visitsTracker, private TrackingOptions $trackingOptions)
|
||||
{
|
||||
}
|
||||
|
||||
public function trackIfApplicable(ShortUrl $shortUrl, ServerRequestInterface $request): void
|
||||
{
|
||||
if ($this->shouldTrackRequest($request)) {
|
||||
$this->visitsTracker->track($shortUrl, Visitor::fromRequest($request));
|
||||
}
|
||||
}
|
||||
|
||||
public function trackNotFoundIfApplicable(ServerRequestInterface $request): void
|
||||
{
|
||||
if (! $this->shouldTrackRequest($request)) {
|
||||
return;
|
||||
}
|
||||
|
||||
/** @var NotFoundType|null $notFoundType */
|
||||
$notFoundType = $request->getAttribute(NotFoundType::class);
|
||||
$visitor = Visitor::fromRequest($request);
|
||||
|
||||
if ($notFoundType?->isBaseUrl()) {
|
||||
$this->visitsTracker->trackBaseUrlVisit($visitor);
|
||||
} elseif ($notFoundType?->isRegularNotFound()) {
|
||||
$this->visitsTracker->trackRegularNotFoundVisit($visitor);
|
||||
} elseif ($notFoundType?->isInvalidShortUrl()) {
|
||||
$this->visitsTracker->trackInvalidShortUrlVisit($visitor);
|
||||
}
|
||||
}
|
||||
|
||||
private function shouldTrackRequest(ServerRequestInterface $request): bool
|
||||
{
|
||||
$query = $request->getQueryParams();
|
||||
$disableTrackParam = $this->trackingOptions->getDisableTrackParam();
|
||||
$forwardedMethod = $request->getAttribute(ImplicitHeadMiddleware::FORWARDED_HTTP_METHOD_ATTRIBUTE);
|
||||
if ($forwardedMethod === self::METHOD_HEAD) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $disableTrackParam === null || ! array_key_exists($disableTrackParam, $query);
|
||||
}
|
||||
}
|
15
module/Core/src/Visit/RequestTrackerInterface.php
Normal file
15
module/Core/src/Visit/RequestTrackerInterface.php
Normal file
|
@ -0,0 +1,15 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Shlinkio\Shlink\Core\Visit;
|
||||
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Shlinkio\Shlink\Core\Entity\ShortUrl;
|
||||
|
||||
interface RequestTrackerInterface
|
||||
{
|
||||
public function trackIfApplicable(ShortUrl $shortUrl, ServerRequestInterface $request): void;
|
||||
|
||||
public function trackNotFoundIfApplicable(ServerRequestInterface $request): void;
|
||||
}
|
|
@ -14,9 +14,8 @@ use Shlinkio\Shlink\Common\Response\PixelResponse;
|
|||
use Shlinkio\Shlink\Core\Action\PixelAction;
|
||||
use Shlinkio\Shlink\Core\Entity\ShortUrl;
|
||||
use Shlinkio\Shlink\Core\Model\ShortUrlIdentifier;
|
||||
use Shlinkio\Shlink\Core\Options\TrackingOptions;
|
||||
use Shlinkio\Shlink\Core\Service\ShortUrl\ShortUrlResolverInterface;
|
||||
use Shlinkio\Shlink\Core\Visit\VisitsTracker;
|
||||
use Shlinkio\Shlink\Core\Visit\RequestTrackerInterface;
|
||||
|
||||
class PixelActionTest extends TestCase
|
||||
{
|
||||
|
@ -24,18 +23,14 @@ class PixelActionTest extends TestCase
|
|||
|
||||
private PixelAction $action;
|
||||
private ObjectProphecy $urlResolver;
|
||||
private ObjectProphecy $visitTracker;
|
||||
private ObjectProphecy $requestTracker;
|
||||
|
||||
public function setUp(): void
|
||||
{
|
||||
$this->urlResolver = $this->prophesize(ShortUrlResolverInterface::class);
|
||||
$this->visitTracker = $this->prophesize(VisitsTracker::class);
|
||||
$this->requestTracker = $this->prophesize(RequestTrackerInterface::class);
|
||||
|
||||
$this->action = new PixelAction(
|
||||
$this->urlResolver->reveal(),
|
||||
$this->visitTracker->reveal(),
|
||||
new TrackingOptions(),
|
||||
);
|
||||
$this->action = new PixelAction($this->urlResolver->reveal(), $this->requestTracker->reveal());
|
||||
}
|
||||
|
||||
/** @test */
|
||||
|
@ -45,7 +40,7 @@ class PixelActionTest extends TestCase
|
|||
$this->urlResolver->resolveEnabledShortUrl(new ShortUrlIdentifier($shortCode, ''))->willReturn(
|
||||
ShortUrl::withLongUrl('http://domain.com/foo/bar'),
|
||||
)->shouldBeCalledOnce();
|
||||
$this->visitTracker->track(Argument::cetera())->shouldBeCalledOnce();
|
||||
$this->requestTracker->trackIfApplicable(Argument::cetera())->shouldBeCalledOnce();
|
||||
|
||||
$request = (new ServerRequest())->withAttribute('shortCode', $shortCode);
|
||||
$response = $this->action->process($request, $this->prophesize(RequestHandlerInterface::class)->reveal());
|
||||
|
|
|
@ -17,13 +17,10 @@ use Shlinkio\Shlink\Core\Action\RedirectAction;
|
|||
use Shlinkio\Shlink\Core\Entity\ShortUrl;
|
||||
use Shlinkio\Shlink\Core\Exception\ShortUrlNotFoundException;
|
||||
use Shlinkio\Shlink\Core\Model\ShortUrlIdentifier;
|
||||
use Shlinkio\Shlink\Core\Options;
|
||||
use Shlinkio\Shlink\Core\Service\ShortUrl\ShortUrlResolverInterface;
|
||||
use Shlinkio\Shlink\Core\ShortUrl\Helper\ShortUrlRedirectionBuilderInterface;
|
||||
use Shlinkio\Shlink\Core\Util\RedirectResponseHelperInterface;
|
||||
use Shlinkio\Shlink\Core\Visit\VisitsTrackerInterface;
|
||||
|
||||
use function array_key_exists;
|
||||
use Shlinkio\Shlink\Core\Visit\RequestTrackerInterface;
|
||||
|
||||
class RedirectActionTest extends TestCase
|
||||
{
|
||||
|
@ -33,13 +30,13 @@ class RedirectActionTest extends TestCase
|
|||
|
||||
private RedirectAction $action;
|
||||
private ObjectProphecy $urlResolver;
|
||||
private ObjectProphecy $visitTracker;
|
||||
private ObjectProphecy $requestTracker;
|
||||
private ObjectProphecy $redirectRespHelper;
|
||||
|
||||
public function setUp(): void
|
||||
{
|
||||
$this->urlResolver = $this->prophesize(ShortUrlResolverInterface::class);
|
||||
$this->visitTracker = $this->prophesize(VisitsTrackerInterface::class);
|
||||
$this->requestTracker = $this->prophesize(RequestTrackerInterface::class);
|
||||
$this->redirectRespHelper = $this->prophesize(RedirectResponseHelperInterface::class);
|
||||
|
||||
$redirectBuilder = $this->prophesize(ShortUrlRedirectionBuilderInterface::class);
|
||||
|
@ -47,45 +44,41 @@ class RedirectActionTest extends TestCase
|
|||
|
||||
$this->action = new RedirectAction(
|
||||
$this->urlResolver->reveal(),
|
||||
$this->visitTracker->reveal(),
|
||||
new Options\TrackingOptions(['disableTrackParam' => 'foobar']),
|
||||
$this->requestTracker->reveal(),
|
||||
$redirectBuilder->reveal(),
|
||||
$this->redirectRespHelper->reveal(),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
* @dataProvider provideQueries
|
||||
*/
|
||||
public function redirectionIsPerformedToLongUrl(array $query): void
|
||||
/** @test */
|
||||
public function redirectionIsPerformedToLongUrl(): void
|
||||
{
|
||||
$shortCode = 'abc123';
|
||||
$shortUrl = ShortUrl::withLongUrl(self::LONG_URL);
|
||||
$shortCodeToUrl = $this->urlResolver->resolveEnabledShortUrl(
|
||||
new ShortUrlIdentifier($shortCode, ''),
|
||||
)->willReturn($shortUrl);
|
||||
$track = $this->visitTracker->track(Argument::cetera())->will(function (): void {
|
||||
$track = $this->requestTracker->trackIfApplicable(Argument::cetera())->will(function (): void {
|
||||
});
|
||||
$expectedResp = new Response\RedirectResponse(self::LONG_URL);
|
||||
$buildResp = $this->redirectRespHelper->buildRedirectResponse(self::LONG_URL)->willReturn($expectedResp);
|
||||
|
||||
$request = (new ServerRequest())->withAttribute('shortCode', $shortCode)->withQueryParams($query);
|
||||
$request = (new ServerRequest())->withAttribute('shortCode', $shortCode);
|
||||
$response = $this->action->process($request, $this->prophesize(RequestHandlerInterface::class)->reveal());
|
||||
|
||||
self::assertSame($expectedResp, $response);
|
||||
$buildResp->shouldHaveBeenCalledOnce();
|
||||
$shortCodeToUrl->shouldHaveBeenCalledOnce();
|
||||
$track->shouldHaveBeenCalledTimes(array_key_exists('foobar', $query) ? 0 : 1);
|
||||
$track->shouldHaveBeenCalledOnce();
|
||||
}
|
||||
|
||||
public function provideQueries(): iterable
|
||||
{
|
||||
yield [[]];
|
||||
yield [['foobar' => 'notrack']];
|
||||
yield [['foobar' => 'barfoo']];
|
||||
yield [['foobar' => null]];
|
||||
}
|
||||
// public function provideQueries(): iterable
|
||||
// {
|
||||
// yield [[]];
|
||||
// yield [['foobar' => 'notrack']];
|
||||
// yield [['foobar' => 'barfoo']];
|
||||
// yield [['foobar' => null]];
|
||||
// }
|
||||
|
||||
/** @test */
|
||||
public function nextMiddlewareIsInvokedIfLongUrlIsNotFound(): void
|
||||
|
@ -94,7 +87,7 @@ class RedirectActionTest extends TestCase
|
|||
$this->urlResolver->resolveEnabledShortUrl(new ShortUrlIdentifier($shortCode, ''))
|
||||
->willThrow(ShortUrlNotFoundException::class)
|
||||
->shouldBeCalledOnce();
|
||||
$this->visitTracker->track(Argument::cetera())->shouldNotBeCalled();
|
||||
$this->requestTracker->trackIfApplicable(Argument::cetera())->shouldNotBeCalled();
|
||||
|
||||
$handler = $this->prophesize(RequestHandlerInterface::class);
|
||||
$handle = $handler->handle(Argument::any())->willReturn(new Response());
|
||||
|
@ -105,26 +98,26 @@ class RedirectActionTest extends TestCase
|
|||
$handle->shouldHaveBeenCalledOnce();
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function trackingIsDisabledWhenRequestIsForwardedFromHead(): void
|
||||
{
|
||||
$shortCode = 'abc123';
|
||||
$shortUrl = ShortUrl::withLongUrl(self::LONG_URL);
|
||||
$this->urlResolver->resolveEnabledShortUrl(new ShortUrlIdentifier($shortCode, ''))->willReturn($shortUrl);
|
||||
$track = $this->visitTracker->track(Argument::cetera())->will(function (): void {
|
||||
});
|
||||
$buildResp = $this->redirectRespHelper->buildRedirectResponse(self::LONG_URL)->willReturn(
|
||||
new Response\RedirectResponse(''),
|
||||
);
|
||||
|
||||
$request = (new ServerRequest())->withAttribute('shortCode', $shortCode)
|
||||
->withAttribute(
|
||||
ImplicitHeadMiddleware::FORWARDED_HTTP_METHOD_ATTRIBUTE,
|
||||
RequestMethodInterface::METHOD_HEAD,
|
||||
);
|
||||
$this->action->process($request, $this->prophesize(RequestHandlerInterface::class)->reveal());
|
||||
|
||||
$buildResp->shouldHaveBeenCalled();
|
||||
$track->shouldNotHaveBeenCalled();
|
||||
}
|
||||
// /** @test */
|
||||
// public function trackingIsDisabledWhenRequestIsForwardedFromHead(): void
|
||||
// {
|
||||
// $shortCode = 'abc123';
|
||||
// $shortUrl = ShortUrl::withLongUrl(self::LONG_URL);
|
||||
// $this->urlResolver->resolveEnabledShortUrl(new ShortUrlIdentifier($shortCode, ''))->willReturn($shortUrl);
|
||||
// $track = $this->requestTracker->trackIfApplicable(Argument::cetera())->will(function (): void {
|
||||
// });
|
||||
// $buildResp = $this->redirectRespHelper->buildRedirectResponse(self::LONG_URL)->willReturn(
|
||||
// new Response\RedirectResponse(''),
|
||||
// );
|
||||
//
|
||||
// $request = (new ServerRequest())->withAttribute('shortCode', $shortCode)
|
||||
// ->withAttribute(
|
||||
// ImplicitHeadMiddleware::FORWARDED_HTTP_METHOD_ATTRIBUTE,
|
||||
// RequestMethodInterface::METHOD_HEAD,
|
||||
// );
|
||||
// $this->action->process($request, $this->prophesize(RequestHandlerInterface::class)->reveal());
|
||||
//
|
||||
// $buildResp->shouldHaveBeenCalled();
|
||||
// $track->shouldNotHaveBeenCalled();
|
||||
// }
|
||||
}
|
||||
|
|
|
@ -14,8 +14,7 @@ use Psr\Http\Message\ServerRequestInterface;
|
|||
use Psr\Http\Server\RequestHandlerInterface;
|
||||
use Shlinkio\Shlink\Core\ErrorHandler\Model\NotFoundType;
|
||||
use Shlinkio\Shlink\Core\ErrorHandler\NotFoundTrackerMiddleware;
|
||||
use Shlinkio\Shlink\Core\Model\Visitor;
|
||||
use Shlinkio\Shlink\Core\Visit\VisitsTrackerInterface;
|
||||
use Shlinkio\Shlink\Core\Visit\RequestTrackerInterface;
|
||||
|
||||
class NotFoundTrackerMiddlewareTest extends TestCase
|
||||
{
|
||||
|
@ -23,7 +22,7 @@ class NotFoundTrackerMiddlewareTest extends TestCase
|
|||
|
||||
private NotFoundTrackerMiddleware $middleware;
|
||||
private ServerRequestInterface $request;
|
||||
private ObjectProphecy $visitsTracker;
|
||||
private ObjectProphecy $requestTracker;
|
||||
private ObjectProphecy $notFoundType;
|
||||
private ObjectProphecy $handler;
|
||||
|
||||
|
@ -33,8 +32,8 @@ class NotFoundTrackerMiddlewareTest extends TestCase
|
|||
$this->handler = $this->prophesize(RequestHandlerInterface::class);
|
||||
$this->handler->handle(Argument::cetera())->willReturn(new Response());
|
||||
|
||||
$this->visitsTracker = $this->prophesize(VisitsTrackerInterface::class);
|
||||
$this->middleware = new NotFoundTrackerMiddleware($this->visitsTracker->reveal());
|
||||
$this->requestTracker = $this->prophesize(RequestTrackerInterface::class);
|
||||
$this->middleware = new NotFoundTrackerMiddleware($this->requestTracker->reveal());
|
||||
|
||||
$this->request = ServerRequestFactory::fromGlobals()->withAttribute(
|
||||
NotFoundType::class,
|
||||
|
@ -43,53 +42,62 @@ class NotFoundTrackerMiddlewareTest extends TestCase
|
|||
}
|
||||
|
||||
/** @test */
|
||||
public function baseUrlErrorIsTracked(): void
|
||||
public function delegatesIntoRequestTracker(): void
|
||||
{
|
||||
$isBaseUrl = $this->notFoundType->isBaseUrl()->willReturn(true);
|
||||
$isRegularNotFound = $this->notFoundType->isRegularNotFound()->willReturn(false);
|
||||
$isInvalidShortUrl = $this->notFoundType->isInvalidShortUrl()->willReturn(false);
|
||||
|
||||
$this->middleware->process($this->request, $this->handler->reveal());
|
||||
|
||||
$isBaseUrl->shouldHaveBeenCalledOnce();
|
||||
$isRegularNotFound->shouldNotHaveBeenCalled();
|
||||
$isInvalidShortUrl->shouldNotHaveBeenCalled();
|
||||
$this->visitsTracker->trackBaseUrlVisit(Argument::type(Visitor::class))->shouldHaveBeenCalledOnce();
|
||||
$this->visitsTracker->trackRegularNotFoundVisit(Argument::type(Visitor::class))->shouldNotHaveBeenCalled();
|
||||
$this->visitsTracker->trackInvalidShortUrlVisit(Argument::type(Visitor::class))->shouldNotHaveBeenCalled();
|
||||
$this->requestTracker->trackNotFoundIfApplicable($this->request)->shouldHaveBeenCalledOnce();
|
||||
$this->handler->handle($this->request)->shouldHaveBeenCalledOnce();
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function regularNotFoundErrorIsTracked(): void
|
||||
{
|
||||
$isBaseUrl = $this->notFoundType->isBaseUrl()->willReturn(false);
|
||||
$isRegularNotFound = $this->notFoundType->isRegularNotFound()->willReturn(true);
|
||||
$isInvalidShortUrl = $this->notFoundType->isInvalidShortUrl()->willReturn(false);
|
||||
|
||||
$this->middleware->process($this->request, $this->handler->reveal());
|
||||
|
||||
$isBaseUrl->shouldHaveBeenCalledOnce();
|
||||
$isRegularNotFound->shouldHaveBeenCalledOnce();
|
||||
$isInvalidShortUrl->shouldNotHaveBeenCalled();
|
||||
$this->visitsTracker->trackBaseUrlVisit(Argument::type(Visitor::class))->shouldNotHaveBeenCalled();
|
||||
$this->visitsTracker->trackRegularNotFoundVisit(Argument::type(Visitor::class))->shouldHaveBeenCalledOnce();
|
||||
$this->visitsTracker->trackInvalidShortUrlVisit(Argument::type(Visitor::class))->shouldNotHaveBeenCalled();
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function invalidShortUrlErrorIsTracked(): void
|
||||
{
|
||||
$isBaseUrl = $this->notFoundType->isBaseUrl()->willReturn(false);
|
||||
$isRegularNotFound = $this->notFoundType->isRegularNotFound()->willReturn(false);
|
||||
$isInvalidShortUrl = $this->notFoundType->isInvalidShortUrl()->willReturn(true);
|
||||
|
||||
$this->middleware->process($this->request, $this->handler->reveal());
|
||||
|
||||
$isBaseUrl->shouldHaveBeenCalledOnce();
|
||||
$isRegularNotFound->shouldHaveBeenCalledOnce();
|
||||
$isInvalidShortUrl->shouldHaveBeenCalledOnce();
|
||||
$this->visitsTracker->trackBaseUrlVisit(Argument::type(Visitor::class))->shouldNotHaveBeenCalled();
|
||||
$this->visitsTracker->trackRegularNotFoundVisit(Argument::type(Visitor::class))->shouldNotHaveBeenCalled();
|
||||
$this->visitsTracker->trackInvalidShortUrlVisit(Argument::type(Visitor::class))->shouldHaveBeenCalledOnce();
|
||||
}
|
||||
// /** @test */
|
||||
// public function baseUrlErrorIsTracked(): void
|
||||
// {
|
||||
// $isBaseUrl = $this->notFoundType->isBaseUrl()->willReturn(true);
|
||||
// $isRegularNotFound = $this->notFoundType->isRegularNotFound()->willReturn(false);
|
||||
// $isInvalidShortUrl = $this->notFoundType->isInvalidShortUrl()->willReturn(false);
|
||||
//
|
||||
// $this->middleware->process($this->request, $this->handler->reveal());
|
||||
//
|
||||
// $isBaseUrl->shouldHaveBeenCalledOnce();
|
||||
// $isRegularNotFound->shouldNotHaveBeenCalled();
|
||||
// $isInvalidShortUrl->shouldNotHaveBeenCalled();
|
||||
// $this->visitsTracker->trackBaseUrlVisit(Argument::type(Visitor::class))->shouldHaveBeenCalledOnce();
|
||||
// $this->visitsTracker->trackRegularNotFoundVisit(Argument::type(Visitor::class))->shouldNotHaveBeenCalled();
|
||||
// $this->visitsTracker->trackInvalidShortUrlVisit(Argument::type(Visitor::class))->shouldNotHaveBeenCalled();
|
||||
// }
|
||||
//
|
||||
// /** @test */
|
||||
// public function regularNotFoundErrorIsTracked(): void
|
||||
// {
|
||||
// $isBaseUrl = $this->notFoundType->isBaseUrl()->willReturn(false);
|
||||
// $isRegularNotFound = $this->notFoundType->isRegularNotFound()->willReturn(true);
|
||||
// $isInvalidShortUrl = $this->notFoundType->isInvalidShortUrl()->willReturn(false);
|
||||
//
|
||||
// $this->middleware->process($this->request, $this->handler->reveal());
|
||||
//
|
||||
// $isBaseUrl->shouldHaveBeenCalledOnce();
|
||||
// $isRegularNotFound->shouldHaveBeenCalledOnce();
|
||||
// $isInvalidShortUrl->shouldNotHaveBeenCalled();
|
||||
// $this->visitsTracker->trackBaseUrlVisit(Argument::type(Visitor::class))->shouldNotHaveBeenCalled();
|
||||
// $this->visitsTracker->trackRegularNotFoundVisit(Argument::type(Visitor::class))->shouldHaveBeenCalledOnce();
|
||||
// $this->visitsTracker->trackInvalidShortUrlVisit(Argument::type(Visitor::class))->shouldNotHaveBeenCalled();
|
||||
// }
|
||||
//
|
||||
// /** @test */
|
||||
// public function invalidShortUrlErrorIsTracked(): void
|
||||
// {
|
||||
// $isBaseUrl = $this->notFoundType->isBaseUrl()->willReturn(false);
|
||||
// $isRegularNotFound = $this->notFoundType->isRegularNotFound()->willReturn(false);
|
||||
// $isInvalidShortUrl = $this->notFoundType->isInvalidShortUrl()->willReturn(true);
|
||||
//
|
||||
// $this->middleware->process($this->request, $this->handler->reveal());
|
||||
//
|
||||
// $isBaseUrl->shouldHaveBeenCalledOnce();
|
||||
// $isRegularNotFound->shouldHaveBeenCalledOnce();
|
||||
// $isInvalidShortUrl->shouldHaveBeenCalledOnce();
|
||||
// $this->visitsTracker->trackBaseUrlVisit(Argument::type(Visitor::class))->shouldNotHaveBeenCalled();
|
||||
// $this->visitsTracker->trackRegularNotFoundVisit(Argument::type(Visitor::class))->shouldNotHaveBeenCalled();
|
||||
// $this->visitsTracker->trackInvalidShortUrlVisit(Argument::type(Visitor::class))->shouldHaveBeenCalledOnce();
|
||||
// }
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue