about summary refs log tree commit diff stats
path: root/tests_integration/ApiTestCase.php
diff options
context:
space:
mode:
Diffstat (limited to 'tests_integration/ApiTestCase.php')
-rw-r--r--tests_integration/ApiTestCase.php31
1 files changed, 31 insertions, 0 deletions
diff --git a/tests_integration/ApiTestCase.php b/tests_integration/ApiTestCase.php
index 0cb7a3d..e4a5f1f 100644
--- a/tests_integration/ApiTestCase.php
+++ b/tests_integration/ApiTestCase.php
@@ -14,12 +14,43 @@ abstract class ApiTestCase extends TestCase {
 	}
 
 	/**
+	 * Extract the epube_csrf_token cookie value from a cookie jar file.
+	 */
+	private function extractCsrfToken(?string $cookieFile): ?string
+	{
+		if (!$cookieFile || !file_exists($cookieFile)) {
+			return null;
+		}
+
+		$content = file_get_contents($cookieFile);
+
+		// localhost      FALSE   /       FALSE   1782498271      epube_csrf_token        xyztest123
+		$lines = explode("\n", $content);
+		foreach ($lines as $line) {
+			$parts = explode("\t", $line);
+
+			if (isset($parts[5]) && $parts[5] === 'epube_csrf_token') {
+				return $parts[6] ?? null;
+			}
+		}
+		return null;
+	}
+
+	/**
 	 * Make an HTTP POST request and return [status_code, decoded_body].
+	 * Automatically includes the epube_csrf_token cookie as a POST parameter.
 	 * @param array<string, string> $data
 	 * @return array<int, mixed>
 	 */
 	protected function post(string $endpoint, array $data, ?string $cookieFile = null): array
 	{
+		// Include CSRF token from cookie in POST body if available
+		$csrfToken = $this->extractCsrfToken($cookieFile);
+
+		if ($csrfToken !== null) {
+			$data['csrf_token'] = $csrfToken;
+		}
+
 		$ch = curl_init($endpoint);
 		curl_setopt_array($ch, [
 			CURLOPT_RETURNTRANSFER => true,