Skip to content

Commit

Permalink
fix: assure stream is returned after access token refresh
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-sc committed Feb 14, 2024
1 parent 58eb37d commit a7974f0
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/PodioClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ public function request($method, $url, $attributes = [], $options = [])
// Access token is expired. Try to refresh it.
if ($this->refresh_access_token()) {
// Try the original request again.
return $this->request($method, $original_url, $attributes);
return $this->request($method, $original_url, $attributes, $options);
} else {
$this->clear_authentication();
throw new PodioAuthorizationError($body_str, $response->status, $url);
Expand Down
19 changes: 19 additions & 0 deletions tests/PodioClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use GuzzleHttp\Psr7\Utils;
use PodioClient;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\StreamInterface;

class PodioClientTest extends TestCase
{
Expand Down Expand Up @@ -59,6 +60,24 @@ public function test_clear_authentication_sets_session_manger_auth()
$client->clear_authentication();
}

public function test_should_retry_after_401_and_token_refresh_and_return_stream()
{
$httpClientMock = $this->createMock(Client::class);
$httpClientMock->expects($this->exactly(3))->method('send')->willReturnOnConsecutiveCalls(
new Response(401, [], Utils::streamFor('{"error_description": "expired_token"}')),
new Response(200, [], Utils::streamFor('{"access_token": "new-token", "refresh_token": "new-refresh-token", "expires_in": 42000, "ref": "test-ref", "scope": "test-scope"}')),
new Response(200, [], Utils::streamFor('{"items": []}'))
);
$client = new PodioClient('test-client', 'test-secret');
$client->oauth = new \PodioOAuth('test-token', 'test-refresh-token');
$client->http_client = $httpClientMock;

$result = $client->request('GET', '/test', [], ['return_raw_as_resource_only' => true]);

$this->assertInstanceOf(StreamInterface::class, $result);
$this->assertEquals('{"items": []}', $result->getContents());
}

public function test_throw_exception_on_400()
{
$client = new PodioClient('test-client', 'test-secret');
Expand Down

0 comments on commit a7974f0

Please sign in to comment.