Skip to content

Commit 26da9a3

Browse files
committed
Added User-Agent
1 parent 8b36458 commit 26da9a3

File tree

3 files changed

+103
-1
lines changed

3 files changed

+103
-1
lines changed

src/Elasticsearch/Client.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
*/
3333
class Client
3434
{
35+
const VERSION = '5.5.0';
36+
3537
/**
3638
* @var Transport
3739
*/
@@ -1564,7 +1566,7 @@ private function verifyNotNullOrEmpty($name, $var)
15641566

15651567
/**
15661568
* @param $endpoint AbstractEndpoint
1567-
*
1569+
*
15681570
* @throws \Exception
15691571
* @return array
15701572
*/

src/Elasticsearch/Connections/Connection.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Elasticsearch\Connections;
44

5+
use Elasticsearch\Client;
56
use Elasticsearch\Common\Exceptions\AlreadyExpiredException;
67
use Elasticsearch\Common\Exceptions\BadRequest400Exception;
78
use Elasticsearch\Common\Exceptions\Conflict409Exception;
@@ -120,6 +121,15 @@ public function __construct($handler, $hostDetails, $connectionParams,
120121
unset($connectionParams['client']['headers']);
121122
}
122123

124+
// Add the User-Agent using the format: <client-repo-name>/<client-version> (metadata-values)
125+
$this->headers['User-Agent'] = [sprintf(
126+
"elasticsearch-php/%s (%s %s, PHP %s)",
127+
Client::VERSION,
128+
php_uname("s"),
129+
php_uname("r"),
130+
phpversion()
131+
)];
132+
123133
$host = $hostDetails['host'].':'.$hostDetails['port'];
124134
$path = null;
125135
if (isset($hostDetails['path']) === true) {
@@ -135,6 +145,16 @@ public function __construct($handler, $hostDetails, $connectionParams,
135145
$this->handler = $this->wrapHandler($handler, $log, $trace);
136146
}
137147

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

0 commit comments

Comments
 (0)