From 85c13d9dc6f48f7c60af433e1a981bc00a461a98 Mon Sep 17 00:00:00 2001 From: Eric Norris Date: Mon, 25 Nov 2024 13:11:51 -0500 Subject: [PATCH] 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. --- ext/curl/tests/curl_persistent_share_001.phpt | 46 +++++++++++++++++++ ext/curl/tests/curl_persistent_share_002.phpt | 42 +++++++++++++++++ ext/curl/tests/curl_persistent_share_003.phpt | 16 +++++++ ext/curl/tests/curl_persistent_share_004.phpt | 16 +++++++ ext/curl/tests/curl_persistent_share_005.phpt | 17 +++++++ ext/curl/tests/skipif-nocaddy.inc | 1 + 6 files changed, 138 insertions(+) create mode 100644 ext/curl/tests/curl_persistent_share_001.phpt create mode 100644 ext/curl/tests/curl_persistent_share_002.phpt create mode 100644 ext/curl/tests/curl_persistent_share_003.phpt create mode 100644 ext/curl/tests/curl_persistent_share_004.phpt create mode 100644 ext/curl/tests/curl_persistent_share_005.phpt diff --git a/ext/curl/tests/curl_persistent_share_001.phpt b/ext/curl/tests/curl_persistent_share_001.phpt new file mode 100644 index 0000000000000..99aa363f178ee --- /dev/null +++ b/ext/curl/tests/curl_persistent_share_001.phpt @@ -0,0 +1,46 @@ +--TEST-- +Basic curl persistent share handle test +--EXTENSIONS-- +curl +--SKIPIF-- + +--FILE-- +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" diff --git a/ext/curl/tests/curl_persistent_share_002.phpt b/ext/curl/tests/curl_persistent_share_002.phpt new file mode 100644 index 0000000000000..daa52ad49d391 --- /dev/null +++ b/ext/curl/tests/curl_persistent_share_002.phpt @@ -0,0 +1,42 @@ +--TEST-- +Curl persistent share handle test with different options +--EXTENSIONS-- +curl +--SKIPIF-- + +--FILE-- +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].*" diff --git a/ext/curl/tests/curl_persistent_share_003.phpt b/ext/curl/tests/curl_persistent_share_003.phpt new file mode 100644 index 0000000000000..81a371221b7d6 --- /dev/null +++ b/ext/curl/tests/curl_persistent_share_003.phpt @@ -0,0 +1,16 @@ +--TEST-- +Curl persistent share handle test with invalid option +--EXTENSIONS-- +curl +--FILE-- + +--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 diff --git a/ext/curl/tests/curl_persistent_share_004.phpt b/ext/curl/tests/curl_persistent_share_004.phpt new file mode 100644 index 0000000000000..2c1a0b6c5278b --- /dev/null +++ b/ext/curl/tests/curl_persistent_share_004.phpt @@ -0,0 +1,16 @@ +--TEST-- +Curl persistent share handle test with disallowed option +--EXTENSIONS-- +curl +--FILE-- + +--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 diff --git a/ext/curl/tests/curl_persistent_share_005.phpt b/ext/curl/tests/curl_persistent_share_005.phpt new file mode 100644 index 0000000000000..ab2811f5e7605 --- /dev/null +++ b/ext/curl/tests/curl_persistent_share_005.phpt @@ -0,0 +1,17 @@ +--TEST-- +Curl persistent share handles cannot be used with curl_share_setopt +--EXTENSIONS-- +curl +--FILE-- + +--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 diff --git a/ext/curl/tests/skipif-nocaddy.inc b/ext/curl/tests/skipif-nocaddy.inc index 21a06c12af106..ae5442ff28ede 100644 --- a/ext/curl/tests/skipif-nocaddy.inc +++ b/ext/curl/tests/skipif-nocaddy.inc @@ -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);