Skip to content

Commit e12e308

Browse files
authored
Merge pull request #898 from ezimuel/feature/user-agent
Added User-Agent
2 parents 10a9127 + eb909cc commit e12e308

File tree

3 files changed

+106
-0
lines changed

3 files changed

+106
-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: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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+
},
36+
$host,
37+
$params,
38+
$this->serializer,
39+
$this->logger,
40+
$this->trace
41+
);
42+
43+
$this->assertInstanceOf(Connection::class, $connection);
44+
}
45+
46+
public function testGetHeadersContainUserAgent()
47+
{
48+
$params = [];
49+
$host = [
50+
'host' => 'localhost'
51+
];
52+
53+
$connection = new Connection(
54+
function () {
55+
},
56+
$host,
57+
$params,
58+
$this->serializer,
59+
$this->logger,
60+
$this->trace
61+
);
62+
63+
$headers = $connection->getHeaders();
64+
65+
$this->assertArrayHasKey('User-Agent', $headers);
66+
$this->assertContains('elasticsearch-php/'. Client::VERSION, $headers['User-Agent'][0]);
67+
}
68+
69+
public function testUserAgentHeaderIsSent()
70+
{
71+
$params = [];
72+
$host = [
73+
'host' => 'localhost'
74+
];
75+
76+
$connection = new Connection(
77+
ClientBuilder::defaultHandler(),
78+
$host,
79+
$params,
80+
$this->serializer,
81+
$this->logger,
82+
$this->trace
83+
);
84+
$result = $connection->performRequest('GET', '/');
85+
$request = $connection->getLastRequestInfo()['request'];
86+
$this->assertArrayHasKey('User-Agent', $request['headers']);
87+
$this->assertContains('elasticsearch-php/'. Client::VERSION, $request['headers']['User-Agent'][0]);
88+
}
89+
}

0 commit comments

Comments
 (0)