Skip to content
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
25 changes: 25 additions & 0 deletions src/RequestData.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@ private function mergeDefaultheaders(array $headers)
{
$port = ($this->getDefaultPort() === $this->getPort()) ? '' : ":{$this->getPort()}";
$connectionHeaders = ('1.1' === $this->protocolVersion) ? array('Connection' => 'close') : array();
$authHeaders = $this->getAuthHeaders();

return array_merge(
array(
'Host' => $this->getHost().$port,
'User-Agent' => 'React/alpha',
),
$connectionHeaders,
$authHeaders,
$headers
);
}
Expand Down Expand Up @@ -78,4 +80,27 @@ public function __toString()

return $data;
}

private function getUrlUserPass()
{
$components = parse_url($this->url);

if (isset($components['user'])) {
return array(
'user' => $components['user'],
'pass' => isset($components['pass']) ? $components['pass'] : null,
);
}
}

private function getAuthHeaders()
{
if (null !== $auth = $this->getUrlUserPass()) {
return array(
'Authorization' => 'Basic ' . base64_encode($auth['user'].':'.$auth['pass']),
);
}

return array();
}
}
37 changes: 37 additions & 0 deletions tests/RequestDataTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace React\Tests\HttpClient;

use React\HttpClient\RequestData;

class RequestDataTest extends TestCase
{
/** @test */
public function toStringReturnsHTTPRequestMessage()
{
$requestData = new RequestData('GET', 'http://www.example.com');

$expected = "GET / HTTP/1.1\r\n" .
"Host: www.example.com\r\n" .
"User-Agent: React/alpha\r\n" .
"Connection: close\r\n" .
"\r\n";

$this->assertSame($expected, $requestData->__toString());
}

/** @test */
public function toStringUsesUserPassFromURL()
{
$requestData = new RequestData('GET', 'http://john:dummy@www.example.com');

$expected = "GET / HTTP/1.1\r\n" .
"Host: www.example.com\r\n" .
"User-Agent: React/alpha\r\n" .
"Connection: close\r\n" .
"Authorization: Basic am9objpkdW1teQ==\r\n" .
"\r\n";

$this->assertSame($expected, $requestData->__toString());
}
}