Skip to content

Commit

Permalink
Add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sidz committed Jan 27, 2024
1 parent ff9abe7 commit cdcf049
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -637,7 +637,7 @@ protected function _buildTransport(array $config): Transport
// API key
if (!empty($config['api_key'])) {
if (!empty($config['username'])) {
throw new InvalidException('You cannot use APIKey and Basic Authentication together');
throw new InvalidException('You cannot use APIKey and Basic Authentication together.');
}

$transport->setHeader('Authorization', \sprintf('ApiKey %s', $config['api_key']));
Expand Down
20 changes: 20 additions & 0 deletions tests/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,4 +154,24 @@ public function testClientConnectionWithCloudId(): void

$this->assertEquals('4de46ced8d8d459696e544fe5f32b999.eu-central-1.aws.cloud.es.io', $node->getUri()->getHost());
}

public function testItThrowsAnExceptionWhenApiKeyAndUserNameInConfigAtTheSameTime(): void
{
$this->expectException(InvalidException::class);
$this->expectExceptionMessage('You cannot use APIKey and Basic Authentication together.');

new Client([
'username' => 'user',
'api_key' => 'key',
]);
}

public function testItSetsAuthorizationHeaderIfApiKeyPassed(): void
{
$apiKey = 'key';

$client = new Client(['api_key' => $apiKey]);

self::assertSame(['Authorization' => \sprintf('ApiKey %s', $apiKey)], $client->getTransport()->getHeaders());
}
}

0 comments on commit cdcf049

Please sign in to comment.