Removed parseHeader from HttpSocket (it was not used). Moved the tests of parseHeader to HttpResponse.

This commit is contained in:
Juan Basso 2010-12-14 11:05:37 -02:00
parent 37303d9c37
commit cf7aae7911
3 changed files with 112 additions and 154 deletions

View file

@ -279,7 +279,6 @@ class HttpSocket extends CakeSocket {
$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 @@ class HttpSocket extends CakeSocket {
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 @@ class HttpSocket extends CakeSocket {
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)
*

View file

@ -27,6 +27,16 @@ App::import('Core', 'HttpResponse');
*/
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 @@ class TestHttpResponse extends HttpResponse {
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 @@ class HttpResponseTest extends CakeTestCase {
$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 @@ class HttpResponseTest extends CakeTestCase {
$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
*

View file

@ -122,16 +122,6 @@ class TestHttpSocket extends HttpSocket {
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 @@ class TestHttpSocket extends HttpSocket {
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 @@ class HttpSocketTest extends CakeTestCase {
}
/**
* 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 @@ class HttpSocketTest extends CakeTestCase {
$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)