Skip to content

Commit 513ffd2

Browse files
committed
Merge branch 'fix/compress' into 7.4
2 parents b395228 + 0c1e0ca commit 513ffd2

File tree

4 files changed

+52
-10
lines changed

4 files changed

+52
-10
lines changed

src/Elasticsearch/ClientBuilder.php

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -396,14 +396,10 @@ public function setElasticCloudId(string $cloudId): ClientBuilder
396396
]
397397
]);
398398

399-
// Merge best practices for the connection
400-
$this->setConnectionParams([
401-
'client' => [
402-
'curl' => [
403-
CURLOPT_ENCODING => 1,
404-
],
405-
]
406-
]);
399+
if (!isset($this->connectionParams['client']['curl'][CURLOPT_ENCODING])) {
400+
// Merge best practices for the connection (enable gzip)
401+
$this->connectionParams['client']['curl'][CURLOPT_ENCODING] = 'gzip';
402+
}
407403

408404
return $this;
409405
}

src/Elasticsearch/Connections/Connection.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ public function logRequestSuccess(array $request, array $response): void
376376
array(
377377
'method' => $request['http_method'],
378378
'uri' => $response['effective_url'],
379-
'port' => $response['transfer_stats']['primary_port'],
379+
'port' => $response['transfer_stats']['primary_port'] ?? '',
380380
'headers' => $request['headers'],
381381
'HTTP code' => $response['status'],
382382
'duration' => $response['transfer_stats']['total_time'],

tests/Elasticsearch/Tests/ClientBuilderTest.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@
44

55
namespace Elasticsearch\Tests;
66

7+
use Elasticsearch\Client;
78
use Elasticsearch\ClientBuilder;
9+
use Elasticsearch\Common\Exceptions\ElasticsearchException;
810
use Elasticsearch\Common\Exceptions\InvalidArgumentException;
911
use Elasticsearch\Tests\ClientBuilder\DummyLogger;
12+
use GuzzleHttp\Ring\Client\MockHandler;
1013
use PHPUnit\Framework\TestCase;
1114

1215
class ClientBuilderTest extends TestCase
@@ -26,4 +29,45 @@ public function testClientBuilderThrowsExceptionForIncorrectTracerClass()
2629
{
2730
ClientBuilder::create()->setTracer(new DummyLogger);
2831
}
32+
33+
public function testGzipEnabledWhenElasticCloudId()
34+
{
35+
$client = ClientBuilder::create()
36+
->setElasticCloudId('foo:' . base64_encode('localhost:9200$foo'))
37+
->build();
38+
39+
$this->assertInstanceOf(Client::class, $client);
40+
41+
try {
42+
$result = $client->info();
43+
} catch (ElasticsearchException $e) {
44+
$request = $client->transport->getLastConnection()->getLastRequestInfo();
45+
$this->assertContains('gzip', $request['request']['client']['curl']);
46+
}
47+
}
48+
49+
public function testElasticCloudIdNotOverrideCurlEncoding()
50+
{
51+
$params = [
52+
'client' => [
53+
'curl' => [
54+
CURLOPT_ENCODING => 'deflate'
55+
]
56+
]
57+
];
58+
$client = ClientBuilder::create()
59+
->setConnectionParams($params)
60+
->setElasticCloudId('foo:' . base64_encode('localhost:9200$foo'))
61+
->build();
62+
63+
$this->assertInstanceOf(Client::class, $client);
64+
65+
try {
66+
$result = $client->info();
67+
} catch (ElasticsearchException $e) {
68+
$request = $client->transport->getLastConnection()->getLastRequestInfo();
69+
$this->assertContains('deflate', $request['request']['client']['curl']);
70+
$this->assertNotContains('gzip', $request['request']['client']['curl']);
71+
}
72+
}
2973
}

tests/Elasticsearch/Tests/ClientIntegrationTests.php renamed to tests/Elasticsearch/Tests/ClientIntegrationTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44

55
namespace Elasticsearch\Tests;
66

7+
use Elasticsearch\Client;
78
use Elasticsearch\ClientBuilder;
9+
use Elasticsearch\Common\Exceptions\ElasticsearchException;
810
use Elasticsearch\Common\Exceptions\Missing404Exception;
911
use Elasticsearch\Tests\ClientBuilder\ArrayLogger;
1012
use Psr\Log\LogLevel;
@@ -19,7 +21,7 @@
1921
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache2
2022
* @link http://elasticsearch.org
2123
*/
22-
class ClientIntegrationTests extends \PHPUnit\Framework\TestCase
24+
class ClientIntegrationTest extends \PHPUnit\Framework\TestCase
2325
{
2426
public function setUp()
2527
{

0 commit comments

Comments
 (0)