-
-
Notifications
You must be signed in to change notification settings - Fork 151
/
Copy pathBrowserTest.php
545 lines (439 loc) · 19.8 KB
/
BrowserTest.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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
<?php
namespace React\Tests\Http;
use Psr\Http\Message\RequestInterface;
use React\EventLoop\LoopInterface;
use React\Http\Io\Transaction;
use React\Http\Browser;
use React\Promise\Promise;
use React\Socket\ConnectorInterface;
class BrowserTest extends TestCase
{
private $loop;
private $sender;
private $browser;
/**
* @before
*/
public function setUpBrowser()
{
$this->loop = $this->createMock(LoopInterface::class);
$this->sender = $this->createMock(Transaction::class);
$this->browser = new Browser(null, $this->loop);
$ref = new \ReflectionProperty($this->browser, 'transaction');
$ref->setAccessible(true);
$ref->setValue($this->browser, $this->sender);
}
public function testConstructWithoutLoopAssignsLoopAutomatically()
{
$browser = new Browser();
$ref = new \ReflectionProperty($browser, 'transaction');
$ref->setAccessible(true);
$transaction = $ref->getValue($browser);
$ref = new \ReflectionProperty($transaction, 'loop');
$ref->setAccessible(true);
$loop = $ref->getValue($transaction);
$this->assertInstanceOf(LoopInterface::class, $loop);
}
public function testConstructWithConnectorAssignsGivenConnector()
{
$connector = $this->createMock(ConnectorInterface::class);
$browser = new Browser($connector);
$ref = new \ReflectionProperty($browser, 'transaction');
$ref->setAccessible(true);
$transaction = $ref->getValue($browser);
$ref = new \ReflectionProperty($transaction, 'sender');
$ref->setAccessible(true);
$sender = $ref->getValue($transaction);
$ref = new \ReflectionProperty($sender, 'http');
$ref->setAccessible(true);
$client = $ref->getValue($sender);
$ref = new \ReflectionProperty($client, 'connectionManager');
$ref->setAccessible(true);
$connectionManager = $ref->getValue($client);
$ref = new \ReflectionProperty($connectionManager, 'connector');
$ref->setAccessible(true);
$ret = $ref->getValue($connectionManager);
$this->assertSame($connector, $ret);
}
public function testConstructWithLoopAssignsGivenLoop()
{
$browser = new Browser(null, $this->loop);
$ref = new \ReflectionProperty($browser, 'transaction');
$ref->setAccessible(true);
$transaction = $ref->getValue($browser);
$ref = new \ReflectionProperty($transaction, 'loop');
$ref->setAccessible(true);
$loop = $ref->getValue($transaction);
$this->assertSame($this->loop, $loop);
}
public function testGetSendsGetRequest()
{
$this->sender->expects($this->once())->method('send')->with($this->callback(function (RequestInterface $request) {
$this->assertEquals('GET', $request->getMethod());
return true;
}))->willReturn(new Promise(function () { }));
$this->browser->get('http://example.com/');
}
public function testPostSendsPostRequest()
{
$this->sender->expects($this->once())->method('send')->with($this->callback(function (RequestInterface $request) {
$this->assertEquals('POST', $request->getMethod());
return true;
}))->willReturn(new Promise(function () { }));
$this->browser->post('http://example.com/');
}
public function testHeadSendsHeadRequest()
{
$this->sender->expects($this->once())->method('send')->with($this->callback(function (RequestInterface $request) {
$this->assertEquals('HEAD', $request->getMethod());
return true;
}))->willReturn(new Promise(function () { }));
$this->browser->head('http://example.com/');
}
public function testPatchSendsPatchRequest()
{
$this->sender->expects($this->once())->method('send')->with($this->callback(function (RequestInterface $request) {
$this->assertEquals('PATCH', $request->getMethod());
return true;
}))->willReturn(new Promise(function () { }));
$this->browser->patch('http://example.com/');
}
public function testPutSendsPutRequest()
{
$this->sender->expects($this->once())->method('send')->with($this->callback(function (RequestInterface $request) {
$this->assertEquals('PUT', $request->getMethod());
return true;
}))->willReturn(new Promise(function () { }));
$this->browser->put('http://example.com/');
}
public function testDeleteSendsDeleteRequest()
{
$this->sender->expects($this->once())->method('send')->with($this->callback(function (RequestInterface $request) {
$this->assertEquals('DELETE', $request->getMethod());
return true;
}))->willReturn(new Promise(function () { }));
$this->browser->delete('http://example.com/');
}
public function testRequestOptionsSendsPutRequestWithStreamingExplicitlyDisabled()
{
$this->sender->expects($this->once())->method('withOptions')->with(['streaming' => false])->willReturnSelf();
$this->sender->expects($this->once())->method('send')->with($this->callback(function (RequestInterface $request) {
$this->assertEquals('OPTIONS', $request->getMethod());
return true;
}))->willReturn(new Promise(function () { }));
$this->browser->request('OPTIONS', 'http://example.com/');
}
public function testRequestStreamingGetSendsGetRequestWithStreamingExplicitlyEnabled()
{
$this->sender->expects($this->once())->method('withOptions')->with(['streaming' => true])->willReturnSelf();
$this->sender->expects($this->once())->method('send')->with($this->callback(function (RequestInterface $request) {
$this->assertEquals('GET', $request->getMethod());
return true;
}))->willReturn(new Promise(function () { }));
$this->browser->requestStreaming('GET', 'http://example.com/');
}
public function testWithTimeoutTrueSetsDefaultTimeoutOption()
{
$this->sender->expects($this->once())->method('withOptions')->with(['timeout' => null])->willReturnSelf();
$this->browser->withTimeout(true);
}
public function testWithTimeoutFalseSetsNegativeTimeoutOption()
{
$this->sender->expects($this->once())->method('withOptions')->with(['timeout' => -1])->willReturnSelf();
$this->browser->withTimeout(false);
}
public function testWithTimeout10SetsTimeoutOption()
{
$this->sender->expects($this->once())->method('withOptions')->with(['timeout' => 10])->willReturnSelf();
$this->browser->withTimeout(10);
}
public function testWithTimeoutNegativeSetsZeroTimeoutOption()
{
$this->sender->expects($this->once())->method('withOptions')->with(['timeout' => null])->willReturnSelf();
$this->browser->withTimeout(-10);
}
public function testWithFollowRedirectsTrueSetsSenderOption()
{
$this->sender->expects($this->once())->method('withOptions')->with(['followRedirects' => true, 'maxRedirects' => null])->willReturnSelf();
$this->browser->withFollowRedirects(true);
}
public function testWithFollowRedirectsFalseSetsSenderOption()
{
$this->sender->expects($this->once())->method('withOptions')->with(['followRedirects' => false, 'maxRedirects' => null])->willReturnSelf();
$this->browser->withFollowRedirects(false);
}
public function testWithFollowRedirectsTenSetsSenderOption()
{
$this->sender->expects($this->once())->method('withOptions')->with(['followRedirects' => true, 'maxRedirects' => 10])->willReturnSelf();
$this->browser->withFollowRedirects(10);
}
public function testWithFollowRedirectsZeroSetsSenderOption()
{
$this->sender->expects($this->once())->method('withOptions')->with(['followRedirects' => true, 'maxRedirects' => 0])->willReturnSelf();
$this->browser->withFollowRedirects(0);
}
public function testWithRejectErrorResponseTrueSetsSenderOption()
{
$this->sender->expects($this->once())->method('withOptions')->with(['obeySuccessCode' => true])->willReturnSelf();
$this->browser->withRejectErrorResponse(true);
}
public function testWithRejectErrorResponseFalseSetsSenderOption()
{
$this->sender->expects($this->once())->method('withOptions')->with(['obeySuccessCode' => false])->willReturnSelf();
$this->browser->withRejectErrorResponse(false);
}
public function testWithResponseBufferThousandSetsSenderOption()
{
$this->sender->expects($this->once())->method('withOptions')->with(['maximumSize' => 1000])->willReturnSelf();
$this->browser->withResponseBuffer(1000);
}
public function testWithBase()
{
$browser = $this->browser->withBase('http://example.com/root');
$this->assertInstanceOf(Browser::class, $browser);
$this->assertNotSame($this->browser, $browser);
}
public static function provideOtherUris()
{
yield 'empty returns base' => [
'http://example.com/base',
'',
'http://example.com/base',
];
yield 'absolute same as base returns base' => [
'http://example.com/base',
'http://example.com/base',
'http://example.com/base',
];
yield 'absolute below base returns absolute' => [
'http://example.com/base',
'http://example.com/base/another',
'http://example.com/base/another',
];
yield 'slash returns base without path' => [
'http://example.com/base',
'/',
'http://example.com/',
];
yield 'relative is added behind base' => [
'http://example.com/base/',
'test',
'http://example.com/base/test',
];
yield 'relative is added behind base without path' => [
'http://example.com/base',
'test',
'http://example.com/test',
];
yield 'relative level up is added behind parent path' => [
'http://example.com/base/foo/',
'../bar',
'http://example.com/base/bar',
];
yield 'absolute with slash is added behind base without path' => [
'http://example.com/base',
'/test',
'http://example.com/test',
];
yield 'query string is added behind base' => [
'http://example.com/base',
'?key=value',
'http://example.com/base?key=value',
];
yield 'query string is added behind base with slash' => [
'http://example.com/base/',
'?key=value',
'http://example.com/base/?key=value',
];
yield 'query string with slash is added behind base without path' => [
'http://example.com/base',
'/?key=value',
'http://example.com/?key=value',
];
yield 'absolute with query string below base is returned as-is' => [
'http://example.com/base',
'http://example.com/base?test',
'http://example.com/base?test',
];
yield 'urlencoded special chars will stay as-is' => [
'http://example.com/%7Bversion%7D/',
'',
'http://example.com/%7Bversion%7D/'
];
yield 'special chars will be urlencoded' => [
'http://example.com/{version}/',
'',
'http://example.com/%7Bversion%7D/'
];
yield 'other domain' => [
'http://example.com/base/',
'http://example.org/base/',
'http://example.org/base/'
];
yield 'other scheme' => [
'http://example.com/base/',
'https://example.com/base/',
'https://example.com/base/'
];
yield 'other port' => [
'http://example.com/base/',
'http://example.com:81/base/',
'http://example.com:81/base/'
];
yield 'other path' => [
'http://example.com/base/',
'http://example.com/other/',
'http://example.com/other/'
];
yield 'other path due to missing slash' => [
'http://example.com/base/',
'http://example.com/other',
'http://example.com/other'
];
}
/**
* @dataProvider provideOtherUris
* @param string $uri
* @param string $expected
*/
public function testResolveUriWithBaseEndsWithoutSlash($base, $uri, $expectedAbsolute)
{
$browser = $this->browser->withBase($base);
$this->sender->expects($this->once())->method('send')->with($this->callback(function (RequestInterface $request) use ($expectedAbsolute) {
$this->assertEquals($expectedAbsolute, $request->getUri());
return true;
}))->willReturn(new Promise(function () { }));
$browser->get($uri);
}
public function testWithBaseUrlNotAbsoluteFails()
{
$this->expectException(\InvalidArgumentException::class);
$this->browser->withBase('hello');
}
public function testWithBaseUrlInvalidSchemeFails()
{
$this->expectException(\InvalidArgumentException::class);
$this->browser->withBase('ftp://example.com');
}
public function testWithoutBaseFollowedByGetRequestTriesToSendIncompleteRequestUrl()
{
$this->browser = $this->browser->withBase('http://example.com')->withBase(null);
$this->sender->expects($this->once())->method('send')->with($this->callback(function (RequestInterface $request) {
$this->assertEquals('path', $request->getUri());
return true;
}))->willReturn(new Promise(function () { }));
$this->browser->get('path');
}
public function testWithProtocolVersionFollowedByGetRequestSendsRequestWithProtocolVersion()
{
$this->browser = $this->browser->withProtocolVersion('1.0');
$this->sender->expects($this->once())->method('send')->with($this->callback(function (RequestInterface $request) {
$this->assertEquals('1.0', $request->getProtocolVersion());
return true;
}))->willReturn(new Promise(function () { }));
$this->browser->get('http://example.com/');
}
public function testWithProtocolVersionInvalidThrows()
{
$this->expectException(\InvalidArgumentException::class);
$this->browser->withProtocolVersion('1.2');
}
public function testCancelGetRequestShouldCancelUnderlyingSocketConnection()
{
$pending = new Promise(function () { }, $this->expectCallableOnce());
$connector = $this->createMock(ConnectorInterface::class);
$connector->expects($this->once())->method('connect')->with('example.com:80')->willReturn($pending);
$this->browser = new Browser($connector, $this->loop);
$promise = $this->browser->get('http://example.com/');
$promise->cancel();
}
public function testWithHeaderShouldOverwriteExistingHeader()
{
$this->browser = $this->browser->withHeader('User-Agent', 'ACMC'); //should be overwritten
$this->browser = $this->browser->withHeader('user-agent', 'ABC'); //should be the user-agent
$this->sender->expects($this->once())->method('send')->with($this->callback(function (RequestInterface $request) {
$this->assertEquals(['ABC'], $request->getHeader('UsEr-AgEnT'));
return true;
}))->willReturn(new Promise(function () { }));
$this->browser->get('http://example.com/');
}
public function testWithHeaderShouldBeOverwrittenByExplicitHeaderInGetMethod()
{
$this->browser = $this->browser->withHeader('User-Agent', 'ACMC');
$this->sender->expects($this->once())->method('send')->with($this->callback(function (RequestInterface $request) {
$this->assertEquals(['ABC'], $request->getHeader('UsEr-AgEnT'));
return true;
}))->willReturn(new Promise(function () { }));
$this->browser->get('http://example.com/', ['user-Agent' => 'ABC']); //should win
}
public function testWithMultipleHeadersShouldBeMergedCorrectlyWithMultipleDefaultHeaders()
{
$this->browser = $this->browser->withHeader('User-Agent', 'ACMC');
$this->browser = $this->browser->withHeader('User-Test', 'Test');
$this->browser = $this->browser->withHeader('Custom-HEADER', 'custom');
$this->browser = $this->browser->withHeader('just-a-header', 'header-value');
$this->sender->expects($this->once())->method('send')->with($this->callback(function (RequestInterface $request) {
$expectedHeaders = [
'Host' => ['example.com'],
'User-Test' => ['Test'],
'just-a-header' => ['header-value'],
'user-Agent' => ['ABC'],
'another-header' => ['value'],
'custom-header' => ['data'],
];
$this->assertEquals($expectedHeaders, $request->getHeaders());
return true;
}))->willReturn(new Promise(function () { }));
$headers = [
'user-Agent' => 'ABC', //should overwrite: 'User-Agent', 'ACMC'
'another-header' => 'value',
'custom-header' => 'data', //should overwrite: 'Custom-header', 'custom'
];
$this->browser->get('http://example.com/', $headers);
}
public function testWithoutHeaderShouldRemoveExistingHeader()
{
$this->browser = $this->browser->withHeader('User-Agent', 'ACMC');
$this->browser = $this->browser->withoutHeader('UsEr-AgEnT'); //should remove case-insensitive header
$this->sender->expects($this->once())->method('send')->with($this->callback(function (RequestInterface $request) {
$this->assertEquals([], $request->getHeader('user-agent'));
return true;
}))->willReturn(new Promise(function () { }));
$this->browser->get('http://example.com/');
}
public function testWithoutHeaderConnectionShouldRemoveDefaultConnectionHeader()
{
$this->browser = $this->browser->withoutHeader('Connection');
$this->sender->expects($this->once())->method('send')->with($this->callback(function (RequestInterface $request) {
$this->assertEquals([], $request->getHeader('Connection'));
return true;
}))->willReturn(new Promise(function () { }));
$this->browser->get('http://example.com/');
}
public function testWithHeaderConnectionShouldOverwriteDefaultConnectionHeader()
{
$this->browser = $this->browser->withHeader('Connection', 'keep-alive');
$this->sender->expects($this->once())->method('send')->with($this->callback(function (RequestInterface $request) {
$this->assertEquals(['keep-alive'], $request->getHeader('Connection'));
return true;
}))->willReturn(new Promise(function () { }));
$this->browser->get('http://example.com/');
}
public function testBrowserShouldSendDefaultUserAgentHeader()
{
$this->sender->expects($this->once())->method('send')->with($this->callback(function (RequestInterface $request) {
$this->assertEquals([0 => 'ReactPHP/1'], $request->getHeader('user-agent'));
return true;
}))->willReturn(new Promise(function () { }));
$this->browser->get('http://example.com/');
}
public function testBrowserShouldNotSendDefaultUserAgentHeaderIfWithoutHeaderRemovesUserAgent()
{
$this->browser = $this->browser->withoutHeader('UsEr-AgEnT');
$this->sender->expects($this->once())->method('send')->with($this->callback(function (RequestInterface $request) {
$this->assertEquals([], $request->getHeader('User-Agent'));
return true;
}))->willReturn(new Promise(function () { }));
$this->browser->get('http://example.com/');
}
}