Skip to content

Added User-Agent #898

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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]);
}
}