Skip to content

Commit 71bb397

Browse files
authored
Provide pending request to retry callback (#41779)
1 parent 8326ba8 commit 71bb397

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

src/Illuminate/Http/Client/PendingRequest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -719,7 +719,7 @@ public function send(string $method, string $url, array $options = [])
719719

720720
if (! $response->successful()) {
721721
try {
722-
$shouldRetry = $this->retryWhenCallback ? call_user_func($this->retryWhenCallback, $response->toException()) : true;
722+
$shouldRetry = $this->retryWhenCallback ? call_user_func($this->retryWhenCallback, $response->toException(), $this) : true;
723723
} catch (Exception $exception) {
724724
$shouldRetry = false;
725725

tests/Http/HttpClientTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1290,6 +1290,31 @@ public function testRequestExceptionIsNotThrownWithoutRetriesIfRetryNotNecessary
12901290
$this->factory->assertSentCount(1);
12911291
}
12921292

1293+
public function testRequestCanBeModifiedInRetryCallback()
1294+
{
1295+
$this->factory->fake([
1296+
'*' => $this->factory->sequence()
1297+
->push(['error'], 500)
1298+
->push(['ok'], 200),
1299+
]);
1300+
1301+
$response = $this->factory
1302+
->retry(2, 1000, function ($exception, $request) {
1303+
$this->assertInstanceOf(PendingRequest::class, $request);
1304+
1305+
$request->withHeaders(['Foo' => 'Bar']);
1306+
1307+
return true;
1308+
}, false)
1309+
->get('http://foo.com/get');
1310+
1311+
$this->assertTrue($response->successful());
1312+
1313+
$this->factory->assertSent(function (Request $request) {
1314+
return $request->hasHeader('Foo') && $request->header('Foo') === ['Bar'];
1315+
});
1316+
}
1317+
12931318
public function testExceptionThrownInRetryCallbackWithoutRetrying()
12941319
{
12951320
$this->factory->fake([

0 commit comments

Comments
 (0)