Skip to content

Commit

Permalink
Headers configuration allows underscores (#111)
Browse files Browse the repository at this point in the history
  • Loading branch information
PWalkow authored and Florian Preusner committed Jul 29, 2017
1 parent 64884a9 commit 38976c6
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 37 deletions.
2 changes: 2 additions & 0 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,15 @@ private function createClientsNode()

// @todo @deprecated
->arrayNode('headers')
->normalizeKeys(false)
->prototype('scalar')
->end()
->end()

->arrayNode('options')
->children()
->arrayNode('headers')
->normalizeKeys(false)
->prototype('scalar')
->end()
->end()
Expand Down
37 changes: 0 additions & 37 deletions DependencyInjection/GuzzleExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,6 @@ public function load(array $configs, ContainerBuilder $container)
'handler' => $this->createHandler($container, $name, $options)
];

// header hotfix/workaround #77
// @todo @deprecated
if (isset($options['headers'])) {
$argument['headers'] = $this->cleanUpHeaders($options['headers']);
}

// if present, add default options to the constructor argument for the Guzzle client
if (array_key_exists('options', $options) && is_array($options['options'])) {

Expand All @@ -68,12 +62,6 @@ public function load(array $configs, ContainerBuilder $container)
continue;
}

// @todo: cleanup
if ($key === 'headers') {
$argument[$key] = $this->cleanUpHeaders($value);
continue;
}

$argument[$key] = $value;
}
}
Expand Down Expand Up @@ -235,31 +223,6 @@ protected function createWsseMiddleware($username, $password, $createdAtExpressi
return $wsse;
}

/**
* Clean up HTTP headers
*
* @since 2015-07
*
* @param array $headers
*
* @return array
*/
protected function cleanUpHeaders(array $headers)
{
foreach ($headers as $name => $value) {

// because of standard conventions in YAML dashes are converted to underscores
// underscores are not allowed in HTTP standard, will be replaced by dash
$nameClean = str_replace('_', '-', $name);

unset($headers[$name]);

$headers[$nameClean] = $value;
}

return $headers;
}

/**
* Returns alias of extension
*
Expand Down
34 changes: 34 additions & 0 deletions Tests/DependencyInjection/ConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,4 +196,38 @@ public function testSingleClientConfigWithProxyAsString()
'clients' => ['test_client' => ['options' => ['proxy' => ['http' => 'http://proxy.org']]]]
]), $processedConfig);
}

public function testHeaderWithUnderscore()
{
$config = [
'guzzle' => [
'clients' => [
'test_client' => [
'headers' => [
'Header_underscored' => 'some-random-hash',
'Header-hyphened' => 'another-random-hash'
],
'options' => [
'headers' => [
'Header_underscored' => 'some-random-hash',
'Header-hyphened' => 'another-random-hash'
],
]
]
]
]
];

$processor = new Processor();
$processedConfig = $processor->processConfiguration(new Configuration(true), $config);

$headers = $processedConfig['clients']['test_client']['headers'];
$optionsHeaders = $processedConfig['clients']['test_client']['options']['headers'];

foreach ([$headers, $optionsHeaders] as $headerConfig)
{
$this->assertArrayHasKey('Header_underscored', $headerConfig);
$this->assertArrayHasKey('Header-hyphened', $headerConfig);
}
}
}

0 comments on commit 38976c6

Please sign in to comment.