From b754ca6c258a9622aa2a9fb92f1daa1dbe22549b Mon Sep 17 00:00:00 2001 From: Tim MacDonald Date: Mon, 22 Jan 2024 09:58:11 +1100 Subject: [PATCH] [10.x] Global default options for the http factory (#49767) * Global default options for the http factory * formatting --------- Co-authored-by: Taylor Otwell --- src/Illuminate/Http/Client/Factory.php | 22 +++++++++++- tests/Http/HttpClientTest.php | 47 ++++++++++++++++++++++++++ 2 files changed, 68 insertions(+), 1 deletion(-) diff --git a/src/Illuminate/Http/Client/Factory.php b/src/Illuminate/Http/Client/Factory.php index d3411ce18777..4bcd13d21b09 100644 --- a/src/Illuminate/Http/Client/Factory.php +++ b/src/Illuminate/Http/Client/Factory.php @@ -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. * @@ -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. * @@ -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); } /** diff --git a/tests/Http/HttpClientTest.php b/tests/Http/HttpClientTest.php index 6b790aa06534..905c81469e06 100644 --- a/tests/Http/HttpClientTest.php +++ b/tests/Http/HttpClientTest.php @@ -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