Initial commit

This commit is contained in:
mareksebera 2014-09-10 20:20:58 +02:00
commit 3b93da31de
1004 changed files with 265840 additions and 0 deletions

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,373 @@
<?php
/**
* SocketTest file
*
* CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
* @package Cake.Test.Case.Network
* @since CakePHP(tm) v 1.2.0.4206
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
App::uses('CakeSocket', 'Network');
/**
* SocketTest class
*
* @package Cake.Test.Case.Network
*/
class CakeSocketTest extends CakeTestCase {
/**
* setUp method
*
* @return void
*/
public function setUp() {
parent::setUp();
$this->Socket = new CakeSocket(array('timeout' => 1));
}
/**
* tearDown method
*
* @return void
*/
public function tearDown() {
parent::tearDown();
unset($this->Socket);
}
/**
* testConstruct method
*
* @return void
*/
public function testConstruct() {
$this->Socket = new CakeSocket();
$config = $this->Socket->config;
$this->assertSame($config, array(
'persistent' => false,
'host' => 'localhost',
'protocol' => getprotobyname('tcp'),
'port' => 80,
'timeout' => 30
));
$this->Socket->reset();
$this->Socket->__construct(array('host' => 'foo-bar'));
$config['host'] = 'foo-bar';
$this->assertSame($this->Socket->config, $config);
$this->Socket = new CakeSocket(array('host' => 'www.cakephp.org', 'port' => 23, 'protocol' => 'udp'));
$config = $this->Socket->config;
$config['host'] = 'www.cakephp.org';
$config['port'] = 23;
$config['protocol'] = 17;
$this->assertSame($this->Socket->config, $config);
}
/**
* testSocketConnection method
*
* @return void
*/
public function testSocketConnection() {
$this->assertFalse($this->Socket->connected);
$this->Socket->disconnect();
$this->assertFalse($this->Socket->connected);
try {
$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);
} catch (SocketException $e) {
$this->markTestSkipped('Cannot test network, skipping.');
}
}
/**
* data provider function for testInvalidConnection
*
* @return array
*/
public static function invalidConnections() {
return array(
array(array('host' => 'invalid.host', 'port' => 9999, 'timeout' => 1)),
array(array('host' => '127.0.0.1', 'port' => '70000', 'timeout' => 1))
);
}
/**
* testInvalidConnection method
*
* @dataProvider invalidConnections
* @expectedException SocketException
* @return void
*/
public function testInvalidConnection($data) {
$this->Socket->config = array_merge($this->Socket->config, $data);
$this->Socket->connect();
}
/**
* testSocketHost method
*
* @return void
*/
public function testSocketHost() {
try {
$this->Socket = new CakeSocket();
$this->Socket->connect();
$this->assertEquals('127.0.0.1', $this->Socket->address());
$this->assertEquals(gethostbyaddr('127.0.0.1'), $this->Socket->host());
$this->assertEquals(null, $this->Socket->lastError());
$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->assertEquals('127.0.0.1', $this->Socket->address());
$this->assertEquals(gethostbyaddr('127.0.0.1'), $this->Socket->host());
$this->assertEquals(null, $this->Socket->lastError());
$this->assertTrue(in_array('127.0.0.1', $this->Socket->addresses()));
} catch (SocketException $e) {
$this->markTestSkipped('Cannot test network, skipping.');
}
}
/**
* testSocketWriting method
*
* @return void
*/
public function testSocketWriting() {
try {
$request = "GET / HTTP/1.1\r\nConnection: close\r\n\r\n";
$this->assertTrue((bool)$this->Socket->write($request));
} catch (SocketException $e) {
$this->markTestSkipped('Cannot test network, skipping.');
}
}
/**
* testSocketReading method
*
* @return void
*/
public function testSocketReading() {
$this->Socket = new CakeSocket(array('timeout' => 5));
try {
$this->Socket->connect();
$this->assertEquals(null, $this->Socket->read(26));
$config = array('host' => 'google.com', 'port' => 80, 'timeout' => 1);
$this->Socket = new CakeSocket($config);
$this->assertTrue($this->Socket->connect());
$this->assertEquals(null, $this->Socket->read(26));
$this->assertEquals('2: ' . __d('cake_dev', 'Connection timed out'), $this->Socket->lastError());
} catch (SocketException $e) {
$this->markTestSkipped('Cannot test network, skipping.');
}
}
/**
* testTimeOutConnection method
*
* @return void
*/
public function testTimeOutConnection() {
$config = array('host' => '127.0.0.1', 'timeout' => 0.5);
$this->Socket = new CakeSocket($config);
try {
$this->assertTrue($this->Socket->connect());
$config = array('host' => '127.0.0.1', 'timeout' => 0.00001);
$this->Socket = new CakeSocket($config);
$this->assertFalse($this->Socket->read(1024 * 1024));
$this->assertEquals('2: ' . __d('cake_dev', 'Connection timed out'), $this->Socket->lastError());
} catch (SocketException $e) {
$this->markTestSkipped('Cannot test network, skipping.');
}
}
/**
* testLastError method
*
* @return void
*/
public function testLastError() {
$this->Socket = new CakeSocket();
$this->Socket->setLastError(4, 'some error here');
$this->assertEquals('4: some error here', $this->Socket->lastError());
}
/**
* testReset method
*
* @return void
*/
public function testReset() {
$config = array(
'persistent' => true,
'host' => '127.0.0.1',
'protocol' => 'udp',
'port' => 80,
'timeout' => 20
);
$anotherSocket = new CakeSocket($config);
$anotherSocket->reset();
$this->assertEquals(array(), $anotherSocket->config);
}
/**
* testEncrypt
*
* @expectedException SocketException
* @return void
*/
public function testEnableCryptoSocketExceptionNoSsl() {
$this->skipIf(!extension_loaded('openssl'), 'OpenSSL is not enabled cannot test SSL.');
$configNoSslOrTls = array('host' => 'localhost', 'port' => 80, 'timeout' => 0.1);
// testing exception on no ssl socket server for ssl and tls methods
$this->Socket = new CakeSocket($configNoSslOrTls);
$this->Socket->connect();
$this->Socket->enableCrypto('sslv3', 'client');
}
/**
* testEnableCryptoSocketExceptionNoTls
*
* @expectedException SocketException
* @return void
*/
public function testEnableCryptoSocketExceptionNoTls() {
$configNoSslOrTls = array('host' => 'localhost', 'port' => 80, 'timeout' => 0.1);
// testing exception on no ssl socket server for ssl and tls methods
$this->Socket = new CakeSocket($configNoSslOrTls);
$this->Socket->connect();
$this->Socket->enableCrypto('tls', 'client');
}
/**
* _connectSocketToSslTls
*
* @return void
*/
protected function _connectSocketToSslTls() {
$this->skipIf(!extension_loaded('openssl'), 'OpenSSL is not enabled cannot test SSL.');
$configSslTls = array('host' => 'smtp.gmail.com', 'port' => 465, 'timeout' => 5);
$this->Socket = new CakeSocket($configSslTls);
try {
$this->Socket->connect();
} catch (SocketException $e) {
$this->markTestSkipped('Cannot test network, skipping.');
}
}
/**
* testEnableCryptoBadMode
*
* @expectedException InvalidArgumentException
* @return void
*/
public function testEnableCryptoBadMode() {
// testing wrong encryption mode
$this->_connectSocketToSslTls();
$this->Socket->enableCrypto('doesntExistMode', 'server');
$this->Socket->disconnect();
}
/**
* testEnableCrypto
*
* @return void
*/
public function testEnableCrypto() {
// testing on ssl server
$this->_connectSocketToSslTls();
$this->assertTrue($this->Socket->enableCrypto('sslv3', 'client'));
$this->Socket->disconnect();
// testing on tls server
$this->_connectSocketToSslTls();
$this->assertTrue($this->Socket->enableCrypto('tls', 'client'));
$this->Socket->disconnect();
}
/**
* testEnableCryptoExceptionEnableTwice
*
* @expectedException SocketException
* @return void
*/
public function testEnableCryptoExceptionEnableTwice() {
// testing on tls server
$this->_connectSocketToSslTls();
$this->Socket->enableCrypto('tls', 'client');
$this->Socket->enableCrypto('tls', 'client');
}
/**
* testEnableCryptoExceptionDisableTwice
*
* @expectedException SocketException
* @return void
*/
public function testEnableCryptoExceptionDisableTwice() {
// testing on tls server
$this->_connectSocketToSslTls();
$this->Socket->enableCrypto('tls', 'client', false);
}
/**
* testEnableCryptoEnableStatus
*
* @return void
*/
public function testEnableCryptoEnableStatus() {
// testing on tls server
$this->_connectSocketToSslTls();
$this->assertFalse($this->Socket->encrypted);
$this->Socket->enableCrypto('tls', 'client', true);
$this->assertTrue($this->Socket->encrypted);
}
/**
* test getting the context for a socket.
*
* @return void
*/
public function testGetContext() {
$this->skipIf(!extension_loaded('openssl'), 'OpenSSL is not enabled cannot test SSL.');
$config = array(
'host' => 'smtp.gmail.com',
'port' => 465,
'timeout' => 5,
'context' => array(
'ssl' => array('capture_peer' => true)
)
);
$this->Socket = new CakeSocket($config);
$this->Socket->connect();
$result = $this->Socket->context();
$this->assertEquals($config['context'], $result);
}
}

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,77 @@
<?php
/**
* DebugTransportTest file
*
* CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
* @package Cake.Test.Case.Network.Email
* @since CakePHP(tm) v 2.0.0
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
App::uses('CakeEmail', 'Network/Email');
App::uses('AbstractTransport', 'Network/Email');
App::uses('DebugTransport', 'Network/Email');
/**
* Test case
*
*/
class DebugTransportTest extends CakeTestCase {
/**
* Setup
*
* @return void
*/
public function setUp() {
parent::setUp();
$this->DebugTransport = new DebugTransport();
}
/**
* testSend method
*
* @return void
*/
public function testSend() {
$email = $this->getMock('CakeEmail', array('message'), array(), 'DebugCakeEmail');
$email->from('noreply@cakephp.org', 'CakePHP Test');
$email->to('cake@cakephp.org', 'CakePHP');
$email->cc(array('mark@cakephp.org' => 'Mark Story', 'juan@cakephp.org' => 'Juan Basso'));
$email->bcc('phpnut@cakephp.org');
$email->messageID('<4d9946cf-0a44-4907-88fe-1d0ccbdd56cb@localhost>');
$email->subject('Testing Message');
$date = date(DATE_RFC2822);
$email->setHeaders(array('X-Mailer' => DebugCakeEmail::EMAIL_CLIENT, 'Date' => $date));
$email->expects($this->once())->method('message')->will($this->returnValue(array('First Line', 'Second Line', '.Third Line', '')));
$headers = "From: CakePHP Test <noreply@cakephp.org>\r\n";
$headers .= "To: CakePHP <cake@cakephp.org>\r\n";
$headers .= "Cc: Mark Story <mark@cakephp.org>, Juan Basso <juan@cakephp.org>\r\n";
$headers .= "X-Mailer: CakePHP Email\r\n";
$headers .= "Date: " . $date . "\r\n";
$headers .= "Message-ID: <4d9946cf-0a44-4907-88fe-1d0ccbdd56cb@localhost>\r\n";
$headers .= "Subject: Testing Message\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/plain; charset=UTF-8\r\n";
$headers .= "Content-Transfer-Encoding: 8bit";
$data = "First Line\r\n";
$data .= "Second Line\r\n";
$data .= ".Third Line\r\n"; // Not use 'RFC5321 4.5.2.Transparency' in DebugTransport.
$result = $this->DebugTransport->send($email);
$this->assertEquals($headers, $result['headers']);
$this->assertEquals($data, $result['message']);
}
}

View file

@ -0,0 +1,91 @@
<?php
/**
* MailTransportTest file
*
* CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
* @package Cake.Test.Case.Network.Email
* @since CakePHP(tm) v 2.0.0
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
App::uses('CakeEmail', 'Network/Email');
App::uses('AbstractTransport', 'Network/Email');
App::uses('MailTransport', 'Network/Email');
/**
* Test case
*
*/
class MailTransportTest extends CakeTestCase {
/**
* Setup
*
* @return void
*/
public function setUp() {
parent::setUp();
$this->MailTransport = $this->getMock('MailTransport', array('_mail'));
$this->MailTransport->config(array('additionalParameters' => '-f'));
}
/**
* testSend method
*
* @return void
*/
public function testSendData() {
$email = $this->getMock('CakeEmail', array('message'), array());
$email->from('noreply@cakephp.org', 'CakePHP Test');
$email->returnPath('pleasereply@cakephp.org', 'CakePHP Return');
$email->to('cake@cakephp.org', 'CakePHP');
$email->cc(array('mark@cakephp.org' => 'Mark Story', 'juan@cakephp.org' => 'Juan Basso'));
$email->bcc('phpnut@cakephp.org');
$email->messageID('<4d9946cf-0a44-4907-88fe-1d0ccbdd56cb@localhost>');
$longNonAscii = 'Foø Bår Béz Foø Bår Béz Foø Bår Béz Foø Bår Béz';
$email->subject($longNonAscii);
$date = date(DATE_RFC2822);
$email->setHeaders(array(
'X-Mailer' => 'CakePHP Email',
'Date' => $date,
'X-add' => mb_encode_mimeheader($longNonAscii, 'utf8', 'B'),
));
$email->expects($this->any())->method('message')
->will($this->returnValue(array('First Line', 'Second Line', '.Third Line', '')));
$encoded = '=?UTF-8?B?Rm/DuCBCw6VyIELDqXogRm/DuCBCw6VyIELDqXogRm/DuCBCw6VyIELDqXog?=';
$encoded .= ' =?UTF-8?B?Rm/DuCBCw6VyIELDqXo=?=';
$data = "From: CakePHP Test <noreply@cakephp.org>" . PHP_EOL;
$data .= "Return-Path: CakePHP Return <pleasereply@cakephp.org>" . PHP_EOL;
$data .= "Cc: Mark Story <mark@cakephp.org>, Juan Basso <juan@cakephp.org>" . PHP_EOL;
$data .= "Bcc: phpnut@cakephp.org" . PHP_EOL;
$data .= "X-Mailer: CakePHP Email" . PHP_EOL;
$data .= "Date: " . $date . PHP_EOL;
$data .= "X-add: " . $encoded . PHP_EOL;
$data .= "Message-ID: <4d9946cf-0a44-4907-88fe-1d0ccbdd56cb@localhost>" . PHP_EOL;
$data .= "MIME-Version: 1.0" . PHP_EOL;
$data .= "Content-Type: text/plain; charset=UTF-8" . PHP_EOL;
$data .= "Content-Transfer-Encoding: 8bit";
$this->MailTransport->expects($this->once())->method('_mail')
->with(
'CakePHP <cake@cakephp.org>',
$encoded,
implode(PHP_EOL, array('First Line', 'Second Line', '.Third Line', '')),
$data,
'-f'
);
$this->MailTransport->send($email);
}
}

View file

@ -0,0 +1,453 @@
<?php
/**
* SmtpTransportTest file
*
* CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
* @package Cake.Test.Case.Network.Email
* @since CakePHP(tm) v 2.0.0
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
App::uses('CakeEmail', 'Network/Email');
App::uses('AbstractTransport', 'Network/Email');
App::uses('SmtpTransport', 'Network/Email');
/**
* Help to test SmtpTransport
*
*/
class SmtpTestTransport extends SmtpTransport {
/**
* Helper to change the socket
*
* @param object $socket
* @return void
*/
public function setSocket(CakeSocket $socket) {
$this->_socket = $socket;
}
/**
* Helper to change the CakeEmail
*
* @param object $cakeEmail
* @return void
*/
public function setCakeEmail($cakeEmail) {
$this->_cakeEmail = $cakeEmail;
}
/**
* Disabled the socket change
*
* @return void
*/
protected function _generateSocket() {
}
/**
* Magic function to call protected methods
*
* @param string $method
* @param string $args
* @return mixed
*/
public function __call($method, $args) {
$method = '_' . $method;
return call_user_func_array(array($this, $method), $args);
}
}
/**
* Test case
*
*/
class SmtpTransportTest extends CakeTestCase {
/**
* Setup
*
* @return void
*/
public function setUp() {
parent::setUp();
$this->socket = $this->getMock('CakeSocket', array('read', 'write', 'connect', 'enableCrypto'));
$this->SmtpTransport = new SmtpTestTransport();
$this->SmtpTransport->setSocket($this->socket);
$this->SmtpTransport->config(array('client' => 'localhost'));
}
/**
* testConnectEhlo method
*
* @return void
*/
public function testConnectEhlo() {
$this->socket->expects($this->any())->method('connect')->will($this->returnValue(true));
$this->socket->expects($this->at(0))->method('read')->will($this->returnValue(false));
$this->socket->expects($this->at(1))->method('read')->will($this->returnValue("220 Welcome message\r\n"));
$this->socket->expects($this->at(2))->method('write')->with("EHLO localhost\r\n");
$this->socket->expects($this->at(3))->method('read')->will($this->returnValue(false));
$this->socket->expects($this->at(4))->method('read')->will($this->returnValue("250 Accepted\r\n"));
$this->SmtpTransport->connect();
}
/**
* testConnectEhloTls method
*
* @return void
*/
public function testConnectEhloTls() {
$this->SmtpTransport->config(array('tls' => true));
$this->socket->expects($this->any())->method('connect')->will($this->returnValue(true));
$this->socket->expects($this->at(0))->method('read')->will($this->returnValue(false));
$this->socket->expects($this->at(1))->method('read')->will($this->returnValue("220 Welcome message\r\n"));
$this->socket->expects($this->at(2))->method('write')->with("EHLO localhost\r\n");
$this->socket->expects($this->at(3))->method('read')->will($this->returnValue(false));
$this->socket->expects($this->at(4))->method('read')->will($this->returnValue("250 Accepted\r\n"));
$this->socket->expects($this->at(5))->method('write')->with("STARTTLS\r\n");
$this->socket->expects($this->at(6))->method('read')->will($this->returnValue(false));
$this->socket->expects($this->at(7))->method('read')->will($this->returnValue("220 Server ready\r\n"));
$this->socket->expects($this->at(8))->method('enableCrypto')->with('tls')->will($this->returnValue(true));
$this->socket->expects($this->at(9))->method('write')->with("EHLO localhost\r\n");
$this->socket->expects($this->at(10))->method('read')->will($this->returnValue(false));
$this->socket->expects($this->at(11))->method('read')->will($this->returnValue("250 Accepted\r\n"));
$this->SmtpTransport->connect();
}
/**
* testConnectEhloTlsOnNonTlsServer method
*
* @expectedException SocketException
* @return void
*/
public function testConnectEhloTlsOnNonTlsServer() {
$this->SmtpTransport->config(array('tls' => true));
$this->socket->expects($this->any())->method('connect')->will($this->returnValue(true));
$this->socket->expects($this->at(0))->method('read')->will($this->returnValue(false));
$this->socket->expects($this->at(1))->method('read')->will($this->returnValue("220 Welcome message\r\n"));
$this->socket->expects($this->at(2))->method('write')->with("EHLO localhost\r\n");
$this->socket->expects($this->at(3))->method('read')->will($this->returnValue(false));
$this->socket->expects($this->at(4))->method('read')->will($this->returnValue("250 Accepted\r\n"));
$this->socket->expects($this->at(5))->method('write')->with("STARTTLS\r\n");
$this->socket->expects($this->at(6))->method('read')->will($this->returnValue(false));
$this->socket->expects($this->at(7))->method('read')->will($this->returnValue("500 5.3.3 Unrecognized command\r\n"));
$this->SmtpTransport->connect();
}
/**
* testConnectEhloNoTlsOnRequiredTlsServer method
*
* @expectedException SocketException
* @return void
*/
public function testConnectEhloNoTlsOnRequiredTlsServer() {
$this->SmtpTransport->config(array('tls' => false, 'username' => 'user', 'password' => 'pass'));
$this->socket->expects($this->any())->method('connect')->will($this->returnValue(true));
$this->socket->expects($this->at(0))->method('read')->will($this->returnValue(false));
$this->socket->expects($this->at(1))->method('read')->will($this->returnValue("220 Welcome message\r\n"));
$this->socket->expects($this->at(2))->method('write')->with("EHLO localhost\r\n");
$this->socket->expects($this->at(3))->method('read')->will($this->returnValue(false));
$this->socket->expects($this->at(4))->method('read')->will($this->returnValue("250 Accepted\r\n"));
$this->socket->expects($this->at(5))->method('write')->with("AUTH LOGIN\r\n");
$this->socket->expects($this->at(6))->method('read')->will($this->returnValue(false));
$this->socket->expects($this->at(7))->method('read')->will($this->returnValue("504 5.7.4 Unrecognized authentication type\r\n"));
$this->SmtpTransport->connect();
$this->SmtpTransport->auth();
}
/**
* testConnectHelo method
*
* @return void
*/
public function testConnectHelo() {
$this->socket->expects($this->any())->method('connect')->will($this->returnValue(true));
$this->socket->expects($this->at(0))->method('read')->will($this->returnValue(false));
$this->socket->expects($this->at(1))->method('read')->will($this->returnValue("220 Welcome message\r\n"));
$this->socket->expects($this->at(2))->method('write')->with("EHLO localhost\r\n");
$this->socket->expects($this->at(3))->method('read')->will($this->returnValue(false));
$this->socket->expects($this->at(4))->method('read')->will($this->returnValue("200 Not Accepted\r\n"));
$this->socket->expects($this->at(5))->method('write')->with("HELO localhost\r\n");
$this->socket->expects($this->at(6))->method('read')->will($this->returnValue(false));
$this->socket->expects($this->at(7))->method('read')->will($this->returnValue("250 Accepted\r\n"));
$this->SmtpTransport->connect();
}
/**
* testConnectFail method
*
* @expectedException SocketException
* @return void
*/
public function testConnectFail() {
$this->socket->expects($this->any())->method('connect')->will($this->returnValue(true));
$this->socket->expects($this->at(0))->method('read')->will($this->returnValue(false));
$this->socket->expects($this->at(1))->method('read')->will($this->returnValue("220 Welcome message\r\n"));
$this->socket->expects($this->at(2))->method('write')->with("EHLO localhost\r\n");
$this->socket->expects($this->at(3))->method('read')->will($this->returnValue(false));
$this->socket->expects($this->at(4))->method('read')->will($this->returnValue("200 Not Accepted\r\n"));
$this->socket->expects($this->at(5))->method('write')->with("HELO localhost\r\n");
$this->socket->expects($this->at(6))->method('read')->will($this->returnValue(false));
$this->socket->expects($this->at(7))->method('read')->will($this->returnValue("200 Not Accepted\r\n"));
$this->SmtpTransport->connect();
}
/**
* testAuth method
*
* @return void
*/
public function testAuth() {
$this->socket->expects($this->at(0))->method('write')->with("AUTH LOGIN\r\n");
$this->socket->expects($this->at(1))->method('read')->will($this->returnValue(false));
$this->socket->expects($this->at(2))->method('read')->will($this->returnValue("334 Login\r\n"));
$this->socket->expects($this->at(3))->method('write')->with("bWFyaw==\r\n");
$this->socket->expects($this->at(4))->method('read')->will($this->returnValue(false));
$this->socket->expects($this->at(5))->method('read')->will($this->returnValue("334 Pass\r\n"));
$this->socket->expects($this->at(6))->method('write')->with("c3Rvcnk=\r\n");
$this->socket->expects($this->at(7))->method('read')->will($this->returnValue(false));
$this->socket->expects($this->at(8))->method('read')->will($this->returnValue("235 OK\r\n"));
$this->SmtpTransport->config(array('username' => 'mark', 'password' => 'story'));
$this->SmtpTransport->auth();
}
/**
* testAuthNoAuth method
*
* @return void
*/
public function testAuthNoAuth() {
$this->socket->expects($this->any())->method('write')->with($this->logicalNot($this->stringContains('AUTH LOGIN')));
$this->SmtpTransport->config(array('username' => null, 'password' => null));
$this->SmtpTransport->auth();
}
/**
* testRcpt method
*
* @return void
*/
public function testRcpt() {
$email = new CakeEmail();
$email->from('noreply@cakephp.org', 'CakePHP Test');
$email->to('cake@cakephp.org', 'CakePHP');
$email->bcc('phpnut@cakephp.org');
$email->cc(array('mark@cakephp.org' => 'Mark Story', 'juan@cakephp.org' => 'Juan Basso'));
$this->socket->expects($this->at(0))->method('write')->with("MAIL FROM:<noreply@cakephp.org>\r\n");
$this->socket->expects($this->at(1))->method('read')->will($this->returnValue(false));
$this->socket->expects($this->at(2))->method('read')->will($this->returnValue("250 OK\r\n"));
$this->socket->expects($this->at(3))->method('write')->with("RCPT TO:<cake@cakephp.org>\r\n");
$this->socket->expects($this->at(4))->method('read')->will($this->returnValue(false));
$this->socket->expects($this->at(5))->method('read')->will($this->returnValue("250 OK\r\n"));
$this->socket->expects($this->at(6))->method('write')->with("RCPT TO:<mark@cakephp.org>\r\n");
$this->socket->expects($this->at(7))->method('read')->will($this->returnValue(false));
$this->socket->expects($this->at(8))->method('read')->will($this->returnValue("250 OK\r\n"));
$this->socket->expects($this->at(9))->method('write')->with("RCPT TO:<juan@cakephp.org>\r\n");
$this->socket->expects($this->at(10))->method('read')->will($this->returnValue(false));
$this->socket->expects($this->at(11))->method('read')->will($this->returnValue("250 OK\r\n"));
$this->socket->expects($this->at(12))->method('write')->with("RCPT TO:<phpnut@cakephp.org>\r\n");
$this->socket->expects($this->at(13))->method('read')->will($this->returnValue(false));
$this->socket->expects($this->at(14))->method('read')->will($this->returnValue("250 OK\r\n"));
$this->SmtpTransport->setCakeEmail($email);
$this->SmtpTransport->sendRcpt();
}
/**
* testRcptWithReturnPath method
*
* @return void
*/
public function testRcptWithReturnPath() {
$email = new CakeEmail();
$email->from('noreply@cakephp.org', 'CakePHP Test');
$email->to('cake@cakephp.org', 'CakePHP');
$email->returnPath('pleasereply@cakephp.org', 'CakePHP Return');
$this->socket->expects($this->at(0))->method('write')->with("MAIL FROM:<pleasereply@cakephp.org>\r\n");
$this->socket->expects($this->at(1))->method('read')->will($this->returnValue(false));
$this->socket->expects($this->at(2))->method('read')->will($this->returnValue("250 OK\r\n"));
$this->socket->expects($this->at(3))->method('write')->with("RCPT TO:<cake@cakephp.org>\r\n");
$this->socket->expects($this->at(4))->method('read')->will($this->returnValue(false));
$this->socket->expects($this->at(5))->method('read')->will($this->returnValue("250 OK\r\n"));
$this->SmtpTransport->setCakeEmail($email);
$this->SmtpTransport->sendRcpt();
}
/**
* testSendData method
*
* @return void
*/
public function testSendData() {
$email = $this->getMock('CakeEmail', array('message'), array(), 'SmtpCakeEmail');
$email->from('noreply@cakephp.org', 'CakePHP Test');
$email->returnPath('pleasereply@cakephp.org', 'CakePHP Return');
$email->to('cake@cakephp.org', 'CakePHP');
$email->cc(array('mark@cakephp.org' => 'Mark Story', 'juan@cakephp.org' => 'Juan Basso'));
$email->bcc('phpnut@cakephp.org');
$email->messageID('<4d9946cf-0a44-4907-88fe-1d0ccbdd56cb@localhost>');
$email->subject('Testing SMTP');
$date = date(DATE_RFC2822);
$email->setHeaders(array('X-Mailer' => SmtpCakeEmail::EMAIL_CLIENT, 'Date' => $date));
$email->expects($this->once())->method('message')->will($this->returnValue(array('First Line', 'Second Line', '.Third Line', '')));
$data = "From: CakePHP Test <noreply@cakephp.org>\r\n";
$data .= "To: CakePHP <cake@cakephp.org>\r\n";
$data .= "Cc: Mark Story <mark@cakephp.org>, Juan Basso <juan@cakephp.org>\r\n";
$data .= "X-Mailer: CakePHP Email\r\n";
$data .= "Date: " . $date . "\r\n";
$data .= "Message-ID: <4d9946cf-0a44-4907-88fe-1d0ccbdd56cb@localhost>\r\n";
$data .= "Subject: Testing SMTP\r\n";
$data .= "MIME-Version: 1.0\r\n";
$data .= "Content-Type: text/plain; charset=UTF-8\r\n";
$data .= "Content-Transfer-Encoding: 8bit\r\n";
$data .= "\r\n";
$data .= "First Line\r\n";
$data .= "Second Line\r\n";
$data .= "..Third Line\r\n"; // RFC5321 4.5.2.Transparency
$data .= "\r\n";
$data .= "\r\n\r\n.\r\n";
$this->socket->expects($this->at(0))->method('write')->with("DATA\r\n");
$this->socket->expects($this->at(1))->method('read')->will($this->returnValue(false));
$this->socket->expects($this->at(2))->method('read')->will($this->returnValue("354 OK\r\n"));
$this->socket->expects($this->at(3))->method('write')->with($data);
$this->socket->expects($this->at(4))->method('read')->will($this->returnValue(false));
$this->socket->expects($this->at(5))->method('read')->will($this->returnValue("250 OK\r\n"));
$this->SmtpTransport->setCakeEmail($email);
$this->SmtpTransport->sendData();
}
/**
* testQuit method
*
* @return void
*/
public function testQuit() {
$this->socket->expects($this->at(0))->method('write')->with("QUIT\r\n");
$this->SmtpTransport->disconnect();
}
/**
* testEmptyConfigArray method
*
* @return void
*/
public function testEmptyConfigArray() {
$expected = $this->SmtpTransport->config(array(
'client' => 'myhost.com',
'port' => 666
));
$this->assertEquals(666, $expected['port']);
$result = $this->SmtpTransport->config(array());
$this->assertEquals($expected, $result);
}
/**
* testGetLastResponse method
*
* @return void
*/
public function testGetLastResponse() {
$this->assertEmpty($this->SmtpTransport->getLastResponse());
$this->socket->expects($this->any())->method('connect')->will($this->returnValue(true));
$this->socket->expects($this->at(0))->method('read')->will($this->returnValue(false));
$this->socket->expects($this->at(1))->method('read')->will($this->returnValue("220 Welcome message\r\n"));
$this->socket->expects($this->at(2))->method('write')->with("EHLO localhost\r\n");
$this->socket->expects($this->at(3))->method('read')->will($this->returnValue(false));
$this->socket->expects($this->at(4))->method('read')->will($this->returnValue("250-PIPELINING\r\n"));
$this->socket->expects($this->at(5))->method('read')->will($this->returnValue("250-SIZE 102400000\r\n"));
$this->socket->expects($this->at(6))->method('read')->will($this->returnValue("250-VRFY\r\n"));
$this->socket->expects($this->at(7))->method('read')->will($this->returnValue("250-ETRN\r\n"));
$this->socket->expects($this->at(8))->method('read')->will($this->returnValue("250-STARTTLS\r\n"));
$this->socket->expects($this->at(9))->method('read')->will($this->returnValue("250-AUTH PLAIN LOGIN\r\n"));
$this->socket->expects($this->at(10))->method('read')->will($this->returnValue("250-AUTH=PLAIN LOGIN\r\n"));
$this->socket->expects($this->at(11))->method('read')->will($this->returnValue("250-ENHANCEDSTATUSCODES\r\n"));
$this->socket->expects($this->at(12))->method('read')->will($this->returnValue("250-8BITMIME\r\n"));
$this->socket->expects($this->at(13))->method('read')->will($this->returnValue("250 DSN\r\n"));
$this->SmtpTransport->connect();
$expected = array(
array('code' => '250', 'message' => 'PIPELINING'),
array('code' => '250', 'message' => 'SIZE 102400000'),
array('code' => '250', 'message' => 'VRFY'),
array('code' => '250', 'message' => 'ETRN'),
array('code' => '250', 'message' => 'STARTTLS'),
array('code' => '250', 'message' => 'AUTH PLAIN LOGIN'),
array('code' => '250', 'message' => 'AUTH=PLAIN LOGIN'),
array('code' => '250', 'message' => 'ENHANCEDSTATUSCODES'),
array('code' => '250', 'message' => '8BITMIME'),
array('code' => '250', 'message' => 'DSN')
);
$result = $this->SmtpTransport->getLastResponse();
$this->assertEquals($expected, $result);
$email = new CakeEmail();
$email->from('noreply@cakephp.org', 'CakePHP Test');
$email->to('cake@cakephp.org', 'CakePHP');
$this->socket->expects($this->at(0))->method('write')->with("MAIL FROM:<noreply@cakephp.org>\r\n");
$this->socket->expects($this->at(1))->method('read')->will($this->returnValue(false));
$this->socket->expects($this->at(2))->method('read')->will($this->returnValue("250 OK\r\n"));
$this->socket->expects($this->at(3))->method('write')->with("RCPT TO:<cake@cakephp.org>\r\n");
$this->socket->expects($this->at(4))->method('read')->will($this->returnValue(false));
$this->socket->expects($this->at(5))->method('read')->will($this->returnValue("250 OK\r\n"));
$this->SmtpTransport->setCakeEmail($email);
$this->SmtpTransport->sendRcpt();
$expected = array(
array('code' => '250', 'message' => 'OK'),
);
$result = $this->SmtpTransport->getLastResponse();
$this->assertEquals($expected, $result);
}
/**
* testBufferResponseLines method
*
* @return void
*/
public function testBufferResponseLines() {
$reponseLines = array(
'123',
"456\tFOO",
'FOOBAR',
'250-PIPELINING',
'250-ENHANCEDSTATUSCODES',
'250-8BITMIME',
'250 DSN',
);
$this->SmtpTransport->bufferResponseLines($reponseLines);
$expected = array(
array('code' => '123', 'message' => null),
array('code' => '250', 'message' => 'PIPELINING'),
array('code' => '250', 'message' => 'ENHANCEDSTATUSCODES'),
array('code' => '250', 'message' => '8BITMIME'),
array('code' => '250', 'message' => 'DSN')
);
$result = $this->SmtpTransport->getLastResponse();
$this->assertEquals($expected, $result);
}
}

View file

@ -0,0 +1,63 @@
<?php
/**
* BasicAuthenticationTest file
*
* CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
* @package Cake.Test.Case.Network.Http
* @since CakePHP(tm) v 2.0.0
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
App::uses('HttpSocket', 'Network/Http');
App::uses('BasicAuthentication', 'Network/Http');
/**
* BasicMethodTest class
*
* @package Cake.Test.Case.Network.Http
*/
class BasicAuthenticationTest extends CakeTestCase {
/**
* testAuthentication method
*
* @return void
*/
public function testAuthentication() {
$http = new HttpSocket();
$auth = array(
'method' => 'Basic',
'user' => 'mark',
'pass' => 'secret'
);
BasicAuthentication::authentication($http, $auth);
$this->assertEquals('Basic bWFyazpzZWNyZXQ=', $http->request['header']['Authorization']);
}
/**
* testProxyAuthentication method
*
* @return void
*/
public function testProxyAuthentication() {
$http = new HttpSocket();
$proxy = array(
'method' => 'Basic',
'user' => 'mark',
'pass' => 'secret'
);
BasicAuthentication::proxyAuthentication($http, $proxy);
$this->assertEquals('Basic bWFyazpzZWNyZXQ=', $http->request['header']['Proxy-Authorization']);
}
}

View file

@ -0,0 +1,201 @@
<?php
/**
* DigestAuthenticationTest file
*
* CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
* @package Cake.Test.Case.Network.Http
* @since CakePHP(tm) v 2.0.0
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
App::uses('HttpSocket', 'Network/Http');
App::uses('DigestAuthentication', 'Network/Http');
/**
* Class DigestHttpSocket
*
* @package Cake.Test.Case.Network.Http
*/
class DigestHttpSocket extends HttpSocket {
/**
* nextHeader attribute
*
* @var string
*/
public $nextHeader = '';
/**
* request method
*
* @param mixed $request
* @return void
*/
public function request($request = array()) {
if ($request === false) {
if (isset($this->response['header']['WWW-Authenticate'])) {
unset($this->response['header']['WWW-Authenticate']);
}
return;
}
$this->response['header']['WWW-Authenticate'] = $this->nextHeader;
}
}
/**
* DigestAuthenticationTest class
*
* @package Cake.Test.Case.Network.Http
*/
class DigestAuthenticationTest extends CakeTestCase {
/**
* Socket property
*
* @var mixed
*/
public $HttpSocket = null;
/**
* This function sets up a HttpSocket instance we are going to use for testing
*
* @return void
*/
public function setUp() {
parent::setUp();
$this->HttpSocket = new DigestHttpSocket();
$this->HttpSocket->request['method'] = 'GET';
$this->HttpSocket->request['uri']['path'] = '/';
}
/**
* We use this function to clean up after the test case was executed
*
* @return void
*/
public function tearDown() {
parent::tearDown();
unset($this->HttpSocket);
}
/**
* testBasic method
*
* @return void
*/
public function testBasic() {
$this->HttpSocket->nextHeader = 'Digest realm="The batcave",nonce="4cded326c6c51"';
$this->assertFalse(isset($this->HttpSocket->request['header']['Authorization']));
$auth = array('user' => 'admin', 'pass' => '1234');
DigestAuthentication::authentication($this->HttpSocket, $auth);
$this->assertTrue(isset($this->HttpSocket->request['header']['Authorization']));
$this->assertEquals('The batcave', $auth['realm']);
$this->assertEquals('4cded326c6c51', $auth['nonce']);
}
/**
* testQop method
*
* @return void
*/
public function testQop() {
$this->HttpSocket->nextHeader = 'Digest realm="The batcave",nonce="4cded326c6c51"';
$auth = array('user' => 'admin', 'pass' => '1234');
DigestAuthentication::authentication($this->HttpSocket, $auth);
$expected = 'Digest username="admin", realm="The batcave", nonce="4cded326c6c51", uri="/", response="da7e2a46b471d77f70a9bb3698c8902b"';
$this->assertEquals($expected, $this->HttpSocket->request['header']['Authorization']);
$this->assertFalse(isset($auth['qop']));
$this->assertFalse(isset($auth['nc']));
$this->HttpSocket->nextHeader = 'Digest realm="The batcave",nonce="4cded326c6c51",qop="auth"';
$auth = array('user' => 'admin', 'pass' => '1234');
DigestAuthentication::authentication($this->HttpSocket, $auth);
$expected = '@Digest username="admin", realm="The batcave", nonce="4cded326c6c51", uri="/", response="[a-z0-9]{32}", qop="auth", nc=00000001, cnonce="[a-z0-9]+"@';
$this->assertRegExp($expected, $this->HttpSocket->request['header']['Authorization']);
$this->assertEquals('auth', $auth['qop']);
$this->assertEquals(2, $auth['nc']);
}
/**
* testOpaque method
*
* @return void
*/
public function testOpaque() {
$this->HttpSocket->nextHeader = 'Digest realm="The batcave",nonce="4cded326c6c51"';
$auth = array('user' => 'admin', 'pass' => '1234');
DigestAuthentication::authentication($this->HttpSocket, $auth);
$this->assertFalse(strpos($this->HttpSocket->request['header']['Authorization'], 'opaque="d8ea7aa61a1693024c4cc3a516f49b3c"'));
$this->HttpSocket->nextHeader = 'Digest realm="The batcave",nonce="4cded326c6c51",opaque="d8ea7aa61a1693024c4cc3a516f49b3c"';
$auth = array('user' => 'admin', 'pass' => '1234');
DigestAuthentication::authentication($this->HttpSocket, $auth);
$this->assertTrue(strpos($this->HttpSocket->request['header']['Authorization'], 'opaque="d8ea7aa61a1693024c4cc3a516f49b3c"') > 0);
}
/**
* testMultipleRequest method
*
* @return void
*/
public function testMultipleRequest() {
$this->HttpSocket->nextHeader = 'Digest realm="The batcave",nonce="4cded326c6c51",qop="auth"';
$auth = array('user' => 'admin', 'pass' => '1234');
DigestAuthentication::authentication($this->HttpSocket, $auth);
$this->assertTrue(strpos($this->HttpSocket->request['header']['Authorization'], 'nc=00000001') > 0);
$this->assertEquals(2, $auth['nc']);
DigestAuthentication::authentication($this->HttpSocket, $auth);
$this->assertTrue(strpos($this->HttpSocket->request['header']['Authorization'], 'nc=00000002') > 0);
$this->assertEquals(3, $auth['nc']);
$responsePos = strpos($this->HttpSocket->request['header']['Authorization'], 'response=');
$response = substr($this->HttpSocket->request['header']['Authorization'], $responsePos + 10, 32);
$this->HttpSocket->nextHeader = '';
DigestAuthentication::authentication($this->HttpSocket, $auth);
$this->assertTrue(strpos($this->HttpSocket->request['header']['Authorization'], 'nc=00000003') > 0);
$this->assertEquals(4, $auth['nc']);
$responsePos = strpos($this->HttpSocket->request['header']['Authorization'], 'response=');
$responseB = substr($this->HttpSocket->request['header']['Authorization'], $responsePos + 10, 32);
$this->assertNotEquals($response, $responseB);
}
/**
* testPathChanged method
*
* @return void
*/
public function testPathChanged() {
$this->HttpSocket->nextHeader = 'Digest realm="The batcave",nonce="4cded326c6c51"';
$this->HttpSocket->request['uri']['path'] = '/admin';
$auth = array('user' => 'admin', 'pass' => '1234');
DigestAuthentication::authentication($this->HttpSocket, $auth);
$responsePos = strpos($this->HttpSocket->request['header']['Authorization'], 'response=');
$response = substr($this->HttpSocket->request['header']['Authorization'], $responsePos + 10, 32);
$this->assertNotEquals('da7e2a46b471d77f70a9bb3698c8902b', $response);
}
/**
* testNoDigestResponse method
*
* @return void
*/
public function testNoDigestResponse() {
$this->HttpSocket->nextHeader = false;
$this->HttpSocket->request['uri']['path'] = '/admin';
$auth = array('user' => 'admin', 'pass' => '1234');
DigestAuthentication::authentication($this->HttpSocket, $auth);
$this->assertFalse(isset($this->HttpSocket->request['header']['Authorization']));
}
}

View file

@ -0,0 +1,582 @@
<?php
/**
* HttpResponseTest file
*
* CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
* @package Cake.Test.Case.Network.Http
* @since CakePHP(tm) v 1.2.0.4206
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
App::uses('HttpResponse', 'Network/Http');
/**
* TestHttpResponse class
*
* @package Cake.Test.Case.Network.Http
*/
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
*
* @param string $body A string containing the body to decode
* @param bool|string $encoding Can be false in case no encoding is being used, or a string representing the encoding
* @return mixed Array or false
*/
public function decodeBody($body, $encoding = 'chunked') {
return parent::_decodeBody($body, $encoding);
}
/**
* Convenience method for testing protected method
*
* @param string $body A string containing the chunked body to decode
* @return mixed Array or false
*/
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 bool $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);
}
}
/**
* HttpResponseTest class
*
* @package Cake.Test.Case.Network.Http
*/
class HttpResponseTest extends CakeTestCase {
/**
* This function sets up a HttpResponse
*
* @return void
*/
public function setUp() {
parent::setUp();
$this->HttpResponse = new TestHttpResponse();
}
/**
* testBody
*
* @return void
*/
public function testBody() {
$this->HttpResponse->body = 'testing';
$this->assertEquals('testing', $this->HttpResponse->body());
$this->HttpResponse->body = null;
$this->assertSame($this->HttpResponse->body(), '');
}
/**
* testToString
*
* @return void
*/
public function testToString() {
$this->HttpResponse->body = 'other test';
$this->assertEquals('other test', $this->HttpResponse->body());
$this->assertEquals('other test', (string)$this->HttpResponse);
$this->assertTrue(strpos($this->HttpResponse, 'test') > 0);
$this->HttpResponse->body = null;
$this->assertEquals('', (string)$this->HttpResponse);
}
/**
* testGetHeader
*
* @return void
*/
public function testGetHeader() {
$this->HttpResponse->headers = array(
'foo' => 'Bar',
'Some' => 'ok',
'HeAdEr' => 'value',
'content-Type' => 'text/plain'
);
$this->assertEquals('Bar', $this->HttpResponse->getHeader('foo'));
$this->assertEquals('Bar', $this->HttpResponse->getHeader('Foo'));
$this->assertEquals('Bar', $this->HttpResponse->getHeader('FOO'));
$this->assertEquals('value', $this->HttpResponse->getHeader('header'));
$this->assertEquals('text/plain', $this->HttpResponse->getHeader('Content-Type'));
$this->assertNull($this->HttpResponse->getHeader(0));
$this->assertEquals('Bar', $this->HttpResponse->getHeader('foo', false));
$this->assertEquals('not from class', $this->HttpResponse->getHeader('foo', array('foo' => 'not from class')));
}
/**
* 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 = 'what?';
$this->assertFalse($this->HttpResponse->isOk());
$this->HttpResponse->code = 200;
$this->assertTrue($this->HttpResponse->isOk());
$this->HttpResponse->code = 201;
$this->assertTrue($this->HttpResponse->isOk());
$this->HttpResponse->code = 202;
$this->assertTrue($this->HttpResponse->isOk());
$this->HttpResponse->code = 203;
$this->assertTrue($this->HttpResponse->isOk());
$this->HttpResponse->code = 204;
$this->assertTrue($this->HttpResponse->isOk());
$this->HttpResponse->code = 205;
$this->assertTrue($this->HttpResponse->isOk());
$this->HttpResponse->code = 206;
$this->assertTrue($this->HttpResponse->isOk());
$this->HttpResponse->code = 207;
$this->assertFalse($this->HttpResponse->isOk());
$this->HttpResponse->code = 208;
$this->assertFalse($this->HttpResponse->isOk());
$this->HttpResponse->code = 209;
$this->assertFalse($this->HttpResponse->isOk());
$this->HttpResponse->code = 210;
$this->assertFalse($this->HttpResponse->isOk());
$this->HttpResponse->code = 226;
$this->assertFalse($this->HttpResponse->isOk());
$this->HttpResponse->code = 288;
$this->assertFalse($this->HttpResponse->isOk());
$this->HttpResponse->code = 301;
$this->assertFalse($this->HttpResponse->isOk());
}
/**
* testIsRedirect
*
* @return void
*/
public function testIsRedirect() {
$this->HttpResponse->code = 0;
$this->assertFalse($this->HttpResponse->isRedirect());
$this->HttpResponse->code = -1;
$this->assertFalse($this->HttpResponse->isRedirect());
$this->HttpResponse->code = 201;
$this->assertFalse($this->HttpResponse->isRedirect());
$this->HttpResponse->code = 'what?';
$this->assertFalse($this->HttpResponse->isRedirect());
$this->HttpResponse->code = 301;
$this->assertFalse($this->HttpResponse->isRedirect());
$this->HttpResponse->code = 302;
$this->assertFalse($this->HttpResponse->isRedirect());
$this->HttpResponse->code = 303;
$this->assertFalse($this->HttpResponse->isRedirect());
$this->HttpResponse->code = 307;
$this->assertFalse($this->HttpResponse->isRedirect());
$this->HttpResponse->code = 301;
$this->HttpResponse->headers['Location'] = 'http://somewhere/';
$this->assertTrue($this->HttpResponse->isRedirect());
$this->HttpResponse->code = 302;
$this->HttpResponse->headers['Location'] = 'http://somewhere/';
$this->assertTrue($this->HttpResponse->isRedirect());
$this->HttpResponse->code = 303;
$this->HttpResponse->headers['Location'] = 'http://somewhere/';
$this->assertTrue($this->HttpResponse->isRedirect());
$this->HttpResponse->code = 307;
$this->HttpResponse->headers['Location'] = 'http://somewhere/';
$this->assertTrue($this->HttpResponse->isRedirect());
}
/**
* 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(array('foo' => 'Bar', 'fOO-bAr' => 'quux'), $r);
$r = $this->HttpResponse->parseHeader(true);
$this->assertEquals(false, $r);
$header = "Host: cakephp.org\t\r\n";
$r = $this->HttpResponse->parseHeader($header);
$expected = array(
'Host' => 'cakephp.org'
);
$this->assertEquals($expected, $r);
$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($expected, $r);
$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($expected, $r);
$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($expected, $r);
$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($expected, $r);
$header = "Esc\"@\"ped: value\r\n";
$r = $this->HttpResponse->parseHeader($header);
$expected = array(
'Esc@ped' => 'value'
);
$this->assertEquals($expected, $r);
}
/**
* testParseResponse method
*
* @return void
*/
public function testParseResponse() {
$tests = array(
'simple-request' => array(
'response' => array(
'status-line' => "HTTP/1.x 200 OK\r\n",
'header' => "Date: Mon, 16 Apr 2007 04:14:16 GMT\r\nServer: CakeHttp Server\r\n",
'body' => "<h1>Hello World</h1>\r\n<p>It's good to be html</p>"
),
'expectations' => array(
'httpVersion' => 'HTTP/1.x',
'code' => 200,
'reasonPhrase' => 'OK',
'headers' => array('Date' => 'Mon, 16 Apr 2007 04:14:16 GMT', 'Server' => 'CakeHttp Server'),
'body' => "<h1>Hello World</h1>\r\n<p>It's good to be html</p>"
)
),
'no-header' => array(
'response' => array(
'status-line' => "HTTP/1.x 404 OK\r\n",
'header' => null
),
'expectations' => array(
'code' => 404,
'headers' => array()
)
)
);
$testResponse = array();
$expectations = array();
foreach ($tests as $name => $test) {
$testResponse = array_merge($testResponse, $test['response']);
$testResponse['response'] = $testResponse['status-line'] . $testResponse['header'] . "\r\n" . $testResponse['body'];
$this->HttpResponse->parseResponse($testResponse['response']);
$expectations = array_merge($expectations, $test['expectations']);
foreach ($expectations as $property => $expectedVal) {
$this->assertEquals($expectedVal, $this->HttpResponse->{$property}, 'Test "' . $name . '": response.' . $property . ' - %s');
}
foreach (array('status-line', 'header', 'body', 'response') as $field) {
$this->assertEquals($this->HttpResponse['raw'][$field], $testResponse[$field], 'Test response.raw.' . $field . ': %s');
}
}
}
/**
* data provider function for testInvalidParseResponseData
*
* @return array
*/
public static function invalidParseResponseDataProvider() {
return array(
array(array('foo' => 'bar')),
array(true),
array("HTTP Foo\r\nBar: La"),
array('HTTP/1.1 TEST ERROR')
);
}
/**
* testInvalidParseResponseData
*
* @dataProvider invalidParseResponseDataProvider
* @expectedException SocketException
* @return void
*/
public function testInvalidParseResponseData($value) {
$this->HttpResponse->parseResponse($value);
}
/**
* testDecodeBody method
*
* @return void
*/
public function testDecodeBody() {
$r = $this->HttpResponse->decodeBody(true);
$this->assertEquals(false, $r);
$r = $this->HttpResponse->decodeBody('Foobar', false);
$this->assertEquals(array('body' => 'Foobar', 'header' => false), $r);
$encoding = 'chunked';
$sample = array(
'encoded' => "19\r\nThis is a chunked message\r\n0\r\n",
'decoded' => array('body' => "This is a chunked message", 'header' => false)
);
$r = $this->HttpResponse->decodeBody($sample['encoded'], $encoding);
$this->assertEquals($r, $sample['decoded']);
$encoding = 'chunked';
$sample = array(
'encoded' => "19\nThis is a chunked message\r\n0\n",
'decoded' => array('body' => "This is a chunked message", 'header' => false)
);
$r = $this->HttpResponse->decodeBody($sample['encoded'], $encoding);
$this->assertEquals($r, $sample['decoded'], 'Inconsistent line terminators should be tolerated.');
}
/**
* testDecodeFooCoded
*
* @return void
*/
public function testDecodeFooCoded() {
$r = $this->HttpResponse->decodeBody(true);
$this->assertEquals(false, $r);
$r = $this->HttpResponse->decodeBody('Foobar', false);
$this->assertEquals(array('body' => 'Foobar', 'header' => false), $r);
$encoding = 'foo-bar';
$sample = array(
'encoded' => '!Foobar!',
'decoded' => array('body' => '!Foobar!', 'header' => false),
);
$r = $this->HttpResponse->decodeBody($sample['encoded'], $encoding);
$this->assertEquals($r, $sample['decoded']);
}
/**
* testDecodeChunkedBody method
*
* @return void
*/
public function testDecodeChunkedBody() {
$r = $this->HttpResponse->decodeChunkedBody(true);
$this->assertEquals(false, $r);
$encoded = "19\r\nThis is a chunked message\r\n0\r\n";
$decoded = "This is a chunked message";
$r = $this->HttpResponse->decodeChunkedBody($encoded);
$this->assertEquals($r['body'], $decoded);
$this->assertEquals(false, $r['header']);
$encoded = "19 \r\nThis is a chunked message\r\n0\r\n";
$r = $this->HttpResponse->decodeChunkedBody($encoded);
$this->assertEquals($r['body'], $decoded);
$encoded = "19\r\nThis is a chunked message\r\nE\r\n\nThat is cool\n\r\n0\r\n";
$decoded = "This is a chunked message\nThat is cool\n";
$r = $this->HttpResponse->decodeChunkedBody($encoded);
$this->assertEquals($r['body'], $decoded);
$this->assertEquals(false, $r['header']);
$encoded = "19\r\nThis is a chunked message\r\nE;foo-chunk=5\r\n\nThat is cool\n\r\n0\r\n";
$r = $this->HttpResponse->decodeChunkedBody($encoded);
$this->assertEquals($r['body'], $decoded);
$this->assertEquals(false, $r['header']);
$encoded = "19\r\nThis is a chunked message\r\nE\r\n\nThat is cool\n\r\n0\r\nfoo-header: bar\r\ncake: PHP\r\n\r\n";
$r = $this->HttpResponse->decodeChunkedBody($encoded);
$this->assertEquals($r['body'], $decoded);
$this->assertEquals(array('foo-header' => 'bar', 'cake' => 'PHP'), $r['header']);
}
/**
* testDecodeChunkedBodyError method
*
* @expectedException SocketException
* @return void
*/
public function testDecodeChunkedBodyError() {
$encoded = "19\r\nThis is a chunked message\r\nE\r\n\nThat is cool\n\r\n";
$this->HttpResponse->decodeChunkedBody($encoded);
}
/**
* testParseCookies method
*
* @return void
*/
public function testParseCookies() {
$header = array(
'Set-Cookie' => array(
'foo=bar',
'people=jim,jack,johnny";";Path=/accounts',
'google=not=nice'
),
'Transfer-Encoding' => 'chunked',
'Date' => 'Sun, 18 Nov 2007 18:57:42 GMT',
);
$cookies = $this->HttpResponse->parseCookies($header);
$expected = array(
'foo' => array(
'value' => 'bar'
),
'people' => array(
'value' => 'jim,jack,johnny";"',
'path' => '/accounts',
),
'google' => array(
'value' => 'not=nice',
)
);
$this->assertEquals($expected, $cookies);
$header['Set-Cookie'][] = 'cakephp=great; Secure';
$expected['cakephp'] = array('value' => 'great', 'secure' => true);
$cookies = $this->HttpResponse->parseCookies($header);
$this->assertEquals($expected, $cookies);
$header['Set-Cookie'] = 'foo=bar';
unset($expected['people'], $expected['cakephp'], $expected['google']);
$cookies = $this->HttpResponse->parseCookies($header);
$this->assertEquals($expected, $cookies);
}
/**
* Test that escaped token strings are properly unescaped by HttpSocket::unescapeToken
*
* @return void
*/
public function testUnescapeToken() {
$this->assertEquals('Foo', $this->HttpResponse->unescapeToken('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($expectedToken, $unescapedToken, '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
*
* @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!";
$expectedOne = "HTTP/1.1 200 OK\r\n";
$this->assertEquals($expectedOne, $this->HttpResponse['raw']['status-line']);
$expectedTwo = "Server: CakePHP\r\nContEnt-Type: text/plain\r\n";
$this->assertEquals($expectedTwo, $this->HttpResponse['raw']['header']);
$expectedThree = 'This is a test!';
$this->assertEquals($expectedThree, $this->HttpResponse['raw']['body']);
$expected = $expectedOne . $expectedTwo . "\r\n" . $expectedThree;
$this->assertEquals($expected, $this->HttpResponse['raw']['response']);
$expected = 'HTTP/1.1';
$this->assertEquals($expected, $this->HttpResponse['status']['http-version']);
$expected = 200;
$this->assertEquals($expected, $this->HttpResponse['status']['code']);
$expected = 'OK';
$this->assertEquals($expected, $this->HttpResponse['status']['reason-phrase']);
$expected = array(
'Server' => 'CakePHP',
'ContEnt-Type' => 'text/plain'
);
$this->assertEquals($expected, $this->HttpResponse['header']);
$expected = 'This is a test!';
$this->assertEquals($expected, $this->HttpResponse['body']);
$expected = array(
'foo' => array('value' => 'bar'),
'bar' => array('value' => 'foo')
);
$this->assertEquals($expected, $this->HttpResponse['cookies']);
$this->HttpResponse->raw = "HTTP/1.1 200 OK\r\n\r\nThis is a test!";
$this->assertNull($this->HttpResponse['raw']['header']);
}
}

File diff suppressed because it is too large Load diff