1
0
mirror of https://github.com/guzzle/guzzle.git synced 2025-03-14 19:29:49 +01:00

Merging in default headers case-insensitively. Closes #767

This commit is contained in:
Michael Dowling 2014-08-14 23:03:39 -07:00
parent e196b8f44f
commit ef1b0c3075
3 changed files with 50 additions and 3 deletions

View File

@ -1,6 +1,11 @@
CHANGELOG
=========
Next Release
------------
* Now merging in default options using a case-insensitive comparison.
4.1.8 (2014-08-14)
------------------

View File

@ -129,9 +129,7 @@ class Client implements ClientInterface
public function createRequest($method, $url = null, array $options = [])
{
// Merge in default options
$options = array_replace_recursive($this->defaults, $options);
$headers = $this->mergeDefaults($options);
// Use a clone of the client's emitter
$options['config']['emitter'] = clone $this->getEmitter();
@ -141,6 +139,15 @@ class Client implements ClientInterface
$options
);
// Merge in default headers
if ($headers) {
foreach ($headers as $key => $value) {
if (!$request->hasHeader($key)) {
$request->setHeader($key, $value);
}
}
}
return $request;
}
@ -361,4 +368,29 @@ class Client implements ClientInterface
$this->parallelAdapter = $this->getDefaultParallelAdapter();
}
}
/**
* Merges default options into the array passed by reference and returns
* an array of headers that need to be merged in after the request is
* created.
*
* @param array $options Options to modify by reference
*
* @return array|null
*/
private function mergeDefaults(&$options)
{
// Merging optimization for when no headers are present
if (!isset($options['headers'])
|| !isset($this->defaults['headers'])) {
$options = array_replace_recursive($this->defaults, $options);
return null;
}
$defaults = $this->defaults;
unset($defaults['headers']);
$options = array_replace_recursive($defaults, $options);
return $this->defaults['headers'];
}
}

View File

@ -226,6 +226,16 @@ class ClientTest extends \PHPUnit_Framework_TestCase
$this->assertEquals('a=b&baz=bam&t=1', $request->getQuery());
}
public function testClientMergesDefaultHeadersCaseInsensitively()
{
$client = new Client(['defaults' => ['headers' => ['Foo' => 'Bar']]]);
$request = $client->createRequest('GET', 'http://foo.com?a=b', [
'headers' => ['foo' => 'custom', 'user-agent' => 'test']
]);
$this->assertEquals('test', $request->getHeader('User-Agent'));
$this->assertEquals('custom', $request->getHeader('Foo'));
}
public function testUsesBaseUrlWhenNoUrlIsSet()
{
$client = new Client(['base_url' => 'http://www.foo.com/baz?bam=bar']);