Skip to content

Commit

Permalink
Provide pending request to retry callback (#41779)
Browse files Browse the repository at this point in the history
  • Loading branch information
gdebrauwer authored Apr 2, 2022
1 parent 8326ba8 commit 71bb397
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Illuminate/Http/Client/PendingRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -719,7 +719,7 @@ public function send(string $method, string $url, array $options = [])

if (! $response->successful()) {
try {
$shouldRetry = $this->retryWhenCallback ? call_user_func($this->retryWhenCallback, $response->toException()) : true;
$shouldRetry = $this->retryWhenCallback ? call_user_func($this->retryWhenCallback, $response->toException(), $this) : true;
} catch (Exception $exception) {
$shouldRetry = false;

Expand Down
25 changes: 25 additions & 0 deletions tests/Http/HttpClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1290,6 +1290,31 @@ public function testRequestExceptionIsNotThrownWithoutRetriesIfRetryNotNecessary
$this->factory->assertSentCount(1);
}

public function testRequestCanBeModifiedInRetryCallback()
{
$this->factory->fake([
'*' => $this->factory->sequence()
->push(['error'], 500)
->push(['ok'], 200),
]);

$response = $this->factory
->retry(2, 1000, function ($exception, $request) {
$this->assertInstanceOf(PendingRequest::class, $request);

$request->withHeaders(['Foo' => 'Bar']);

return true;
}, false)
->get('http://foo.com/get');

$this->assertTrue($response->successful());

$this->factory->assertSent(function (Request $request) {
return $request->hasHeader('Foo') && $request->header('Foo') === ['Bar'];
});
}

public function testExceptionThrownInRetryCallbackWithoutRetrying()
{
$this->factory->fake([
Expand Down

0 comments on commit 71bb397

Please sign in to comment.