diff --git a/src/Elasticsearch/Client.php b/src/Elasticsearch/Client.php index a6b7a9b42..ec4abe36c 100644 --- a/src/Elasticsearch/Client.php +++ b/src/Elasticsearch/Client.php @@ -34,6 +34,8 @@ */ class Client { + const VERSION = '7.0.0'; + /** * @var Transport */ diff --git a/src/Elasticsearch/Connections/Connection.php b/src/Elasticsearch/Connections/Connection.php index 3a0151487..0a1a63168 100644 --- a/src/Elasticsearch/Connections/Connection.php +++ b/src/Elasticsearch/Connections/Connection.php @@ -4,6 +4,7 @@ namespace Elasticsearch\Connections; +use Elasticsearch\Client; use Elasticsearch\Common\Exceptions\AlreadyExpiredException; use Elasticsearch\Common\Exceptions\BadRequest400Exception; use Elasticsearch\Common\Exceptions\Conflict409Exception; @@ -133,6 +134,15 @@ public function __construct( unset($connectionParams['client']['headers']); } + // Add the User-Agent using the format: / (metadata-values) + $this->headers['User-Agent'] = [sprintf( + "elasticsearch-php/%s (%s %s; PHP %s)", + Client::VERSION, + php_uname("s"), + php_uname("r"), + phpversion() + )]; + $host = $hostDetails['host'].':'.$hostDetails['port']; $path = null; if (isset($hostDetails['path']) === true) { @@ -317,6 +327,11 @@ function (&$value, &$key) { return $uri ?? ''; } + public function getHeaders(): array + { + return $this->headers; + } + /** * Log a successful request * diff --git a/tests/Elasticsearch/Tests/Connections/ConnectionTest.php b/tests/Elasticsearch/Tests/Connections/ConnectionTest.php new file mode 100644 index 000000000..a9eac8a68 --- /dev/null +++ b/tests/Elasticsearch/Tests/Connections/ConnectionTest.php @@ -0,0 +1,89 @@ +logger = $this->createMock(LoggerInterface::class); + $this->trace = $this->createMock(LoggerInterface::class); + $this->serializer = $this->createMock(SerializerInterface::class); + } + + public function testConstructor() + { + $params = []; + $host = [ + 'host' => 'localhost' + ]; + + $connection = new Connection( + function () { + }, + $host, + $params, + $this->serializer, + $this->logger, + $this->trace + ); + + $this->assertInstanceOf(Connection::class, $connection); + } + + public function testGetHeadersContainUserAgent() + { + $params = []; + $host = [ + 'host' => 'localhost' + ]; + + $connection = new Connection( + function () { + }, + $host, + $params, + $this->serializer, + $this->logger, + $this->trace + ); + + $headers = $connection->getHeaders(); + + $this->assertArrayHasKey('User-Agent', $headers); + $this->assertContains('elasticsearch-php/'. Client::VERSION, $headers['User-Agent'][0]); + } + + public function testUserAgentHeaderIsSent() + { + $params = []; + $host = [ + 'host' => 'localhost' + ]; + + $connection = new Connection( + ClientBuilder::defaultHandler(), + $host, + $params, + $this->serializer, + $this->logger, + $this->trace + ); + $result = $connection->performRequest('GET', '/'); + $request = $connection->getLastRequestInfo()['request']; + $this->assertArrayHasKey('User-Agent', $request['headers']); + $this->assertContains('elasticsearch-php/'. Client::VERSION, $request['headers']['User-Agent'][0]); + } +}