Skip to content

Commit

Permalink
Added User-Agent
Browse files Browse the repository at this point in the history
  • Loading branch information
ezimuel committed Jul 18, 2019
1 parent 8b36458 commit 26da9a3
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/Elasticsearch/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
*/
class Client
{
const VERSION = '5.5.0';

/**
* @var Transport
*/
Expand Down Expand Up @@ -1564,7 +1566,7 @@ private function verifyNotNullOrEmpty($name, $var)

/**
* @param $endpoint AbstractEndpoint
*
*
* @throws \Exception
* @return array
*/
Expand Down
20 changes: 20 additions & 0 deletions src/Elasticsearch/Connections/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,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 @@ -120,6 +121,15 @@ public function __construct($handler, $hostDetails, $connectionParams,
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 All @@ -135,6 +145,16 @@ public function __construct($handler, $hostDetails, $connectionParams,
$this->handler = $this->wrapHandler($handler, $log, $trace);
}

/**
* Get the HTTP headers
*
* @return array
*/
public function getHeaders()
{
return $this->headers;
}

/**
* @param $method
* @param $uri
Expand Down
80 changes: 80 additions & 0 deletions tests/Elasticsearch/Tests/Connections/ConnectionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php
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 26da9a3

Please sign in to comment.