Skip to content

Commit

Permalink
[8.x] Fix replacing request options (#40954)
Browse files Browse the repository at this point in the history
* Fix replacing request options

* wip

* keep using array merge on headers and cookies

* formatting

Co-authored-by: Taylor Otwell <taylor@laravel.com>
  • Loading branch information
driesvints and taylorotwell committed Feb 11, 2022
1 parent c48540d commit d037fc5
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
18 changes: 14 additions & 4 deletions src/Illuminate/Http/Client/PendingRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -464,15 +464,15 @@ public function retry(int $times, int $sleep = 0, ?callable $when = null)
}

/**
* Merge new options into the client.
* Replace the specified options on the request.
*
* @param array $options
* @return $this
*/
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);
});
}

Expand Down Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -1093,4 +1093,14 @@ public function setHandler($handler)

return $this;
}

/**
* Get the pending request options.
*
* @return array
*/
public function getOptions()
{
return $this->options;
}
}
13 changes: 13 additions & 0 deletions tests/Http/HttpClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -941,6 +941,19 @@ public function testClientCanBeSet()
$this->assertSame($client, $request->buildClient());
}

public function testRequestsCanReplaceOptions()
{
$request = new PendingRequest($this->factory);

$request = $request->withOptions(['http_errors' => true, 'connect_timeout' => 10]);

$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()
{
$this->factory->fake([
Expand Down

0 comments on commit d037fc5

Please sign in to comment.