From f2804b18f9cbcc22f122d8045eca9cd34d0657b0 Mon Sep 17 00:00:00 2001 From: Dries Vints Date: Fri, 11 Feb 2022 09:59:11 +0100 Subject: [PATCH 1/4] Fix replacing request options --- src/Illuminate/Http/Client/PendingRequest.php | 22 ++++++++++++++----- tests/Http/HttpClientTest.php | 9 ++++++++ 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/src/Illuminate/Http/Client/PendingRequest.php b/src/Illuminate/Http/Client/PendingRequest.php index 95194911e0dc..1d72ee7d76d8 100644 --- a/src/Illuminate/Http/Client/PendingRequest.php +++ b/src/Illuminate/Http/Client/PendingRequest.php @@ -319,7 +319,7 @@ public function accept($contentType) public function withHeaders(array $headers) { return tap($this, function ($request) use ($headers) { - return $this->options = array_merge_recursive($this->options, [ + return $this->options = array_replace_recursive($this->options, [ 'headers' => $headers, ]); }); @@ -390,7 +390,7 @@ public function withUserAgent($userAgent) public function withCookies(array $cookies, string $domain) { return tap($this, function ($request) use ($cookies, $domain) { - return $this->options = array_merge_recursive($this->options, [ + return $this->options = array_replace_recursive($this->options, [ 'cookies' => CookieJar::fromArray($cookies, $domain), ]); }); @@ -464,7 +464,7 @@ public function retry(int $times, int $sleep = 0, ?callable $when = null) } /** - * Merge new options into the client. + * Replace new options into the client. * * @param array $options * @return $this @@ -472,7 +472,7 @@ public function retry(int $times, int $sleep = 0, ?callable $when = null) public function withOptions(array $options) { return tap($this, function ($request) use ($options) { - return $this->options = array_merge_recursive($this->options, $options); + return $this->options = array_replace_recursive($this->options, $options); }); } @@ -980,14 +980,14 @@ public function runBeforeSendingCallbacks($request, array $options) } /** - * Merge the given options with the current request options. + * Replace the given options with the current request options. * * @param array $options * @return array */ public function mergeOptions(...$options) { - return array_merge_recursive($this->options, ...$options); + return array_replace_recursive($this->options, ...$options); } /** @@ -1093,4 +1093,14 @@ public function setHandler($handler) return $this; } + + /** + * Get the pending request options. + * + * @return array + */ + public function getOptions() + { + return $this->options; + } } diff --git a/tests/Http/HttpClientTest.php b/tests/Http/HttpClientTest.php index 1f326ae24d52..409ddda1b216 100644 --- a/tests/Http/HttpClientTest.php +++ b/tests/Http/HttpClientTest.php @@ -941,6 +941,15 @@ public function testClientCanBeSet() $this->assertSame($client, $request->buildClient()); } + public function testRequestsCanReplaceOptions() + { + $request = new PendingRequest($this->factory); + + $request = $request->withOptions(['http_errors' => true]); + + $this->assertSame(['http_errors' => true], $request->getOptions()); + } + public function testMultipleRequestsAreSentInThePool() { $this->factory->fake([ From f523ece244c2acfeef0f49dc7a9d56d9e6e504be Mon Sep 17 00:00:00 2001 From: Dries Vints Date: Fri, 11 Feb 2022 10:06:24 +0100 Subject: [PATCH 2/4] wip --- tests/Http/HttpClientTest.php | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/tests/Http/HttpClientTest.php b/tests/Http/HttpClientTest.php index 409ddda1b216..338276362425 100644 --- a/tests/Http/HttpClientTest.php +++ b/tests/Http/HttpClientTest.php @@ -945,9 +945,13 @@ public function testRequestsCanReplaceOptions() { $request = new PendingRequest($this->factory); - $request = $request->withOptions(['http_errors' => true]); + $request = $request->withOptions(['http_errors' => true, 'connect_timeout' => 10]); - $this->assertSame(['http_errors' => true], $request->getOptions()); + $this->assertSame(['http_errors' => true, 'connect_timeout' => 10], $request->getOptions()); + + $request = $request->withOptions(['connect_timeout' => 20]); + + $this->assertSame(['http_errors' => true, 'connect_timeout' => 20], $request->getOptions()); } public function testMultipleRequestsAreSentInThePool() From 5a2331246d30c18487b05c1c342fec9463214cd7 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Fri, 11 Feb 2022 08:57:16 -0600 Subject: [PATCH 3/4] keep using array merge on headers and cookies --- src/Illuminate/Http/Client/PendingRequest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Illuminate/Http/Client/PendingRequest.php b/src/Illuminate/Http/Client/PendingRequest.php index 1d72ee7d76d8..c4ab37df7596 100644 --- a/src/Illuminate/Http/Client/PendingRequest.php +++ b/src/Illuminate/Http/Client/PendingRequest.php @@ -319,7 +319,7 @@ public function accept($contentType) public function withHeaders(array $headers) { return tap($this, function ($request) use ($headers) { - return $this->options = array_replace_recursive($this->options, [ + return $this->options = array_merge_recursive($this->options, [ 'headers' => $headers, ]); }); @@ -390,7 +390,7 @@ public function withUserAgent($userAgent) public function withCookies(array $cookies, string $domain) { return tap($this, function ($request) use ($cookies, $domain) { - return $this->options = array_replace_recursive($this->options, [ + return $this->options = array_merge_recursive($this->options, [ 'cookies' => CookieJar::fromArray($cookies, $domain), ]); }); From d20a5a4fffbf425a295ef58fe5694830c9e47b8d Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Fri, 11 Feb 2022 09:01:53 -0600 Subject: [PATCH 4/4] formatting --- src/Illuminate/Http/Client/PendingRequest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Illuminate/Http/Client/PendingRequest.php b/src/Illuminate/Http/Client/PendingRequest.php index c4ab37df7596..78cca8b3ddce 100644 --- a/src/Illuminate/Http/Client/PendingRequest.php +++ b/src/Illuminate/Http/Client/PendingRequest.php @@ -464,7 +464,7 @@ public function retry(int $times, int $sleep = 0, ?callable $when = null) } /** - * Replace new options into the client. + * Replace the specified options on the request. * * @param array $options * @return $this