From e1f08fa87ff6ed03999094c3bad7bb5440137197 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Peter=20Hosta=C4=8Dn=C3=BD?= Date: Fri, 2 Jul 2021 01:50:49 +0200 Subject: [PATCH 1/2] Repeat the request with new auth headers --- src/Http/RestClient.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/Http/RestClient.php b/src/Http/RestClient.php index a6a43f0..59b90d0 100644 --- a/src/Http/RestClient.php +++ b/src/Http/RestClient.php @@ -182,6 +182,10 @@ private function send(Request $request) $authenticator = $this->getAuthenticator(); if ($authenticator->shouldAutoRefreshAccessToken()) { $authenticator->fetchAccessAndRefreshToken(); + // replace the auth headers in the Request object + foreach ($this->getAuthHeader() as $header => $value) { + $request = $request->withHeader($header, $value); + } $response = $this->client->send($request, $options); } else { throw $e; From d3703cc72cee9c5ab23a8b3541ffad17dab1050e Mon Sep 17 00:00:00 2001 From: Miguel Rosales Sueiro Date: Thu, 15 Jul 2021 13:20:05 +0200 Subject: [PATCH 2/2] Add test. --- src/Http/RestClient.php | 2 +- tests/Http/RestClientTest.php | 9 +++++++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/Http/RestClient.php b/src/Http/RestClient.php index 59b90d0..e459493 100644 --- a/src/Http/RestClient.php +++ b/src/Http/RestClient.php @@ -182,7 +182,7 @@ private function send(Request $request) $authenticator = $this->getAuthenticator(); if ($authenticator->shouldAutoRefreshAccessToken()) { $authenticator->fetchAccessAndRefreshToken(); - // replace the auth headers in the Request object + // Replace the auth headers in the Request object. foreach ($this->getAuthHeader() as $header => $value) { $request = $request->withHeader($header, $value); } diff --git a/tests/Http/RestClientTest.php b/tests/Http/RestClientTest.php index 425223b..00c7bed 100644 --- a/tests/Http/RestClientTest.php +++ b/tests/Http/RestClientTest.php @@ -88,7 +88,7 @@ public function testSendingRequestDoesntRefreshToken() ->andThrow($exception); $this->authenticator->shouldReceive([ - 'getAuthHeader' => [], + 'getAuthHeader' => ['the-header' => 'the-value'], 'shouldAutoRefreshAccessToken' => false, ]); $this->authenticator->shouldReceive('fetchAccessAndRefreshToken') @@ -106,7 +106,7 @@ public function testSendingRequestRefreshesToken() ->once(); $this->authenticator->shouldReceive([ - 'getAuthHeader' => [], + 'getAuthHeader' => ['the-header' => 'the-value'], 'shouldAutoRefreshAccessToken' => true, ]); $this->authenticator->shouldReceive('fetchAccessAndRefreshToken') @@ -118,6 +118,11 @@ public function testSendingRequestRefreshesToken() ]; $response = new Response(200, [], json_encode($responseData)); $this->methodsClient->shouldReceive('send') + ->with(\Mockery::on(function (Request $request) { + // Ensure retry request includes new auth headers. + $this->assertSame(['the-value'], $request->getHeader('the-header')); + return true; + }), \Mockery::any()) ->andReturn($response); $restClient = new RestClient($this->methodsClient, $this->authenticator);