1<?php
2
3declare(strict_types = 1);
4
5namespace Elasticsearch\Tests\Connections;
6
7use Elasticsearch\Client;
8use Elasticsearch\ClientBuilder;
9use Elasticsearch\Connections\Connection;
10use Elasticsearch\Serializers\SerializerInterface;
11use Psr\Log\LoggerInterface;
12
13class 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}
90