Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Guzzle to resolve the URL against the base URL #51799

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/Illuminate/Http/Client/PendingRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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]);
Expand Down
60 changes: 49 additions & 11 deletions tests/Http/HttpClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
use Mockery as m;
use OutOfBoundsException;
use PHPUnit\Framework\AssertionFailedError;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
Expand Down Expand Up @@ -985,23 +986,60 @@ 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()
Expand Down