Skip to content

Commit

Permalink
fix: If withProxy(apiKey) is set the client does not set custom heade…
Browse files Browse the repository at this point in the history
…rs (#191)

Since our README explicitly states that using withProxy($apiKey) is in
lieu of using custom headers for the Authorization header, our SDK
now makes sure to check if the proxyApiKey is set, and if so, uses it
to set the Authorization header also for client registration and client
metrics.
  • Loading branch information
chriswk authored Jan 10, 2024
1 parent 102057f commit 22fb05e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/UnleashBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,7 @@ public function build(): Unleash

if ($this->proxyKey !== null) {
$configuration->setProxyKey($this->proxyKey);
$configuration->setHeaders(array_merge($this->headers, ['Authorization' => $this->proxyKey]));
$proxyRepository = new DefaultUnleashProxyRepository(
$configuration,
$httpClient,
Expand Down
21 changes: 21 additions & 0 deletions tests/UnleashBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -883,6 +883,27 @@ public function testBuilderWithProxyKeyYieldsProxyUnleash()
self::assertInstanceOf(DefaultProxyUnleash::class, $base->build());
}

public function testbuilderWithProxyKeyYieldsAuthorizationHeader()
{
$base = $this->instance->withProxy('proxy-key')->withAppUrl('https://localhost')->withInstanceId('test')->withAppName('test');
$unleash = $base->build();
$reflection = new ReflectionObject($unleash);
$repositoryProperty = $reflection->getProperty('repository');
$repositoryProperty->setAccessible(true);
$repository = $repositoryProperty->getValue($unleash);

$reflection = new ReflectionObject($repository);
$configurationProperty = $reflection->getProperty('configuration');
$configurationProperty->setAccessible(true);
$configuration = $configurationProperty->getValue($repository);

$reflection = new ReflectionObject($configuration);
$headersPropertyBuilt = $reflection->getProperty('headers');
$headersPropertyBuilt->setAccessible(true);
$headersBuilt = $headersPropertyBuilt->getValue($configuration);
self::assertArrayHasKey('Authorization', $headersBuilt);
}

private function getConfiguration(DefaultUnleash $unleash): UnleashConfiguration
{
$configurationProperty = (new ReflectionObject($unleash))->getProperty('configuration');
Expand Down

0 comments on commit 22fb05e

Please sign in to comment.