From 343ee04acbb17ffc38c94953ef0799a9eb360186 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Tue, 18 Feb 2020 19:21:34 +0100 Subject: [PATCH] Created middleware which injects default short code length from config when a value was not explicitly provided --- docs/swagger/paths/v1_short-urls.json | 4 ++ module/Rest/config/dependencies.config.php | 4 ++ module/Rest/config/routes.config.php | 6 ++- .../DefaultShortCodesLengthMiddleware.php | 31 +++++++++++ .../DefaultShortCodesLengthMiddlewareTest.php | 54 +++++++++++++++++++ 5 files changed, 98 insertions(+), 1 deletion(-) create mode 100644 module/Rest/src/Middleware/ShortUrl/DefaultShortCodesLengthMiddleware.php create mode 100644 module/Rest/test/Middleware/ShortUrl/DefaultShortCodesLengthMiddlewareTest.php diff --git a/docs/swagger/paths/v1_short-urls.json b/docs/swagger/paths/v1_short-urls.json index be274ab6..ee8a6060 100644 --- a/docs/swagger/paths/v1_short-urls.json +++ b/docs/swagger/paths/v1_short-urls.json @@ -243,6 +243,10 @@ "domain": { "description": "The domain to which the short URL will be attached", "type": "string" + }, + "shortCodeLength": { + "description": "The length for generated short code. It has to be at least 4 and defaults to 5. It will be ignored when customSlug is provided", + "type": "number" } } } diff --git a/module/Rest/config/dependencies.config.php b/module/Rest/config/dependencies.config.php index dc4c0e3b..b24ec1ee 100644 --- a/module/Rest/config/dependencies.config.php +++ b/module/Rest/config/dependencies.config.php @@ -38,6 +38,7 @@ return [ Middleware\CrossDomainMiddleware::class => InvokableFactory::class, Middleware\ShortUrl\CreateShortUrlContentNegotiationMiddleware::class => InvokableFactory::class, Middleware\ShortUrl\DropDefaultDomainFromRequestMiddleware::class => ConfigAbstractFactory::class, + Middleware\ShortUrl\DefaultShortCodesLengthMiddleware::class => ConfigAbstractFactory::class, ], ], @@ -75,6 +76,9 @@ return [ Action\Tag\UpdateTagAction::class => [Service\Tag\TagService::class, LoggerInterface::class], Middleware\ShortUrl\DropDefaultDomainFromRequestMiddleware::class => ['config.url_shortener.domain.hostname'], + Middleware\ShortUrl\DefaultShortCodesLengthMiddleware::class => [ + 'config.url_shortener.default_short_codes_length', + ], ], ]; diff --git a/module/Rest/config/routes.config.php b/module/Rest/config/routes.config.php index 61abb1b7..b104d81b 100644 --- a/module/Rest/config/routes.config.php +++ b/module/Rest/config/routes.config.php @@ -13,7 +13,11 @@ return [ Action\HealthAction::getRouteDef(), // Short codes - Action\ShortUrl\CreateShortUrlAction::getRouteDef([$contentNegotiationMiddleware, $dropDomainMiddleware]), + Action\ShortUrl\CreateShortUrlAction::getRouteDef([ + $contentNegotiationMiddleware, + $dropDomainMiddleware, + Middleware\ShortUrl\DefaultShortCodesLengthMiddleware::class, + ]), Action\ShortUrl\SingleStepCreateShortUrlAction::getRouteDef([$contentNegotiationMiddleware]), Action\ShortUrl\EditShortUrlAction::getRouteDef([$dropDomainMiddleware]), Action\ShortUrl\DeleteShortUrlAction::getRouteDef([$dropDomainMiddleware]), diff --git a/module/Rest/src/Middleware/ShortUrl/DefaultShortCodesLengthMiddleware.php b/module/Rest/src/Middleware/ShortUrl/DefaultShortCodesLengthMiddleware.php new file mode 100644 index 00000000..bcad748e --- /dev/null +++ b/module/Rest/src/Middleware/ShortUrl/DefaultShortCodesLengthMiddleware.php @@ -0,0 +1,31 @@ +defaultShortCodesLength = $defaultShortCodesLength; + } + + public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface + { + $body = $request->getParsedBody(); + if (! isset($body[ShortUrlMetaInputFilter::SHORT_CODE_LENGTH])) { + $body[ShortUrlMetaInputFilter::SHORT_CODE_LENGTH] = $this->defaultShortCodesLength; + } + + return $handler->handle($request->withParsedBody($body)); + } +} diff --git a/module/Rest/test/Middleware/ShortUrl/DefaultShortCodesLengthMiddlewareTest.php b/module/Rest/test/Middleware/ShortUrl/DefaultShortCodesLengthMiddlewareTest.php new file mode 100644 index 00000000..38d875d9 --- /dev/null +++ b/module/Rest/test/Middleware/ShortUrl/DefaultShortCodesLengthMiddlewareTest.php @@ -0,0 +1,54 @@ +handler = $this->prophesize(RequestHandlerInterface::class); + $this->middleware = new DefaultShortCodesLengthMiddleware(8); + } + + /** + * @test + * @dataProvider provideBodies + */ + public function defaultValueIsInjectedInBodyWhenNotProvided(array $body, int $expectedLength): void + { + $request = ServerRequestFactory::fromGlobals()->withParsedBody($body); + $handle = $this->handler->handle(Argument::that(function (ServerRequestInterface $req) use ($expectedLength) { + $parsedBody = $req->getParsedBody(); + Assert::assertArrayHasKey(ShortUrlMetaInputFilter::SHORT_CODE_LENGTH, $parsedBody); + Assert::assertEquals($expectedLength, $parsedBody[ShortUrlMetaInputFilter::SHORT_CODE_LENGTH]); + + return $req; + }))->willReturn(new Response()); + + $this->middleware->process($request, $this->handler->reveal()); + + $handle->shouldHaveBeenCalledOnce(); + } + + public function provideBodies(): iterable + { + yield 'value provided' => [[ShortUrlMetaInputFilter::SHORT_CODE_LENGTH => 6], 6]; + yield 'value not provided' => [[], 8]; + } +}