Skip to content

Commit

Permalink
Merge pull request #32 from M6Web/feat/host-header
Browse files Browse the repository at this point in the history
feat(header): Allow to add specific Host header
  • Loading branch information
b-viguier committed Jun 29, 2021
2 parents 0f9bcba + f3a173b commit a659f4e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace M6Web\Bundle\ElasticsearchBundle\Handler;

use GuzzleHttp\Ring\Core;
use GuzzleHttp\Ring\Future\FutureInterface;

/**
Expand Down Expand Up @@ -57,6 +58,10 @@ public function setHeader($key, $value)
*/
public function __invoke(array $request)
{
// By default, host is stored in headers and final url is forged later.
// We need to build url now to handle the case when we want to add a custom Host header.
$request['url'] = Core::url($request);

$handler = $this->handler;
foreach ($this->headers as $key => $value) {
if ($key == 'Accept-Encoding' && in_array('gzip', $value)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,32 +55,34 @@ protected function testInvokeDataProvider()
{
return [
[
'request' => [],
'expectedRequest' => [],
'request' => ['headers' => ['host' => ['localhost:9200']]],
'expectedRequest' => ['headers' => ['host' => ['localhost:9200']], 'url' => 'http://localhost:9200'],
'headers' => [],
],
[
'request' => ['headers' => ['host' => ['localhost:9200']]],
'expectedRequest' => ['headers' => ['host' => ['localhost:9200']]],
'headers' => [],
'expectedRequest' => ['headers' => ['host' => ['localhost:9200'], 'X-AddMe' => ['Hello']], 'url' => 'http://localhost:9200'],
'headers' => ['X-AddMe' => ['Hello']],
],
[
'request' => ['headers' => ['host' => ['localhost:9200']]],
'expectedRequest' => ['headers' => ['host' => ['localhost:9200'], 'X-AddMe' => ['Hello']]],
'headers' => ['X-AddMe' => ['Hello']],
'expectedRequest' => ['headers' => ['host' => ['other-host.com']], 'url' => 'http://localhost:9200'],
'headers' => ['host' => ['other-host.com']],
],
[
'request' => ['headers' => ['host' => ['localhost:9200']]],
'expectedRequest' => [
'headers' => ['host' => ['localhost:9200'], 'Accept-Encoding' => ['gzip']],
'client' => ['decode_content' => true],
'url' => 'http://localhost:9200'
],
'headers' => ['Accept-Encoding' => ['gzip']],
],
[
'request' => ['headers' => ['host' => ['localhost:9200']]],
'expectedRequest' => [
'headers' => ['host' => ['localhost:9200'], 'Accept-Encoding' => ['deflate']],
'url' => 'http://localhost:9200'
],
'headers' => ['Accept-Encoding' => ['deflate']],
],
Expand Down

0 comments on commit a659f4e

Please sign in to comment.