Skip to content

[10.x] Add global default options feature to HTTP client. #46637

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

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
63 changes: 62 additions & 1 deletion src/Illuminate/Http/Client/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use GuzzleHttp\Psr7\Response as Psr7Response;
use GuzzleHttp\TransferStats;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use Illuminate\Support\Traits\Macroable;
use PHPUnit\Framework\Assert as PHPUnit;
Expand Down Expand Up @@ -62,6 +63,20 @@ class Factory
*/
protected $preventStrayRequests = false;

/**
* The default options for the request.
*
* @var array
*/
protected $defaultOptions = [];

/**
* Whether to ignore the default options for the next request.
*
* @var bool
*/
protected $ignoreDefaultOptions = false;

/**
* Create a new factory instance.
*
Expand Down Expand Up @@ -357,6 +372,48 @@ protected function newPendingRequest()
return new PendingRequest($this);
}

/**
* Ignore the default options for the next request.
*
* @return void
*/
public function ignoreDefaultOptions()
{
$this->ignoreDefaultOptions = true;
}

/**
* Set the default options for the request.
*
* @param array $options
* @return $this
*/
public function withDefaultOptions(array $options)
{
$this->defaultOptions = $options;

return $this;
}

/**
* Remove the specified keys from the default options.
*
* @param $keys
* @return $this
*/
public function withoutDefaultOptions($keys = null)
{
if (is_null($keys)) {
return $this->ignoreDefaultOptions();
}

foreach (Arr::wrap($keys) as $key) {
Arr::forget($this->defaultOptions, $key);
}

return $this;
}

/**
* Get the current event dispatcher implementation.
*
Expand All @@ -381,7 +438,11 @@ public function __call($method, $parameters)
}

return tap($this->newPendingRequest(), function ($request) {
$request->stub($this->stubCallbacks)->preventStrayRequests($this->preventStrayRequests);
$request->when($this->defaultOptions && ! $this->ignoreDefaultOptions, function ($request) {
$request->withOptions($this->defaultOptions);
})
->stub($this->stubCallbacks)
->preventStrayRequests($this->preventStrayRequests);
})->{$method}(...$parameters);
}
}
108 changes: 108 additions & 0 deletions tests/Http/HttpClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2215,4 +2215,112 @@ public function testTheTransferStatsAreCustomizableOnFake(): void

$this->assertTrue($onStatsFunctionCalled);
}

public function testDefaultOptionsAreApplied()
{
$this->factory->fake();

$this->factory->withDefaultOptions([
'headers' => [
'X-Custom-Header' => 'custom-value',
],
]);

$this->factory->get('https://foo.com');

$this->factory->assertSent(function ($request) {
return $request->hasHeader('X-Custom-Header', 'custom-value');
});
}

public function testDefaultOptionsWillBeIgnored()
{
$this->factory->fake();

$this->factory->withDefaultOptions([
'headers' => [
'X-Custom-Header' => 'custom-value',
],
]);

$this->factory->withoutDefaultOptions();

$this->factory->get('https://foo.com');

$this->factory->assertNotSent(function ($request) {
return $request->hasHeader('X-Custom-Header', 'custom-value');
});
}

public function testChainAdditionalOptionsBesideDefaultOptions()
{
$this->factory->fake();

$this->factory->withDefaultOptions([
'headers' => [
'X-Custom-Header' => 'custom-value',
],
]);

$this->factory->withOptions([
'headers' => [
'X-Another-Custom-Header' => 'another-custom-value',
],
])->get('https://foo.com');

$this->factory->assertSent(function ($request) {
return $request->hasHeader('X-Custom-Header', 'custom-value') &&
$request->hasHeader('X-Another-Custom-Header', 'another-custom-value');
});
}

public function testIgnoreSpecificDefaultOption()
{
$this->factory->fake();

$this->factory->withDefaultOptions([
'headers' => [
'X-Custom-Header' => 'custom-value',
],
]);

$this->factory->withoutDefaultOptions('headers');

$this->factory->get('https://foo.com');

$this->factory->assertNotSent(function ($request) {
return $request->hasHeader('X-Custom-Header', 'custom-value');
});
}

public function testIgnoreSpecificDefaultOptionUsingDotNotation()
{
$this->factory->fake();

$this->factory->withDefaultOptions([
'headers' => [
'X-Custom-Header' => 'custom-value',
'X-Another-Custom-Header' => 'another-custom-value',
],
]);

$this->factory->withoutDefaultOptions('headers.X-Another-Custom-Header');

$this->factory->get('https://foo.com');

$this->factory->assertNotSent(function ($request) {
return $request->hasHeader('X-Another-Custom-Header', 'another-custom-value');
});
}

public function testUsageOfTheClientWithoutDefaultOptionsSet()
{
$this->factory->fake([
'*' => $this->factory->response(),
]);

$response = $this->factory->get('https://foo.com');

$this->assertEquals(200, $response->status());
}
}