Skip to content

Commit

Permalink
Merge pull request #898 from ezimuel/feature/user-agent
Browse files Browse the repository at this point in the history
Added User-Agent
  • Loading branch information
ezimuel authored Jun 13, 2019
2 parents 10a9127 + eb909cc commit e12e308
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/Elasticsearch/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
*/
class Client
{
const VERSION = '7.0.0';

/**
* @var Transport
*/
Expand Down
15 changes: 15 additions & 0 deletions src/Elasticsearch/Connections/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -133,6 +134,15 @@ public function __construct(
unset($connectionParams['client']['headers']);
}

// Add the User-Agent using the format: <client-repo-name>/<client-version> (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) {
Expand Down Expand Up @@ -317,6 +327,11 @@ function (&$value, &$key) {
return $uri ?? '';
}

public function getHeaders(): array
{
return $this->headers;
}

/**
* Log a successful request
*
Expand Down
89 changes: 89 additions & 0 deletions tests/Elasticsearch/Tests/Connections/ConnectionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php

declare(strict_types = 1);

namespace Elasticsearch\Tests\Connections;

use Elasticsearch\Client;
use Elasticsearch\ClientBuilder;
use Elasticsearch\Connections\Connection;
use Elasticsearch\Serializers\SerializerInterface;
use Psr\Log\LoggerInterface;

class ConnectionTest extends \PHPUnit\Framework\TestCase
{
private $logger;
private $trace;
private $serializer;

protected function setUp()
{
$this->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]);
}
}

0 comments on commit e12e308

Please sign in to comment.