diff --git a/src/Client.php b/src/Client.php index 031fbc245..a68c481a8 100644 --- a/src/Client.php +++ b/src/Client.php @@ -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'])); diff --git a/tests/ClientTest.php b/tests/ClientTest.php index 2c717b062..f7ac85929 100644 --- a/tests/ClientTest.php +++ b/tests/ClientTest.php @@ -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()); + } }