comparison cake/tests/cases/libs/cake_socket.test.php @ 0:261e66bd5a0c

hg init
author Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp>
date Sun, 24 Jul 2011 21:08:31 +0900
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:261e66bd5a0c
1 <?php
2 /**
3 * SocketTest file
4 *
5 * PHP versions 4 and 5
6 *
7 * CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
8 * Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
9 *
10 * Licensed under The Open Group Test Suite License
11 * Redistributions of files must retain the above copyright notice.
12 *
13 * @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
14 * @link http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests
15 * @package cake
16 * @subpackage cake.tests.cases.libs
17 * @since CakePHP(tm) v 1.2.0.4206
18 * @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
19 */
20 App::import('Core', 'CakeSocket');
21
22 /**
23 * SocketTest class
24 *
25 * @package cake
26 * @subpackage cake.tests.cases.libs
27 */
28 class CakeSocketTest extends CakeTestCase {
29
30 /**
31 * setUp method
32 *
33 * @access public
34 * @return void
35 */
36 function setUp() {
37 $this->Socket = new CakeSocket();
38 }
39
40 /**
41 * tearDown method
42 *
43 * @access public
44 * @return void
45 */
46 function tearDown() {
47 unset($this->Socket);
48 }
49
50 /**
51 * testConstruct method
52 *
53 * @access public
54 * @return void
55 */
56 function testConstruct() {
57 $this->Socket->__construct();
58 $baseConfig = $this->Socket->_baseConfig;
59 $this->assertIdentical($baseConfig, array(
60 'persistent' => false,
61 'host' => 'localhost',
62 'protocol' => 'tcp',
63 'port' => 80,
64 'timeout' => 30
65 ));
66
67 $this->Socket->reset();
68 $this->Socket->__construct(array('host' => 'foo-bar'));
69 $baseConfig['host'] = 'foo-bar';
70 $baseConfig['protocol'] = getprotobyname($baseConfig['protocol']);
71 $this->assertIdentical($this->Socket->config, $baseConfig);
72
73 $this->Socket = new CakeSocket(array('host' => 'www.cakephp.org', 'port' => 23, 'protocol' => 'udp'));
74 $baseConfig = $this->Socket->_baseConfig;
75
76 $baseConfig['host'] = 'www.cakephp.org';
77 $baseConfig['port'] = 23;
78 $baseConfig['protocol'] = 17;
79
80 $this->assertIdentical($this->Socket->config, $baseConfig);
81 }
82
83 /**
84 * testSocketConnection method
85 *
86 * @access public
87 * @return void
88 */
89 function testSocketConnection() {
90 $this->assertFalse($this->Socket->connected);
91 $this->Socket->disconnect();
92 $this->assertFalse($this->Socket->connected);
93 $this->Socket->connect();
94 $this->assertTrue($this->Socket->connected);
95 $this->Socket->connect();
96 $this->assertTrue($this->Socket->connected);
97
98 $this->Socket->disconnect();
99 $config = array('persistent' => true);
100 $this->Socket = new CakeSocket($config);
101 $this->Socket->connect();
102 $this->assertTrue($this->Socket->connected);
103 }
104
105 /**
106 * testSocketHost method
107 *
108 * @access public
109 * @return void
110 */
111 function testSocketHost() {
112 $this->Socket = new CakeSocket();
113 $this->Socket->connect();
114 $this->assertEqual($this->Socket->address(), '127.0.0.1');
115 $this->assertEqual(gethostbyaddr('127.0.0.1'), $this->Socket->host());
116 $this->assertEqual($this->Socket->lastError(), null);
117 $this->assertTrue(in_array('127.0.0.1', $this->Socket->addresses()));
118
119 $this->Socket = new CakeSocket(array('host' => '127.0.0.1'));
120 $this->Socket->connect();
121 $this->assertEqual($this->Socket->address(), '127.0.0.1');
122 $this->assertEqual(gethostbyaddr('127.0.0.1'), $this->Socket->host());
123 $this->assertEqual($this->Socket->lastError(), null);
124 $this->assertTrue(in_array('127.0.0.1', $this->Socket->addresses()));
125 }
126
127 /**
128 * testSocketWriting method
129 *
130 * @access public
131 * @return void
132 */
133 function testSocketWriting() {
134 $request = "GET / HTTP/1.1\r\nConnection: close\r\n\r\n";
135 $this->assertTrue($this->Socket->write($request));
136 }
137
138 /**
139 * testSocketReading method
140 *
141 * @access public
142 * @return void
143 */
144 function testSocketReading() {
145 $this->Socket = new CakeSocket(array('timeout' => 5));
146 $this->Socket->connect();
147 $this->assertEqual($this->Socket->read(26), null);
148
149 $config = array('host' => 'www.cakephp.org', 'timeout' => 1);
150 $this->Socket = new CakeSocket($config);
151 $this->assertTrue($this->Socket->connect());
152 $this->assertFalse($this->Socket->read(1024 * 1024));
153 $this->assertEqual($this->Socket->lastError(), '2: ' . __('Connection timed out', true));
154
155 $config = array('host' => 'www.cakephp.org', 'timeout' => 30);
156 $this->Socket = new CakeSocket($config);
157 $this->assertTrue($this->Socket->connect());
158 $this->assertEqual($this->Socket->read(26), null);
159 $this->assertEqual($this->Socket->lastError(), null);
160 }
161
162 /**
163 * testLastError method
164 *
165 * @access public
166 * @return void
167 */
168 function testLastError() {
169 $this->Socket = new CakeSocket();
170 $this->Socket->setLastError(4, 'some error here');
171 $this->assertEqual($this->Socket->lastError(), '4: some error here');
172 }
173
174 /**
175 * testReset method
176 *
177 * @access public
178 * @return void
179 */
180 function testReset() {
181 $config = array(
182 'persistent' => true,
183 'host' => '127.0.0.1',
184 'protocol' => 'udp',
185 'port' => 80,
186 'timeout' => 20
187 );
188 $anotherSocket = new CakeSocket($config);
189 $anotherSocket->reset();
190 $this->assertEqual(array(), $anotherSocket->config);
191 }
192 }