Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #85 from Seldaek/headers

Allow associative arrays for headers
  • Loading branch information...
commit 2ba5da13b1b03590f0164b3fb61578078d671fcc 2 parents 62a5a27 + 4037f9d
@kriswallsmith authored
View
10 lib/Buzz/Browser.php
@@ -76,10 +76,7 @@ public function call($url, $method, $headers = array(), $content = '')
$url->applyToRequest($request);
- foreach ($headers as $header) {
- $request->addHeader($header);
- }
-
+ $request->addHeaders($headers);
$request->setContent($content);
return $this->send($request);
@@ -105,10 +102,7 @@ public function submit($url, array $fields, $method = RequestInterface::METHOD_P
$url->applyToRequest($request);
- foreach ($headers as $header) {
- $request->addHeader($header);
- }
-
+ $request->addHeaders($headers);
$request->setMethod($method);
$request->setFields($fields);
View
18 lib/Buzz/Message/AbstractMessage.php
@@ -95,7 +95,7 @@ public function toDomDocument()
public function setHeaders(array $headers)
{
- $this->headers = $headers;
+ $this->headers = $this->flattenHeaders($headers);
}
public function addHeader($header)
@@ -105,7 +105,7 @@ public function addHeader($header)
public function addHeaders(array $headers)
{
- $this->headers = array_merge($this->headers, $headers);
+ $this->headers = array_merge($this->headers, $this->flattenHeaders($headers));
}
public function getHeaders()
@@ -133,4 +133,18 @@ public function __toString()
return $string;
}
+
+ protected function flattenHeaders(array $headers)
+ {
+ $flattened = array();
+ foreach ($headers as $key => $header) {
+ if (is_int($key)) {
+ $flattened[] = $header;
+ } else {
+ $flattened[] = $key.': '.$header;
+ }
+ }
+
+ return $flattened;
+ }
}
View
13 lib/Buzz/Message/MessageInterface.php
@@ -22,13 +22,15 @@ function getHeader($name, $glue = "\r\n");
/**
* Returns an array of header lines.
*
- * @return array An array of header lines (integer indexes)
+ * @return array An array of header lines (integer indexes, e.g. ["Header: value"])
*/
function getHeaders();
/**
* Sets all headers on the current message.
*
+ * Headers can be complete ["Header: value"] pairs or an associative array ["Header" => "value"]
+ *
* @param array $headers An array of header lines
*/
function setHeaders(array $headers);
@@ -41,6 +43,15 @@ function setHeaders(array $headers);
function addHeader($header);
/**
+ * Adds a set of headers to this message.
+ *
+ * Headers can be complete ["Header: value"] pairs or an associative array ["Header" => "value"]
+ *
+ * @param array $headers Headers
+ */
+ function addHeaders(array $headers);
+
+ /**
* Returns the content of the message.
*
* @return string The message content
View
2  lib/Buzz/Message/Request.php
@@ -29,7 +29,7 @@ public function setHeaders(array $headers)
{
parent::setHeaders(array());
- foreach ($headers as $header) {
+ foreach ($this->flattenHeaders($headers) as $header) {
$this->addHeader($header);
}
}
View
22 test/Buzz/Test/BrowserTest.php
@@ -25,6 +25,7 @@ public function testBasicMethods($method, $content)
{
$request = $this->getMock('Buzz\Message\RequestInterface');
$response = $this->getMock('Buzz\Message\MessageInterface');
+ $headers = array('X-Foo: bar');
$this->factory->expects($this->once())
->method('createRequest')
@@ -37,8 +38,8 @@ public function testBasicMethods($method, $content)
->method('setResource')
->with('/');
$request->expects($this->once())
- ->method('addHeader')
- ->with('X-Foo: bar');
+ ->method('addHeaders')
+ ->with($headers);
$request->expects($this->once())
->method('setContent')
->with($content);
@@ -49,7 +50,7 @@ public function testBasicMethods($method, $content)
->method('send')
->with($request, $response);
- $actual = $this->browser->$method('http://google.com/', array('X-Foo: bar'), $content);
+ $actual = $this->browser->$method('http://google.com/', $headers, $content);
$this->assertSame($response, $actual);
}
@@ -57,10 +58,10 @@ public function testBasicMethods($method, $content)
public function provideMethods()
{
return array(
- array('get', ''),
- array('head', ''),
- array('post', 'content'),
- array('put', 'content'),
+ array('get', ''),
+ array('head', ''),
+ array('post', 'content'),
+ array('put', 'content'),
array('delete', 'content'),
);
}
@@ -69,6 +70,7 @@ public function testSubmit()
{
$request = $this->getMock('Buzz\Message\Form\FormRequestInterface');
$response = $this->getMock('Buzz\Message\MessageInterface');
+ $headers = array('X-Foo: bar');
$this->factory->expects($this->once())
->method('createFormRequest')
@@ -83,8 +85,8 @@ public function testSubmit()
->method('setResource')
->with('/');
$request->expects($this->once())
- ->method('addHeader')
- ->with('X-Foo: bar');
+ ->method('addHeaders')
+ ->with($headers);
$request->expects($this->once())
->method('setFields')
->with(array('foo' => 'bar', 'bar' => 'foo'));
@@ -95,7 +97,7 @@ public function testSubmit()
->method('send')
->with($request, $response);
- $actual = $this->browser->submit('http://google.com', array('foo' => 'bar', 'bar' => 'foo'), 'PUT', array('X-Foo: bar'));
+ $actual = $this->browser->submit('http://google.com', array('foo' => 'bar', 'bar' => 'foo'), 'PUT', $headers);
$this->assertSame($response, $actual);
}
View
17 test/Buzz/Test/Message/AbstractMessageTest.php
@@ -64,8 +64,21 @@ public function testGetNotFoundHeaderAttribute()
public function testAddHeaders()
{
$message = new Message();
- $message->addHeaders(array('Content-Type: text/xml; charset=utf8'));
- $this->assertEquals(1, count($message->getHeaders()));
+ $message->addHeaders(array('Content-Type: text/xml; charset=utf8', 'Foo' => 'test'));
+ $message->addHeaders(array('Test' => 'foo', 'Foo' => 'test'));
+
+ $expected = array('Content-Type: text/xml; charset=utf8', 'Foo: test', 'Test: foo', 'Foo: test');
+ $this->assertEquals($expected, $message->getHeaders());
+ }
+
+ public function testSetHeaders()
+ {
+ $message = new Message();
+ $message->setHeaders(array('Content-Type: text/xml; charset=utf8', 'Foo' => 'test'));
+ $message->setHeaders(array('Test: foo', 'Foo' => 'test'));
+
+ $expected = array('Test: foo', 'Foo: test');
+ $this->assertEquals($expected, $message->getHeaders());
}
public function testToDomDocument()
Please sign in to comment.
Something went wrong with that request. Please try again.