diff --git a/src/Illuminate/Http/Client/PendingRequest.php b/src/Illuminate/Http/Client/PendingRequest.php index 8db4e46d05d5..b93e2933a127 100644 --- a/src/Illuminate/Http/Client/PendingRequest.php +++ b/src/Illuminate/Http/Client/PendingRequest.php @@ -886,10 +886,6 @@ public function pool(callable $callback) */ public function send(string $method, string $url, array $options = []) { - if (! Str::startsWith($url, ['http://', 'https://'])) { - $url = ltrim(rtrim($this->baseUrl, '/').'/'.ltrim($url, '/'), '/'); - } - $url = $this->expandUrlParameters($url); $options = $this->parseHttpOptions($options); @@ -966,6 +962,10 @@ protected function expandUrlParameters(string $url) */ protected function parseHttpOptions(array $options) { + if ($this->baseUrl && ! isset($options['base_uri'])) { + $options['base_uri'] = $this->baseUrl; + } + if (isset($options[$this->bodyFormat])) { if ($this->bodyFormat === 'multipart') { $options[$this->bodyFormat] = $this->parseMultipartBodyFormat($options[$this->bodyFormat]); diff --git a/tests/Http/HttpClientTest.php b/tests/Http/HttpClientTest.php index 75fd630a418f..b690f024bff8 100644 --- a/tests/Http/HttpClientTest.php +++ b/tests/Http/HttpClientTest.php @@ -985,23 +985,62 @@ public function testGetWithArrayQueryParamEncodes() }); } - public function testWithBaseUrl() + /** + * @dataProvider baseUriWithRefProvider + */ + public function testWithBaseUrl(string $baseUri, string $ref, string $expectedResolvedUri) { $this->factory->fake(); - $this->factory->baseUrl('http://foo.com/')->get('get'); + $this->factory->baseUrl($baseUri)->get($ref); - $this->factory->assertSent(function (Request $request) { - return $request->url() === 'http://foo.com/get'; + $this->factory->assertSent(function (Request $request) use ($expectedResolvedUri) { + return $request->url() === $expectedResolvedUri; }); + } - $this->factory->fake(); - - $this->factory->baseUrl('http://foo.com/')->get('http://bar.com/get'); - - $this->factory->assertSent(function (Request $request) { - return $request->url() === 'http://bar.com/get'; - }); + /** + * @return array + */ + public static function baseUriWithRefProvider() + { + return [ + [ + '', + 'bar', + 'bar' + ], + [ + 'http://foo.com', + '/bar', + 'http://foo.com/bar' + ], + [ + 'http://foo.com/foo', + '/bar', + 'http://foo.com/bar' + ], + [ + 'http://foo.com/foo', + 'bar', + 'http://foo.com/bar' + ], + [ + 'http://foo.com/foo/', + 'bar', + 'http://foo.com/foo/bar' + ], + [ + 'http://foo.com', + 'http://baz.com', + 'http://baz.com' + ], + [ + 'http://foo.com/?bar', + 'bar', + 'http://foo.com/bar' + ], + ]; } public function testCanConfirmManyHeaders()