Skip to content

Commit c1374e0

Browse files
authored
Merge pull request #100 from clue-labs/headers
Support passing arrays for request header values
2 parents 9ef920e + 792028b commit c1374e0

File tree

3 files changed

+34
-3
lines changed

3 files changed

+34
-3
lines changed

README.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,16 @@ Event-driven, streaming HTTP client for [ReactPHP](http://reactphp.org)
1414

1515
## Basic usage
1616

17-
Requests are prepared using the ``Client#request()`` method.
17+
The `request(string $method, string $uri, array $headers = array(), string $version = '1.0'): Request`
18+
method can be used to prepare new Request objects.
19+
20+
The optional `$headers` parameter can be used to pass additional request
21+
headers.
22+
You can use an associative array (key=value) or an array for each header value
23+
(key=values).
24+
The Request will automatically include an appropriate `Host`,
25+
`User-Agent: react/alpha` and `Connection: close` header if applicable.
26+
You can pass custom header values or use an empty array to omit any of these.
1827

1928
The `Request#write(string $data)` method can be used to
2029
write data to the request body.

src/RequestData.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,10 @@ public function __toString()
8181

8282
$data = '';
8383
$data .= "{$this->method} {$this->getPath()} HTTP/{$this->protocolVersion}\r\n";
84-
foreach ($headers as $name => $value) {
85-
$data .= "$name: $value\r\n";
84+
foreach ($headers as $name => $values) {
85+
foreach ((array)$values as $value) {
86+
$data .= "$name: $value\r\n";
87+
}
8688
}
8789
$data .= "\r\n";
8890

tests/RequestDataTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,26 @@ public function toStringReturnsHTTPRequestMessageWithProtocolVersion()
8686
$this->assertSame($expected, $requestData->__toString());
8787
}
8888

89+
/** @test */
90+
public function toStringReturnsHTTPRequestMessageWithHeaders()
91+
{
92+
$requestData = new RequestData('GET', 'http://www.example.com', array(
93+
'User-Agent' => array(),
94+
'Via' => array(
95+
'first',
96+
'second'
97+
)
98+
));
99+
100+
$expected = "GET / HTTP/1.0\r\n" .
101+
"Host: www.example.com\r\n" .
102+
"Via: first\r\n" .
103+
"Via: second\r\n" .
104+
"\r\n";
105+
106+
$this->assertSame($expected, $requestData->__toString());
107+
}
108+
89109
/** @test */
90110
public function toStringReturnsHTTPRequestMessageWithProtocolVersionThroughConstructor()
91111
{

0 commit comments

Comments
 (0)