@@ -217,9 +217,19 @@ TEST(client, shutdown_all) {
217
217
}
218
218
219
219
#ifdef SW_USE_OPENSSL
220
- TEST (client, ssl_1) {
221
- int ret;
222
220
221
+ static const char *request_baidu = " GET / HTTP/1.1\r\n "
222
+ " Host: www.baidu.com\r\n "
223
+ " Connection: close\r\n "
224
+ " User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) "
225
+ " Chrome/51.0.2704.106 Safari/537.36"
226
+ " \r\n\r\n " ;
227
+
228
+ static const char *domain_baidu = " www.baidu.com" ;
229
+
230
+ #define SOCKS5_WITH_AUTH 1
231
+
232
+ TEST (client, ssl_1) {
223
233
bool connected = false ;
224
234
bool closed = false ;
225
235
swoole::String buf (65536 );
@@ -230,21 +240,14 @@ TEST(client, ssl_1) {
230
240
client.enable_ssl_encrypt ();
231
241
client.onConnect = [&connected](Client *cli) {
232
242
connected = true ;
233
- cli->send (cli,
234
- SW_STRL (" GET / HTTP/1.1\r\n "
235
- " Host: www.baidu.com\r\n "
236
- " Connection: close\r\n "
237
- " User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) "
238
- " Chrome/51.0.2704.106 Safari/537.36"
239
- " \r\n\r\n " ),
240
- 0 );
243
+ cli->send (cli, request_baidu, strlen (request_baidu), 0 );
241
244
};
242
245
243
246
client.onError = [](Client *cli) {};
244
247
client.onClose = [&closed](Client *cli) { closed = true ; };
245
248
client.onReceive = [&buf](Client *cli, const char *data, size_t length) { buf.append (data, length); };
246
- ret = client. connect (&client, " www.baidu.com " , 443 , - 1 , 0 );
247
- ASSERT_EQ (ret , 0 );
249
+
250
+ ASSERT_EQ (client. connect (&client, domain_baidu, 443 , - 1 , 0 ) , 0 );
248
251
249
252
swoole_event_wait ();
250
253
@@ -253,89 +256,121 @@ TEST(client, ssl_1) {
253
256
ASSERT_TRUE (buf.contains (" Baidu" ));
254
257
}
255
258
256
-
257
- TEST (client, http_proxy) {
259
+ static void proxy_async_test (Client &client, bool https) {
258
260
int ret;
259
261
262
+ swoole_event_init (SW_EVENTLOOP_WAIT_EXIT);
263
+
260
264
bool connected = false ;
261
265
bool closed = false ;
262
266
swoole::String buf (65536 );
263
267
264
- swoole_event_init (SW_EVENTLOOP_WAIT_EXIT);
265
-
266
- Client client (SW_SOCK_TCP, true );
267
- client.enable_ssl_encrypt ();
268
- client.http_proxy = new HttpProxy ();
269
- client.http_proxy ->proxy_host = std::string (TEST_HTTP_PROXY_HOST);
270
- client.http_proxy ->proxy_port = TEST_HTTP_PROXY_PORT;
268
+ if (https) {
269
+ client.enable_ssl_encrypt ();
270
+ }
271
271
272
272
client.onConnect = [&connected](Client *cli) {
273
273
connected = true ;
274
- cli->send (cli,
275
- SW_STRL (" GET / HTTP/1.1\r\n "
276
- " Host: www.baidu.com\r\n "
277
- " Connection: close\r\n "
278
- " User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) "
279
- " Chrome/51.0.2704.106 Safari/537.36"
280
- " \r\n\r\n " ),
281
- 0 );
274
+ cli->send (cli, request_baidu, strlen (request_baidu), 0 );
282
275
};
283
276
284
277
client.onError = [](Client *cli) {};
285
278
client.onClose = [&closed](Client *cli) { closed = true ; };
286
279
client.onReceive = [&buf](Client *cli, const char *data, size_t length) { buf.append (data, length); };
287
- ret = client. connect (&client, " www.baidu.com " , 443 , - 1 , 0 );
288
- ASSERT_EQ (ret , 0 );
280
+
281
+ ASSERT_EQ (client. connect (&client, domain_baidu, https ? 443 : 80 , - 1 , 0 ) , 0 );
289
282
290
283
swoole_event_wait ();
291
284
292
285
ASSERT_TRUE (connected);
293
286
ASSERT_TRUE (closed);
294
- ASSERT_TRUE (buf.contains (" Baidu " ));
287
+ ASSERT_TRUE (buf.contains (" www.baidu.com " ));
295
288
}
296
289
297
- TEST (client, socks5_proxy) {
298
- int ret;
299
-
300
- bool connected = false ;
301
- bool closed = false ;
290
+ static void proxy_sync_test (Client &client, bool https) {
302
291
swoole::String buf (65536 );
292
+ if (https) {
293
+ client.enable_ssl_encrypt ();
294
+ }
295
+
296
+ ASSERT_EQ (client.connect (&client, domain_baidu, https ? 443 : 80 , -1 , 0 ), 0 );
297
+ ASSERT_GT (client.send (&client, request_baidu, strlen (request_baidu), 0 ), 0 );
298
+
299
+ while (true ) {
300
+ char rbuf[4096 ];
301
+ auto nr = client.recv (&client, rbuf, sizeof (rbuf), 0 );
302
+ if (nr <= 0 ) {
303
+ break ;
304
+ }
305
+ buf.append (rbuf, nr);
306
+ }
307
+
308
+ ASSERT_TRUE (buf.contains (" www.baidu.com" ));
309
+ }
303
310
304
- swoole_event_init (SW_EVENTLOOP_WAIT_EXIT);
305
-
306
- Client client (SW_SOCK_TCP, true );
307
- client.enable_ssl_encrypt ();
308
-
311
+ static void proxy_set_socks5_proxy (Client &client) {
309
312
client.socks5_proxy = new Socks5Proxy ();
310
313
client.socks5_proxy ->host = std::string (" 127.0.0.1" );
311
314
client.socks5_proxy ->port = 1080 ;
312
315
client.socks5_proxy ->dns_tunnel = 1 ;
313
- client.socks5_proxy ->method = 0x02 ;
316
+ #if SOCKS5_WITH_AUTH
317
+ client.socks5_proxy ->method = SW_SOCKS5_METHOD_AUTH;
314
318
client.socks5_proxy ->username = std::string (" user" );
315
319
client.socks5_proxy ->password = std::string (" password" );
320
+ #endif
321
+ }
316
322
317
- client.onConnect = [&connected](Client *cli) {
318
- connected = true ;
319
- cli->send (cli,
320
- SW_STRL (" GET / HTTP/1.1\r\n "
321
- " Host: www.baidu.com\r\n "
322
- " Connection: close\r\n "
323
- " User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) "
324
- " Chrome/51.0.2704.106 Safari/537.36"
325
- " \r\n\r\n " ),
326
- 0 );
327
- };
323
+ static void proxy_set_http_proxy (Client &client) {
324
+ client.http_proxy = new HttpProxy ();
325
+ client.http_proxy ->proxy_host = std::string (TEST_HTTP_PROXY_HOST);
326
+ client.http_proxy ->proxy_port = TEST_HTTP_PROXY_PORT;
327
+ }
328
328
329
- client. onError = [](Client *cli ) {};
330
- client. onClose = [&closed](Client *cli) { closed = true ; } ;
331
- client. onReceive = [&buf](Client *cli, const char *data, size_t length) { buf. append (data, length); } ;
332
- ret = client. connect (& client, " www.baidu.com " , 443 , - 1 , 0 );
333
- ASSERT_EQ (ret, 0 );
329
+ TEST ( client, https_get_async_with_http_proxy ) {
330
+ Client client (SW_SOCK_TCP, true ) ;
331
+ proxy_set_http_proxy (client) ;
332
+ proxy_async_test ( client, true );
333
+ }
334
334
335
- swoole_event_wait ();
335
+ TEST (client, https_get_async_with_socks5_proxy) {
336
+ Client client (SW_SOCK_TCP, true );
337
+ proxy_set_socks5_proxy (client);
338
+ proxy_async_test (client, true );
339
+ }
336
340
337
- ASSERT_TRUE (connected);
338
- ASSERT_TRUE (closed);
339
- ASSERT_TRUE (buf.contains (" Baidu" ));
341
+ TEST (client, https_get_sync_with_http_proxy) {
342
+ Client client (SW_SOCK_TCP, false );
343
+ proxy_set_http_proxy (client);
344
+ proxy_sync_test (client, true );
345
+ }
346
+
347
+ TEST (client, https_get_sync_with_socks5_proxy) {
348
+ Client client (SW_SOCK_TCP, false );
349
+ proxy_set_socks5_proxy (client);
350
+ proxy_sync_test (client, true );
351
+ }
352
+
353
+ TEST (client, http_get_async_with_http_proxy) {
354
+ Client client (SW_SOCK_TCP, true );
355
+ proxy_set_http_proxy (client);
356
+ proxy_async_test (client, false );
357
+ }
358
+
359
+ TEST (client, http_get_async_with_socks5_proxy) {
360
+ Client client (SW_SOCK_TCP, true );
361
+ proxy_set_socks5_proxy (client);
362
+ proxy_async_test (client, false );
363
+ }
364
+
365
+ TEST (client, http_get_sync_with_http_proxy) {
366
+ Client client (SW_SOCK_TCP, false );
367
+ proxy_set_http_proxy (client);
368
+ proxy_sync_test (client, false );
369
+ }
370
+
371
+ TEST (client, http_get_sync_with_socks5_proxy) {
372
+ Client client (SW_SOCK_TCP, false );
373
+ proxy_set_socks5_proxy (client);
374
+ proxy_sync_test (client, false );
340
375
}
341
376
#endif
0 commit comments