diff --git a/src/Elasticsearch/ClientBuilder.php b/src/Elasticsearch/ClientBuilder.php index bc36abaf8..7a716b666 100644 --- a/src/Elasticsearch/ClientBuilder.php +++ b/src/Elasticsearch/ClientBuilder.php @@ -442,6 +442,23 @@ public function build() if (is_null($this->connectionParams)) { $this->connectionParams = []; } + + // Make sure we are setting Content-type and Accept (unless the user has explicitly + // overridden it + if (isset($this->connectionParams['client']['headers']) === false) { + $this->connectionParams['client']['headers'] = [ + 'Content-type' => ['application/json'], + 'Accept' => ['application/json'] + ]; + } else { + if (isset($this->connectionParams['client']['headers']['Content-type']) === false) { + $this->connectionParams['client']['headers']['Content-type'] = ['application/json']; + } + if (isset($this->connectionParams['client']['headers']['Accept']) === false) { + $this->connectionParams['client']['headers']['Accept'] = ['application/json']; + } + } + $this->connectionFactory = new ConnectionFactory($this->handler, $this->connectionParams, $this->serializer, $this->logger, $this->tracer); } diff --git a/src/Elasticsearch/Connections/Connection.php b/src/Elasticsearch/Connections/Connection.php index 2d9e28984..f8aa71a69 100644 --- a/src/Elasticsearch/Connections/Connection.php +++ b/src/Elasticsearch/Connections/Connection.php @@ -72,6 +72,9 @@ class Connection implements ConnectionInterface */ protected $connectionParams; + /** @var array */ + protected $headers = []; + /** @var bool */ protected $isAlive = false; @@ -112,6 +115,11 @@ public function __construct($handler, $hostDetails, $connectionParams, $connectionParams['client']['curl'][CURLOPT_USERPWD] = $hostDetails['user'].':'.$hostDetails['pass']; } + if (isset($connectionParams['client']['headers']) === true) { + $this->headers = $connectionParams['client']['headers']; + unset($connectionParams['client']['headers']); + } + $host = $hostDetails['host'].':'.$hostDetails['port']; $path = null; if (isset($hostDetails['path']) === true) { @@ -147,13 +155,17 @@ public function performRequest($method, $uri, $params = null, $body = null, $opt 'scheme' => $this->transportSchema, 'uri' => $this->getURI($uri, $params), 'body' => $body, - 'headers' => [ + 'headers' => array_merge([ 'host' => [$this->host] - ] - + ], $this->headers) ]; + $request = array_merge_recursive($request, $this->connectionParams, $options); + // RingPHP does not like if client is empty + if (empty($request['client'])) { + unset($request['client']); + } $handler = $this->handler; $future = $handler($request, $this, $transport, $options); diff --git a/tests/Elasticsearch/Tests/YamlRunnerTest.php b/tests/Elasticsearch/Tests/YamlRunnerTest.php index c22d5e4bf..685ea0061 100644 --- a/tests/Elasticsearch/Tests/YamlRunnerTest.php +++ b/tests/Elasticsearch/Tests/YamlRunnerTest.php @@ -291,6 +291,11 @@ public function operationDo($operation, $lastOperationResult, &$context, $testNa self::markTestIncomplete(sprintf('Method "%s" not implement in "%s"', $method, get_class($caller))); } + // TODO remove this after cat testing situation resolved + if ($caller instanceof Elasticsearch\Namespaces\CatNamespace) { + $endpointParams->format = 'text'; + } + // Exist* methods have to be manually 'unwrapped' into true/false for async if (strpos($method, "exist") !== false && $async === true) { return $this->executeAsyncExistRequest($caller, $method, $endpointParams, $expectedError, $expectedWarnings, $testName); @@ -384,32 +389,31 @@ public function checkForWarnings($expectedWarnings) { $last = $this->client->transport->getLastConnection()->getLastRequestInfo(); - // We have some warnings to check - if ($expectedWarnings !== null) { - if (isset($last['response']['headers']['Warning']) === true) { - foreach ($last['response']['headers']['Warning'] as $warning) { - $position = array_search($warning, $expectedWarnings); - if ($position !== false) { - // found the warning - unset($expectedWarnings[$position]); - } else { - // didn't find, throw error - //throw new \Exception("Expected to find warning [$warning] but did not."); - } - } - if (count($expectedWarnings) > 0) { - throw new \Exception("Expected to find more warnings: ". print_r($expectedWarnings, true)); + // We have some warnings to check + if ($expectedWarnings !== null) { + if (isset($last['response']['headers']['Warning']) === true) { + foreach ($last['response']['headers']['Warning'] as $warning) { + $position = array_search($warning, $expectedWarnings); + if ($position !== false) { + // found the warning + unset($expectedWarnings[$position]); + } else { + // didn't find, throw error + //throw new \Exception("Expected to find warning [$warning] but did not."); } } - } - /* else { - // no expected warnings, make sure we have none returned - if (isset($last['response']['headers']['Warning']) === true) { - throw new \Exception("Did not expect to find warnings, found some instead: " - . print_r($last['response']['headers']['Warning'], true)); + if (count($expectedWarnings) > 0) { + throw new \Exception("Expected to find more warnings: ". print_r($expectedWarnings, true)); } } - */ + } + + // Check to make sure we're adding headers + static::assertArrayHasKey('Content-type', $last['request']['headers'], print_r($last['request']['headers'], true)); + static::assertEquals('application/json', $last['request']['headers']['Content-type'][0], print_r($last['request']['headers'], true)); + static::assertArrayHasKey('Accept', $last['request']['headers'], print_r($last['request']['headers'], true)); + static::assertEquals('application/json', $last['request']['headers']['Accept'][0], print_r($last['request']['headers'], true)); + } /**