adding Socket::reset() and refactoring HttpSocket to use it,

adding tests for socket class to get it up to 90+% coverage

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@6756 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
DarkAngelBGE 2008-05-05 14:33:35 +00:00
parent e7609469d4
commit c74b83ca7f
3 changed files with 96 additions and 13 deletions

View file

@ -469,7 +469,7 @@ class HttpSocket extends CakeSocket {
$chunkLength = null; $chunkLength = null;
while ($chunkLength !== 0) { while ($chunkLength !== 0) {
if (!preg_match("/^([0-9a-f]+) *(?:;(.+)=(.+))?\r\n/iU", $body, $match)) { if (!preg_match("/^([0-9a-f]+)(?:;(.+)=(.+))?\r\n/iU", $body, $match)) {
if (!$this->quirksMode) { if (!$this->quirksMode) {
trigger_error(__('HttpSocket::decodeChunkedBody - Could not parse malformed chunk. Activate quirks mode to do this.', true), E_USER_WARNING); trigger_error(__('HttpSocket::decodeChunkedBody - Could not parse malformed chunk. Activate quirks mode to do this.', true), E_USER_WARNING);
return false; return false;
@ -956,7 +956,7 @@ class HttpSocket extends CakeSocket {
* @access public * @access public
*/ */
function reset($full = true) { function reset($full = true) {
static $initalState = array() ; static $initalState = array();
if (empty($initalState)) { if (empty($initalState)) {
$initalState = get_class_vars(__CLASS__); $initalState = get_class_vars(__CLASS__);
} }
@ -966,10 +966,7 @@ class HttpSocket extends CakeSocket {
$this->response = $initalState['response']; $this->response = $initalState['response'];
return true; return true;
} }
parent::reset($initalState);
foreach ($initalState as $property => $value) {
$this->{$property} = $value;
}
return true; return true;
} }
} }

View file

@ -82,7 +82,7 @@ class CakeSocket extends Object {
* @var array * @var array
* @access public * @access public
*/ */
var $error = array(); var $lastError = array();
/** /**
* Constructor. * Constructor.
* *
@ -116,16 +116,14 @@ class CakeSocket extends Object {
$tmp = null; $tmp = null;
$this->connection = @pfsockopen($scheme.$this->config['host'], $this->config['port'], $errNum, $errStr, $this->config['timeout']); $this->connection = @pfsockopen($scheme.$this->config['host'], $this->config['port'], $errNum, $errStr, $this->config['timeout']);
} else { } else {
$this->connection = fsockopen($scheme.$this->config['host'], $this->config['port'], $errNum, $errStr, $this->config['timeout']); $this->connection = @fsockopen($scheme.$this->config['host'], $this->config['port'], $errNum, $errStr, $this->config['timeout']);
} }
if (!empty($errNum) || !empty($errStr)) { if (!empty($errNum) || !empty($errStr)) {
$this->setLastError($errStr, $errNum); $this->setLastError($errStr, $errNum);
} }
$this->connected = is_resource($this->connection); return $this->connected = is_resource($this->connection);
return $this->connected;
} }
/** /**
@ -174,8 +172,8 @@ class CakeSocket extends Object {
* @access public * @access public
*/ */
function lastError() { function lastError() {
if (!empty($this->error)) { if (!empty($this->lastError)) {
return $this->error['num'].': '.$this->error['str']; return $this->lastError['num'].': '.$this->lastError['str'];
} else { } else {
return null; return null;
} }
@ -262,6 +260,25 @@ class CakeSocket extends Object {
function __destruct() { function __destruct() {
$this->disconnect(); $this->disconnect();
} }
/**
* Resets the state of this Socket instance to it's initial state (before Object::__construct got executed)
*
* @return boolean True on success
* @access public
*/
function reset($initialState = null) {
if (empty($initalState)) {
static $initalState = array();
if (empty($initalState)) {
$initalState = get_class_vars(__CLASS__);
}
}
foreach ($initalState as $property => $value) {
$this->{$property} = $value;
}
return true;
}
} }
?> ?>

View file

@ -39,15 +39,59 @@ class SocketTest extends UnitTestCase {
$this->Socket = new CakeSocket(); $this->Socket = new CakeSocket();
} }
function testConstruct() {
$this->Socket->__construct();
$baseConfig = $this->Socket->_baseConfig;
$this->assertIdentical($baseConfig, array(
'persistent' => false,
'host' => 'localhost',
'protocol' => 'tcp',
'port' => 80,
'timeout' => 30
));
$this->Socket->reset();
$this->Socket->__construct(array('host' => 'foo-bar'));
$baseConfig['host'] = 'foo-bar';
$baseConfig['protocol'] = getprotobyname($baseConfig['protocol']);
$this->assertIdentical($this->Socket->config, $baseConfig);
$this->Socket = new CakeSocket(array('host' => 'www.cakephp.org', 'port' => 23, 'protocol' => 'udp'));
$baseConfig = $this->Socket->_baseConfig;
$baseConfig['host'] = 'www.cakephp.org';
$baseConfig['port'] = 23;
$baseConfig['protocol'] = 17;
$this->assertIdentical($this->Socket->config, $baseConfig);
}
function testSocketConnection() { function testSocketConnection() {
$this->assertFalse($this->Socket->connected); $this->assertFalse($this->Socket->connected);
$this->Socket->disconnect(); $this->Socket->disconnect();
$this->assertFalse($this->Socket->connected); $this->assertFalse($this->Socket->connected);
$this->Socket->connect(); $this->Socket->connect();
$this->assertTrue($this->Socket->connected); $this->assertTrue($this->Socket->connected);
$this->Socket->connect();
$this->assertTrue($this->Socket->connected);
$this->Socket->disconnect();
$config = array('persistent' => true);
$this->Socket = new CakeSocket($config);
$this->Socket->connect();
$this->assertTrue($this->Socket->connected);
} }
function testSocketHost() { function testSocketHost() {
$this->Socket = new CakeSocket();
$this->Socket->connect();
$this->assertEqual($this->Socket->address(), '127.0.0.1');
$this->assertPattern('/local/', $this->Socket->host());
$this->assertEqual($this->Socket->lastError(), null);
$this->assertTrue(in_array('127.0.0.1', $this->Socket->addresses()));
$this->Socket = new CakeSocket(array('host' => '127.0.0.1'));
$this->Socket->connect();
$this->assertEqual($this->Socket->address(), '127.0.0.1'); $this->assertEqual($this->Socket->address(), '127.0.0.1');
$this->assertPattern('/local/', $this->Socket->host()); $this->assertPattern('/local/', $this->Socket->host());
$this->assertEqual($this->Socket->lastError(), null); $this->assertEqual($this->Socket->lastError(), null);
@ -59,6 +103,31 @@ class SocketTest extends UnitTestCase {
$this->assertTrue($this->Socket->write($request)); $this->assertTrue($this->Socket->write($request));
} }
function testSocketReading() {
$this->Socket = new CakeSocket(array('timeout' => 5));
$this->Socket->connect();
$this->assertEqual($this->Socket->read(26), null);
}
function testLastError() {
$this->Socket = new CakeSocket();
$this->Socket->setLastError(4, 'some error here');
$this->assertEqual($this->Socket->lastError(), '4: some error here');
}
function testReset() {
$config = array(
'persistent' => true,
'host' => '127.0.0.1',
'protocol' => 'udp',
'port' => 80,
'timeout' => 20
);
$anotherSocket = new CakeSocket($config);
$anotherSocket->reset();
$this->assertEqual(array(), $anotherSocket->config);
}
function tearDown() { function tearDown() {
unset($this->Socket); unset($this->Socket);
} }