Skip to content

Commit 1119817

Browse files
committed
Added User-Agent
1 parent 10a9127 commit 1119817

File tree

3 files changed

+104
-0
lines changed

3 files changed

+104
-0
lines changed

src/Elasticsearch/Client.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
*/
3535
class Client
3636
{
37+
const VERSION = '7.0.0';
38+
3739
/**
3840
* @var Transport
3941
*/

src/Elasticsearch/Connections/Connection.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Elasticsearch\Connections;
66

7+
use Elasticsearch\Client;
78
use Elasticsearch\Common\Exceptions\AlreadyExpiredException;
89
use Elasticsearch\Common\Exceptions\BadRequest400Exception;
910
use Elasticsearch\Common\Exceptions\Conflict409Exception;
@@ -133,6 +134,15 @@ public function __construct(
133134
unset($connectionParams['client']['headers']);
134135
}
135136

137+
// Add the User-Agent using the format: <client-repo-name>/<client-version> (metadata-values)
138+
$this->headers['User-Agent'] = [sprintf(
139+
"elasticsearch-php/%s (%s %s, PHP %s)",
140+
Client::VERSION,
141+
php_uname("s"),
142+
php_uname("r"),
143+
phpversion()
144+
)];
145+
136146
$host = $hostDetails['host'].':'.$hostDetails['port'];
137147
$path = null;
138148
if (isset($hostDetails['path']) === true) {
@@ -317,6 +327,11 @@ function (&$value, &$key) {
317327
return $uri ?? '';
318328
}
319329

330+
public function getHeaders(): array
331+
{
332+
return $this->headers;
333+
}
334+
320335
/**
321336
* Log a successful request
322337
*
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<?php
2+
3+
declare(strict_types = 1);
4+
5+
namespace Elasticsearch\Tests\Connections;
6+
7+
use Elasticsearch\Client;
8+
use Elasticsearch\ClientBuilder;
9+
use Elasticsearch\Connections\Connection;
10+
use Elasticsearch\Serializers\SerializerInterface;
11+
use Psr\Log\LoggerInterface;
12+
13+
class ConnectionTest extends \PHPUnit\Framework\TestCase
14+
{
15+
private $logger;
16+
private $trace;
17+
private $serializer;
18+
19+
protected function setUp()
20+
{
21+
$this->logger = $this->createMock(LoggerInterface::class);
22+
$this->trace = $this->createMock(LoggerInterface::class);
23+
$this->serializer = $this->createMock(SerializerInterface::class);
24+
}
25+
26+
public function testConstructor()
27+
{
28+
$params = [];
29+
$host = [
30+
'host' => 'localhost'
31+
];
32+
33+
$connection = new Connection(
34+
function(){},
35+
$host,
36+
$params,
37+
$this->serializer,
38+
$this->logger,
39+
$this->trace
40+
);
41+
42+
$this->assertInstanceOf(Connection::class, $connection);
43+
}
44+
45+
public function testGetHeadersContainUserAgent()
46+
{
47+
$params = [];
48+
$host = [
49+
'host' => 'localhost'
50+
];
51+
52+
$connection = new Connection(
53+
function(){},
54+
$host,
55+
$params,
56+
$this->serializer,
57+
$this->logger,
58+
$this->trace
59+
);
60+
61+
$headers = $connection->getHeaders();
62+
63+
$this->assertArrayHasKey('User-Agent', $headers);
64+
$this->assertContains('elasticsearch-php/'. Client::VERSION, $headers['User-Agent'][0]);
65+
}
66+
67+
public function testUserAgentHeaderIsSent()
68+
{
69+
$params = [];
70+
$host = [
71+
'host' => 'localhost'
72+
];
73+
74+
$connection = new Connection(
75+
ClientBuilder::defaultHandler(),
76+
$host,
77+
$params,
78+
$this->serializer,
79+
$this->logger,
80+
$this->trace
81+
);
82+
$result = $connection->performRequest('GET', '/');
83+
$request = $connection->getLastRequestInfo()['request'];
84+
$this->assertArrayHasKey('User-Agent', $request['headers']);
85+
$this->assertContains('elasticsearch-php/'. Client::VERSION, $request['headers']['User-Agent'][0]);
86+
}
87+
}

0 commit comments

Comments
 (0)