Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[8.x] Fix replacing request options #40954

Merged
merged 4 commits into from
Feb 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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