Skip to content

Support passing arrays for request header values #100

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 26, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
6 changes: 4 additions & 2 deletions src/RequestData.php
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down
20 changes: 20 additions & 0 deletions tests/RequestDataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down