@@ -135,6 +135,145 @@ public function testWriteFunction()
135
135
/**
136
136
* @covers \Swoole\Curl\Handler::execute()
137
137
*/
138
+ public function testResolve ()
139
+ {
140
+ Coroutine \run (function () {
141
+ $ host = 'httpbin.org ' ;
142
+ $ url = 'https://httpbin.org/get ' ;
143
+ $ ip = Coroutine::gethostbyname ($ host );
144
+ $ ch = curl_init ();
145
+
146
+ curl_setopt ($ ch , CURLOPT_URL , $ url );
147
+ curl_setopt ($ ch , CURLOPT_RETURNTRANSFER , 1 );
148
+ curl_setopt ($ ch , CURLOPT_RESOLVE , ["{$ host }:443: {$ ip }" ]);
149
+
150
+ $ data = curl_exec ($ ch );
151
+ $ httpPrimaryIp = curl_getinfo ($ ch , CURLINFO_PRIMARY_IP );
152
+ curl_close ($ ch );
153
+ $ body = json_decode ($ data , true );
154
+ self ::assertSame ($ body ['headers ' ]['Host ' ], 'httpbin.org ' );
155
+ self ::assertEquals ($ body ['url ' ], $ url );
156
+ self ::assertEquals ($ ip , $ httpPrimaryIp );
157
+ });
158
+ }
159
+
160
+ /**
161
+ * @covers \Swoole\Curl\Handler::execute()
162
+ */
163
+ public function testInvalidResolve ()
164
+ {
165
+ Coroutine \run (function () {
166
+ $ host = 'httpbin.org ' ;
167
+ $ url = 'https://httpbin.org/get ' ;
168
+ $ ip = '127.0.0.1 ' ; // An incorrect IP in use.
169
+ $ ch = curl_init ();
170
+
171
+ curl_setopt ($ ch , CURLOPT_URL , $ url );
172
+ curl_setopt ($ ch , CURLOPT_RETURNTRANSFER , 1 );
173
+ curl_setopt ($ ch , CURLOPT_RESOLVE , ["{$ host }:443: {$ ip }" ]);
174
+
175
+ $ body = curl_exec ($ ch );
176
+ $ httpPrimaryIp = curl_getinfo ($ ch , CURLINFO_PRIMARY_IP );
177
+ curl_close ($ ch );
178
+ self ::assertFalse ($ body );
179
+ self ::assertSame ('' , $ httpPrimaryIp );
180
+ });
181
+ }
182
+
183
+ /**
184
+ * @covers \Swoole\Curl\Handler::execute()
185
+ */
186
+ public function testResolve2 ()
187
+ {
188
+ Coroutine \run (function () {
189
+ $ host = 'httpbin.org ' ;
190
+ $ url = 'https://httpbin.org/get ' ;
191
+ $ ip = Coroutine::gethostbyname ($ host );
192
+ $ ch = curl_init ();
193
+
194
+ curl_setopt ($ ch , CURLOPT_URL , $ url );
195
+ curl_setopt ($ ch , CURLOPT_RETURNTRANSFER , 1 );
196
+ curl_setopt ($ ch , CURLOPT_RESOLVE , ["{$ host }:443:127.0.0.1 " , "{$ host }:443: {$ ip }" ]);
197
+
198
+ $ data = curl_exec ($ ch );
199
+ $ httpPrimaryIp = curl_getinfo ($ ch , CURLINFO_PRIMARY_IP );
200
+ $ body = json_decode ($ data , true );
201
+ self ::assertSame ($ body ['headers ' ]['Host ' ], 'httpbin.org ' );
202
+ self ::assertEquals ($ body ['url ' ], $ url );
203
+ self ::assertEquals ($ ip , $ httpPrimaryIp );
204
+ });
205
+ }
206
+
207
+ /**
208
+ * @covers \Swoole\Curl\Handler::execute()
209
+ */
210
+ public function testInvalidResolve2 ()
211
+ {
212
+ Coroutine \run (function () {
213
+ $ host = 'httpbin.org ' ;
214
+ $ url = 'https://httpbin.org/get ' ;
215
+ $ ip = Coroutine::gethostbyname ($ host );
216
+ $ ch = curl_init ();
217
+
218
+ curl_setopt ($ ch , CURLOPT_URL , $ url );
219
+ curl_setopt ($ ch , CURLOPT_RETURNTRANSFER , 1 );
220
+ curl_setopt ($ ch , CURLOPT_RESOLVE , ["{$ host }:443: {$ ip }" , "+ {$ host }:443:127.0.0.1 " ]);
221
+
222
+ $ body = curl_exec ($ ch );
223
+ $ httpPrimaryIp = curl_getinfo ($ ch , CURLINFO_PRIMARY_IP );
224
+ curl_close ($ ch );
225
+ self ::assertFalse ($ body );
226
+ self ::assertSame ('' , $ httpPrimaryIp );
227
+ });
228
+ }
229
+
230
+ /**
231
+ * @covers \Swoole\Curl\Handler::execute()
232
+ */
233
+ public function testInvalidResolve3 ()
234
+ {
235
+ Coroutine \run (function () {
236
+ $ host = 'httpbin.org ' ;
237
+ $ url = 'https://httpbin.org/get ' ;
238
+ $ ip = Coroutine::gethostbyname ($ host );
239
+ $ ch = curl_init ();
240
+
241
+ curl_setopt ($ ch , CURLOPT_URL , $ url );
242
+ curl_setopt ($ ch , CURLOPT_RETURNTRANSFER , 1 );
243
+ curl_setopt ($ ch , CURLOPT_RESOLVE , ["{$ host }:443: {$ ip }" , "{$ host }:443:127.0.0.1 " ]);
244
+
245
+ $ body = curl_exec ($ ch );
246
+ $ httpPrimaryIp = curl_getinfo ($ ch , CURLINFO_PRIMARY_IP );
247
+ curl_close ($ ch );
248
+ self ::assertFalse ($ body );
249
+ self ::assertSame ('' , $ httpPrimaryIp );
250
+ });
251
+ }
252
+
253
+ /**
254
+ * @covers \Swoole\Curl\Handler::execute()
255
+ */
256
+ public function testResolve3 ()
257
+ {
258
+ Coroutine \run (function () {
259
+ $ host = 'httpbin.org ' ;
260
+ $ url = 'https://httpbin.org/get ' ;
261
+ $ ip = Coroutine::gethostbyname ($ host );
262
+ $ ch = curl_init ();
263
+
264
+ curl_setopt ($ ch , CURLOPT_URL , $ url );
265
+ curl_setopt ($ ch , CURLOPT_RETURNTRANSFER , 1 );
266
+ curl_setopt ($ ch , CURLOPT_RESOLVE , ["{$ host }:443: {$ ip }" , "{$ host }:443:127.0.0.1 " , "- {$ host }:443:127.0.0.1 " ]);
267
+
268
+ $ data = curl_exec ($ ch );
269
+ $ httpPrimaryIp = curl_getinfo ($ ch , CURLINFO_PRIMARY_IP );
270
+ $ body = json_decode ($ data , true );
271
+ self ::assertSame ($ body ['headers ' ]['Host ' ], 'httpbin.org ' );
272
+ self ::assertEquals ($ body ['url ' ], $ url );
273
+ self ::assertSame ('' , $ httpPrimaryIp );
274
+ });
275
+ }
276
+
138
277
public function testOptPrivate ()
139
278
{
140
279
Coroutine \run (function () {
0 commit comments