bug #39286 [HttpClient] throw clearer error when no scheme is provided (BackEndTea)

This PR was squashed before being merged into the 4.4 branch.

Discussion
----------

[HttpClient] throw clearer error when no scheme is provided

| Q             | A
| ------------- | ---
| Branch?       | 4.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | Fix #39285
| License       | MIT
| Doc PR        | N/A

This could be considred a BC break, as previously this would've  a `TransportExcepiton`, instead of an `InvalidArgumentException`. But i see no reason to catch this specific error, as it would generally be a configuration error.

Commits
-------

4d821d6c34 [HttpClient] throw clearer error when no scheme is provided
This commit is contained in:
Fabien Potencier 2020-12-05 07:03:15 +01:00
commit 1177baa770
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
*/