-
Notifications
You must be signed in to change notification settings - Fork 7.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
1 parent
d210959
commit 2d37665
Showing
6 changed files
with
138 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: ([0-9]*[.])?[0-9]*[1-9][0-9]*" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 %s/curl_persistent_share_003.php:3 | ||
Stack trace: | ||
#0 %s/curl_persistent_share_003.php(3): curl_persistent_share_init(Array) | ||
#1 {main} | ||
thrown in %s/curl_persistent_share_003.php on line 3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 %s/curl_persistent_share_004.php:3 | ||
Stack trace: | ||
#0 %s/curl_persistent_share_004.php(3): curl_persistent_share_init(Array) | ||
#1 {main} | ||
thrown in %s/curl_persistent_share_004.php on line 3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 %s/curl_persistent_share_005.php:4 | ||
Stack trace: | ||
#0 %s/curl_persistent_share_005.php(4): curl_share_setopt(Object(CurlPersistentShareHandle), %d, %d) | ||
#1 {main} | ||
thrown in %s/curl_persistent_share_005.php on line 4 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters