Skip to content

Commit 2d37665

Browse files
committed
test: persistent curl share handles
I opted to use the existing Caddy testing infrastructure since it supports keepalives, whereas it seems the PHP development server does not. Alternatively I could write just enough of a socket listener to confirm that there is only ever a single connection coming from curl, but I believe it is safe to rely on connect_time being zero when a connection is reused.
1 parent d210959 commit 2d37665

6 files changed

+138
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
--TEST--
2+
Basic curl persistent share handle test
3+
--EXTENSIONS--
4+
curl
5+
--SKIPIF--
6+
<?php
7+
include 'skipif-nocaddy.inc';
8+
?>
9+
--FILE--
10+
<?php
11+
12+
function get_localhost_curl_handle(CurlPersistentShareHandle $sh): CurlHandle {
13+
$ch = curl_init("https://localhost");
14+
15+
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
16+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
17+
curl_setopt($ch, CURLOPT_SHARE, $sh);
18+
19+
return $ch;
20+
}
21+
22+
$sh1 = curl_persistent_share_init([CURL_LOCK_DATA_CONNECT, CURL_LOCK_DATA_DNS]);
23+
$ch1 = get_localhost_curl_handle($sh1);
24+
25+
// Expect the two options we set above, but sorted.
26+
var_dump($sh1->options);
27+
28+
$sh2 = curl_persistent_share_init([CURL_LOCK_DATA_DNS, CURL_LOCK_DATA_CONNECT]);
29+
$ch2 = get_localhost_curl_handle($sh2);
30+
31+
// Expect the connect time on the subsequent request to be zero, since it's reusing the connection.
32+
var_dump(curl_exec($ch1));
33+
var_dump(curl_exec($ch2));
34+
var_dump("second connect_time: " . (curl_getinfo($ch2)["connect_time"]));
35+
36+
?>
37+
--EXPECT--
38+
array(2) {
39+
[0]=>
40+
int(3)
41+
[1]=>
42+
int(5)
43+
}
44+
string(23) "Caddy is up and running"
45+
string(23) "Caddy is up and running"
46+
string(22) "second connect_time: 0"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
--TEST--
2+
Curl persistent share handle test with different options
3+
--EXTENSIONS--
4+
curl
5+
--SKIPIF--
6+
<?php
7+
include 'skipif-nocaddy.inc';
8+
?>
9+
--FILE--
10+
<?php
11+
12+
function get_localhost_curl_handle(CurlPersistentShareHandle $sh): CurlHandle {
13+
$ch = curl_init("https://localhost");
14+
15+
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
16+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
17+
curl_setopt($ch, CURLOPT_SHARE, $sh);
18+
19+
return $ch;
20+
}
21+
22+
$sh1 = curl_persistent_share_init([CURL_LOCK_DATA_DNS, CURL_LOCK_DATA_CONNECT]);
23+
$ch1 = get_localhost_curl_handle($sh1);
24+
25+
// Note that we're using different share options and thus should get a different persistent share
26+
// handle.
27+
$sh2 = curl_persistent_share_init([CURL_LOCK_DATA_CONNECT]);
28+
$ch2 = get_localhost_curl_handle($sh2);
29+
30+
var_dump($sh1->options != $sh2->options);
31+
32+
// Expect the connect time on the subsequent request to be non-zero, since it's reusing the connection.
33+
var_dump(curl_exec($ch1));
34+
var_dump(curl_exec($ch2));
35+
var_dump("second connect_time: " . (curl_getinfo($ch2)["connect_time"]));
36+
37+
?>
38+
--EXPECTREGEX--
39+
bool\(true\)
40+
string\(23\) "Caddy is up and running"
41+
string\(23\) "Caddy is up and running"
42+
string\(\d+\) "second connect_time: ([0-9]*[.])?[0-9]*[1-9][0-9]*"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
--TEST--
2+
Curl persistent share handle test with invalid option
3+
--EXTENSIONS--
4+
curl
5+
--FILE--
6+
<?php
7+
8+
$sh = curl_persistent_share_init([CURL_LOCK_DATA_DNS, CURL_LOCK_DATA_CONNECT, 30]);
9+
10+
?>
11+
--EXPECTF--
12+
Fatal error: Uncaught Exception: Could not construct persistent cURL share handle: Unknown share option in %s/curl_persistent_share_003.php:3
13+
Stack trace:
14+
#0 %s/curl_persistent_share_003.php(3): curl_persistent_share_init(Array)
15+
#1 {main}
16+
thrown in %s/curl_persistent_share_003.php on line 3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
--TEST--
2+
Curl persistent share handle test with disallowed option
3+
--EXTENSIONS--
4+
curl
5+
--FILE--
6+
<?php
7+
8+
$sh = curl_persistent_share_init([CURL_LOCK_DATA_COOKIE]);
9+
10+
?>
11+
--EXPECTF--
12+
Fatal error: Uncaught Exception: CURL_LOCK_DATA_COOKIE is not allowed with persistent curl share handles in %s/curl_persistent_share_004.php:3
13+
Stack trace:
14+
#0 %s/curl_persistent_share_004.php(3): curl_persistent_share_init(Array)
15+
#1 {main}
16+
thrown in %s/curl_persistent_share_004.php on line 3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
--TEST--
2+
Curl persistent share handles cannot be used with curl_share_setopt
3+
--EXTENSIONS--
4+
curl
5+
--FILE--
6+
<?php
7+
8+
$sh = curl_persistent_share_init([CURL_LOCK_DATA_DNS]);
9+
curl_share_setopt($sh, CURLOPT_SHARE, CURL_LOCK_DATA_CONNECT);
10+
11+
?>
12+
--EXPECTF--
13+
Fatal error: Uncaught TypeError: curl_share_setopt(): Argument #1 ($share_handle) must be of type CurlShareHandle, CurlPersistentShareHandle given in %s/curl_persistent_share_005.php:4
14+
Stack trace:
15+
#0 %s/curl_persistent_share_005.php(4): curl_share_setopt(Object(CurlPersistentShareHandle), %d, %d)
16+
#1 {main}
17+
thrown in %s/curl_persistent_share_005.php on line 4

ext/curl/tests/skipif-nocaddy.inc

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
$ch = curl_init("https://localhost");
44
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
5+
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
56

67
$body = curl_exec($ch);
78

0 commit comments

Comments
 (0)