Skip to content

Commit 31a4d8c

Browse files
committed
Added tests for elastic-api-version and compatibility mode
1 parent e52bd51 commit 31a4d8c

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

tests/ClientTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
use Elastic\Elasticsearch\Exception\ClientResponseException;
2020
use Elastic\Elasticsearch\Exception\ServerResponseException;
2121
use Elastic\Elasticsearch\Response\Elasticsearch;
22-
use Elastic\Transport\NodePool\NodePoolInterface;
2322
use Elastic\Transport\Transport;
2423
use Elastic\Transport\TransportBuilder;
2524
use Http\Mock\Client as MockClient;

tests/Traits/EndpointTraitTest.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,20 @@
1515
namespace Elastic\Elasticsearch\Tests\Traits;
1616

1717
use Elastic\Elasticsearch\Client;
18+
use Elastic\Elasticsearch\ClientInterface;
1819
use Elastic\Elasticsearch\Exception\ContentTypeException;
1920
use Elastic\Elasticsearch\Exception\MissingParameterException;
2021
use Elastic\Elasticsearch\Traits\EndpointTrait;
22+
use Elastic\Transport\TransportBuilder;
23+
use Http\Mock\Client as MockClient;
2124
use PHPUnit\Framework\TestCase;
25+
use Psr\Log\LoggerInterface;
26+
use Psr\Log\NullLogger;
2227

2328
class EndpointTraitTest extends TestCase
2429
{
30+
protected ClientInterface $client;
31+
2532
use EndpointTrait;
2633

2734
public static function getQueryStrings(): array
@@ -153,4 +160,42 @@ public function testCheckRequiredParameters(array $required, array $params, bool
153160
$this->checkRequiredParameters($required, $params);
154161
}
155162
}
163+
164+
public function testCreateRequestContainsApiVersionIfServerlessTrue()
165+
{
166+
$logger = new NullLogger();
167+
$httpClient = new MockClient();
168+
169+
$transport = TransportBuilder::create()
170+
->setClient($httpClient)
171+
->build();
172+
173+
$this->client = new Client($transport, $logger);
174+
$this->client->setServerless(true);
175+
176+
$request = $this->createRequest('GET', 'localhost:9200', []);
177+
178+
$this->assertEquals(Client::API_VERSION, $request->getHeader(Client::API_VERSION_HEADER)[0]);
179+
}
180+
181+
public function testCreateRequestContainsCompatibilityModeIfServerlessFalse()
182+
{
183+
$logger = new NullLogger();
184+
$httpClient = new MockClient();
185+
186+
$transport = TransportBuilder::create()
187+
->setClient($httpClient)
188+
->build();
189+
190+
$this->client = new Client($transport, $logger);
191+
$this->client->setServerless(false);
192+
193+
$request = $this->createRequest('GET', 'localhost:9200', [
194+
'Content-Type' => 'application/json',
195+
'Accept' => 'application/json'
196+
]);
197+
198+
$this->assertEquals(sprintf(Client::API_COMPATIBILITY_HEADER, 'application', 'json'), $request->getHeader('Content-Type')[0]);
199+
$this->assertEquals(sprintf(Client::API_COMPATIBILITY_HEADER, 'application', 'json'), $request->getHeader('Accept')[0]);
200+
}
156201
}

0 commit comments

Comments
 (0)