Skip to content

Commit 3209af1

Browse files
committed
use endpoint params for opaque id support + test
1 parent a3a28b1 commit 3209af1

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

src/Elasticsearch/Endpoints/AbstractEndpoint.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ private function checkUserParams(array $params)
225225

226226
$whitelist = array_merge(
227227
$this->getParamWhitelist(),
228-
[ 'pretty', 'human', 'error_trace', 'source', 'filter_path' ]
228+
[ 'pretty', 'human', 'error_trace', 'source', 'filter_path', 'opaqueId' ]
229229
);
230230

231231
$invalid = array_diff(array_keys($params), $whitelist);
@@ -262,6 +262,15 @@ private function extractOptions(&$params)
262262
$this->options['client']['ignore'] = [$ignore];
263263
}
264264
}
265+
266+
// Check if the opaqueId is populated and add the header
267+
if (isset($params['opaqueId']) === true) {
268+
if (isset($this->options['client']['headers']) === false) {
269+
$this->options['client']['headers'] = [];
270+
}
271+
$this->options['client']['headers']['x-opaque-id'] = [trim($params['opaqueId'])];
272+
unset($params['opaqueId']);
273+
}
265274
}
266275

267276
private function convertCustom(array $params): array

tests/Elasticsearch/Tests/Endpoints/AbstractEndpointTest.php

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ class AbstractEndpointTest extends \PHPUnit\Framework\TestCase
1010
{
1111
private $endpoint;
1212

13+
protected function setUp()
14+
{
15+
$this->endpoint = $this->getMockForAbstractClass(AbstractEndpoint::class);
16+
}
17+
1318
public static function invalidParameters(): array
1419
{
1520
return [
@@ -20,6 +25,8 @@ public static function invalidParameters(): array
2025

2126
/**
2227
* @dataProvider invalidParameters
28+
*
29+
* @covers AbstractEndpoint::setParams
2330
*/
2431
public function testInvalidParamsCauseErrorsWhenProvidedToSetParams(array $params)
2532
{
@@ -32,8 +39,22 @@ public function testInvalidParamsCauseErrorsWhenProvidedToSetParams(array $param
3239
$this->endpoint->setParams($params);
3340
}
3441

35-
protected function setUp()
42+
/**
43+
* @covers AbstractEndpoint::setParams
44+
* @covers AbstractEndpoint::extractOptions
45+
* @covers AbstractEndpoint::getOptions
46+
*/
47+
public function testOpaqueIdInHeaders()
3648
{
37-
$this->endpoint = $this->getMockForAbstractClass(AbstractEndpoint::class);
49+
$params = ['opaqueId' => 'test_id_' . rand(1000, 9999)];
50+
$this->endpoint->setParams($params);
51+
52+
$options = $this->endpoint->getOptions();
53+
$this->assertArrayHasKey('client', $options);
54+
$this->assertArrayHasKey('headers', $options['client']);
55+
$this->assertArrayHasKey('x-opaque-id', $options['client']['headers']);
56+
$this->assertNotEmpty($options['client']['headers']['x-opaque-id']);
57+
$this->assertEquals($params['opaqueId'], $options['client']['headers']['x-opaque-id'][0]);
3858
}
59+
3960
}

0 commit comments

Comments
 (0)