/
StreamTest.php
434 lines (387 loc) · 12.6 KB
/
StreamTest.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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
<?php
/**
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
* @link https://cakephp.org CakePHP(tm) Project
* @since 3.0.0
* @license https://opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Test\TestCase\Http\Client\Adapter;
use Cake\Http\Client\Adapter\Stream;
use Cake\Http\Client\Request;
use Cake\TestSuite\TestCase;
/**
* CakeStreamWrapper class
*/
class CakeStreamWrapper implements \ArrayAccess
{
private $_stream;
private $_query = [];
private $_data = [
'headers' => [
'HTTP/1.1 200 OK',
],
];
public function stream_open($path, $mode, $options, &$openedPath)
{
$query = parse_url($path, PHP_URL_QUERY);
if ($query) {
parse_str($query, $this->_query);
}
$this->_stream = fopen('php://memory', 'rb+');
fwrite($this->_stream, str_repeat('x', 20000));
rewind($this->_stream);
return true;
}
public function stream_close()
{
return fclose($this->_stream);
}
public function stream_read($count)
{
if (isset($this->_query['sleep'])) {
sleep(1);
}
return fread($this->_stream, $count);
}
public function stream_eof()
{
return feof($this->_stream);
}
public function stream_set_option($option, $arg1, $arg2)
{
return false;
}
public function offsetExists($offset)
{
return isset($this->_data[$offset]);
}
public function offsetGet($offset)
{
return $this->_data[$offset];
}
public function offsetSet($offset, $value)
{
$this->_data[$offset] = $value;
}
public function offsetUnset($offset)
{
unset($this->_data[$offset]);
}
}
/**
* HTTP stream adapter test.
*/
class StreamTest extends TestCase
{
public function setUp()
{
parent::setUp();
$this->stream = $this->getMockBuilder('Cake\Http\Client\Adapter\Stream')
->setMethods(['_send'])
->getMock();
stream_wrapper_unregister('http');
stream_wrapper_register('http', __NAMESPACE__ . '\CakeStreamWrapper');
}
public function tearDown()
{
parent::tearDown();
stream_wrapper_restore('http');
}
/**
* Test the send method
*
* @return void
*/
public function testSend()
{
$stream = new Stream();
$request = new Request('http://localhost', 'GET', [
'User-Agent' => 'CakePHP TestSuite',
'Cookie' => 'testing=value'
]);
try {
$responses = $stream->send($request, []);
} catch (\Cake\Core\Exception\Exception $e) {
$this->markTestSkipped('Could not connect to localhost, skipping');
}
$this->assertInstanceOf('Cake\Http\Client\Response', $responses[0]);
}
/**
* Test the send method by using cakephp:// protocol.
*
* @return void
*/
public function testSendByUsingCakephpProtocol()
{
$stream = new Stream();
$request = new Request('http://dummy/');
$responses = $stream->send($request, []);
$this->assertInstanceOf('Cake\Http\Client\Response', $responses[0]);
$this->assertEquals(20000, strlen($responses[0]->body()));
}
/**
* Test building the context headers
*
* @return void
*/
public function testBuildingContextHeader()
{
$request = new Request(
'http://localhost',
'GET',
[
'User-Agent' => 'CakePHP TestSuite',
'Content-Type' => 'application/json',
'Cookie' => 'a=b; c=do%20it'
]
);
$options = [
'redirect' => 20,
];
$this->stream->send($request, $options);
$result = $this->stream->contextOptions();
$expected = [
'User-Agent: CakePHP TestSuite',
'Content-Type: application/json',
'Cookie: a=b; c=do%20it',
'Connection: close',
];
$this->assertEquals(implode("\r\n", $expected), $result['header']);
$this->assertSame(0, $result['max_redirects']);
$this->assertTrue($result['ignore_errors']);
}
/**
* Test send() + context options with string content.
*
* @return void
*/
public function testSendContextContentString()
{
$content = json_encode(['a' => 'b']);
$request = new Request(
'http://localhost',
'GET',
['Content-Type' => 'application/json'],
$content
);
$options = [
'redirect' => 20
];
$this->stream->send($request, $options);
$result = $this->stream->contextOptions();
$expected = [
'Content-Type: application/json',
'Connection: close',
'User-Agent: CakePHP',
];
$this->assertEquals(implode("\r\n", $expected), $result['header']);
$this->assertEquals($content, $result['content']);
}
/**
* Test send() + context options with array content.
*
* @return void
*/
public function testSendContextContentArray()
{
$request = new Request(
'http://localhost',
'GET',
[
'Content-Type' => 'application/json'
],
['a' => 'my value']
);
$this->stream->send($request, []);
$result = $this->stream->contextOptions();
$expected = [
'Content-Type: application/x-www-form-urlencoded',
'Connection: close',
'User-Agent: CakePHP',
];
$this->assertStringStartsWith(implode("\r\n", $expected), $result['header']);
$this->assertContains('a=my+value', $result['content']);
$this->assertContains('my+value', $result['content']);
}
/**
* Test send() + context options with array content.
*
* @return void
*/
public function testSendContextContentArrayFiles()
{
$request = new Request(
'http://localhost',
'GET',
['Content-Type' => 'application/json'],
['upload' => fopen(CORE_PATH . 'VERSION.txt', 'r')]
);
$this->stream->send($request, []);
$result = $this->stream->contextOptions();
$this->assertContains("Content-Type: multipart/form-data", $result['header']);
$this->assertContains("Connection: close\r\n", $result['header']);
$this->assertContains("User-Agent: CakePHP", $result['header']);
$this->assertContains('name="upload"', $result['content']);
$this->assertContains('filename="VERSION.txt"', $result['content']);
}
/**
* Test send() + context options for SSL.
*
* @return void
*/
public function testSendContextSsl()
{
$request = new Request('https://localhost.com/test.html');
$options = [
'ssl_verify_host' => true,
'ssl_verify_peer' => true,
'ssl_verify_peer_name' => true,
'ssl_verify_depth' => 9000,
'ssl_allow_self_signed' => false,
'proxy' => [
'proxy' => '127.0.0.1:8080'
]
];
$this->stream->send($request, $options);
$result = $this->stream->contextOptions();
$expected = [
'peer_name' => 'localhost.com',
'verify_peer' => true,
'verify_peer_name' => true,
'verify_depth' => 9000,
'allow_self_signed' => false,
'proxy' => '127.0.0.1:8080',
];
foreach ($expected as $k => $v) {
$this->assertEquals($v, $result[$k]);
}
$this->assertIsReadable($result['cafile']);
}
/**
* Test send() + context options for SSL.
*
* @return void
*/
public function testSendContextSslNoVerifyPeerName()
{
$request = new Request('https://localhost.com/test.html');
$options = [
'ssl_verify_host' => true,
'ssl_verify_peer' => true,
'ssl_verify_peer_name' => false,
'ssl_verify_depth' => 9000,
'ssl_allow_self_signed' => false,
'proxy' => [
'proxy' => '127.0.0.1:8080'
]
];
$this->stream->send($request, $options);
$result = $this->stream->contextOptions();
$expected = [
'peer_name' => 'localhost.com',
'verify_peer' => true,
'verify_peer_name' => false,
'verify_depth' => 9000,
'allow_self_signed' => false,
'proxy' => '127.0.0.1:8080',
];
foreach ($expected as $k => $v) {
$this->assertEquals($v, $result[$k]);
}
$this->assertIsReadable($result['cafile']);
}
/**
* The PHP stream API returns ALL the headers for ALL the requests when
* there are redirects.
*
* @return void
*/
public function testCreateResponseWithRedirects()
{
$headers = [
'HTTP/1.1 302 Found',
'Date: Mon, 31 Dec 2012 16:53:16 GMT',
'Server: Apache/2.2.22 (Unix) DAV/2 PHP/5.4.9 mod_ssl/2.2.22 OpenSSL/0.9.8r',
'X-Powered-By: PHP/5.4.9',
'Location: http://localhost/cake3/tasks/second',
'Content-Length: 0',
'Connection: close',
'Content-Type: text/html; charset=UTF-8',
'Set-Cookie: first=value',
'HTTP/1.1 302 Found',
'Date: Mon, 31 Dec 2012 16:53:16 GMT',
'Server: Apache/2.2.22 (Unix) DAV/2 PHP/5.4.9 mod_ssl/2.2.22 OpenSSL/0.9.8r',
'X-Powered-By: PHP/5.4.9',
'Location: http://localhost/cake3/tasks/third',
'Content-Length: 0',
'Connection: close',
'Content-Type: text/html; charset=UTF-8',
'Set-Cookie: second=val',
'HTTP/1.1 200 OK',
'Date: Mon, 31 Dec 2012 16:53:16 GMT',
'Server: Apache/2.2.22 (Unix) DAV/2 PHP/5.4.9 mod_ssl/2.2.22 OpenSSL/0.9.8r',
'X-Powered-By: PHP/5.4.9',
'Content-Length: 22',
'Connection: close',
'Content-Type: text/html; charset=UTF-8',
'Set-Cookie: third=works',
];
$content = 'This is the third page';
$responses = $this->stream->createResponses($headers, $content);
$this->assertCount(3, $responses);
$this->assertEquals('close', $responses[0]->getHeaderLine('Connection'));
$this->assertEquals('', (string)$responses[0]->getBody());
$this->assertEquals('', (string)$responses[1]->getBody());
$this->assertEquals($content, (string)$responses[2]->getBody());
$this->assertEquals(302, $responses[0]->getStatusCode());
$this->assertEquals(302, $responses[1]->getStatusCode());
$this->assertEquals(200, $responses[2]->getStatusCode());
$this->assertEquals('value', $responses[0]->getCookie('first'));
$this->assertNull($responses[0]->getCookie('second'));
$this->assertNull($responses[0]->getCookie('third'));
$this->assertNull($responses[1]->getCookie('first'));
$this->assertEquals('val', $responses[1]->getCookie('second'));
$this->assertNull($responses[1]->getCookie('third'));
$this->assertNull($responses[2]->getCookie('first'));
$this->assertNull($responses[2]->getCookie('second'));
$this->assertEquals('works', $responses[2]->getCookie('third'));
}
/**
* Test that no exception is radied when not timed out.
*
* @return void
*/
public function testKeepDeadline()
{
$request = new Request('http://dummy/?sleep');
$options = [
'timeout' => 5,
];
$t = microtime(true);
$stream = new Stream();
$stream->send($request, $options);
$this->assertLessThan(5, microtime(true) - $t);
}
/**
* Test that an exception is raised when timed out.
*
* @return void
*/
public function testMissDeadline()
{
$this->expectException(\Cake\Http\Exception\HttpException::class);
$this->expectExceptionMessage('Connection timed out http://dummy/?sleep');
$request = new Request('http://dummy/?sleep');
$options = [
'timeout' => 2,
];
$stream = new Stream();
$stream->send($request, $options);
}
}