3
3
namespace React \Tests \Http \Client ;
4
4
5
5
use Psr \Http \Message \ResponseInterface ;
6
+ use React \EventLoop \Loop ;
6
7
use React \Http \Client \Client ;
7
8
use React \Http \Io \ClientConnectionManager ;
8
9
use React \Http \Message \Request ;
@@ -47,7 +48,7 @@ public function testRequestToLocalhostEmitsSingleRemoteConnection()
47
48
});
48
49
$ port = parse_url ($ socket ->getAddress (), PHP_URL_PORT );
49
50
50
- $ client = new Client (new ClientConnectionManager (new Connector ()));
51
+ $ client = new Client (new ClientConnectionManager (new Connector (), Loop:: get () ));
51
52
$ request = $ client ->request (new Request ('GET ' , 'http://localhost: ' . $ port , array (), '' , '1.0 ' ));
52
53
53
54
$ promise = Stream \first ($ request , 'close ' );
@@ -56,7 +57,7 @@ public function testRequestToLocalhostEmitsSingleRemoteConnection()
56
57
\React \Async \await (\React \Promise \Timer \timeout ($ promise , self ::TIMEOUT_LOCAL ));
57
58
}
58
59
59
- public function testRequestToLocalhostWillConnectAndCloseConnectionAfterResponseUntilKeepAliveIsActuallySupported ()
60
+ public function testRequestToLocalhostWillConnectAndCloseConnectionAfterResponseWhenKeepAliveTimesOut ()
60
61
{
61
62
$ socket = new SocketServer ('127.0.0.1:0 ' );
62
63
$ socket ->on ('connection ' , $ this ->expectCallableOnce ());
@@ -72,14 +73,42 @@ public function testRequestToLocalhostWillConnectAndCloseConnectionAfterResponse
72
73
});
73
74
$ port = parse_url ($ socket ->getAddress (), PHP_URL_PORT );
74
75
75
- $ client = new Client (new ClientConnectionManager (new Connector ()));
76
+ $ client = new Client (new ClientConnectionManager (new Connector (), Loop:: get () ));
76
77
$ request = $ client ->request (new Request ('GET ' , 'http://localhost: ' . $ port , array (), '' , '1.1 ' ));
77
78
78
79
$ request ->end ();
79
80
80
81
\React \Async \await (\React \Promise \Timer \timeout ($ promise , self ::TIMEOUT_LOCAL ));
81
82
}
82
83
84
+ public function testRequestToLocalhostWillReuseExistingConnectionForSecondRequest ()
85
+ {
86
+ $ socket = new SocketServer ('127.0.0.1:0 ' );
87
+ $ socket ->on ('connection ' , $ this ->expectCallableOnce ());
88
+
89
+ $ socket ->on ('connection ' , function (ConnectionInterface $ connection ) use ($ socket ) {
90
+ $ connection ->on ('data ' , function () use ($ connection ) {
91
+ $ connection ->write ("HTTP/1.1 200 OK \r\nContent-Length: 2 \r\n\r\nOK " );
92
+ });
93
+ $ socket ->close ();
94
+ });
95
+ $ port = parse_url ($ socket ->getAddress (), PHP_URL_PORT );
96
+
97
+ $ client = new Client (new ClientConnectionManager (new Connector (), Loop::get ()));
98
+
99
+ $ request = $ client ->request (new Request ('GET ' , 'http://localhost: ' . $ port , array (), '' , '1.1 ' ));
100
+ $ promise = Stream \first ($ request , 'close ' );
101
+ $ request ->end ();
102
+
103
+ \React \Async \await (\React \Promise \Timer \timeout ($ promise , self ::TIMEOUT_LOCAL ));
104
+
105
+ $ request = $ client ->request (new Request ('GET ' , 'http://localhost: ' . $ port , array (), '' , '1.1 ' ));
106
+ $ promise = Stream \first ($ request , 'close ' );
107
+ $ request ->end ();
108
+
109
+ \React \Async \await (\React \Promise \Timer \timeout ($ promise , self ::TIMEOUT_LOCAL ));
110
+ }
111
+
83
112
public function testRequestLegacyHttpServerWithOnlyLineFeedReturnsSuccessfulResponse ()
84
113
{
85
114
$ socket = new SocketServer ('127.0.0.1:0 ' );
@@ -88,7 +117,7 @@ public function testRequestLegacyHttpServerWithOnlyLineFeedReturnsSuccessfulResp
88
117
$ socket ->close ();
89
118
});
90
119
91
- $ client = new Client (new ClientConnectionManager (new Connector ()));
120
+ $ client = new Client (new ClientConnectionManager (new Connector (), Loop:: get () ));
92
121
$ request = $ client ->request (new Request ('GET ' , str_replace ('tcp: ' , 'http: ' , $ socket ->getAddress ()), array (), '' , '1.0 ' ));
93
122
94
123
$ once = $ this ->expectCallableOnceWith ('body ' );
@@ -108,7 +137,7 @@ public function testSuccessfulResponseEmitsEnd()
108
137
// max_nesting_level was set to 100 for PHP Versions < 5.4 which resulted in failing test for legacy PHP
109
138
ini_set ('xdebug.max_nesting_level ' , 256 );
110
139
111
- $ client = new Client (new ClientConnectionManager (new Connector ()));
140
+ $ client = new Client (new ClientConnectionManager (new Connector (), Loop:: get () ));
112
141
113
142
$ request = $ client ->request (new Request ('GET ' , 'http://www.google.com/ ' , array (), '' , '1.0 ' ));
114
143
@@ -133,7 +162,7 @@ public function testPostDataReturnsData()
133
162
// max_nesting_level was set to 100 for PHP Versions < 5.4 which resulted in failing test for legacy PHP
134
163
ini_set ('xdebug.max_nesting_level ' , 256 );
135
164
136
- $ client = new Client (new ClientConnectionManager (new Connector ()));
165
+ $ client = new Client (new ClientConnectionManager (new Connector (), Loop:: get () ));
137
166
138
167
$ data = str_repeat ('. ' , 33000 );
139
168
$ request = $ client ->request (new Request ('POST ' , 'https:// ' . (mt_rand (0 , 1 ) === 0 ? 'eu. ' : '' ) . 'httpbin.org/post ' , array ('Content-Length ' => strlen ($ data )), '' , '1.0 ' ));
@@ -165,7 +194,7 @@ public function testPostJsonReturnsData()
165
194
$ this ->markTestSkipped ('Not supported on HHVM ' );
166
195
}
167
196
168
- $ client = new Client (new ClientConnectionManager (new Connector ()));
197
+ $ client = new Client (new ClientConnectionManager (new Connector (), Loop:: get () ));
169
198
170
199
$ data = json_encode (array ('numbers ' => range (1 , 50 )));
171
200
$ request = $ client ->request (new Request ('POST ' , 'https://httpbin.org/post ' , array ('Content-Length ' => strlen ($ data ), 'Content-Type ' => 'application/json ' ), '' , '1.0 ' ));
@@ -195,7 +224,7 @@ public function testCancelPendingConnectionEmitsClose()
195
224
// max_nesting_level was set to 100 for PHP Versions < 5.4 which resulted in failing test for legacy PHP
196
225
ini_set ('xdebug.max_nesting_level ' , 256 );
197
226
198
- $ client = new Client (new ClientConnectionManager (new Connector ()));
227
+ $ client = new Client (new ClientConnectionManager (new Connector (), Loop:: get () ));
199
228
200
229
$ request = $ client ->request (new Request ('GET ' , 'http://www.google.com/ ' , array (), '' , '1.0 ' ));
201
230
$ request ->on ('error ' , $ this ->expectCallableNever ());
0 commit comments