Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Omniphx/Forrest/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ public function request($url, $options)
$this->refresh();

$this->url = $url;
$this->options = array_replace_recursive($this->settings['defaults'], $options);

return $this->handleRequest();
}
Expand Down
18 changes: 18 additions & 0 deletions tests/Fixtures/ResourceRefreshingClient.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Tests\Fixtures;

class ResourceRefreshingClient extends InspectableClient
{
/**
* Mirrors the side effect of an OAuth refresh: re-fetching the resource
* listing issues a nested request that overwrites $this->options,
* which Client::request() must restore before retrying.
*/
public function refresh()
{
parent::refresh();

$this->resources(['format' => 'json']);
}
}
51 changes: 51 additions & 0 deletions tests/Unit/Authentications/AuthenticationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,57 @@ public function testOauthJwtAuthenticateStoresReturnedToken(): void
$this->addToAssertionCount(1);
}

public function testOauthJwtRetryAfterTokenExpiryRepeatsOriginalRequest(): void
{
// OAuthJWT::refresh() re-runs authenticate(), which re-fetches the version and resource
// listings. Those nested requests overwrite the client's in-flight request state, so
// the retry after a mid-request 401 must still repeat the original method and body.
$calls = [];

$http = $this->createMock(ClientInterface::class);
$http->expects($this->exactly(5))
->method('request')
->willReturnCallback(function ($method, $url, $options) use (&$calls) {
$calls[] = ['method' => $method, 'url' => $url, 'options' => $options];

return match (count($calls)) {
// initial POST: token expired
1 => throw $this->requestException(401),
// refresh -> authenticate(): new token
2 => $this->jsonResponse([
'access_token' => 'token',
'instance_url' => 'https://instance.salesforce.com',
'id' => 'https://login.salesforce.com/id/org/user',
'token_type' => 'Bearer',
]),
// refresh -> storeVersion()
3 => $this->jsonResponse([
['label' => 'Winter 24', 'url' => '/services/data/v59.0', 'version' => '59.0'],
]),
// refresh -> storeResources()
4 => $this->jsonResponse(['query' => '/services/data/v59.0/query']),
// retried POST succeeds
default => $this->jsonResponse(['id' => '001']),
};
});

$client = $this->makePasswordStyleClient(OAuthJWT::class, $http, $this->tokenRepo());

$response = $client->sobjects('Account', [
'method' => 'post',
'body' => ['Name' => 'Dunder Mifflin'],
]);

$this->assertSame(['id' => '001'], $response);

// The retry (fifth HTTP call) must repeat the original request the refresh interrupted.
$initial = $calls[0];
$retry = $calls[4];
$this->assertSame('post', $retry['method']);
$this->assertSame($initial['url'], $retry['url']);
$this->assertSame('{"Name":"Dunder Mifflin"}', $retry['options']['body']);
}

public function testUserPasswordSoapTransformsSoapLoginResponses(): void
{
$http = $this->createMock(ClientInterface::class);
Expand Down
43 changes: 41 additions & 2 deletions tests/Unit/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Omniphx\Forrest\Interfaces\RepositoryInterface;
use Omniphx\Forrest\Interfaces\ResourceRepositoryInterface;
use Tests\Fixtures\InspectableClient;
use Tests\Fixtures\ResourceRefreshingClient;
use Tests\TestCase;

class ClientTest extends TestCase
Expand Down Expand Up @@ -67,6 +68,44 @@ public function testRequestRetriesAfterTokenExpiry(): void
$this->assertSame(1, $client->refreshCalls);
}

public function testRequestRestoresOriginalUrlAndOptionsAfterTokenExpiry(): void
{
$calls = [];

$http = $this->createMock(ClientInterface::class);
$http->expects($this->exactly(3))
->method('request')
->willReturnCallback(function ($method, $url, $parameters) use (&$calls) {
$calls[] = ['method' => $method, 'url' => $url, 'parameters' => $parameters];

return match (count($calls)) {
// initial POST: token expired
1 => throw $this->requestException(401),
// refresh re-fetches resources
2 => $this->jsonResponse(['sobjects' => '/services/data/v59.0/sobjects']),
// retried POST succeeds
default => $this->jsonResponse(['id' => '001']),
};
});

$client = $this->makeClient($http, clientClass: ResourceRefreshingClient::class);

$response = $client->sobjects('Account', [
'method' => 'post',
'body' => ['Name' => 'Dunder Mifflin'],
]);

$this->assertSame(['id' => '001'], $response);
$this->assertSame(1, $client->refreshCalls);

// The retry (third HTTP call) must repeat the original request url and options
$retry = $calls[2];
$this->assertSame('post', $retry['method']);
$this->assertSame('https://instance.salesforce.com/services/data/v59.0/sobjects/Account', $retry['url']);
$this->assertArrayHasKey('body', $retry['parameters']);
$this->assertSame('{"Name":"Dunder Mifflin"}', $retry['parameters']['body']);
}

public function testRequestCanReturnRawResponses(): void
{
$http = $this->createMock(ClientInterface::class);
Expand Down Expand Up @@ -134,7 +173,7 @@ public function testStaticMacroable(): void
$this->assertSame('macro test', $client::test());
}

private function makeClient(?ClientInterface $http = null, ?EventInterface $event = null, ?RepositoryInterface $tokenRepo = null): InspectableClient
private function makeClient(?ClientInterface $http = null, ?EventInterface $event = null, ?RepositoryInterface $tokenRepo = null, string $clientClass = InspectableClient::class): InspectableClient
{
$http = $http ?: $this->createMock(ClientInterface::class);
$event = $event ?: $this->createStub(EventInterface::class);
Expand All @@ -153,7 +192,7 @@ private function makeClient(?ClientInterface $http = null, ?EventInterface $even
['sobjects', '/services/data/v59.0/sobjects'],
]);

return new InspectableClient(
return new $clientClass(
$http,
$this->createStub(EncryptorInterface::class),
$event,
Expand Down
Loading