Skip to content
Permalink
Browse files

Removed parseHeader from HttpSocket (it was not used). Moved the test…

…s of parseHeader to HttpResponse.
  • Loading branch information...
jrbasso committed Dec 14, 2010
1 parent 37303d9 commit cf7aae791132a3ca418edad5894f52f14cb86826
Showing with 112 additions and 154 deletions.
  1. +0 −48 cake/libs/http_socket.php
  2. +112 −0 cake/tests/cases/libs/http_response.test.php
  3. +0 −106 cake/tests/cases/libs/http_socket.test.php
@@ -279,7 +279,6 @@ public function request($request = array()) {
$cookies = null;
if (is_array($this->request['header'])) {
$this->request['header'] = $this->_parseHeader($this->request['header']);
if (!empty($this->request['cookies'])) {
$cookies = $this->buildCookies($this->request['cookies']);
}
@@ -869,39 +868,6 @@ protected function _buildHeader($header, $mode = 'standard') {
return $returnHeader;
}
/**
* Parses an array based header.
*
* @param array $header Header as an indexed array (field => value)
* @return array Parsed header
*/
protected function _parseHeader($header) {
if (is_array($header)) {
return $header;
} elseif (!is_string($header)) {
return false;
}
preg_match_all("/(.+):(.+)(?:(?<![\t ])\r\n|\$)/Uis", $header, $matches, PREG_SET_ORDER);
$header = array();
foreach ($matches as $match) {
list(, $field, $value) = $match;
$value = trim($value);
$value = preg_replace("/[\t ]\r\n/", "\r\n", $value);
$field = $this->_unescapeToken($field);
if (!isset($header[$field])) {
$header[$field] = $value;
} else {
$header[$field] = array_merge((array)$header[$field], (array)$value);
}
}
return $header;
}
/**
* Builds cookie headers for a request.
*
@@ -917,20 +883,6 @@ public function buildCookies($cookies) {
return $this->_buildHeader(array('Cookie' => implode('; ', $header)), 'pragmatic');
}
/**
* Unescapes a given $token according to RFC 2616 (HTTP 1.1 specs)
*
* @param string $token Token to unescape
* @param array $chars
* @return string Unescaped token
* @todo Test $chars parameter
*/
protected function _unescapeToken($token, $chars = null) {
$regex = '/"([' . implode('', $this->_tokenEscapeChars(true, $chars)) . '])"/';
$token = preg_replace($regex, '\\1', $token);
return $token;
}
/**
* Escapes a given $token according to RFC 2616 (HTTP 1.1 specs)
*
@@ -27,6 +27,16 @@
*/
class TestHttpResponse extends HttpResponse {
/**
* Convenience method for testing protected method
*
* @param array $header Header as an indexed array (field => value)
* @return array Parsed header
*/
public function parseHeader($header) {
return parent::_parseHeader($header);
}
/**
* Convenience method for testing protected method
*
@@ -48,6 +58,26 @@ public function decodeChunkedBody($body) {
return parent::_decodeChunkedBody($body);
}
/**
* Convenience method for testing protected method
*
* @param string $token Token to unescape
* @return string Unescaped token
*/
public function unescapeToken($token, $chars = null) {
return parent::_unescapeToken($token, $chars);
}
/**
* Convenience method for testing protected method
*
* @param boolean $hex true to get them as HEX values, false otherwise
* @return array Escape chars
*/
public function tokenEscapeChars($hex = true, $chars = null) {
return parent::_tokenEscapeChars($hex, $chars);
}
}
/**
@@ -136,6 +166,65 @@ public function testIsOk() {
$this->assertTrue($this->HttpResponse->isOk());
}
/**
* Test that HttpSocket::parseHeader can take apart a given (and valid) $header string and turn it into an array.
*
* @return void
*/
public function testParseHeader() {
$r = $this->HttpResponse->parseHeader(array('foo' => 'Bar', 'fOO-bAr' => 'quux'));
$this->assertEquals($r, array('foo' => 'Bar', 'fOO-bAr' => 'quux'));
$r = $this->HttpResponse->parseHeader(true);
$this->assertEquals($r, false);
$header = "Host: cakephp.org\t\r\n";
$r = $this->HttpResponse->parseHeader($header);
$expected = array(
'Host' => 'cakephp.org'
);
$this->assertEquals($r, $expected);
$header = "Date:Sat, 07 Apr 2007 10:10:25 GMT\r\nX-Powered-By: PHP/5.1.2\r\n";
$r = $this->HttpResponse->parseHeader($header);
$expected = array(
'Date' => 'Sat, 07 Apr 2007 10:10:25 GMT',
'X-Powered-By' => 'PHP/5.1.2'
);
$this->assertEquals($r, $expected);
$header = "people: Jim,John\r\nfoo-LAND: Bar\r\ncAKe-PHP: rocks\r\n";
$r = $this->HttpResponse->parseHeader($header);
$expected = array(
'people' => 'Jim,John',
'foo-LAND' => 'Bar',
'cAKe-PHP' => 'rocks'
);
$this->assertEquals($r, $expected);
$header = "People: Jim,John,Tim\r\nPeople: Lisa,Tina,Chelsea\r\n";
$r = $this->HttpResponse->parseHeader($header);
$expected = array(
'People' => array('Jim,John,Tim', 'Lisa,Tina,Chelsea')
);
$this->assertEquals($r, $expected);
$header = "Multi-Line: I am a \r\nmulti line\t\r\nfield value.\r\nSingle-Line: I am not\r\n";
$r = $this->HttpResponse->parseHeader($header);
$expected = array(
'Multi-Line' => "I am a\r\nmulti line\r\nfield value.",
'Single-Line' => 'I am not'
);
$this->assertEquals($r, $expected);
$header = "Esc\"@\"ped: value\r\n";
$r = $this->HttpResponse->parseHeader($header);
$expected = array(
'Esc@ped' => 'value'
);
$this->assertEquals($r, $expected);
}
/**
* testParseResponse method
*
@@ -339,6 +428,29 @@ public function testParseCookies() {
$this->assertEqual($cookies, $expected);
}
/**
* Test that escaped token strings are properly unescaped by HttpSocket::unescapeToken
*
* @return void
*/
public function testUnescapeToken() {
$this->assertEquals($this->HttpResponse->unescapeToken('Foo'), 'Foo');
$escape = $this->HttpResponse->tokenEscapeChars(false);
foreach ($escape as $char) {
$token = 'My-special-"' . $char . '"-Token';
$unescapedToken = $this->HttpResponse->unescapeToken($token);
$expectedToken = 'My-special-' . $char . '-Token';
$this->assertEquals($unescapedToken, $expectedToken, 'Test token unescaping for ASCII '.ord($char));
}
$token = 'Extreme-":"Token-" "-""""@"-test';
$escapedToken = $this->HttpResponse->unescapeToken($token);
$expectedToken = 'Extreme-:Token- -"@-test';
$this->assertEquals($expectedToken, $escapedToken);
}
/**
* testArrayAccess
*
@@ -122,16 +122,6 @@ public function buildHeader($header, $mode = 'standard') {
return parent::_buildHeader($header, $mode);
}
/**
* Convenience method for testing protected method
*
* @param array $header Header as an indexed array (field => value)
* @return array Parsed header
*/
public function parseHeader($header) {
return parent::_parseHeader($header);
}
/**
* Convenience method for testing protected method
*
@@ -173,16 +163,6 @@ public function EscapeToken($token, $chars = null) {
return parent::_escapeToken($token, $chars);
}
/**
* Convenience method for testing protected method
*
* @param string $token Token to unescape
* @return string Unescaped token
*/
public function unescapeToken($token, $chars = null) {
return parent::_unescapeToken($token, $chars);
}
}
/**
@@ -1375,67 +1355,6 @@ public function testBuildHeader() {
}
/**
* Test that HttpSocket::parseHeader can take apart a given (and valid) $header string and turn it into an array.
*
* @return void
*/
public function testParseHeader() {
$this->Socket->reset();
$r = $this->Socket->parseHeader(array('foo' => 'Bar', 'fOO-bAr' => 'quux'));
$this->assertEquals($r, array('foo' => 'Bar', 'fOO-bAr' => 'quux'));
$r = $this->Socket->parseHeader(true);
$this->assertEquals($r, false);
$header = "Host: cakephp.org\t\r\n";
$r = $this->Socket->parseHeader($header);
$expected = array(
'Host' => 'cakephp.org'
);
$this->assertEquals($r, $expected);
$header = "Date:Sat, 07 Apr 2007 10:10:25 GMT\r\nX-Powered-By: PHP/5.1.2\r\n";
$r = $this->Socket->parseHeader($header);
$expected = array(
'Date' => 'Sat, 07 Apr 2007 10:10:25 GMT',
'X-Powered-By' => 'PHP/5.1.2'
);
$this->assertEquals($r, $expected);
$header = "people: Jim,John\r\nfoo-LAND: Bar\r\ncAKe-PHP: rocks\r\n";
$r = $this->Socket->parseHeader($header);
$expected = array(
'people' => 'Jim,John',
'foo-LAND' => 'Bar',
'cAKe-PHP' => 'rocks'
);
$this->assertEquals($r, $expected);
$header = "People: Jim,John,Tim\r\nPeople: Lisa,Tina,Chelsea\r\n";
$r = $this->Socket->parseHeader($header);
$expected = array(
'People' => array('Jim,John,Tim', 'Lisa,Tina,Chelsea')
);
$this->assertEquals($r, $expected);
$header = "Multi-Line: I am a \r\nmulti line\t\r\nfield value.\r\nSingle-Line: I am not\r\n";
$r = $this->Socket->parseHeader($header);
$expected = array(
'Multi-Line' => "I am a\r\nmulti line\r\nfield value.",
'Single-Line' => 'I am not'
);
$this->assertEquals($r, $expected);
$header = "Esc\"@\"ped: value\r\n";
$r = $this->Socket->parseHeader($header);
$expected = array(
'Esc@ped' => 'value'
);
$this->assertEquals($r, $expected);
}
/**
* testBuildCookies method
*
@@ -1507,31 +1426,6 @@ public function testEscapeToken() {
$this->assertEquals($expectedToken, $escapedToken);
}
/**
* Test that escaped token strings are properly unescaped by HttpSocket::unescapeToken
*
* @return void
*/
public function testUnescapeToken() {
$this->Socket->reset();
$this->assertEquals($this->Socket->unescapeToken('Foo'), 'Foo');
$escape = $this->Socket->tokenEscapeChars(false);
foreach ($escape as $char) {
$token = 'My-special-"' . $char . '"-Token';
$unescapedToken = $this->Socket->unescapeToken($token);
$expectedToken = 'My-special-' . $char . '-Token';
$this->assertEquals($unescapedToken, $expectedToken, 'Test token unescaping for ASCII '.ord($char));
}
$token = 'Extreme-":"Token-" "-""""@"-test';
$escapedToken = $this->Socket->unescapeToken($token);
$expectedToken = 'Extreme-:Token- -"@-test';
$this->assertEquals($expectedToken, $escapedToken);
}
/**
* This tests asserts HttpSocket::reset() resets a HttpSocket instance to it's initial state (before Object::__construct
* got executed)

0 comments on commit cf7aae7

Please sign in to comment.
You can’t perform that action at this time.