Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Created the HttpResponse to get HttpSocket responses.
- Loading branch information
Showing
3 changed files
with
338 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,180 @@ | ||
<?php | ||
/** | ||
* HTTP Response from HttpSocket. | ||
* | ||
* PHP 5 | ||
* | ||
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org) | ||
* Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org) | ||
* | ||
* Licensed under The MIT License | ||
* Redistributions of files must retain the above copyright notice. | ||
* | ||
* @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org) | ||
* @link http://cakephp.org CakePHP(tm) Project | ||
* @package cake | ||
* @subpackage cake.cake.libs | ||
* @since CakePHP(tm) v 2.0.0 | ||
* @license MIT License (http://www.opensource.org/licenses/mit-license.php) | ||
*/ | ||
|
||
class HttpResponse implements ArrayAccess { | ||
|
||
/** | ||
* Body content | ||
* | ||
* @var string | ||
*/ | ||
public $body = ''; | ||
|
||
/** | ||
* Headers | ||
* | ||
* @var array | ||
*/ | ||
public $headers = array(); | ||
|
||
/** | ||
* Cookies | ||
* | ||
* @var array | ||
*/ | ||
public $cookies = array(); | ||
|
||
/** | ||
* HTTP version | ||
* | ||
* @var string | ||
*/ | ||
public $httpVersion = 'HTTP/1.1'; | ||
|
||
/** | ||
* Response code | ||
* | ||
* @var integer | ||
*/ | ||
public $code = 0; | ||
|
||
/** | ||
* Reason phrase | ||
* | ||
* @var string | ||
*/ | ||
public $reasonPhrase = ''; | ||
|
||
/** | ||
* Pure raw content | ||
* | ||
* @var string | ||
*/ | ||
public $raw = ''; | ||
|
||
/** | ||
* Body content | ||
* | ||
* @return string | ||
*/ | ||
public function body() { | ||
return (string)$this->body; | ||
} | ||
|
||
/** | ||
* Get header in case insensitive | ||
* | ||
* @param string $name Header name | ||
* @return mixed String if header exists or null | ||
*/ | ||
public function getHeader($name) { | ||
if (isset($this->headers[$name])) { | ||
return $this->headers[$name]; | ||
} | ||
foreach ($this->headers as $key => $value) { | ||
if (strcasecmp($key, $name) == 0) { | ||
return $value; | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
/** | ||
* If return is 200 (OK) | ||
* | ||
* @return boolean | ||
*/ | ||
public function isOk() { | ||
return $this->code == 200; | ||
} | ||
|
||
/** | ||
* ArrayAccess - Offset Exists | ||
* | ||
* @param mixed $offset | ||
* @return boolean | ||
*/ | ||
public function offsetExists($offset) { | ||
return in_array($offset, array('raw', 'status', 'header', 'body', 'cookies')); | ||
} | ||
|
||
/** | ||
* ArrayAccess - Offset Get | ||
* | ||
* @param mixed $offset | ||
* @return mixed | ||
*/ | ||
public function offsetGet($offset) { | ||
switch ($offset) { | ||
case 'raw': | ||
$firstLineLength = strpos($this->raw, "\r\n") + 2; | ||
return array( | ||
'status-line' => $this->httpVersion . ' ' . $this->code . ' ' . $this->reasonPhrase, | ||
'header' => substr($this->raw, $firstLineLength, strpos($this->raw, "\r\n\r\n") - $firstLineLength), | ||
'body' => $this->body, | ||
'response' => $this->raw | ||
); | ||
case 'status': | ||
return array( | ||
'http-version' => $this->httpVersion, | ||
'code' => $this->code, | ||
'reason-phrase' => $this->reasonPhrase | ||
); | ||
case 'header': | ||
return $this->headers; | ||
case 'body': | ||
return $this->body; | ||
case 'cookies': | ||
return $this->cookies; | ||
} | ||
return null; | ||
} | ||
|
||
/** | ||
* ArrayAccess - 0ffset Set | ||
* | ||
* @param mixed $offset | ||
* @param mixed $value | ||
* @return void | ||
*/ | ||
public function offsetSet($offset, $value) { | ||
return; | ||
} | ||
|
||
/** | ||
* ArrayAccess - Offset Unset | ||
* | ||
* @param mixed @offset | ||
* @return void | ||
*/ | ||
public function offsetUnset($offset) { | ||
return; | ||
} | ||
|
||
/** | ||
* Instance as string | ||
* | ||
* @return string | ||
*/ | ||
public function __toString() { | ||
return $this->body(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
<?php | ||
/** | ||
* HttpSocketTest file | ||
* | ||
* PHP 5 | ||
* | ||
* CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing> | ||
* Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org) | ||
* | ||
* Licensed under The MIT License | ||
* Redistributions of files must retain the above copyright notice | ||
* | ||
* @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org) | ||
* @link http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests | ||
* @package cake | ||
* @subpackage cake.tests.cases.libs | ||
* @since CakePHP(tm) v 1.2.0.4206 | ||
* @license MIT License (http://www.opensource.org/licenses/mit-license.php) | ||
*/ | ||
App::import('Core', 'HttpResponse'); | ||
|
||
/** | ||
* HttpResponseTest class | ||
* | ||
* @package cake | ||
* @subpackage cake.tests.cases.libs | ||
*/ | ||
class HttpResponseTest extends CakeTestCase { | ||
/** | ||
* This function sets up a HttpResponse | ||
* | ||
* @return void | ||
*/ | ||
public function setUp() { | ||
$this->HttpResponse = new HttpResponse(); | ||
} | ||
|
||
/** | ||
* testBody | ||
* | ||
* @return void | ||
*/ | ||
public function testBody() { | ||
$this->HttpResponse->body = 'testing'; | ||
$this->assertEqual($this->HttpResponse->body(), 'testing'); | ||
|
||
$this->HttpResponse->body = null; | ||
$this->assertIdentical($this->HttpResponse->body(), ''); | ||
} | ||
|
||
/** | ||
* testToString | ||
* | ||
* @return void | ||
*/ | ||
public function testToString() { | ||
$this->HttpResponse->body = 'other test'; | ||
$this->assertEqual($this->HttpResponse->body(), 'other test'); | ||
$this->assertEqual((string)$this->HttpResponse, 'other test'); | ||
$this->assertTrue(strpos($this->HttpResponse, 'test') > 0); | ||
|
||
$this->HttpResponse->body = null; | ||
$this->assertEqual((string)$this->HttpResponse, ''); | ||
} | ||
|
||
/** | ||
* testGetHeadr | ||
* | ||
* @return void | ||
*/ | ||
public function testGetHeader() { | ||
$this->HttpResponse->headers = array( | ||
'foo' => 'Bar', | ||
'Some' => 'ok', | ||
'HeAdEr' => 'value', | ||
'content-Type' => 'text/plain' | ||
); | ||
|
||
$this->assertEqual($this->HttpResponse->getHeader('foo'), 'Bar'); | ||
$this->assertEqual($this->HttpResponse->getHeader('Foo'), 'Bar'); | ||
$this->assertEqual($this->HttpResponse->getHeader('FOO'), 'Bar'); | ||
$this->assertEqual($this->HttpResponse->getHeader('header'), 'value'); | ||
$this->assertEqual($this->HttpResponse->getHeader('Content-Type'), 'text/plain'); | ||
$this->assertIdentical($this->HttpResponse->getHeader(0), null); | ||
} | ||
|
||
/** | ||
* testIsOk | ||
* | ||
* @return void | ||
*/ | ||
public function testIsOk() { | ||
$this->HttpResponse->code = 0; | ||
$this->assertFalse($this->HttpResponse->isOk()); | ||
$this->HttpResponse->code = -1; | ||
$this->assertFalse($this->HttpResponse->isOk()); | ||
$this->HttpResponse->code = 201; | ||
$this->assertFalse($this->HttpResponse->isOk()); | ||
$this->HttpResponse->code = 'what?'; | ||
$this->assertFalse($this->HttpResponse->isOk()); | ||
$this->HttpResponse->code = 200; | ||
$this->assertTrue($this->HttpResponse->isOk()); | ||
} | ||
|
||
/** | ||
* testArrayAccess | ||
* | ||
* @return void | ||
*/ | ||
public function testArrayAccess() { | ||
$this->HttpResponse->httpVersion = 'HTTP/1.1'; | ||
$this->HttpResponse->code = 200; | ||
$this->HttpResponse->reasonPhrase = 'OK'; | ||
$this->HttpResponse->headers = array( | ||
'Server' => 'CakePHP', | ||
'ContEnt-Type' => 'text/plain' | ||
); | ||
$this->HttpResponse->cookies = array( | ||
'foo' => array('value' => 'bar'), | ||
'bar' => array('value' => 'foo') | ||
); | ||
$this->HttpResponse->body = 'This is a test!'; | ||
$this->HttpResponse->raw = "HTTP/1.1 200 OK\r\nServer: CakePHP\r\nContEnt-Type: text/plain\r\n\r\nThis is a test!"; | ||
|
||
$expected1 = 'HTTP/1.1 200 OK'; | ||
$this->assertEqual($this->HttpResponse['raw']['status-line'], $expected1); | ||
$expected2 = "Server: CakePHP\r\nContEnt-Type: text/plain"; | ||
$this->assertEqual($this->HttpResponse['raw']['header'], $expected2); | ||
$expected3 = 'This is a test!'; | ||
$this->assertEqual($this->HttpResponse['raw']['body'], $expected3); | ||
$expected = $expected1 . "\r\n" . $expected2 . "\r\n\r\n" . $expected3; | ||
$this->assertEqual($this->HttpResponse['raw']['response'], $expected); | ||
|
||
$expected = 'HTTP/1.1'; | ||
$this->assertEqual($this->HttpResponse['status']['http-version'], $expected); | ||
$expected = 200; | ||
$this->assertEqual($this->HttpResponse['status']['code'], $expected); | ||
$expected = 'OK'; | ||
$this->assertEqual($this->HttpResponse['status']['reason-phrase'], $expected); | ||
|
||
$expected = array( | ||
'Server' => 'CakePHP', | ||
'ContEnt-Type' => 'text/plain' | ||
); | ||
$this->assertEqual($this->HttpResponse['header'], $expected); | ||
|
||
$expected = 'This is a test!'; | ||
$this->assertEqual($this->HttpResponse['body'], $expected); | ||
|
||
$expected = array( | ||
'foo' => array('value' => 'bar'), | ||
'bar' => array('value' => 'foo') | ||
); | ||
$this->assertEqual($this->HttpResponse['cookies'], $expected); | ||
} | ||
|
||
} |