Skip to content

Commit

Permalink
Adding the ability to set default HTTP headers that will be set on al…
Browse files Browse the repository at this point in the history
…l requests created by a client. Closes #37.
  • Loading branch information
mtdowling committed Mar 26, 2012
1 parent a68cdd5 commit 85803bd
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 8 deletions.
55 changes: 47 additions & 8 deletions src/Guzzle/Http/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
class Client extends AbstractHasDispatcher implements ClientInterface
{
/**
* @var string User-Agent header to apply to all requests
* @var Collection Default HTTP headers to set on each request
*/
protected $userAgent = null;
protected $defaultHeaders;

/**
* @var Collection Parameter object holding configuration data
Expand Down Expand Up @@ -63,6 +63,7 @@ public function __construct($baseUrl = '', $config = null)
{
$this->setConfig($config ?: new Collection());
$this->setBaseUrl($baseUrl);
$this->defaultHeaders = new Collection();
}

/**
Expand Down Expand Up @@ -112,6 +113,36 @@ public final function getConfig($key = false)
return $key ? $this->config->get($key) : $this->config;
}

/**
* Get the default HTTP headers to add to each request created by the client
*
* @return Collection
*/
public function getDefaultHeaders()
{
return $this->defaultHeaders;
}

/**
* Set the default HTTP headers to add to each request created by the client
*
* @param array|Collection $headers Default HTTP headers
*
* @return Client
*/
public function setDefaultHeaders($headers)
{
if ($headers instanceof Collection) {
$this->defaultHeaders = $headers;
} else if (is_array($headers)) {
$this->defaultHeaders = new Collection($headers);
} else {
throw new \InvalidArgumentException('Headers must be an array or Collection');
}

return $this;
}

/**
* Expand a URI template using client configuration data
*
Expand Down Expand Up @@ -200,6 +231,18 @@ public function createRequest($method = RequestInterface::GET, $uri = null, $hea
$url = Url::factory($this->getBaseUrl())->combine($this->expandTemplate($uri, $templateVars));
}

// If default headers are provided, then merge them into exising headers
// If a collision occurs, the header is completely replaced
if (count($this->defaultHeaders)) {
if ($headers instanceof Collection) {
$headers = array_merge($this->defaultHeaders->getAll(), $headers->getAll());
} else if (is_array($headers)) {
$headers = array_merge($this->defaultHeaders->getAll(), $headers);
} else if ($headers === null) {
$headers = $this->defaultHeaders;
}
}

return $this->prepareRequest(
RequestFactory::create($method, (string) $url, $headers, $body)
);
Expand All @@ -217,10 +260,6 @@ public function prepareRequest(RequestInterface $request)
{
$request->setClient($this);

if ($this->userAgent) {
$request->setHeader('User-Agent', $this->userAgent);
}

foreach ($this->getConfig()->getAll() as $key => $value) {
// Add any curl options that might in the config to the request
if (strpos($key, 'curl.') === 0) {
Expand Down Expand Up @@ -287,10 +326,10 @@ public function setBaseUrl($url)
*/
public function setUserAgent($userAgent, $includeDefault = false)
{
$this->userAgent = $userAgent;
if ($includeDefault) {
$this->userAgent .= ' ' . Guzzle::getDefaultUserAgent();
$userAgent .= ' ' . Guzzle::getDefaultUserAgent();
}
$this->defaultHeaders->set('User-Agent', $userAgent);

return $this;
}
Expand Down
16 changes: 16 additions & 0 deletions src/Guzzle/Http/ClientInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,22 @@ function setConfig($config);
*/
function getConfig($key = false);

/**
* Get the default HTTP headers to add to each request created by the client
*
* @return Collection
*/
function getDefaultHeaders();

/**
* Set the default HTTP headers to add to each request created by the client
*
* @param array|Collection $headers Default HTTP headers
*
* @return ClientInterface
*/
function setDefaultHeaders($headers);

/**
* Set the URI template expander to use with the client
*
Expand Down
42 changes: 42 additions & 0 deletions tests/Guzzle/Tests/Http/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -542,4 +542,46 @@ public function testUriArrayAllowsCustomTemplateVariables()
$this->assertEquals('/hi', (string) $client->head(array('/{var}', $vars))->getUrl());
$this->assertEquals('/hi', (string) $client->options(array('/{var}', $vars))->getUrl());
}

/**
* @covers Guzzle\Http\Client::setDefaultHeaders
* @covers Guzzle\Http\Client::getDefaultHeaders
* @covers Guzzle\Http\Client::createRequest
*/
public function testAllowsDefaultHeaders()
{
$default = array(
'X-Test' => 'Hi!'
);
$other = array(
'X-Other' => 'Foo'
);

$client = new Client();
$client->setDefaultHeaders($default);
$this->assertEquals($default, $client->getDefaultHeaders()->getAll());
$client->setDefaultHeaders(new Collection($default));
$this->assertEquals($default, $client->getDefaultHeaders()->getAll());

$request = $client->createRequest('GET', null, $other);
$this->assertEquals('Hi!', $request->getHeader('X-Test'));
$this->assertEquals('Foo', $request->getHeader('X-Other'));

$request = $client->createRequest('GET', null, new Collection($other));
$this->assertEquals('Hi!', $request->getHeader('X-Test'));
$this->assertEquals('Foo', $request->getHeader('X-Other'));

$request = $client->createRequest('GET');
$this->assertEquals('Hi!', $request->getHeader('X-Test'));
}

/**
* @covers Guzzle\Http\Client::setDefaultHeaders
* @expectedException InvalidArgumentException
*/
public function testValidatesDefaultHeaders()
{
$client = new Client();
$client->setDefaultHeaders('foo');
}
}

0 comments on commit 85803bd

Please sign in to comment.