forked from zendframework/zendframework
/
SocketTest.php
282 lines (242 loc) · 8.33 KB
/
SocketTest.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @package Zend_Http
*/
namespace ZendTest\Http\Client;
use Zend\Http\Client\Adapter;
use Zend\Uri\Uri;
/**
* This Testsuite includes all Zend_Http_Client that require a working web
* server to perform. It was designed to be extendable, so that several
* test suites could be run against several servers, with different client
* adapters and configurations.
*
* Note that $this->baseuri must point to a directory on a web server
* containing all the files under the files directory. You should symlink
* or copy these files and set 'baseuri' properly.
*
* You can also set the proper constant in your test configuration file to
* point to the right place.
*
* @category Zend
* @package Zend_Http_Client
* @subpackage UnitTests
* @group Zend_Http
* @group Zend_Http_Client
*/
class SocketTest extends CommonHttpTests
{
/**
* Configuration array
*
* @var array
*/
protected $config = array(
'adapter' => 'Zend\Http\Client\Adapter\Socket'
);
/**
* Off-line common adapter tests
*/
/**
* Test that we can set a valid configuration array with some options
* @group ZHC001
*/
public function testConfigSetAsArray()
{
$config = array(
'timeout' => 500,
'someoption' => 'hasvalue'
);
$this->_adapter->setOptions($config);
$hasConfig = $this->_adapter->getConfig();
foreach ($config as $k => $v) {
$this->assertEquals($v, $hasConfig[$k]);
}
}
public function testDefaultConfig()
{
$config = $this->_adapter->getConfig();
$this->assertEquals(TRUE, $config['sslverifypeer']);
$this->assertEquals(FALSE, $config['sslallowselfsigned']);
}
public function testConnectingViaSslEnforcesDefaultSslOptionsOnContext()
{
$config = array('timeout' => 30);
$this->_adapter->setOptions($config);
try {
$this->_adapter->connect('localhost', 443, true);
} catch (\Zend\Http\Client\Adapter\Exception\RuntimeException $e) {
// Test is designed to allow connect failure because we're interested
// only in the stream context state created within that method.
}
$context = $this->_adapter->getStreamContext();
$options = stream_context_get_options($context);
$this->assertTrue($options['ssl']['verify_peer']);
$this->assertFalse($options['ssl']['allow_self_signed']);
}
/**
* Test that a Zend_Config object can be used to set configuration
*
* @link http://framework.zend.com/issues/browse/ZF-5577
*/
public function testConfigSetAsZendConfig()
{
$config = new \Zend\Config\Config(array(
'timeout' => 400,
'nested' => array(
'item' => 'value',
)
));
$this->_adapter->setOptions($config);
$hasConfig = $this->_adapter->getConfig();
$this->assertEquals($config->timeout, $hasConfig['timeout']);
$this->assertEquals($config->nested->item, $hasConfig['nested']['item']);
}
/**
* Check that an exception is thrown when trying to set invalid config
*
* @dataProvider invalidConfigProvider
*/
public function testSetConfigInvalidConfig($config)
{
$this->setExpectedException(
'Zend\Http\Client\Adapter\Exception\InvalidArgumentException',
'Array or Zend_Config object expected');
$this->_adapter->setOptions($config);
}
/**
* Stream context related tests
*/
public function testGetNewStreamContext()
{
$adapterClass = $this->config['adapter'];
$adapter = new $adapterClass;
$context = $adapter->getStreamContext();
$this->assertEquals('stream-context', get_resource_type($context));
}
public function testSetNewStreamContextResource()
{
$adapterClass = $this->config['adapter'];
$adapter = new $adapterClass;
$context = stream_context_create();
$adapter->setStreamContext($context);
$this->assertEquals($context, $adapter->getStreamContext());
}
public function testSetNewStreamContextOptions()
{
$adapterClass = $this->config['adapter'];
$adapter = new $adapterClass;
$options = array(
'socket' => array(
'bindto' => '1.2.3.4:0'
),
'ssl' => array(
'capath' => null,
'verify_peer' => true,
'allow_self_signed' => false
)
);
$adapter->setStreamContext($options);
$this->assertEquals($options, stream_context_get_options($adapter->getStreamContext()));
}
/**
* Test that setting invalid options / context causes an exception
*
* @dataProvider invalidContextProvider
*/
public function testSetInvalidContextOptions($invalid)
{
$this->setExpectedException(
'Zend\Http\Client\Adapter\Exception\InvalidArgumentException',
'Expecting either a stream context resource or array');
$adapterClass = $this->config['adapter'];
$adapter = new $adapterClass;
$adapter->setStreamContext($invalid);
}
public function testSetHttpsStreamContextParam()
{
if ($this->client->getUri()->getScheme() != 'https') {
$this->markTestSkipped();
}
$adapterClass = $this->config['adapter'];
$adapter = new $adapterClass;
$adapter->setStreamContext(array(
'ssl' => array(
'capture_peer_cert' => true,
'capture_peer_chain' => true
)
));
$this->client->setAdapter($adapter);
$this->client->setUri($this->baseuri . '/testSimpleRequests.php');
$this->client->request();
$opts = stream_context_get_options($adapter->getStreamContext());
$this->assertTrue(isset($opts['ssl']['peer_certificate']));
}
/**
* Test that we get the right exception after a socket timeout
*
* @link http://framework.zend.com/issues/browse/ZF-7309
*/
public function testExceptionOnReadTimeout()
{
// Set 1 second timeout
$this->client->setOptions(array('timeout' => 1));
$start = microtime(true);
try {
$this->client->send();
$this->fail('Expected a timeout Zend\Http\Client\Adapter\Exception\TimeoutException');
} catch (Adapter\Exception\TimeoutException $e) {
$this->assertEquals(Adapter\Exception\TimeoutException::READ_TIMEOUT, $e->getCode());
}
$time = (microtime(true) - $start);
// We should be very close to 1 second
$this->assertLessThan(2, $time);
}
/**
* Test that a chunked response with multibyte characters is properly read
*
* This can fail in various PHP environments - for example, when mbstring
* overloads substr() and strlen(), and mbstring's internal encoding is
* not a single-byte encoding.
*
* @link http://framework.zend.com/issues/browse/ZF-6218
*/
public function testMultibyteChunkedResponseZF6218()
{
$md5 = '7667818873302f9995be3798d503d8d3';
$response = $this->client->send();
$this->assertEquals($md5, md5($response->getBody()));
}
/**
* Verifies that writing on a socket is considered valid even if 0 bytes
* were written.
*/
public function testAllowsZeroWrittenBytes()
{
$this->_adapter->connect('localhost');
require_once __DIR__ . '/_files/fwrite.php';
$this->_adapter->write('GET', new Uri('tcp://localhost:80/'), '1.1', array(), 'test body');
}
/**
* Data Providers
*/
/**
* Provide invalid context resources / options
*
* @return array
*/
public static function invalidContextProvider()
{
return array(
array(new \stdClass()),
array(fopen('data://text/plain,', 'r')),
array(false),
array(null)
);
}
}