diff --git a/src/AmznSPAHttp.php b/src/AmznSPAHttp.php index 898c27a..e69aac9 100644 --- a/src/AmznSPAHttp.php +++ b/src/AmznSPAHttp.php @@ -288,9 +288,7 @@ private function refreshRdtToken(string $url, string $method) private function setupHttp(PendingRequest $http, bool $grantless = false, string $url = '', string $method = ''): void { if ($proxy = $this->config->getProxy()) { - $this->http = $http->withHeaders([ - 'Authorization' => "Bearer {$proxy->auth_token}", - ]); + $this->http = $http->withHeaders($proxy->headers); } else { $this->http = $http->withHeaders([ 'x-amz-access-token' => $this->getToken($grantless, $url, $method), diff --git a/src/Data/Proxy.php b/src/Data/Proxy.php index 123fa83..f244dd7 100644 --- a/src/Data/Proxy.php +++ b/src/Data/Proxy.php @@ -6,7 +6,7 @@ class Proxy { public function __construct( public readonly string $url, - public readonly string $auth_token, + public readonly array $headers, ) { } } diff --git a/src/Resources/ResourceGetter.php b/src/Resources/ResourceGetter.php index 58c87ea..40b3d57 100644 --- a/src/Resources/ResourceGetter.php +++ b/src/Resources/ResourceGetter.php @@ -148,9 +148,11 @@ public function getListingsItems(): ListingsItemsResource private function constructResource(string $class, ?string $grantless_resource = null): ResourceContract { + $url = $this->config->getProxy() ? $this->config->getProxy()->url : $this->config->getMarketplace()->getBaseUrl(); + return new $class( $this->validateAndSetupHttpForStandardResource($grantless_resource), - $this->config->getMarketplace()->getBaseUrl(), + $url, ); } diff --git a/tests/Setup/SetupAmznSPAConfig.php b/tests/Setup/SetupAmznSPAConfig.php index 667900a..7155d9a 100644 --- a/tests/Setup/SetupAmznSPAConfig.php +++ b/tests/Setup/SetupAmznSPAConfig.php @@ -6,6 +6,7 @@ use Illuminate\Support\Str; use Jasara\AmznSPA\AmznSPAConfig; use Jasara\AmznSPA\Constants\MarketplacesList; +use Jasara\AmznSPA\Data\Proxy; trait SetupAmznSPAConfig { @@ -31,6 +32,32 @@ public function setupMinimalConfig(string $marketplace_id = null, Factory $http return $config; } + public function setupMinimalProxyConfig( + Proxy $proxy, + string $marketplace_id = null, + Factory $http = null, + ): AmznSPAConfig { + $config = new AmznSPAConfig( + marketplace_id: $marketplace_id ?: MarketplacesList::allIdentifiers()[rand(0, 15)], + application_id: Str::random(), + redirect_url: Str::random() . '.com', + lwa_refresh_token: Str::random(), + lwa_access_token: Str::random(), + grantless_access_token: Str::random(), + aws_access_key: Str::random(), + aws_secret_key: Str::random(), + lwa_client_id: Str::random(), + lwa_client_secret: Str::random(), + proxy: $proxy, + ); + + if ($http) { + $config->setHttp($http); + } + + return $config; + } + public function setupLiveConfig(): AmznSPAConfig { $config = new AmznSPAConfig( diff --git a/tests/Unit/AmznSPAConfigTest.php b/tests/Unit/AmznSPAConfigTest.php index 6f8f60e..51becfa 100644 --- a/tests/Unit/AmznSPAConfigTest.php +++ b/tests/Unit/AmznSPAConfigTest.php @@ -45,9 +45,13 @@ public function testGetNewConfig() $grantless_access_token_expires_at = CarbonImmutable::now()->addSeconds(rand(100, 500)); $restricted_data_token = Str::random(); $restricted_data_token_expires_at = CarbonImmutable::now()->addSeconds(rand(100, 500)); + + $proxy_auth_token = Str::random(); $proxy = new Proxy( url: Str::random(), - auth_token: Str::random(), + headers: [ + 'Authorization' => "Bearer {$proxy_auth_token}", + ], ); $config = new AmznSPAConfig( @@ -99,7 +103,7 @@ public function testGetNewConfig() $this->assertTrue($config->shouldGetRdtTokens()); $this->assertEquals($proxy->url, $config->getProxy()->url); - $this->assertEquals($proxy->auth_token, $config->getProxy()->auth_token); + $this->assertEquals($proxy->headers, $config->getProxy()->headers); } public function testSetters() diff --git a/tests/Unit/AmznSPAHttpTest.php b/tests/Unit/AmznSPAHttpTest.php index b92508e..3a2388a 100644 --- a/tests/Unit/AmznSPAHttpTest.php +++ b/tests/Unit/AmznSPAHttpTest.php @@ -431,6 +431,34 @@ function (Request $request) { $http->assertSentInOrder($request_validation); } + public function testSetProxyHeaders() + { + [$config, $http] = $this->setupConfigWithFakeHttp(['tokens/create-restricted-data-token', 'orders/get-orders']); + + $amzn = new AmznSPA($config); + $amzn = $amzn->usingMarketplace('ATVPDKIKX0DER'); + $amzn->orders->getOrders( + marketplace_ids: ['ATVPDKIKX0DER'], + ); + + $request_validation = [ + function (Request $request) { + $this->assertEquals('POST', $request->method()); + $this->assertEquals('https://sellingpartnerapi-na.amazon.com/tokens/2021-03-01/restrictedDataToken', $request->url()); + + return true; + }, + function (Request $request) { + $this->assertEquals('GET', $request->method()); + $this->assertEquals('https://sellingpartnerapi-na.amazon.com/orders/v0/orders?MarketplaceIds=ATVPDKIKX0DER', urldecode($request->url())); + + return true; + }, + ]; + + $http->assertSentInOrder($request_validation); + } + public function testInvalidPartyId() { $this->expectException(AuthenticationException::class); @@ -484,7 +512,7 @@ public function testSetupHttpProxy() application_id: Str::random(), proxy: new Proxy( url: 'https://www.amazon.com', - auth_token: Str::random(), + headers: [], ) ); diff --git a/tests/Unit/Data/ProxyTest.php b/tests/Unit/Data/ProxyTest.php index cb7178d..d302edd 100644 --- a/tests/Unit/Data/ProxyTest.php +++ b/tests/Unit/Data/ProxyTest.php @@ -12,12 +12,15 @@ class ProxyTest extends UnitTestCase { public function testSetupTokens() { + $proxy_auth_token = Str::random(); $proxy = new Proxy( url: $proxy_url = Str::random(), - auth_token: $proxy_auth_token = Str::random(), + headers: [ + 'Authorization' => "Bearer {$proxy_auth_token}", + ], ); $this->assertEquals($proxy_url, $proxy->url); - $this->assertEquals($proxy_auth_token, $proxy->auth_token); + $this->assertEquals("Bearer {$proxy_auth_token}", $proxy->headers['Authorization']); } }