Skip to content

Commit

Permalink
Automatically set Content-type and Accept headers
Browse files Browse the repository at this point in the history
  • Loading branch information
polyfractal committed Feb 2, 2017
1 parent ec6b832 commit 3d52e11
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 25 deletions.
17 changes: 17 additions & 0 deletions src/Elasticsearch/ClientBuilder.php
Expand Up @@ -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);
}

Expand Down
18 changes: 15 additions & 3 deletions src/Elasticsearch/Connections/Connection.php
Expand Up @@ -72,6 +72,9 @@ class Connection implements ConnectionInterface
*/
protected $connectionParams;

/** @var array */
protected $headers = [];

/** @var bool */
protected $isAlive = false;

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
Expand Down
48 changes: 26 additions & 22 deletions tests/Elasticsearch/Tests/YamlRunnerTest.php
Expand Up @@ -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);
Expand Down Expand Up @@ -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));

}

/**
Expand Down

0 comments on commit 3d52e11

Please sign in to comment.