Skip to content

Commit dc518f6

Browse files
BackEndTeafabpot
authored andcommitted
[HttpClient] throw clearer error when no scheme is provided
1 parent 7196e9b commit dc518f6

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

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
}

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)