forked from reactphp/http
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFunctionalIntegrationTest.php
180 lines (139 loc) · 6.42 KB
/
FunctionalIntegrationTest.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
<?php
namespace React\Tests\Http\Client;
use Psr\Http\Message\ResponseInterface;
use React\EventLoop\Loop;
use React\Http\Client\Client;
use React\Http\Message\Request;
use React\Promise\Deferred;
use React\Promise\Stream;
use React\Socket\ConnectionInterface;
use React\Socket\SocketServer;
use React\Stream\ReadableStreamInterface;
use React\Tests\Http\TestCase;
class FunctionalIntegrationTest extends TestCase
{
/**
* Test timeout to use for local tests.
*
* In practice this would be near 0.001s, but let's leave some time in case
* the local system is currently busy.
*
* @var float
*/
const TIMEOUT_LOCAL = 1.0;
/**
* Test timeout to use for remote (internet) tests.
*
* In pratice this should be below 1s, but this relies on infrastructure
* outside our control, so consider this a maximum to avoid running for hours.
*
* @var float
*/
const TIMEOUT_REMOTE = 10.0;
public function testRequestToLocalhostEmitsSingleRemoteConnection()
{
$socket = new SocketServer('127.0.0.1:0');
$socket->on('connection', $this->expectCallableOnce());
$socket->on('connection', function (ConnectionInterface $conn) use ($socket) {
$conn->end("HTTP/1.1 200 OK\r\n\r\nOk");
$socket->close();
});
$port = parse_url($socket->getAddress(), PHP_URL_PORT);
$client = new Client(Loop::get());
$request = $client->request(new Request('GET', 'http://localhost:' . $port, array(), '', '1.0'));
$promise = Stream\first($request, 'close');
$request->end();
\React\Async\await(\React\Promise\Timer\timeout($promise, self::TIMEOUT_LOCAL));
}
public function testRequestLegacyHttpServerWithOnlyLineFeedReturnsSuccessfulResponse()
{
$socket = new SocketServer('127.0.0.1:0');
$socket->on('connection', function (ConnectionInterface $conn) use ($socket) {
$conn->end("HTTP/1.0 200 OK\n\nbody");
$socket->close();
});
$client = new Client(Loop::get());
$request = $client->request(new Request('GET', str_replace('tcp:', 'http:', $socket->getAddress()), array(), '', '1.0'));
$once = $this->expectCallableOnceWith('body');
$request->on('response', function (ResponseInterface $response, ReadableStreamInterface $body) use ($once) {
$body->on('data', $once);
});
$promise = Stream\first($request, 'close');
$request->end();
\React\Async\await(\React\Promise\Timer\timeout($promise, self::TIMEOUT_LOCAL));
}
/** @group internet */
public function testSuccessfulResponseEmitsEnd()
{
// max_nesting_level was set to 100 for PHP Versions < 5.4 which resulted in failing test for legacy PHP
ini_set('xdebug.max_nesting_level', 256);
$client = new Client(Loop::get());
$request = $client->request(new Request('GET', 'http://www.google.com/', array(), '', '1.0'));
$once = $this->expectCallableOnce();
$request->on('response', function (ResponseInterface $response, ReadableStreamInterface $body) use ($once) {
$body->on('end', $once);
});
$promise = Stream\first($request, 'close');
$request->end();
\React\Async\await(\React\Promise\Timer\timeout($promise, self::TIMEOUT_REMOTE));
}
/** @group internet */
public function testPostDataReturnsData()
{
if (defined('HHVM_VERSION')) {
$this->markTestSkipped('Not supported on HHVM');
}
// max_nesting_level was set to 100 for PHP Versions < 5.4 which resulted in failing test for legacy PHP
ini_set('xdebug.max_nesting_level', 256);
$client = new Client(Loop::get());
$data = str_repeat('.', 33000);
$request = $client->request(new Request('POST', 'https://' . (mt_rand(0, 1) === 0 ? 'eu.' : '') . 'httpbin.org/post', array('Content-Length' => strlen($data)), '', '1.0'));
$deferred = new Deferred();
$request->on('response', function (ResponseInterface $response, ReadableStreamInterface $body) use ($deferred) {
$deferred->resolve(Stream\buffer($body));
});
$request->on('error', 'printf');
$request->on('error', $this->expectCallableNever());
$request->end($data);
$buffer = \React\Async\await(\React\Promise\Timer\timeout($deferred->promise(), self::TIMEOUT_REMOTE));
$this->assertNotEquals('', $buffer);
$parsed = json_decode($buffer, true);
$this->assertTrue(is_array($parsed) && isset($parsed['data']));
$this->assertEquals(strlen($data), strlen($parsed['data']));
$this->assertEquals($data, $parsed['data']);
}
/** @group internet */
public function testPostJsonReturnsData()
{
if (defined('HHVM_VERSION')) {
$this->markTestSkipped('Not supported on HHVM');
}
$client = new Client(Loop::get());
$data = json_encode(array('numbers' => range(1, 50)));
$request = $client->request(new Request('POST', 'https://httpbin.org/post', array('Content-Length' => strlen($data), 'Content-Type' => 'application/json'), '', '1.0'));
$deferred = new Deferred();
$request->on('response', function (ResponseInterface $response, ReadableStreamInterface $body) use ($deferred) {
$deferred->resolve(Stream\buffer($body));
});
$request->on('error', 'printf');
$request->on('error', $this->expectCallableNever());
$request->end($data);
$buffer = \React\Async\await(\React\Promise\Timer\timeout($deferred->promise(), self::TIMEOUT_REMOTE));
$this->assertNotEquals('', $buffer);
$parsed = json_decode($buffer, true);
$this->assertTrue(is_array($parsed) && isset($parsed['json']));
$this->assertEquals(json_decode($data, true), $parsed['json']);
}
/** @group internet */
public function testCancelPendingConnectionEmitsClose()
{
// max_nesting_level was set to 100 for PHP Versions < 5.4 which resulted in failing test for legacy PHP
ini_set('xdebug.max_nesting_level', 256);
$client = new Client(Loop::get());
$request = $client->request(new Request('GET', 'http://www.google.com/', array(), '', '1.0'));
$request->on('error', $this->expectCallableNever());
$request->on('close', $this->expectCallableOnce());
$request->end();
$request->close();
}
}