Skip to content

Commit d037fc5

Browse files
[8.x] Fix replacing request options (#40954)
* Fix replacing request options * wip * keep using array merge on headers and cookies * formatting Co-authored-by: Taylor Otwell <[email protected]>
1 parent c48540d commit d037fc5

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
lines changed

src/Illuminate/Http/Client/PendingRequest.php

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -464,15 +464,15 @@ public function retry(int $times, int $sleep = 0, ?callable $when = null)
464464
}
465465

466466
/**
467-
* Merge new options into the client.
467+
* Replace the specified options on the request.
468468
*
469469
* @param array $options
470470
* @return $this
471471
*/
472472
public function withOptions(array $options)
473473
{
474474
return tap($this, function ($request) use ($options) {
475-
return $this->options = array_merge_recursive($this->options, $options);
475+
return $this->options = array_replace_recursive($this->options, $options);
476476
});
477477
}
478478

@@ -980,14 +980,14 @@ public function runBeforeSendingCallbacks($request, array $options)
980980
}
981981

982982
/**
983-
* Merge the given options with the current request options.
983+
* Replace the given options with the current request options.
984984
*
985985
* @param array $options
986986
* @return array
987987
*/
988988
public function mergeOptions(...$options)
989989
{
990-
return array_merge_recursive($this->options, ...$options);
990+
return array_replace_recursive($this->options, ...$options);
991991
}
992992

993993
/**
@@ -1093,4 +1093,14 @@ public function setHandler($handler)
10931093

10941094
return $this;
10951095
}
1096+
1097+
/**
1098+
* Get the pending request options.
1099+
*
1100+
* @return array
1101+
*/
1102+
public function getOptions()
1103+
{
1104+
return $this->options;
1105+
}
10961106
}

tests/Http/HttpClientTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -941,6 +941,19 @@ public function testClientCanBeSet()
941941
$this->assertSame($client, $request->buildClient());
942942
}
943943

944+
public function testRequestsCanReplaceOptions()
945+
{
946+
$request = new PendingRequest($this->factory);
947+
948+
$request = $request->withOptions(['http_errors' => true, 'connect_timeout' => 10]);
949+
950+
$this->assertSame(['http_errors' => true, 'connect_timeout' => 10], $request->getOptions());
951+
952+
$request = $request->withOptions(['connect_timeout' => 20]);
953+
954+
$this->assertSame(['http_errors' => true, 'connect_timeout' => 20], $request->getOptions());
955+
}
956+
944957
public function testMultipleRequestsAreSentInThePool()
945958
{
946959
$this->factory->fake([

0 commit comments

Comments
 (0)