Skip to content

Commit 3d52e11

Browse files
committed
Automatically set Content-type and Accept headers
1 parent ec6b832 commit 3d52e11

File tree

3 files changed

+58
-25
lines changed

3 files changed

+58
-25
lines changed

src/Elasticsearch/ClientBuilder.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,23 @@ public function build()
442442
if (is_null($this->connectionParams)) {
443443
$this->connectionParams = [];
444444
}
445+
446+
// Make sure we are setting Content-type and Accept (unless the user has explicitly
447+
// overridden it
448+
if (isset($this->connectionParams['client']['headers']) === false) {
449+
$this->connectionParams['client']['headers'] = [
450+
'Content-type' => ['application/json'],
451+
'Accept' => ['application/json']
452+
];
453+
} else {
454+
if (isset($this->connectionParams['client']['headers']['Content-type']) === false) {
455+
$this->connectionParams['client']['headers']['Content-type'] = ['application/json'];
456+
}
457+
if (isset($this->connectionParams['client']['headers']['Accept']) === false) {
458+
$this->connectionParams['client']['headers']['Accept'] = ['application/json'];
459+
}
460+
}
461+
445462
$this->connectionFactory = new ConnectionFactory($this->handler, $this->connectionParams, $this->serializer, $this->logger, $this->tracer);
446463
}
447464

src/Elasticsearch/Connections/Connection.php

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,9 @@ class Connection implements ConnectionInterface
7272
*/
7373
protected $connectionParams;
7474

75+
/** @var array */
76+
protected $headers = [];
77+
7578
/** @var bool */
7679
protected $isAlive = false;
7780

@@ -112,6 +115,11 @@ public function __construct($handler, $hostDetails, $connectionParams,
112115
$connectionParams['client']['curl'][CURLOPT_USERPWD] = $hostDetails['user'].':'.$hostDetails['pass'];
113116
}
114117

118+
if (isset($connectionParams['client']['headers']) === true) {
119+
$this->headers = $connectionParams['client']['headers'];
120+
unset($connectionParams['client']['headers']);
121+
}
122+
115123
$host = $hostDetails['host'].':'.$hostDetails['port'];
116124
$path = null;
117125
if (isset($hostDetails['path']) === true) {
@@ -147,13 +155,17 @@ public function performRequest($method, $uri, $params = null, $body = null, $opt
147155
'scheme' => $this->transportSchema,
148156
'uri' => $this->getURI($uri, $params),
149157
'body' => $body,
150-
'headers' => [
158+
'headers' => array_merge([
151159
'host' => [$this->host]
152-
]
153-
160+
], $this->headers)
154161
];
162+
155163
$request = array_merge_recursive($request, $this->connectionParams, $options);
156164

165+
// RingPHP does not like if client is empty
166+
if (empty($request['client'])) {
167+
unset($request['client']);
168+
}
157169

158170
$handler = $this->handler;
159171
$future = $handler($request, $this, $transport, $options);

tests/Elasticsearch/Tests/YamlRunnerTest.php

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,11 @@ public function operationDo($operation, $lastOperationResult, &$context, $testNa
291291
self::markTestIncomplete(sprintf('Method "%s" not implement in "%s"', $method, get_class($caller)));
292292
}
293293

294+
// TODO remove this after cat testing situation resolved
295+
if ($caller instanceof Elasticsearch\Namespaces\CatNamespace) {
296+
$endpointParams->format = 'text';
297+
}
298+
294299
// Exist* methods have to be manually 'unwrapped' into true/false for async
295300
if (strpos($method, "exist") !== false && $async === true) {
296301
return $this->executeAsyncExistRequest($caller, $method, $endpointParams, $expectedError, $expectedWarnings, $testName);
@@ -384,32 +389,31 @@ public function checkForWarnings($expectedWarnings) {
384389
$last = $this->client->transport->getLastConnection()->getLastRequestInfo();
385390

386391

387-
// We have some warnings to check
388-
if ($expectedWarnings !== null) {
389-
if (isset($last['response']['headers']['Warning']) === true) {
390-
foreach ($last['response']['headers']['Warning'] as $warning) {
391-
$position = array_search($warning, $expectedWarnings);
392-
if ($position !== false) {
393-
// found the warning
394-
unset($expectedWarnings[$position]);
395-
} else {
396-
// didn't find, throw error
397-
//throw new \Exception("Expected to find warning [$warning] but did not.");
398-
}
399-
}
400-
if (count($expectedWarnings) > 0) {
401-
throw new \Exception("Expected to find more warnings: ". print_r($expectedWarnings, true));
392+
// We have some warnings to check
393+
if ($expectedWarnings !== null) {
394+
if (isset($last['response']['headers']['Warning']) === true) {
395+
foreach ($last['response']['headers']['Warning'] as $warning) {
396+
$position = array_search($warning, $expectedWarnings);
397+
if ($position !== false) {
398+
// found the warning
399+
unset($expectedWarnings[$position]);
400+
} else {
401+
// didn't find, throw error
402+
//throw new \Exception("Expected to find warning [$warning] but did not.");
402403
}
403404
}
404-
}
405-
/* else {
406-
// no expected warnings, make sure we have none returned
407-
if (isset($last['response']['headers']['Warning']) === true) {
408-
throw new \Exception("Did not expect to find warnings, found some instead: "
409-
. print_r($last['response']['headers']['Warning'], true));
405+
if (count($expectedWarnings) > 0) {
406+
throw new \Exception("Expected to find more warnings: ". print_r($expectedWarnings, true));
410407
}
411408
}
412-
*/
409+
}
410+
411+
// Check to make sure we're adding headers
412+
static::assertArrayHasKey('Content-type', $last['request']['headers'], print_r($last['request']['headers'], true));
413+
static::assertEquals('application/json', $last['request']['headers']['Content-type'][0], print_r($last['request']['headers'], true));
414+
static::assertArrayHasKey('Accept', $last['request']['headers'], print_r($last['request']['headers'], true));
415+
static::assertEquals('application/json', $last['request']['headers']['Accept'][0], print_r($last['request']['headers'], true));
416+
413417
}
414418

415419
/**

0 commit comments

Comments
 (0)