Skip to content

Commit bfa8e42

Browse files
committed
Use ephemeral ports during curl tests with dev server
1 parent a61a9fe commit bfa8e42

File tree

3 files changed

+35
-11
lines changed

3 files changed

+35
-11
lines changed

ext/curl/tests/CONFLICTS

Lines changed: 0 additions & 1 deletion
This file was deleted.

ext/curl/tests/bug79033.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ var_dump(curl_getinfo($ch)["request_header"]);
2121
string(%d) "array(0) {
2222
}
2323
"
24-
string(90) "POST /get.inc?test=post HTTP/1.1
24+
string(%d) "POST /get.inc?test=post HTTP/1.1
2525
Host: localhost:%d
2626
Accept: */*
2727
Content-Length: 0

ext/curl/tests/server.inc

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
<?php
2-
3-
define ("PHP_CURL_SERVER_HOSTNAME", "localhost");
4-
define ("PHP_CURL_SERVER_PORT", 8964);
5-
define ("PHP_CURL_SERVER_ADDRESS", PHP_CURL_SERVER_HOSTNAME.":".PHP_CURL_SERVER_PORT);
1+
<?php declare(strict_types=1);
62

73
function curl_cli_server_start() {
84
if(getenv('PHP_CURL_HTTP_REMOTE_SERVER')) {
@@ -12,21 +8,50 @@ function curl_cli_server_start() {
128
$php_executable = getenv('TEST_PHP_EXECUTABLE');
139
$doc_root = __DIR__;
1410
$router = "responder/get.inc";
15-
$cmd = [$php_executable, '-t', $doc_root, '-n', '-S', PHP_CURL_SERVER_ADDRESS, $router];
11+
$cmd = [$php_executable, '-t', $doc_root, '-n', '-S', 'localhost:0', $router];
1612
$descriptorspec = array(
1713
0 => STDIN,
1814
1 => STDOUT,
19-
2 => array("null"),
15+
2 => ['pipe', 'w'],
2016
);
2117
$handle = proc_open($cmd, $descriptorspec, $pipes, $doc_root, null, array("suppress_errors" => true));
2218

19+
// First, wait for the dev server to declare itself ready.
20+
$bound = null;
21+
stream_set_blocking($pipes[2], false);
22+
for ($i = 0; $i < 60; $i++) {
23+
usleep(50000); // 50ms per try
24+
$status = proc_get_status($handle);
25+
if (empty($status['running'])) {
26+
echo "Server is not running\n";
27+
proc_terminate($handle);
28+
exit(1);
29+
}
30+
31+
while (($line = fgets($pipes[2])) !== false) {
32+
if (preg_match('@PHP \S* Development Server \(https?://(.*?:\d+)\) started@', $line, $matches)) {
33+
$bound = $matches[1];
34+
// Now that we've identified the listen address, close STDERR.
35+
// Otherwise the pipe may clog up with unread log messages.
36+
fclose($pipes[2]);
37+
break 2;
38+
}
39+
}
40+
}
41+
if ($bound === null) {
42+
echo "Server did not output startup message";
43+
proc_terminate($handle);
44+
exit(1);
45+
}
46+
47+
// Now wait for a connection to succeed.
2348
// note: even when server prints 'Listening on localhost:8964...Press Ctrl-C to quit.'
2449
// it might not be listening yet...need to wait until fsockopen() call returns
2550
$error = "Unable to connect to server\n";
2651
for ($i=0; $i < 60; $i++) {
2752
usleep(50000); // 50ms per try
2853
$status = proc_get_status($handle);
29-
$fp = @fsockopen(PHP_CURL_SERVER_HOSTNAME, PHP_CURL_SERVER_PORT);
54+
$fp = @fsockopen("tcp://$bound");
3055
// Failure, the server is no longer running
3156
if (!($status && $status['running'])) {
3257
$error = "Server is not running\n";
@@ -64,5 +89,5 @@ function curl_cli_server_start() {
6489
$handle
6590
);
6691

67-
return PHP_CURL_SERVER_ADDRESS;
92+
return $bound;
6893
}

0 commit comments

Comments
 (0)