Skip to content

Commit

Permalink
[10.x] Global default options for the http factory (#49767)
Browse files Browse the repository at this point in the history
* Global default options for the http factory

* formatting

---------

Co-authored-by: Taylor Otwell <[email protected]>
  • Loading branch information
timacdonald and taylorotwell authored Jan 21, 2024
1 parent 37eea84 commit b754ca6
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 1 deletion.
22 changes: 21 additions & 1 deletion src/Illuminate/Http/Client/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ class Factory
*/
protected $globalMiddleware = [];

/**
* The options to apply to every request.
*
* @var array
*/
protected $globalOptions = [];

/**
* The stub callables that will handle requests.
*
Expand Down Expand Up @@ -123,6 +130,19 @@ public function globalResponseMiddleware($middleware)
return $this;
}

/**
* Set the options to apply to every request.
*
* @param array $options
* @return $this
*/
public function globalOptions($options)
{
$this->globalOptions = $options;

return $this;
}

/**
* Create a new response instance for use during stubbing.
*
Expand Down Expand Up @@ -400,7 +420,7 @@ public function recorded($callback = null)
*/
protected function newPendingRequest()
{
return new PendingRequest($this, $this->globalMiddleware);
return (new PendingRequest($this, $this->globalMiddleware))->withOptions($this->globalOptions);
}

/**
Expand Down
47 changes: 47 additions & 0 deletions tests/Http/HttpClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2680,6 +2680,53 @@ public function testItCanReturnCustomResponseClass(): void
$this->assertInstanceOf(TestResponse::class, $response);
$this->assertSame('expected content', $response->body());
}

public function testItCanHaveGlobalDefaultValues()
{
$factory = new Factory;
$timeout = null;
$allowRedirects = null;
$headers = null;
$factory->fake(function ($request, $options) use (&$timeout, &$allowRedirects, &$headers, $factory) {
$timeout = $options['timeout'];
$allowRedirects = $options['allow_redirects'];
$headers = $request->headers();

return $factory->response('');
});

$factory->get('https://laravel.com');
$this->assertSame(30, $timeout);
$this->assertSame(['max' => 5, 'protocols' => ['http', 'https'], 'strict' => false, 'referer' => false, 'track_redirects' => false], $allowRedirects);
$this->assertNull($headers['X-Foo'] ?? null);

$factory->globalOptions([
'timeout' => 5,
'allow_redirects' => false,
'headers' => [
'X-Foo' => 'true',
],
]);

$factory->get('https://laravel.com');
$this->assertSame(5, $timeout);
$this->assertFalse($allowRedirects);
$this->assertSame(['true'], $headers['X-Foo']);

$factory->globalOptions([
'timeout' => 10,
'headers' => [
'X-Foo' => 'false',
'X-Bar' => 'true',
],
]);

$factory->get('https://laravel.com');
$this->assertSame(10, $timeout);
$this->assertSame(['max' => 5, 'protocols' => ['http', 'https'], 'strict' => false, 'referer' => false, 'track_redirects' => false], $allowRedirects);
$this->assertSame(['false'], $headers['X-Foo']);
$this->assertSame(['true'], $headers['X-Bar']);
}
}

class CustomFactory extends Factory
Expand Down

0 comments on commit b754ca6

Please sign in to comment.