From baf3093893c84dc079eae6ea08b5fa606168c0bd Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Fri, 4 Oct 2019 21:17:02 +0200 Subject: [PATCH] Added support for domain param to command and action to resolve a short URL --- ...global.php => client-detection.global.php} | 7 ++++++ docs/swagger/paths/v1_short-urls.json | 4 ++++ .../paths/v1_short-urls_{shortCode}.json | 9 ++++++++ .../Command/ShortUrl/ResolveUrlCommand.php | 7 ++++-- .../ShortUrl/ResolveUrlCommandTest.php | 18 +++++++-------- .../Action/ShortUrl/ResolveShortUrlAction.php | 3 ++- .../ShortUrl/ResolveShortUrlActionTest.php | 22 +++++++++---------- 7 files changed, 47 insertions(+), 23 deletions(-) rename config/autoload/{ip-address.global.php => client-detection.global.php} (73%) diff --git a/config/autoload/ip-address.global.php b/config/autoload/client-detection.global.php similarity index 73% rename from config/autoload/ip-address.global.php rename to config/autoload/client-detection.global.php index edbebf69..c3d150bc 100644 --- a/config/autoload/ip-address.global.php +++ b/config/autoload/client-detection.global.php @@ -16,4 +16,11 @@ return [ ], ], + 'host_resolution' => [ + 'headers_to_inspect' => [ + 'Host', + 'X-Forwarded-Host', + ], + ], + ]; diff --git a/docs/swagger/paths/v1_short-urls.json b/docs/swagger/paths/v1_short-urls.json index 2254a732..f8fa1ec8 100644 --- a/docs/swagger/paths/v1_short-urls.json +++ b/docs/swagger/paths/v1_short-urls.json @@ -216,6 +216,10 @@ "findIfExists": { "description": "Will force existing matching URL to be returned if found, instead of creating a new one", "type": "boolean" + }, + "domain": { + "description": "The domain to which the short URL will be attached", + "type": "string" } } } diff --git a/docs/swagger/paths/v1_short-urls_{shortCode}.json b/docs/swagger/paths/v1_short-urls_{shortCode}.json index 41d1499c..bbb7145c 100644 --- a/docs/swagger/paths/v1_short-urls_{shortCode}.json +++ b/docs/swagger/paths/v1_short-urls_{shortCode}.json @@ -15,6 +15,15 @@ "schema": { "type": "string" } + }, + { + "name": "domain", + "in": "query", + "description": "The domain in which the short code should be searched for. Will fall back to default domain if not found.", + "required": false, + "schema": { + "type": "string" + } } ], "security": [ diff --git a/module/CLI/src/Command/ShortUrl/ResolveUrlCommand.php b/module/CLI/src/Command/ShortUrl/ResolveUrlCommand.php index 48fae0a0..13370c02 100644 --- a/module/CLI/src/Command/ShortUrl/ResolveUrlCommand.php +++ b/module/CLI/src/Command/ShortUrl/ResolveUrlCommand.php @@ -10,6 +10,7 @@ use Shlinkio\Shlink\Core\Service\UrlShortenerInterface; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Style\SymfonyStyle; @@ -35,7 +36,8 @@ class ResolveUrlCommand extends Command ->setName(self::NAME) ->setAliases(self::ALIASES) ->setDescription('Returns the long URL behind a short code') - ->addArgument('shortCode', InputArgument::REQUIRED, 'The short code to parse'); + ->addArgument('shortCode', InputArgument::REQUIRED, 'The short code to parse') + ->addOption('domain', 'd', InputOption::VALUE_REQUIRED, 'The domain to which the short URL is attached.'); } protected function interact(InputInterface $input, OutputInterface $output): void @@ -56,9 +58,10 @@ class ResolveUrlCommand extends Command { $io = new SymfonyStyle($input, $output); $shortCode = $input->getArgument('shortCode'); + $domain = $input->getOption('domain'); try { - $url = $this->urlShortener->shortCodeToUrl($shortCode); + $url = $this->urlShortener->shortCodeToUrl($shortCode, $domain); $output->writeln(sprintf('Long URL: %s', $url->getLongUrl())); return ExitCodes::EXIT_SUCCESS; } catch (InvalidShortCodeException $e) { diff --git a/module/CLI/test/Command/ShortUrl/ResolveUrlCommandTest.php b/module/CLI/test/Command/ShortUrl/ResolveUrlCommandTest.php index a4ee7f45..ce145165 100644 --- a/module/CLI/test/Command/ShortUrl/ResolveUrlCommandTest.php +++ b/module/CLI/test/Command/ShortUrl/ResolveUrlCommandTest.php @@ -33,13 +33,13 @@ class ResolveUrlCommandTest extends TestCase } /** @test */ - public function correctShortCodeResolvesUrl() + public function correctShortCodeResolvesUrl(): void { $shortCode = 'abc123'; $expectedUrl = 'http://domain.com/foo/bar'; $shortUrl = new ShortUrl($expectedUrl); - $this->urlShortener->shortCodeToUrl($shortCode)->willReturn($shortUrl) - ->shouldBeCalledOnce(); + $this->urlShortener->shortCodeToUrl($shortCode, null)->willReturn($shortUrl) + ->shouldBeCalledOnce(); $this->commandTester->execute(['shortCode' => $shortCode]); $output = $this->commandTester->getDisplay(); @@ -47,11 +47,11 @@ class ResolveUrlCommandTest extends TestCase } /** @test */ - public function incorrectShortCodeOutputsErrorMessage() + public function incorrectShortCodeOutputsErrorMessage(): void { $shortCode = 'abc123'; - $this->urlShortener->shortCodeToUrl($shortCode)->willThrow(EntityDoesNotExistException::class) - ->shouldBeCalledOnce(); + $this->urlShortener->shortCodeToUrl($shortCode, null)->willThrow(EntityDoesNotExistException::class) + ->shouldBeCalledOnce(); $this->commandTester->execute(['shortCode' => $shortCode]); $output = $this->commandTester->getDisplay(); @@ -59,11 +59,11 @@ class ResolveUrlCommandTest extends TestCase } /** @test */ - public function wrongShortCodeFormatOutputsErrorMessage() + public function wrongShortCodeFormatOutputsErrorMessage(): void { $shortCode = 'abc123'; - $this->urlShortener->shortCodeToUrl($shortCode)->willThrow(new InvalidShortCodeException()) - ->shouldBeCalledOnce(); + $this->urlShortener->shortCodeToUrl($shortCode, null)->willThrow(new InvalidShortCodeException()) + ->shouldBeCalledOnce(); $this->commandTester->execute(['shortCode' => $shortCode]); $output = $this->commandTester->getDisplay(); diff --git a/module/Rest/src/Action/ShortUrl/ResolveShortUrlAction.php b/module/Rest/src/Action/ShortUrl/ResolveShortUrlAction.php index 13fc1258..24288da1 100644 --- a/module/Rest/src/Action/ShortUrl/ResolveShortUrlAction.php +++ b/module/Rest/src/Action/ShortUrl/ResolveShortUrlAction.php @@ -45,10 +45,11 @@ class ResolveShortUrlAction extends AbstractRestAction public function handle(Request $request): Response { $shortCode = $request->getAttribute('shortCode'); + $domain = $request->getQueryParams()['domain'] ?? null; $transformer = new ShortUrlDataTransformer($this->domainConfig); try { - $url = $this->urlShortener->shortCodeToUrl($shortCode); + $url = $this->urlShortener->shortCodeToUrl($shortCode, $domain); return new JsonResponse($transformer->transform($url)); } catch (InvalidShortCodeException $e) { $this->logger->warning('Provided short code with invalid format. {e}', ['e' => $e]); diff --git a/module/Rest/test/Action/ShortUrl/ResolveShortUrlActionTest.php b/module/Rest/test/Action/ShortUrl/ResolveShortUrlActionTest.php index fc8f5d01..84fbf14e 100644 --- a/module/Rest/test/Action/ShortUrl/ResolveShortUrlActionTest.php +++ b/module/Rest/test/Action/ShortUrl/ResolveShortUrlActionTest.php @@ -30,11 +30,11 @@ class ResolveShortUrlActionTest extends TestCase } /** @test */ - public function incorrectShortCodeReturnsError() + public function incorrectShortCodeReturnsError(): void { $shortCode = 'abc123'; - $this->urlShortener->shortCodeToUrl($shortCode)->willThrow(EntityDoesNotExistException::class) - ->shouldBeCalledOnce(); + $this->urlShortener->shortCodeToUrl($shortCode, null)->willThrow(EntityDoesNotExistException::class) + ->shouldBeCalledOnce(); $request = (new ServerRequest())->withAttribute('shortCode', $shortCode); $response = $this->action->handle($request); @@ -43,10 +43,10 @@ class ResolveShortUrlActionTest extends TestCase } /** @test */ - public function correctShortCodeReturnsSuccess() + public function correctShortCodeReturnsSuccess(): void { $shortCode = 'abc123'; - $this->urlShortener->shortCodeToUrl($shortCode)->willReturn( + $this->urlShortener->shortCodeToUrl($shortCode, null)->willReturn( new ShortUrl('http://domain.com/foo/bar') )->shouldBeCalledOnce(); @@ -57,11 +57,11 @@ class ResolveShortUrlActionTest extends TestCase } /** @test */ - public function invalidShortCodeExceptionReturnsError() + public function invalidShortCodeExceptionReturnsError(): void { $shortCode = 'abc123'; - $this->urlShortener->shortCodeToUrl($shortCode)->willThrow(InvalidShortCodeException::class) - ->shouldBeCalledOnce(); + $this->urlShortener->shortCodeToUrl($shortCode, null)->willThrow(InvalidShortCodeException::class) + ->shouldBeCalledOnce(); $request = (new ServerRequest())->withAttribute('shortCode', $shortCode); $response = $this->action->handle($request); @@ -70,11 +70,11 @@ class ResolveShortUrlActionTest extends TestCase } /** @test */ - public function unexpectedExceptionWillReturnError() + public function unexpectedExceptionWillReturnError(): void { $shortCode = 'abc123'; - $this->urlShortener->shortCodeToUrl($shortCode)->willThrow(Exception::class) - ->shouldBeCalledOnce(); + $this->urlShortener->shortCodeToUrl($shortCode, null)->willThrow(Exception::class) + ->shouldBeCalledOnce(); $request = (new ServerRequest())->withAttribute('shortCode', $shortCode); $response = $this->action->handle($request);