/
CakeSocketTest.php
217 lines (195 loc) · 5.59 KB
/
CakeSocketTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
<?php
/**
* SocketTest file
*
* PHP 5
*
* CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
* Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice
*
* @copyright Copyright 2005-2012, 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 MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
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);
$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);
}
/**
* 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() {
$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()));
}
/**
* testSocketWriting method
*
* @return void
*/
public function testSocketWriting() {
$request = "GET / HTTP/1.1\r\nConnection: close\r\n\r\n";
$this->assertTrue((bool)$this->Socket->write($request));
}
/**
* testSocketReading method
*
* @return void
*/
public function testSocketReading() {
$this->Socket = new CakeSocket(array('timeout' => 5));
$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());
}
/**
* testTimeOutConnection method
*
* @return void
*/
public function testTimeOutConnection() {
$config = array('host' => '127.0.0.1', 'timeout' => 0.5);
$this->Socket = new CakeSocket($config);
$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());
}
/**
* 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);
}
}