diff --git a/module/Core/src/ShortUrl/Helper/ShortUrlStringifier.php b/module/Core/src/ShortUrl/Helper/ShortUrlStringifier.php index 1ec36677..d238f2fc 100644 --- a/module/Core/src/ShortUrl/Helper/ShortUrlStringifier.php +++ b/module/Core/src/ShortUrl/Helper/ShortUrlStringifier.php @@ -17,19 +17,17 @@ class ShortUrlStringifier implements ShortUrlStringifierInterface public function stringify(ShortUrl $shortUrl): string { - return (new Uri())->withPath($shortUrl->getShortCode()) - ->withScheme($this->domainConfig['schema'] ?? 'http') - ->withHost($this->resolveDomain($shortUrl)) - ->__toString(); + $uriWithoutShortCode = (new Uri())->withScheme($this->domainConfig['schema'] ?? 'http') + ->withHost($this->resolveDomain($shortUrl)) + ->withPath($this->basePath) + ->__toString(); + + // The short code needs to be appended to avoid it from being URL-encoded + return sprintf('%s/%s', $uriWithoutShortCode, $shortUrl->getShortCode()); } private function resolveDomain(ShortUrl $shortUrl): string { - $domain = $shortUrl->getDomain(); - if ($domain === null) { - return $this->domainConfig['hostname'] ?? ''; - } - - return sprintf('%s%s', $domain->getAuthority(), $this->basePath); + return $shortUrl->getDomain()?->getAuthority() ?? $this->domainConfig['hostname'] ?? ''; } } diff --git a/module/Core/test/ShortUrl/Helper/ShortUrlStringifierTest.php b/module/Core/test/ShortUrl/Helper/ShortUrlStringifierTest.php index b4acc417..4fed4329 100644 --- a/module/Core/test/ShortUrl/Helper/ShortUrlStringifierTest.php +++ b/module/Core/test/ShortUrl/Helper/ShortUrlStringifierTest.php @@ -43,6 +43,18 @@ class ShortUrlStringifierTest extends TestCase $shortUrlWithShortCode('bar'), 'http://example.com/bar', ]; + yield 'special chars in short code' => [ + ['hostname' => 'example.com'], + '', + $shortUrlWithShortCode('グーグル'), + 'http://example.com/グーグル', + ]; + yield 'emojis in short code' => [ + ['hostname' => 'example.com'], + '', + $shortUrlWithShortCode('🦣-🍅'), + 'http://example.com/🦣-🍅', + ]; yield 'hostname with base path in config' => [ ['hostname' => 'example.com/foo/bar'], '',