diff --git a/config/autoload/middleware-pipeline.global.php b/config/autoload/middleware-pipeline.global.php index dc17bd53..308315bb 100644 --- a/config/autoload/middleware-pipeline.global.php +++ b/config/autoload/middleware-pipeline.global.php @@ -1,12 +1,8 @@ [ ErrorHandler::class, Expressive\Helper\ContentLengthMiddleware::class, - LocaleMiddleware::class, + Common\Middleware\LocaleMiddleware::class, ], - 'priority' => 11, + 'priority' => 12, ], 'pre-routing-rest' => [ 'path' => '/rest', 'middleware' => [ - PathVersionMiddleware::class, + Rest\Middleware\PathVersionMiddleware::class, + Rest\Middleware\ShortUrl\ShortCodePathMiddleware::class, ], 'priority' => 11, ], @@ -39,10 +36,10 @@ return [ 'rest' => [ 'path' => '/rest', 'middleware' => [ - CrossDomainMiddleware::class, + Rest\Middleware\CrossDomainMiddleware::class, Expressive\Router\Middleware\ImplicitOptionsMiddleware::class, - BodyParserMiddleware::class, - CheckAuthenticationMiddleware::class, + Rest\Middleware\BodyParserMiddleware::class, + Rest\Middleware\CheckAuthenticationMiddleware::class, ], 'priority' => 5, ], @@ -50,7 +47,7 @@ return [ 'post-routing' => [ 'middleware' => [ Expressive\Router\Middleware\DispatchMiddleware::class, - NotFoundHandler::class, + Core\Response\NotFoundHandler::class, ], 'priority' => 1, ], diff --git a/module/Rest/config/dependencies.config.php b/module/Rest/config/dependencies.config.php index 595377d7..c9a9da98 100644 --- a/module/Rest/config/dependencies.config.php +++ b/module/Rest/config/dependencies.config.php @@ -1,12 +1,11 @@ [ 'factories' => [ - JWTService::class => ConfigAbstractFactory::class, + Authentication\JWTService::class => ConfigAbstractFactory::class, ApiKeyService::class => ConfigAbstractFactory::class, Action\AuthenticateAction::class => ConfigAbstractFactory::class, @@ -38,14 +37,20 @@ return [ Middleware\PathVersionMiddleware::class => InvokableFactory::class, Middleware\CheckAuthenticationMiddleware::class => ConfigAbstractFactory::class, Middleware\ShortUrl\CreateShortUrlContentNegotiationMiddleware::class => InvokableFactory::class, + Middleware\ShortUrl\ShortCodePathMiddleware::class => InvokableFactory::class, ], ], ConfigAbstractFactory::class => [ - JWTService::class => [AppOptions::class], + Authentication\JWTService::class => [AppOptions::class], ApiKeyService::class => ['em'], - Action\AuthenticateAction::class => [ApiKeyService::class, JWTService::class, 'translator', 'Logger_Shlink'], + Action\AuthenticateAction::class => [ + ApiKeyService::class, + Authentication\JWTService::class, + 'translator', + 'Logger_Shlink', + ], Action\ShortUrl\CreateShortUrlAction::class => [ Service\UrlShortener::class, 'translator', @@ -88,7 +93,7 @@ return [ Action\Tag\UpdateTagAction::class => [Service\Tag\TagService::class, Translator::class, LoggerInterface::class], Middleware\CheckAuthenticationMiddleware::class => [ - JWTService::class, + Authentication\JWTService::class, 'translator', 'config.auth.routes_whitelist', 'Logger_Shlink', diff --git a/module/Rest/src/Action/ShortUrl/CreateShortUrlAction.php b/module/Rest/src/Action/ShortUrl/CreateShortUrlAction.php index 925da8d0..13c92994 100644 --- a/module/Rest/src/Action/ShortUrl/CreateShortUrlAction.php +++ b/module/Rest/src/Action/ShortUrl/CreateShortUrlAction.php @@ -12,7 +12,7 @@ use Zend\Diactoros\Uri; class CreateShortUrlAction extends AbstractCreateShortUrlAction { - protected const ROUTE_PATH = '/short-codes'; + protected const ROUTE_PATH = '/short-urls'; protected const ROUTE_ALLOWED_METHODS = [self::METHOD_POST]; /** diff --git a/module/Rest/src/Action/ShortUrl/DeleteShortUrlAction.php b/module/Rest/src/Action/ShortUrl/DeleteShortUrlAction.php index aea480cb..d752cc9b 100644 --- a/module/Rest/src/Action/ShortUrl/DeleteShortUrlAction.php +++ b/module/Rest/src/Action/ShortUrl/DeleteShortUrlAction.php @@ -16,7 +16,7 @@ use Zend\I18n\Translator\TranslatorInterface; class DeleteShortUrlAction extends AbstractRestAction { - protected const ROUTE_PATH = '/short-codes/{shortCode}'; + protected const ROUTE_PATH = '/short-urls/{shortCode}'; protected const ROUTE_ALLOWED_METHODS = [self::METHOD_DELETE]; /** diff --git a/module/Rest/src/Action/ShortUrl/EditShortUrlAction.php b/module/Rest/src/Action/ShortUrl/EditShortUrlAction.php index 523edc3d..0c1e243a 100644 --- a/module/Rest/src/Action/ShortUrl/EditShortUrlAction.php +++ b/module/Rest/src/Action/ShortUrl/EditShortUrlAction.php @@ -17,7 +17,7 @@ use Zend\I18n\Translator\TranslatorInterface; class EditShortUrlAction extends AbstractRestAction { - protected const ROUTE_PATH = '/short-codes/{shortCode}'; + protected const ROUTE_PATH = '/short-urls/{shortCode}'; protected const ROUTE_ALLOWED_METHODS = [self::METHOD_PUT]; /** diff --git a/module/Rest/src/Action/ShortUrl/EditShortUrlTagsAction.php b/module/Rest/src/Action/ShortUrl/EditShortUrlTagsAction.php index 3db0d900..064f5d19 100644 --- a/module/Rest/src/Action/ShortUrl/EditShortUrlTagsAction.php +++ b/module/Rest/src/Action/ShortUrl/EditShortUrlTagsAction.php @@ -15,7 +15,7 @@ use Zend\I18n\Translator\TranslatorInterface; class EditShortUrlTagsAction extends AbstractRestAction { - protected const ROUTE_PATH = '/short-codes/{shortCode}/tags'; + protected const ROUTE_PATH = '/short-urls/{shortCode}/tags'; protected const ROUTE_ALLOWED_METHODS = [self::METHOD_PUT]; /** diff --git a/module/Rest/src/Action/ShortUrl/ListShortUrlsAction.php b/module/Rest/src/Action/ShortUrl/ListShortUrlsAction.php index c58dcba0..5e19f866 100644 --- a/module/Rest/src/Action/ShortUrl/ListShortUrlsAction.php +++ b/module/Rest/src/Action/ShortUrl/ListShortUrlsAction.php @@ -18,7 +18,7 @@ class ListShortUrlsAction extends AbstractRestAction { use PaginatorUtilsTrait; - protected const ROUTE_PATH = '/short-codes'; + protected const ROUTE_PATH = '/short-urls'; protected const ROUTE_ALLOWED_METHODS = [self::METHOD_GET]; /** diff --git a/module/Rest/src/Action/ShortUrl/ResolveShortUrlAction.php b/module/Rest/src/Action/ShortUrl/ResolveShortUrlAction.php index e1ab9639..8afb55f3 100644 --- a/module/Rest/src/Action/ShortUrl/ResolveShortUrlAction.php +++ b/module/Rest/src/Action/ShortUrl/ResolveShortUrlAction.php @@ -17,7 +17,7 @@ use Zend\I18n\Translator\TranslatorInterface; class ResolveShortUrlAction extends AbstractRestAction { - protected const ROUTE_PATH = '/short-codes/{shortCode}'; + protected const ROUTE_PATH = '/short-urls/{shortCode}'; protected const ROUTE_ALLOWED_METHODS = [self::METHOD_GET]; /** diff --git a/module/Rest/src/Action/ShortUrl/SingleStepCreateShortUrlAction.php b/module/Rest/src/Action/ShortUrl/SingleStepCreateShortUrlAction.php index 1003ebfd..399e6393 100644 --- a/module/Rest/src/Action/ShortUrl/SingleStepCreateShortUrlAction.php +++ b/module/Rest/src/Action/ShortUrl/SingleStepCreateShortUrlAction.php @@ -15,7 +15,7 @@ use Zend\I18n\Translator\TranslatorInterface; class SingleStepCreateShortUrlAction extends AbstractCreateShortUrlAction { - protected const ROUTE_PATH = '/short-codes/shorten'; + protected const ROUTE_PATH = '/short-urls/shorten'; protected const ROUTE_ALLOWED_METHODS = [self::METHOD_GET]; /** diff --git a/module/Rest/src/Middleware/ShortUrl/ShortCodePathMiddleware.php b/module/Rest/src/Middleware/ShortUrl/ShortCodePathMiddleware.php new file mode 100644 index 00000000..a04a50b3 --- /dev/null +++ b/module/Rest/src/Middleware/ShortUrl/ShortCodePathMiddleware.php @@ -0,0 +1,30 @@ +getUri(); + $path = $uri->getPath(); + + // If the path starts with the old prefix, replace it by the new one + return $handler->handle( + $request->withUri($uri->withPath(\str_replace(self::OLD_PATH_PREFIX, self::NEW_PATH_PREFIX, $path))) + ); + } +} diff --git a/module/Rest/test/Middleware/ShortUrl/ShortCodePathMiddlewareTest.php b/module/Rest/test/Middleware/ShortUrl/ShortCodePathMiddlewareTest.php new file mode 100644 index 00000000..d28acf40 --- /dev/null +++ b/module/Rest/test/Middleware/ShortUrl/ShortCodePathMiddlewareTest.php @@ -0,0 +1,50 @@ +middleware = new ShortCodePathMiddleware(); + $this->requestHandler = $this->prophesize(RequestHandlerInterface::class); + $this->requestHandler->handle(Argument::type(ServerRequestInterface::class))->willReturn(new Response()); + } + + /** + * @test + */ + public function properlyReplacesTheOldPathByTheNewOne() + { + $uri = new Uri('/short-codes/foo'); + + $request = $this->prophesize(ServerRequestInterface::class); + $request->getUri()->willReturn($uri); + $withUri = $request->withUri(Argument::that(function (UriInterface $uri) { + $path = $uri->getPath(); + + Assert::assertContains('/short-urls', $path); + Assert::assertNotContains('/short-codes', $path); + + return $uri; + }))->willReturn($request->reveal()); + + $this->middleware->process($request->reveal(), $this->requestHandler->reveal()); + + $withUri->shouldHaveBeenCalledTimes(1); + } +}