From 4d821d6c3490c648b8c7f35d7cebb60c6d545e78 Mon Sep 17 00:00:00 2001 From: Gert de Pagter Date: Wed, 2 Dec 2020 13:28:13 +0100 Subject: [PATCH] [HttpClient] throw clearer error when no scheme is provided --- .../Component/HttpClient/HttpClientTrait.php | 4 ++++ .../HttpClient/Tests/HttpClientTraitTest.php | 15 +++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/src/Symfony/Component/HttpClient/HttpClientTrait.php b/src/Symfony/Component/HttpClient/HttpClientTrait.php index 2431e806b0..cf439c6ff0 100644 --- a/src/Symfony/Component/HttpClient/HttpClientTrait.php +++ b/src/Symfony/Component/HttpClient/HttpClientTrait.php @@ -377,6 +377,10 @@ trait HttpClientTrait throw new InvalidArgumentException(sprintf('Invalid "base_uri" option: host or scheme is missing in "%s".', implode('', $base))); } + if (null === $url['scheme'] && (null === $base || null === $base['scheme'])) { + throw new InvalidArgumentException(sprintf('Invalid URL: scheme is missing in "%s". Did you forget to add "http(s)://"?', implode('', $base ?? $url))); + } + if (null === $base && '' === $url['scheme'].$url['authority']) { throw new InvalidArgumentException(sprintf('Invalid URL: no "base_uri" option was provided and host or scheme is missing in "%s".', implode('', $url))); } diff --git a/src/Symfony/Component/HttpClient/Tests/HttpClientTraitTest.php b/src/Symfony/Component/HttpClient/Tests/HttpClientTraitTest.php index bce71ce823..f3ece3c140 100644 --- a/src/Symfony/Component/HttpClient/Tests/HttpClientTraitTest.php +++ b/src/Symfony/Component/HttpClient/Tests/HttpClientTraitTest.php @@ -12,6 +12,7 @@ namespace Symfony\Component\HttpClient\Tests; use PHPUnit\Framework\TestCase; +use Symfony\Component\HttpClient\Exception\InvalidArgumentException; use Symfony\Component\HttpClient\HttpClientTrait; use Symfony\Contracts\HttpClient\HttpClientInterface; @@ -120,6 +121,20 @@ class HttpClientTraitTest extends TestCase ]; } + public function testResolveUrlWithoutScheme() + { + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage('Invalid URL: scheme is missing in "//localhost:8080". Did you forget to add "http(s)://"?'); + self::resolveUrl(self::parseUrl('localhost:8080'), null); + } + + public function testResolveBaseUrlWitoutScheme() + { + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage('Invalid URL: scheme is missing in "//localhost:8081". Did you forget to add "http(s)://"?'); + self::resolveUrl(self::parseUrl('/foo'), self::parseUrl('localhost:8081')); + } + /** * @dataProvider provideParseUrl */