From abcf0e7e476126e383a6570cd58bb4c6fca27aea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gu=CC=88nther=20Debrauwer?= Date: Fri, 1 Apr 2022 18:31:13 +0200 Subject: [PATCH] Provide pending request to retry callback --- src/Illuminate/Http/Client/PendingRequest.php | 2 +- tests/Http/HttpClientTest.php | 25 +++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/Illuminate/Http/Client/PendingRequest.php b/src/Illuminate/Http/Client/PendingRequest.php index f987f0aed653..23bd4c119063 100644 --- a/src/Illuminate/Http/Client/PendingRequest.php +++ b/src/Illuminate/Http/Client/PendingRequest.php @@ -715,7 +715,7 @@ public function send(string $method, string $url, array $options = []) $this->populateResponse($response); if (! $response->successful()) { - $shouldRetry = $this->retryWhenCallback ? call_user_func($this->retryWhenCallback, $response->toException()) : true; + $shouldRetry = $this->retryWhenCallback ? call_user_func($this->retryWhenCallback, $response->toException(), $this) : true; if ($attempt < $this->tries && $shouldRetry) { $response->throw(); diff --git a/tests/Http/HttpClientTest.php b/tests/Http/HttpClientTest.php index 5a528d4445de..cdebc45c9bce 100644 --- a/tests/Http/HttpClientTest.php +++ b/tests/Http/HttpClientTest.php @@ -1271,6 +1271,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 testMiddlewareRunsWhenFaked() { $this->factory->fake(function (Request $request) {