Skip to content

Commit

Permalink
Merge pull request #280 from scr4bble/patch-5
Browse files Browse the repository at this point in the history
Retry the request with new auth headers.
  • Loading branch information
miguelrs authored Jul 15, 2021
2 parents eae536d + 4fea459 commit 3a0984d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
4 changes: 4 additions & 0 deletions src/Http/RestClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
9 changes: 7 additions & 2 deletions tests/Http/RestClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public function testSendingRequestDoesntRefreshToken()
->andThrow($exception);

$this->authenticator->shouldReceive([
'getAuthHeader' => [],
'getAuthHeader' => ['the-header' => 'the-value'],
'shouldAutoRefreshAccessToken' => false,
]);
$this->authenticator->shouldReceive('fetchAccessAndRefreshToken')
Expand All @@ -106,7 +106,7 @@ public function testSendingRequestRefreshesToken()
->once();

$this->authenticator->shouldReceive([
'getAuthHeader' => [],
'getAuthHeader' => ['the-header' => 'the-value'],
'shouldAutoRefreshAccessToken' => true,
]);
$this->authenticator->shouldReceive('fetchAccessAndRefreshToken')
Expand All @@ -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);
Expand Down

0 comments on commit 3a0984d

Please sign in to comment.