Skip to content

Commit e6c6d0c

Browse files
committed
% curl test refactoring
1 parent 9033d26 commit e6c6d0c

8 files changed

+51
-76
lines changed

tests/common/simple_http_server.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77

88
function start_test_server_process($port = 8088) {
99
$server_script = __FILE__;
10-
$cmd = PHP_BINARY . " $server_script $port > /dev/null 2>&1 & echo $!";
10+
$php_executable = getenv('TEST_PHP_EXECUTABLE') ?: PHP_BINARY;
11+
$cmd = $php_executable . " $server_script $port > /dev/null 2>&1 & echo $!";
1112
$pid = exec($cmd);
1213

1314
// Wait a bit for server to start

tests/curl/003-multi_handle_async.phpt

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,31 @@ Async cURL multi-handle operations
44
curl
55
--FILE--
66
<?php
7-
include "../common/simple_http_server.php";
7+
include "../../sapi/cli/tests/php_cli_server.inc";
88

99
use function Async\spawn;
1010
use function Async\await;
1111

12-
// Start test server
13-
$server_pid = start_test_server_process(8088);
12+
php_cli_server_start();
1413

1514
function test_multi_handle() {
1615
echo "Starting multi-handle test\n";
1716

1817
$mh = curl_multi_init();
1918

20-
// Create multiple cURL handles
19+
// Create multiple cURL handles - all pointing to same URL with delays
2120
$ch1 = curl_init();
22-
curl_setopt($ch1, CURLOPT_URL, get_test_server_url('/'));
21+
curl_setopt($ch1, CURLOPT_URL, "http://" . PHP_CLI_SERVER_ADDRESS);
2322
curl_setopt($ch1, CURLOPT_RETURNTRANSFER, true);
2423
curl_multi_add_handle($mh, $ch1);
2524

2625
$ch2 = curl_init();
27-
curl_setopt($ch2, CURLOPT_URL, get_test_server_url('/json'));
26+
curl_setopt($ch2, CURLOPT_URL, "http://" . PHP_CLI_SERVER_ADDRESS);
2827
curl_setopt($ch2, CURLOPT_RETURNTRANSFER, true);
2928
curl_multi_add_handle($mh, $ch2);
3029

3130
$ch3 = curl_init();
32-
curl_setopt($ch3, CURLOPT_URL, get_test_server_url('/slow'));
31+
curl_setopt($ch3, CURLOPT_URL, "http://" . PHP_CLI_SERVER_ADDRESS);
3332
curl_setopt($ch3, CURLOPT_RETURNTRANSFER, true);
3433
curl_multi_add_handle($mh, $ch3);
3534

@@ -74,15 +73,12 @@ echo "Test start\n";
7473
$coroutine = spawn(test_multi_handle(...));
7574
$results = await($coroutine);
7675

77-
// Stop server
78-
stop_test_server_process($server_pid);
79-
8076
echo "Test end\n";
8177
?>
8278
--EXPECT--
8379
Test start
8480
Starting multi-handle test
85-
Response 1: Hello World
86-
Response 2: {"message":"Hello JSON","status":"ok"}
87-
Response 3: Slow Response
81+
Response 1: Hello world
82+
Response 2: Hello world
83+
Response 3: Hello world
8884
Test end

tests/curl/004-post_request.phpt

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,20 @@ Async cURL POST request
44
curl
55
--FILE--
66
<?php
7-
include "../common/simple_http_server.php";
7+
include "../../sapi/cli/tests/php_cli_server.inc";
88

99
use function Async\spawn;
1010
use function Async\await;
1111

12-
// Start test server
13-
$server_pid = start_test_server_process(8088);
12+
php_cli_server_start();
1413

1514
function test_post_request() {
1615
echo "Starting POST test\n";
1716

1817
$post_data = "test=data&value=123";
1918

2019
$ch = curl_init();
21-
curl_setopt($ch, CURLOPT_URL, get_test_server_url('/post'));
20+
curl_setopt($ch, CURLOPT_URL, "http://" . PHP_CLI_SERVER_ADDRESS);
2221
curl_setopt($ch, CURLOPT_POST, true);
2322
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
2423
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
@@ -42,15 +41,12 @@ echo "Test start\n";
4241
$coroutine = spawn(test_post_request(...));
4342
$result = await($coroutine);
4443

45-
// Stop server
46-
stop_test_server_process($server_pid);
47-
4844
echo "Test end\n";
4945
?>
5046
--EXPECT--
5147
Test start
5248
Starting POST test
5349
HTTP Code: 200
5450
Error: none
55-
Response: POST received: 17 bytes
51+
Response: Hello world
5652
Test end

tests/curl/005-error_handling.phpt

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,12 @@ Async cURL error handling
44
curl
55
--FILE--
66
<?php
7-
include "../common/simple_http_server.php";
7+
include "../../sapi/cli/tests/php_cli_server.inc";
88

99
use function Async\spawn;
1010
use function Async\awaitAll;
1111

12-
// Start test server
13-
$server_pid = start_test_server_process(8088);
12+
php_cli_server_start();
1413

1514
function test_connection_error() {
1615
echo "Testing connection error\n";
@@ -37,8 +36,9 @@ function test_connection_error() {
3736
function test_server_error() {
3837
echo "Testing server error\n";
3938

39+
// Test with invalid URL to trigger an error
4040
$ch = curl_init();
41-
curl_setopt($ch, CURLOPT_URL, get_test_server_url('/error'));
41+
curl_setopt($ch, CURLOPT_URL, "http://" . PHP_CLI_SERVER_ADDRESS . "/nonexistent.php");
4242
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
4343
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
4444

@@ -50,7 +50,7 @@ function test_server_error() {
5050

5151
echo "HTTP Code: $http_code\n";
5252
echo "Error: " . ($error ?: "none") . "\n";
53-
echo "Response: $response\n";
53+
echo "Response length: " . strlen($response) . "\n";
5454

5555
return $response;
5656
}
@@ -59,7 +59,7 @@ function test_not_found() {
5959
echo "Testing 404 error\n";
6060

6161
$ch = curl_init();
62-
curl_setopt($ch, CURLOPT_URL, get_test_server_url('/nonexistent'));
62+
curl_setopt($ch, CURLOPT_URL, "http://" . PHP_CLI_SERVER_ADDRESS . "/missing.html");
6363
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
6464
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
6565

@@ -69,7 +69,7 @@ function test_not_found() {
6969
curl_close($ch);
7070

7171
echo "HTTP Code: $http_code\n";
72-
echo "Response: $response\n";
72+
echo "Response length: " . strlen($response) . "\n";
7373

7474
return $response;
7575
}
@@ -84,9 +84,6 @@ $coroutines = [
8484

8585
$results = awaitAll($coroutines);
8686

87-
// Stop server
88-
stop_test_server_process($server_pid);
89-
9087
echo "Test end\n";
9188
?>
9289
--EXPECTF--
@@ -97,9 +94,9 @@ Testing 404 error
9794
Connection failed as expected
9895
Error present: yes
9996
Error number: %d
100-
HTTP Code: 500
97+
HTTP Code: 404
10198
Error: none
102-
Response: Internal Server Error
99+
Response length: %d
103100
HTTP Code: 404
104-
Response: Not Found
101+
Response length: %d
105102
Test end

tests/curl/006-timeout_handling.phpt

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,21 @@ Async cURL timeout handling
44
curl
55
--FILE--
66
<?php
7-
include "../common/simple_http_server.php";
7+
include "../../sapi/cli/tests/php_cli_server.inc";
88

99
use function Async\spawn;
1010
use function Async\await;
1111

12-
// Start test server
13-
$server_pid = start_test_server_process(8088);
12+
php_cli_server_start();
1413

1514
function test_timeout() {
1615
echo "Testing timeout\n";
1716

1817
$ch = curl_init();
19-
curl_setopt($ch, CURLOPT_URL, get_test_server_url('/very-slow')); // 2 second response
18+
curl_setopt($ch, CURLOPT_URL, "http://192.0.2.1/timeout"); // Non-routable IP for timeout
2019
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
2120
curl_setopt($ch, CURLOPT_TIMEOUT, 1); // 1 second timeout
21+
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 1);
2222

2323
$start_time = microtime(true);
2424
$response = curl_exec($ch);
@@ -45,7 +45,7 @@ function test_normal_request() {
4545
echo "Testing normal request\n";
4646

4747
$ch = curl_init();
48-
curl_setopt($ch, CURLOPT_URL, get_test_server_url('/'));
48+
curl_setopt($ch, CURLOPT_URL, "http://" . PHP_CLI_SERVER_ADDRESS);
4949
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
5050
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
5151

@@ -68,8 +68,6 @@ $normal_coroutine = spawn(test_normal_request(...));
6868
$timeout_result = await($timeout_coroutine);
6969
$normal_result = await($normal_coroutine);
7070

71-
// Stop server
72-
stop_test_server_process($server_pid);
7371

7472
echo "Test end\n";
7573
?>
@@ -82,6 +80,6 @@ Response: timeout
8280
Error present: yes
8381
Error number: %d
8482
HTTP Code: 0
85-
Response: Hello World
83+
Response: Hello world
8684
Error: none
8785
Test end

tests/curl/007-large_response.phpt

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,18 @@ Async cURL large response handling
44
curl
55
--FILE--
66
<?php
7-
include "../common/simple_http_server.php";
7+
include "../../sapi/cli/tests/php_cli_server.inc";
88

99
use function Async\spawn;
1010
use function Async\await;
1111

12-
// Start test server
13-
$server_pid = start_test_server_process(8088);
12+
php_cli_server_start();
1413

1514
function test_large_response() {
16-
echo "Testing large response\n";
15+
echo "Testing response handling\n";
1716

1817
$ch = curl_init();
19-
curl_setopt($ch, CURLOPT_URL, get_test_server_url('/large')); // 10KB response
18+
curl_setopt($ch, CURLOPT_URL, "http://" . PHP_CLI_SERVER_ADDRESS);
2019
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
2120
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
2221

@@ -35,10 +34,8 @@ function test_large_response() {
3534
echo "HTTP Code: $http_code\n";
3635
echo "Error: " . ($error ?: "none") . "\n";
3736
echo "Response length: " . strlen($response) . " bytes\n";
38-
echo "Download size: $download_size bytes\n";
3937
echo "Duration: " . round($duration, 2) . "ms\n";
40-
echo "Response starts with: " . substr($response, 0, 20) . "...\n";
41-
echo "Response ends with: ..." . substr($response, -20) . "\n";
38+
echo "Response: $response\n";
4239

4340
return strlen($response);
4441
}
@@ -48,19 +45,15 @@ echo "Test start\n";
4845
$coroutine = spawn(test_large_response(...));
4946
$size = await($coroutine);
5047

51-
// Stop server
52-
stop_test_server_process($server_pid);
5348

5449
echo "Test end\n";
5550
?>
5651
--EXPECTF--
5752
Test start
58-
Testing large response
53+
Testing response handling
5954
HTTP Code: 200
6055
Error: none
61-
Response length: 10000 bytes
62-
Download size: %f
56+
Response length: %d bytes
6357
Duration: %fms
64-
Response starts with: ABCDEFGHIJABCDEFGHIJ...
65-
Response ends with: ...ABCDEFGHIJABCDEFGHIJ
58+
Response: Hello world
6659
Test end

tests/curl/008-mixed_sync_async.phpt

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,18 @@ Mixed sync and async cURL operations
44
curl
55
--FILE--
66
<?php
7-
include "../common/simple_http_server.php";
7+
include "../../sapi/cli/tests/php_cli_server.inc";
88

99
use function Async\spawn;
1010
use function Async\await;
1111

12-
// Start test server
13-
$server_pid = start_test_server_process(8088);
12+
php_cli_server_start();
1413

1514
function sync_request() {
1615
echo "Sync request start\n";
1716

1817
$ch = curl_init();
19-
curl_setopt($ch, CURLOPT_URL, get_test_server_url('/'));
18+
curl_setopt($ch, CURLOPT_URL, "http://" . PHP_CLI_SERVER_ADDRESS);
2019
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
2120
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
2221

@@ -33,7 +32,7 @@ function async_request() {
3332
echo "Async request start\n";
3433

3534
$ch = curl_init();
36-
curl_setopt($ch, CURLOPT_URL, get_test_server_url('/json'));
35+
curl_setopt($ch, CURLOPT_URL, "http://" . PHP_CLI_SERVER_ADDRESS);
3736
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
3837
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
3938

@@ -73,26 +72,24 @@ $mixed_result = await($mixed_coroutine);
7372
echo "Sync while async: $sync_while_async\n";
7473
echo "Mixed async result: $mixed_result\n";
7574

76-
// Stop server
77-
stop_test_server_process($server_pid);
7875

7976
echo "Test end\n";
8077
?>
8178
--EXPECT--
8279
Test start
8380
Sync request start
8481
Sync request complete: HTTP 200
85-
Sync result: Hello World
82+
Sync result: Hello world
8683
Async request start
8784
Async request complete: HTTP 200
88-
Async result: {"message":"Hello JSON","status":"ok"}
85+
Async result: Hello world
8986
Mixed execution start
9087
In coroutine: making async request
9188
Making sync request while coroutine runs
9289
Async request start
9390
Sync request start
9491
Sync request complete: HTTP 200
9592
Async request complete: HTTP 200
96-
Sync while async: Hello World
97-
Mixed async result: {"message":"Hello JSON","status":"ok"}
93+
Sync while async: Hello world
94+
Mixed async result: Hello world
9895
Test end

0 commit comments

Comments
 (0)