mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2025-01-18 18:46:17 +00:00
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:
parent
e7609469d4
commit
c74b83ca7f
3 changed files with 96 additions and 13 deletions
|
@ -469,7 +469,7 @@ class HttpSocket extends CakeSocket {
|
|||
$chunkLength = null;
|
||||
|
||||
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) {
|
||||
trigger_error(__('HttpSocket::decodeChunkedBody - Could not parse malformed chunk. Activate quirks mode to do this.', true), E_USER_WARNING);
|
||||
return false;
|
||||
|
@ -966,10 +966,7 @@ class HttpSocket extends CakeSocket {
|
|||
$this->response = $initalState['response'];
|
||||
return true;
|
||||
}
|
||||
|
||||
foreach ($initalState as $property => $value) {
|
||||
$this->{$property} = $value;
|
||||
}
|
||||
parent::reset($initalState);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -82,7 +82,7 @@ class CakeSocket extends Object {
|
|||
* @var array
|
||||
* @access public
|
||||
*/
|
||||
var $error = array();
|
||||
var $lastError = array();
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
|
@ -116,16 +116,14 @@ class CakeSocket extends Object {
|
|||
$tmp = null;
|
||||
$this->connection = @pfsockopen($scheme.$this->config['host'], $this->config['port'], $errNum, $errStr, $this->config['timeout']);
|
||||
} 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)) {
|
||||
$this->setLastError($errStr, $errNum);
|
||||
}
|
||||
|
||||
$this->connected = is_resource($this->connection);
|
||||
|
||||
return $this->connected;
|
||||
return $this->connected = is_resource($this->connection);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -174,8 +172,8 @@ class CakeSocket extends Object {
|
|||
* @access public
|
||||
*/
|
||||
function lastError() {
|
||||
if (!empty($this->error)) {
|
||||
return $this->error['num'].': '.$this->error['str'];
|
||||
if (!empty($this->lastError)) {
|
||||
return $this->lastError['num'].': '.$this->lastError['str'];
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
|
@ -262,6 +260,25 @@ class CakeSocket extends Object {
|
|||
function __destruct() {
|
||||
$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;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
|
@ -39,15 +39,59 @@ class SocketTest extends UnitTestCase {
|
|||
$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() {
|
||||
$this->assertFalse($this->Socket->connected);
|
||||
$this->Socket->disconnect();
|
||||
$this->assertFalse($this->Socket->connected);
|
||||
$this->Socket->connect();
|
||||
$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() {
|
||||
$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->assertPattern('/local/', $this->Socket->host());
|
||||
$this->assertEqual($this->Socket->lastError(), null);
|
||||
|
@ -59,6 +103,31 @@ class SocketTest extends UnitTestCase {
|
|||
$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() {
|
||||
unset($this->Socket);
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue