Skip to content

Commit

Permalink
test: persistent curl share handles
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ericnorris committed Nov 25, 2024
1 parent 9281130 commit 85c13d9
Show file tree
Hide file tree
Showing 6 changed files with 138 additions and 0 deletions.
46 changes: 46 additions & 0 deletions ext/curl/tests/curl_persistent_share_001.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
--TEST--
Basic curl persistent share handle test
--EXTENSIONS--
curl
--SKIPIF--
<?php
include 'skipif-nocaddy.inc';
?>
--FILE--
<?php

function get_localhost_curl_handle(CurlPersistentShareHandle $sh): CurlHandle {
$ch = curl_init("https://localhost");

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SHARE, $sh);

return $ch;
}

$sh1 = curl_persistent_share_init([CURL_LOCK_DATA_CONNECT, CURL_LOCK_DATA_DNS]);
$ch1 = get_localhost_curl_handle($sh1);

// Expect the two options we set above, but sorted.
var_dump($sh1->options);

$sh2 = curl_persistent_share_init([CURL_LOCK_DATA_DNS, CURL_LOCK_DATA_CONNECT]);
$ch2 = get_localhost_curl_handle($sh2);

// Expect the connect time on the subsequent request to be zero, since it's reusing the connection.
var_dump(curl_exec($ch1));
var_dump(curl_exec($ch2));
var_dump("second connect_time: " . (curl_getinfo($ch2)["connect_time"]));

?>
--EXPECT--
array(2) {
[0]=>
int(3)
[1]=>
int(5)
}
string(23) "Caddy is up and running"
string(23) "Caddy is up and running"
string(22) "second connect_time: 0"
42 changes: 42 additions & 0 deletions ext/curl/tests/curl_persistent_share_002.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
--TEST--
Curl persistent share handle test with different options
--EXTENSIONS--
curl
--SKIPIF--
<?php
include 'skipif-nocaddy.inc';
?>
--FILE--
<?php

function get_localhost_curl_handle(CurlPersistentShareHandle $sh): CurlHandle {
$ch = curl_init("https://localhost");

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SHARE, $sh);

return $ch;
}

$sh1 = curl_persistent_share_init([CURL_LOCK_DATA_DNS, CURL_LOCK_DATA_CONNECT]);
$ch1 = get_localhost_curl_handle($sh1);

// Note that we're using different share options and thus should get a different persistent share
// handle.
$sh2 = curl_persistent_share_init([CURL_LOCK_DATA_CONNECT]);
$ch2 = get_localhost_curl_handle($sh2);

var_dump($sh1->options != $sh2->options);

// Expect the connect time on the subsequent request to be non-zero, since it's reusing the connection.
var_dump(curl_exec($ch1));
var_dump(curl_exec($ch2));
var_dump("second connect_time: " . (curl_getinfo($ch2)["connect_time"]));

?>
--EXPECTREGEX--
bool\(true\)
string\(23\) "Caddy is up and running"
string\(23\) "Caddy is up and running"
string\(\d+\) "second connect_time: .*[1-9].*"
16 changes: 16 additions & 0 deletions ext/curl/tests/curl_persistent_share_003.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
--TEST--
Curl persistent share handle test with invalid option
--EXTENSIONS--
curl
--FILE--
<?php

$sh = curl_persistent_share_init([CURL_LOCK_DATA_DNS, CURL_LOCK_DATA_CONNECT, 30]);

?>
--EXPECTF--
Fatal error: Uncaught Exception: Could not construct persistent cURL share handle: Unknown share option in %scurl_persistent_share_003.php:3
Stack trace:
#0 %scurl_persistent_share_003.php(3): curl_persistent_share_init(Array)
#1 {main}
thrown in %scurl_persistent_share_003.php on line 3
16 changes: 16 additions & 0 deletions ext/curl/tests/curl_persistent_share_004.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
--TEST--
Curl persistent share handle test with disallowed option
--EXTENSIONS--
curl
--FILE--
<?php

$sh = curl_persistent_share_init([CURL_LOCK_DATA_COOKIE]);

?>
--EXPECTF--
Fatal error: Uncaught Exception: CURL_LOCK_DATA_COOKIE is not allowed with persistent curl share handles in %scurl_persistent_share_004.php:3
Stack trace:
#0 %scurl_persistent_share_004.php(3): curl_persistent_share_init(Array)
#1 {main}
thrown in %scurl_persistent_share_004.php on line 3
17 changes: 17 additions & 0 deletions ext/curl/tests/curl_persistent_share_005.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
--TEST--
Curl persistent share handles cannot be used with curl_share_setopt
--EXTENSIONS--
curl
--FILE--
<?php

$sh = curl_persistent_share_init([CURL_LOCK_DATA_DNS]);
curl_share_setopt($sh, CURLOPT_SHARE, CURL_LOCK_DATA_CONNECT);

?>
--EXPECTF--
Fatal error: Uncaught TypeError: curl_share_setopt(): Argument #1 ($share_handle) must be of type CurlShareHandle, CurlPersistentShareHandle given in %scurl_persistent_share_005.php:4
Stack trace:
#0 %scurl_persistent_share_005.php(4): curl_share_setopt(Object(CurlPersistentShareHandle), %d, %d)
#1 {main}
thrown in %scurl_persistent_share_005.php on line 4
1 change: 1 addition & 0 deletions ext/curl/tests/skipif-nocaddy.inc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

$ch = curl_init("https://localhost");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

$body = curl_exec($ch);

Expand Down

0 comments on commit 85c13d9

Please sign in to comment.