Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1177baa

Browse files
committed
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 ------- 4d821d6 [HttpClient] throw clearer error when no scheme is provided
2 parents a8441e1 + 4d821d6 commit 1177baa

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

src/Symfony/Component/HttpClient/HttpClientTrait.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,10 @@ private static function resolveUrl(array $url, ?array $base, array $queryDefault
377377
throw new InvalidArgumentException(sprintf('Invalid "base_uri" option: host or scheme is missing in "%s".', implode('', $base)));
378378
}
379379

380+
if (null === $url['scheme'] && (null === $base || null === $base['scheme'])) {
381+
throw new InvalidArgumentException(sprintf('Invalid URL: scheme is missing in "%s". Did you forget to add "http(s)://"?', implode('', $base ?? $url)));
382+
}
383+
380384
if (null === $base && '' === $url['scheme'].$url['authority']) {
381385
throw new InvalidArgumentException(sprintf('Invalid URL: no "base_uri" option was provided and host or scheme is missing in "%s".', implode('', $url)));
382386
}

src/Symfony/Component/HttpClient/Tests/HttpClientTraitTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\HttpClient\Tests;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\HttpClient\Exception\InvalidArgumentException;
1516
use Symfony\Component\HttpClient\HttpClientTrait;
1617
use Symfony\Contracts\HttpClient\HttpClientInterface;
1718

@@ -120,6 +121,20 @@ public function provideResolveUrl(): array
120121
];
121122
}
122123

124+
public function testResolveUrlWithoutScheme()
125+
{
126+
$this->expectException(InvalidArgumentException::class);
127+
$this->expectExceptionMessage('Invalid URL: scheme is missing in "//localhost:8080". Did you forget to add "http(s)://"?');
128+
self::resolveUrl(self::parseUrl('localhost:8080'), null);
129+
}
130+
131+
public function testResolveBaseUrlWitoutScheme()
132+
{
133+
$this->expectException(InvalidArgumentException::class);
134+
$this->expectExceptionMessage('Invalid URL: scheme is missing in "//localhost:8081". Did you forget to add "http(s)://"?');
135+
self::resolveUrl(self::parseUrl('/foo'), self::parseUrl('localhost:8081'));
136+
}
137+
123138
/**
124139
* @dataProvider provideParseUrl
125140
*/

0 commit comments

Comments
 (0)