From 843e9432511ae6446da032211b612466600cfe9c Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Sat, 7 Dec 2019 21:01:14 +0100 Subject: [PATCH] Updated to guzzle 6.5 and removed custom code --- composer.json | 2 +- module/Core/src/Util/UrlValidator.php | 18 +++--------------- module/Core/test/Util/UrlValidatorTest.php | 19 ++++++------------- 3 files changed, 10 insertions(+), 29 deletions(-) diff --git a/composer.json b/composer.json index 0ded66fe..84290c2a 100644 --- a/composer.json +++ b/composer.json @@ -25,7 +25,7 @@ "endroid/qr-code": "^3.6", "firebase/php-jwt": "^4.0", "geoip2/geoip2": "^2.9", - "guzzlehttp/guzzle": "^6.3", + "guzzlehttp/guzzle": "^6.5", "lstrojny/functional-php": "^1.9", "mikehaertl/phpwkhtmltopdf": "^2.2", "monolog/monolog": "^2.0", diff --git a/module/Core/src/Util/UrlValidator.php b/module/Core/src/Util/UrlValidator.php index a3ffe0d8..c91f37ff 100644 --- a/module/Core/src/Util/UrlValidator.php +++ b/module/Core/src/Util/UrlValidator.php @@ -10,13 +10,8 @@ use GuzzleHttp\ClientInterface; use GuzzleHttp\Exception\GuzzleException; use GuzzleHttp\RequestOptions; use Shlinkio\Shlink\Core\Exception\InvalidUrlException; -use Zend\Diactoros\Uri; use function Functional\contains; -use function idn_to_ascii; - -use const IDNA_DEFAULT; -use const INTL_IDNA_VARIANT_UTS46; class UrlValidator implements UrlValidatorInterface, RequestMethodInterface, StatusCodeInterface { @@ -43,17 +38,10 @@ class UrlValidator implements UrlValidatorInterface, RequestMethodInterface, Sta */ private function doValidateUrl(string $url, int $redirectNum = 1): void { - // FIXME Guzzle is about to add support for this https://github.com/guzzle/guzzle/pull/2286 - // Remove custom implementation and manual redirect handling when Guzzle's PR is merged - $uri = new Uri($url); - $originalHost = $uri->getHost(); - $normalizedHost = idn_to_ascii($originalHost, IDNA_DEFAULT, INTL_IDNA_VARIANT_UTS46); - if ($originalHost !== $normalizedHost) { - $uri = $uri->withHost($normalizedHost); - } - + // TODO Guzzle does not properly handle IDNs on redirects, just on first request. + // Because of that, we have to handle redirects manually. try { - $resp = $this->httpClient->request(self::METHOD_GET, (string) $uri, [ + $resp = $this->httpClient->request(self::METHOD_GET, $url, [ // RequestOptions::ALLOW_REDIRECTS => ['max' => self::MAX_REDIRECTS], RequestOptions::ALLOW_REDIRECTS => false, ]); diff --git a/module/Core/test/Util/UrlValidatorTest.php b/module/Core/test/Util/UrlValidatorTest.php index 5dbfe582..af558e44 100644 --- a/module/Core/test/Util/UrlValidatorTest.php +++ b/module/Core/test/Util/UrlValidatorTest.php @@ -64,31 +64,24 @@ class UrlValidatorTest extends TestCase }); } - /** - * @test - * @dataProvider provideUrls - */ - public function expectedUrlIsCalledInOrderToVerifyProvidedUrl(string $providedUrl, string $expectedUrl): void + /** @test */ + public function expectedUrlIsCalledWhenTryingToVerify(): void { + $expectedUrl = 'http://foobar.com'; + $request = $this->httpClient->request( RequestMethodInterface::METHOD_GET, $expectedUrl, Argument::cetera() )->willReturn(new Response()); - $this->urlValidator->validateUrl($providedUrl); + $this->urlValidator->validateUrl($expectedUrl); $request->shouldHaveBeenCalledOnce(); } - public function provideUrls(): iterable - { - yield 'regular domain' => ['http://foobar.com', 'http://foobar.com']; - yield 'IDN' => ['https://tést.shlink.io', 'https://xn--tst-bma.shlink.io']; - } - /** @test */ - public function considersUrlValidWhenTooManyRedirectsAreReturned(): void + public function urlIsConsideredValidWhenTooManyRedirectsAreReturned(): void { $request = $this->httpClient->request(Argument::cetera())->willReturn( new Response('php://memory', 302, ['Location' => 'http://foo.com'])