Skip to content

Commit 792028b

Browse files
committed
Support passing arrays for request header values
1 parent caf646e commit 792028b

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
@@ -73,8 +73,10 @@ public function __toString()
7373

7474
$data = '';
7575
$data .= "{$this->method} {$this->getPath()} HTTP/{$this->protocolVersion}\r\n";
76-
foreach ($headers as $name => $value) {
77-
$data .= "$name: $value\r\n";
76+
foreach ($headers as $name => $values) {
77+
foreach ((array)$values as $value) {
78+
$data .= "$name: $value\r\n";
79+
}
7880
}
7981
$data .= "\r\n";
8082

tests/RequestDataTest.php

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

37+
/** @test */
38+
public function toStringReturnsHTTPRequestMessageWithHeaders()
39+
{
40+
$requestData = new RequestData('GET', 'http://www.example.com', array(
41+
'User-Agent' => array(),
42+
'Via' => array(
43+
'first',
44+
'second'
45+
)
46+
));
47+
48+
$expected = "GET / HTTP/1.0\r\n" .
49+
"Host: www.example.com\r\n" .
50+
"Via: first\r\n" .
51+
"Via: second\r\n" .
52+
"\r\n";
53+
54+
$this->assertSame($expected, $requestData->__toString());
55+
}
56+
3757
/** @test */
3858
public function toStringReturnsHTTPRequestMessageWithProtocolVersionThroughConstructor()
3959
{

0 commit comments

Comments
 (0)