[HttpClient] throw clearer error when no scheme is provided

This commit is contained in:
Gert de Pagter 2020-12-02 13:28:13 +01:00 committed by Fabien Potencier
parent 7882c4a0b1
commit 4d821d6c34
2 changed files with 19 additions and 0 deletions

View File

@ -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)));
}

View File

@ -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
*/