mirror of
https://github.com/php-curl-class/php-curl-class.git
synced 2026-08-25 02:19:08 +00:00
Add test server support for specifying which requests fail and succeed
This commit is contained in:
+28
-7
@@ -377,15 +377,36 @@ if ($test === 'http_basic_auth') {
|
||||
} elseif ($test === 'retry') {
|
||||
session_start();
|
||||
|
||||
if (isset($_SESSION['failures_remaining'])) {
|
||||
$failures_remaining = $_SESSION['failures_remaining'];
|
||||
if (isset($_SESSION['should_fail_entries'])) {
|
||||
$should_fail_entries = $_SESSION['should_fail_entries'];
|
||||
} else {
|
||||
$failures_remaining = (int)$_GET['failures'];
|
||||
$_SESSION['failures_remaining'] = $failures_remaining;
|
||||
// Support specifying which requests fail and succeed (e.g.
|
||||
// http://127.0.0.1:8000/?failures=1,1,0 to fail, fail, succeed).
|
||||
if (strpos($_GET['failures'], ',') !== false) {
|
||||
$should_fail_entries = explode(',', $_GET['failures']);
|
||||
array_walk($should_fail_entries, function (&$value, $key) {
|
||||
if ($value === '1') {
|
||||
$value = true;
|
||||
} elseif ($value === '0') {
|
||||
$value = false;
|
||||
} else {
|
||||
$value = '';
|
||||
}
|
||||
});
|
||||
|
||||
// Support specifying the number of failures before a success (e.g.
|
||||
// http://127.0.0.1:8000/?failures=3).
|
||||
} else {
|
||||
$failure_count = (int)$_GET['failures'];
|
||||
$should_fail_entries = array_fill(0, $failure_count, true);
|
||||
$should_fail_entries[] = false;
|
||||
}
|
||||
}
|
||||
|
||||
if ($failures_remaining >= 1) {
|
||||
$_SESSION['failures_remaining'] -= 1;
|
||||
$should_fail = array_shift($should_fail_entries);
|
||||
$_SESSION['should_fail_entries'] = $should_fail_entries;
|
||||
|
||||
if ($should_fail) {
|
||||
$message = '503 Service Unavailable';
|
||||
} else {
|
||||
$message = '202 Accepted';
|
||||
@@ -393,7 +414,7 @@ if ($test === 'http_basic_auth') {
|
||||
|
||||
$response = json_encode([
|
||||
'message' => $message,
|
||||
'remaining_failures' => $_SESSION['failures_remaining'],
|
||||
'remaining_should_fail_entries' => $_SESSION['should_fail_entries'],
|
||||
], JSON_PRETTY_PRINT);
|
||||
|
||||
header('HTTP/1.1 ' . $message);
|
||||
|
||||
Reference in New Issue
Block a user