Merge pull request #568 from acelaya-forks/feature/guzzle-update

Updated to guzzle 6.5 and removed custom code
This commit is contained in:
Alejandro Celaya 2019-12-07 21:13:17 +01:00 committed by GitHub
commit 83757ed390
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 10 additions and 29 deletions

View File

@ -25,7 +25,7 @@
"endroid/qr-code": "^3.6", "endroid/qr-code": "^3.6",
"firebase/php-jwt": "^4.0", "firebase/php-jwt": "^4.0",
"geoip2/geoip2": "^2.9", "geoip2/geoip2": "^2.9",
"guzzlehttp/guzzle": "^6.3", "guzzlehttp/guzzle": "^6.5",
"lstrojny/functional-php": "^1.9", "lstrojny/functional-php": "^1.9",
"mikehaertl/phpwkhtmltopdf": "^2.2", "mikehaertl/phpwkhtmltopdf": "^2.2",
"monolog/monolog": "^2.0", "monolog/monolog": "^2.0",

View File

@ -10,13 +10,8 @@ use GuzzleHttp\ClientInterface;
use GuzzleHttp\Exception\GuzzleException; use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\RequestOptions; use GuzzleHttp\RequestOptions;
use Shlinkio\Shlink\Core\Exception\InvalidUrlException; use Shlinkio\Shlink\Core\Exception\InvalidUrlException;
use Zend\Diactoros\Uri;
use function Functional\contains; 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 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 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 // TODO Guzzle does not properly handle IDNs on redirects, just on first request.
// Remove custom implementation and manual redirect handling when Guzzle's PR is merged // Because of that, we have to handle redirects manually.
$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);
}
try { 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 => ['max' => self::MAX_REDIRECTS],
RequestOptions::ALLOW_REDIRECTS => false, RequestOptions::ALLOW_REDIRECTS => false,
]); ]);

View File

@ -64,31 +64,24 @@ class UrlValidatorTest extends TestCase
}); });
} }
/** /** @test */
* @test public function expectedUrlIsCalledWhenTryingToVerify(): void
* @dataProvider provideUrls
*/
public function expectedUrlIsCalledInOrderToVerifyProvidedUrl(string $providedUrl, string $expectedUrl): void
{ {
$expectedUrl = 'http://foobar.com';
$request = $this->httpClient->request( $request = $this->httpClient->request(
RequestMethodInterface::METHOD_GET, RequestMethodInterface::METHOD_GET,
$expectedUrl, $expectedUrl,
Argument::cetera() Argument::cetera()
)->willReturn(new Response()); )->willReturn(new Response());
$this->urlValidator->validateUrl($providedUrl); $this->urlValidator->validateUrl($expectedUrl);
$request->shouldHaveBeenCalledOnce(); $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 */ /** @test */
public function considersUrlValidWhenTooManyRedirectsAreReturned(): void public function urlIsConsideredValidWhenTooManyRedirectsAreReturned(): void
{ {
$request = $this->httpClient->request(Argument::cetera())->willReturn( $request = $this->httpClient->request(Argument::cetera())->willReturn(
new Response('php://memory', 302, ['Location' => 'http://foo.com']) new Response('php://memory', 302, ['Location' => 'http://foo.com'])