Skip to content

Commit

Permalink
Added support for using auth informations from URL
Browse files Browse the repository at this point in the history
  • Loading branch information
arnaud-lb committed Jun 2, 2015
1 parent e0d9994 commit 4e30aba
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
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:[email protected]');

$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());
}
}

0 comments on commit 4e30aba

Please sign in to comment.