diff --git a/README.md b/README.md index 1a01df7..425484b 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,16 @@ Event-driven, streaming HTTP client for [ReactPHP](http://reactphp.org) ## Basic usage -Requests are prepared using the ``Client#request()`` method. +The `request(string $method, string $uri, array $headers = array(), string $version = '1.0'): Request` +method can be used to prepare new Request objects. + +The optional `$headers` parameter can be used to pass additional request +headers. +You can use an associative array (key=value) or an array for each header value +(key=values). +The Request will automatically include an appropriate `Host`, +`User-Agent: react/alpha` and `Connection: close` header if applicable. +You can pass custom header values or use an empty array to omit any of these. The `Request#write(string $data)` method can be used to write data to the request body. diff --git a/src/RequestData.php b/src/RequestData.php index 961db22..e9e39e5 100644 --- a/src/RequestData.php +++ b/src/RequestData.php @@ -73,8 +73,10 @@ public function __toString() $data = ''; $data .= "{$this->method} {$this->getPath()} HTTP/{$this->protocolVersion}\r\n"; - foreach ($headers as $name => $value) { - $data .= "$name: $value\r\n"; + foreach ($headers as $name => $values) { + foreach ((array)$values as $value) { + $data .= "$name: $value\r\n"; + } } $data .= "\r\n"; diff --git a/tests/RequestDataTest.php b/tests/RequestDataTest.php index 6fc8f15..9d63f8d 100644 --- a/tests/RequestDataTest.php +++ b/tests/RequestDataTest.php @@ -34,6 +34,26 @@ public function toStringReturnsHTTPRequestMessageWithProtocolVersion() $this->assertSame($expected, $requestData->__toString()); } + /** @test */ + public function toStringReturnsHTTPRequestMessageWithHeaders() + { + $requestData = new RequestData('GET', 'http://www.example.com', array( + 'User-Agent' => array(), + 'Via' => array( + 'first', + 'second' + ) + )); + + $expected = "GET / HTTP/1.0\r\n" . + "Host: www.example.com\r\n" . + "Via: first\r\n" . + "Via: second\r\n" . + "\r\n"; + + $this->assertSame($expected, $requestData->__toString()); + } + /** @test */ public function toStringReturnsHTTPRequestMessageWithProtocolVersionThroughConstructor() {