Skip to content

Commit 02b24c7

Browse files
authored
Fix #1176 adding includePortInHostHeader in fromConfig (#1181)
* Fix #1176 adding includePortInHostHeader in fromConfig * Added EXTRA_METHODS_FROM_CONFIG const
1 parent e0bc5fd commit 02b24c7

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

src/Elasticsearch/ClientBuilder.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@
4242

4343
class ClientBuilder
4444
{
45+
const ALLOWED_METHODS_FROM_CONFIG = ['includePortInHostHeader'];
46+
4547
/**
4648
* @var Transport
4749
*/
@@ -197,7 +199,7 @@ public static function fromConfig(array $config, bool $quiet = false): Client
197199
{
198200
$builder = new static;
199201
foreach ($config as $key => $value) {
200-
$method = "set$key";
202+
$method = in_array($key, self::ALLOWED_METHODS_FROM_CONFIG) ? $key : "set$key";
201203
$reflection = new ReflectionClass($builder);
202204
if ($reflection->hasMethod($method)) {
203205
$func = $reflection->getMethod($method);

tests/Elasticsearch/Tests/ClientBuilderTest.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,9 @@ public function testFromConfig(array $params)
216216
$this->assertInstanceOf(Client::class, $client);
217217
}
218218

219+
/**
220+
* @doesNotPerformAssertions
221+
*/
219222
public function testFromConfigQuiteTrueWithUnknownKey()
220223
{
221224
$client = ClientBuilder::fromConfig(
@@ -344,4 +347,33 @@ public function testCompatibilityHeaderDefaultIsOff()
344347
$this->assertNotContains('application/vnd.elasticsearch+json;compatible-with=7', $request['request']['headers']['Accept']);
345348
}
346349
}
350+
351+
/**
352+
* @see https://github.com/elastic/elasticsearch-php/issues/1176
353+
*/
354+
public function testFromConfigWithIncludePortInHostHeader()
355+
{
356+
$url = 'localhost:1234';
357+
$config = [
358+
'hosts' => [$url],
359+
'includePortInHostHeader' => true,
360+
'connectionParams' => [
361+
'client' => [
362+
'verbose' => true
363+
]
364+
],
365+
];
366+
367+
$client = ClientBuilder::fromConfig($config);
368+
369+
$this->assertInstanceOf(Client::class, $client);
370+
371+
try {
372+
$result = $client->info();
373+
} catch (ElasticsearchException $e) {
374+
$request = $client->transport->getLastConnection()->getLastRequestInfo();
375+
$this->assertTrue(isset($request['request']['headers']['Host'][0]));
376+
$this->assertEquals($url, $request['request']['headers']['Host'][0]);
377+
}
378+
}
347379
}

0 commit comments

Comments
 (0)