Skip to content

Commit

Permalink
100% unittest coverage except the client.
Browse files Browse the repository at this point in the history
hasHeader / getHeaders is in line with psr/http.
  • Loading branch information
evert committed Sep 23, 2014
1 parent 5ddc682 commit 5c341a8
Show file tree
Hide file tree
Showing 16 changed files with 146 additions and 43 deletions.
7 changes: 3 additions & 4 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ ChangeLog
3.0.0 (2014-09-23)
------------------

* No changes from 2.1.0-alpha1, but doing a major version bump due to the
api breaks.

* `getHeaders()` now returns header values as an array, just like psr/http.
* Added `hasHeader()`.

2.1.0-alpha1 (2014-09-15)
-------------------------
Expand All @@ -16,7 +15,7 @@ ChangeLog
* This means that `setHeaders()` does not wipe out every existing header
anymore.
* We also support multiple headers with the same name.
* Use `Request::getHeadersAsArray()` and `Response::getHeadersAsArray()` to
* Use `Request::getHeaderAsArray()` and `Response::getHeaderAsArray()` to
get a hold off multiple headers with the same name.
* If you use `getHeader()`, and there's more than 1 header with that name, we
concatenate all these with a comma.
Expand Down
2 changes: 1 addition & 1 deletion lib/Auth/AWS.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ protected function getAmzHeaders() {
$headers = $this->request->getHeaders();
foreach($headers as $headerName => $headerValue) {
if (strpos(strtolower($headerName),'x-amz-')===0) {
$amzHeaders[strtolower($headerName)] = str_replace( ["\r\n"], [' '],$headerValue) . "\n";
$amzHeaders[strtolower($headerName)] = str_replace( ["\r\n"], [' '],$headerValue[0]) . "\n";
}
}
ksort($amzHeaders);
Expand Down
6 changes: 4 additions & 2 deletions lib/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -415,9 +415,11 @@ protected function createCurlSettingsArray(RequestInterface $request) {
}

$nHeaders = [];
foreach($request->getHeaders() as $key=>$value) {
foreach($request->getHeaders() as $key=>$values) {

$nHeaders[] = $key . ': ' . $value;
foreach($values as $value) {
$nHeaders[] = $key . ': ' . $value;
}

}
$settings[CURLOPT_HTTPHEADER] = $nHeaders;
Expand Down
17 changes: 14 additions & 3 deletions lib/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,21 +106,32 @@ function setBody($body) {
/**
* Returns all the HTTP headers as an array.
*
* Any HTTP headers with more than one value will be concatenated with
* comma (,).
* Every header is returned as an array, with one or more values.
*
* @return array
*/
function getHeaders() {

$result = [];
foreach($this->headers as $headerInfo) {
$result[$headerInfo[0]] = implode(',', $headerInfo[1]);
$result[$headerInfo[0]] = $headerInfo[1];
}
return $result;

}

/**
* Will return true or false, depending on if a http header exists.
*
* @param string $name
* @return bool
*/
function hasHeader($name) {

return isset($this->headers[strtolower($name)]);

}

/**
* Returns a specific HTTP header, based on it's name.
*
Expand Down
15 changes: 13 additions & 2 deletions lib/MessageDecoratorTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,7 @@ function setBody($body) {
/**
* Returns all the HTTP headers as an array.
*
* Any HTTP headers with more than one value will be concatenated with
* comma (,).
* Every header is returned as an array, with one or more values.
*
* @return array
*/
Expand All @@ -91,6 +90,18 @@ function getHeaders() {

}

/**
* Will return true or false, depending on if a http header exists.
*
* @param string $name
* @return bool
*/
function hasHeader($name) {

return $this->inner->hasHeader($name);

}

/**
* Returns a specific HTTP header, based on it's name.
*
Expand Down
11 changes: 9 additions & 2 deletions lib/MessageInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,20 @@ function setBody($body);
/**
* Returns all the HTTP headers as an array.
*
* Any HTTP headers with more than one value will be concatenated with
* comma (,).
* Every header is returned as an array, with one or more values.
*
* @return array
*/
function getHeaders();

/**
* Will return true or false, depending on if a http header exists.
*
* @param string $name
* @return bool
*/
function hasHeader($name);

/**
* Returns a specific HTTP header, based on it's name.
*
Expand Down
4 changes: 3 additions & 1 deletion lib/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,9 @@ function __toString() {

$str = $this->getMethod() . ' ' . $this->getUrl() . ' HTTP/' . $this->getHTTPVersion() . "\r\n";
foreach($this->getHeaders() as $key=>$value) {
$str.= $key . ": " . $value . "\r\n";
foreach($value as $v) {
$str.= $key . ": " . $v . "\r\n";
}
}
$str.="\r\n";
$str.=$this->getBodyAsString();
Expand Down
4 changes: 3 additions & 1 deletion lib/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,9 @@ function __toString() {

$str = 'HTTP/' . $this->httpVersion . ' ' . $this->getStatus() . ' ' . $this->getStatusText() . "\r\n";
foreach($this->getHeaders() as $key=>$value) {
$str.= $key . ": " . $value . "\r\n";
foreach($value as $v) {
$str.= $key . ": " . $v . "\r\n";
}
}
$str.="\r\n";
$str.=$this->getBodyAsString();
Expand Down
8 changes: 7 additions & 1 deletion lib/Sapi.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,13 @@ static function sendResponse(ResponseInterface $response) {
header('HTTP/' . $response->getHttpVersion() . ' ' . $response->getStatus() . ' ' . $response->getStatusText());
foreach($response->getHeaders() as $key=>$value) {

header($key . ': ' . $value);
foreach($value as $k=>$v) {
if ($k === 0) {
header($key . ': ' . $v);
} else {
header($key . ': ' . $v, false);
}
}

}
file_put_contents('php://output', $response->getBody());
Expand Down
30 changes: 30 additions & 0 deletions tests/HTTP/Auth/DigestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,35 @@ public function testInvalidDigest2() {

public function testDigestAuthInt() {

$this->auth->setQOP(Digest::QOP_AUTHINT);
list($nonce,$opaque) = $this->getServerTokens(Digest::QOP_AUTHINT);

$username = 'admin';
$password = 12345;
$nc = '00003';
$cnonce = uniqid();

$digestHash = md5(
md5($username . ':' . self::REALM . ':' . $password) . ':' .
$nonce . ':' .
$nc . ':' .
$cnonce . ':' .
'auth-int:' .
md5('POST' . ':' . '/' . ':' . md5('body'))
);

$this->request->setMethod('POST');
$this->request->setHeader('Authorization','Digest username="'.$username.'", realm="' . self::REALM . '", nonce="' . $nonce . '", uri="/", response="' . $digestHash . '", opaque="' . $opaque . '", qop=auth-int,nc='.$nc.',cnonce="' . $cnonce . '"');
$this->request->setBody('body');

$this->auth->init();

$this->assertTrue($this->auth->validateA1(md5($username . ':' . self::REALM . ':' . $password)),'Authentication is deemed invalid through validateA1');

}

public function testDigestAuthBoth() {

$this->auth->setQOP(Digest::QOP_AUTHINT | Digest::QOP_AUTH);
list($nonce,$opaque) = $this->getServerTokens(Digest::QOP_AUTHINT| Digest::QOP_AUTH);

Expand Down Expand Up @@ -133,6 +162,7 @@ public function testDigestAuthInt() {

}


private function getServerTokens($qop = Digest::QOP_AUTH) {

$this->auth->requireLogin();
Expand Down
4 changes: 2 additions & 2 deletions tests/HTTP/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ function testParseCurlResult() {
$this->assertEquals(Client::STATUS_SUCCESS, $result['status']);
$this->assertEquals(200, $result['http_code']);
$this->assertEquals(200, $result['response']->getStatus());
$this->assertEquals(['Header1' => 'Val1'], $result['response']->getHeaders());
$this->assertEquals(['Header1' => ['Val1']], $result['response']->getHeaders());
$this->assertEquals('Foo', $result['response']->getBodyAsString());

}
Expand Down Expand Up @@ -299,7 +299,7 @@ function testDoRequest() {
});
$response = $client->doRequest($request);
$this->assertEquals(200, $response->getStatus());
$this->assertEquals(['Header1' => 'Val1'], $response->getHeaders());
$this->assertEquals(['Header1' => ['Val1']], $response->getHeaders());
$this->assertEquals('Foo', $response->getBodyAsString());

}
Expand Down
16 changes: 10 additions & 6 deletions tests/HTTP/MessageDecoratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,27 +32,31 @@ function testHeaders() {
'a' => 'b',
]);

$this->assertEquals(['a' => 'b'], $this->inner->getHeaders());
$this->assertEquals(['a' => 'b'], $this->outer->getHeaders());
$this->assertEquals(['a' => ['b']], $this->inner->getHeaders());
$this->assertEquals(['a' => ['b']], $this->outer->getHeaders());

$this->outer->setHeaders([
'c' => 'd',
]);

$this->assertEquals(['a' => 'b', 'c' => 'd'], $this->inner->getHeaders());
$this->assertEquals(['a' => 'b', 'c' => 'd'], $this->outer->getHeaders());
$this->assertEquals(['a' => ['b'], 'c' => ['d']], $this->inner->getHeaders());
$this->assertEquals(['a' => ['b'], 'c' => ['d']], $this->outer->getHeaders());

$this->outer->addHeaders([
'e' => 'f',
]);

$this->assertEquals(['a' => 'b', 'c' => 'd', 'e' => 'f'], $this->inner->getHeaders());
$this->assertEquals(['a' => 'b', 'c' => 'd', 'e' => 'f'], $this->outer->getHeaders());
$this->assertEquals(['a' => ['b'], 'c' => ['d'], 'e' => ['f']], $this->inner->getHeaders());
$this->assertEquals(['a' => ['b'], 'c' => ['d'], 'e' => ['f']], $this->outer->getHeaders());
}

function testHeader() {

$this->assertFalse($this->outer->hasHeader('a'));
$this->assertFalse($this->inner->hasHeader('a'));
$this->outer->setHeader('a', 'c');
$this->assertTrue($this->outer->hasHeader('a'));
$this->assertTrue($this->inner->hasHeader('a'));

$this->assertEquals('c', $this->inner->getHeader('A'));
$this->assertEquals('c', $this->outer->getHeader('A'));
Expand Down
26 changes: 18 additions & 8 deletions tests/HTTP/MessageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ function testSetHeaders() {
$message = new MessageMock();

$headers = [
'X-Foo' => '1',
'X-Bar' => '2',
'X-Foo' => ['1'],
'X-Bar' => ['2'],
];

$message->setHeaders($headers);
Expand All @@ -98,8 +98,8 @@ function testSetHeaders() {
]);

$expected = [
'X-Foo' => '3,4',
'X-Bar' => '5',
'X-Foo' => ['3','4'],
'X-Bar' => ['5'],
];

$this->assertEquals($expected, $message->getHeaders());
Expand All @@ -111,8 +111,8 @@ function testAddHeaders() {
$message = new MessageMock();

$headers = [
'X-Foo' => '1',
'X-Bar' => '2',
'X-Foo' => ['1'],
'X-Bar' => ['2'],
];

$message->addHeaders($headers);
Expand All @@ -124,8 +124,8 @@ function testAddHeaders() {
]);

$expected = [
'X-Foo' => '1,3,4',
'X-Bar' => '2,5',
'X-Foo' => ['1','3','4'],
'X-Bar' => ['2','5'],
];

$this->assertEquals($expected, $message->getHeaders());
Expand Down Expand Up @@ -182,6 +182,16 @@ function testMultipleHeaders() {

}

function testHasHeaders() {

$message = new MessageMock();

$this->assertFalse($message->hasHeader('X-Foo'));
$message->setHeader('X-Foo', 'Bar');
$this->assertTrue($message->hasHeader('X-Foo'));

}

}

class MessageMock extends Message { }
2 changes: 1 addition & 1 deletion tests/HTTP/RequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ function testConstruct() {
$this->assertEquals('GET', $request->getMethod());
$this->assertEquals('/foo', $request->getUrl());
$this->assertEquals(array(
'User-Agent' => 'Evert',
'User-Agent' => ['Evert'],
), $request->getHeaders());

}
Expand Down
Loading

0 comments on commit 5c341a8

Please sign in to comment.