Ensured emojis in short URLs are not URL-encoded

This commit is contained in:
Alejandro Celaya 2022-01-10 11:13:16 +01:00
parent 41d3826c1a
commit 45de3f0128
2 changed files with 20 additions and 10 deletions

View File

@ -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'] ?? '';
}
}

View File

@ -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'],
'',