about summary refs log tree commit diff stats
path: root/tests_integration
diff options
context:
space:
mode:
authorAndrew Dolgov <fox@fakecake.org>2026-05-28 06:59:47 +0300
committerAndrew Dolgov <fox@fakecake.org>2026-05-28 06:59:47 +0300
commit78bfc53e084afddcff6450173ae594ee8df279ac (patch)
treeab29a1be6b046e61306393112fb65cc2c9e9d12d /tests_integration
parent69b46e2fd39294d347206edd8b4ea01b749bdb51 (diff)
downloadthe-epube-78bfc53e084afddcff6450173ae594ee8df279ac.tar.gz
the-epube-78bfc53e084afddcff6450173ae594ee8df279ac.tar.xz
try setting content-type conditionally, remove duplicate testcase code
Diffstat (limited to 'tests_integration')
-rw-r--r--tests_integration/ApiTestCase.php67
-rw-r--r--tests_integration/CoverHandlerTest.php16
2 files changed, 28 insertions, 55 deletions
diff --git a/tests_integration/ApiTestCase.php b/tests_integration/ApiTestCase.php
index e1cba9f..90ac357 100644
--- a/tests_integration/ApiTestCase.php
+++ b/tests_integration/ApiTestCase.php
@@ -19,11 +19,14 @@ abstract class ApiTestCase extends TestCase {
 	}
 
 	/**
-	 * Make an HTTP POST request and return [status_code, decoded_body].
+	 * Make an HTTP POST request and return [status_code, decoded_body, headers].
+	 * Includes CSRF token automatically unless passed via $data
+	 *
 	 * @param array<string, string|int> $data
+	 * @param bool $parseJson parse returned body as JSON object
 	 * @return array<int, mixed>
 	 */
-	protected function post(string $endpoint, array $data): array
+	protected function post(string $endpoint, array $data, bool $parseJson = true): array
 	{
 
 		// allow overring automatic csrf_token to fail requests or w/e
@@ -38,54 +41,13 @@ abstract class ApiTestCase extends TestCase {
 			CURLOPT_TIMEOUT        => 10,
 			CURLOPT_CONNECTTIMEOUT => 5,
 			CURLOPT_COOKIEFILE     => $this->cookieFile,
-			CURLOPT_COOKIEJAR      => $this->cookieFile
-		]);
-		$body    = curl_exec($ch);
-		$status  = curl_getinfo($ch, CURLINFO_HTTP_CODE);
-		$errno   = curl_errno($ch);
-		$error   = curl_error($ch);
-		curl_close($ch);
-
-		if ($errno) {
-			$this->fail("cURL error ($errno): $error");
-		}
-
-		/** @var array<string, mixed>|null $decoded */
-		$decoded = json_decode($body, true);
-
-		if (isset($decoded["csrf_token"]))
-			$this->csrfToken = $decoded["csrf_token"];
-
-		return [$status, $decoded ?? []];
-	}
-
-	/**
-	 * Make an HTTP POST request and return [status_code, raw_body, headers].
-	 * Useful for endpoints that return non-JSON (e.g., binary image data).
-	 * @param array<string, string|int> $data
-	 * @return array{0: int, 1: string, 2: array<string, string>}
-	 */
-	protected function postRaw(string $endpoint, array $data): array
-	{
-		// allow overriding automatic csrf_token to fail requests or w/e
-		if ($this->csrfToken && !isset($data['csrf_token']))
-			$data['csrf_token'] = $this->csrfToken;
-
-		$ch = curl_init($endpoint);
-		curl_setopt_array($ch, [
-			CURLOPT_RETURNTRANSFER => true,
-			CURLOPT_POST           => true,
-			CURLOPT_POSTFIELDS     => http_build_query($data),
-			CURLOPT_TIMEOUT        => 10,
-			CURLOPT_CONNECTTIMEOUT => 5,
-			CURLOPT_COOKIEFILE     => $this->cookieFile,
 			CURLOPT_COOKIEJAR      => $this->cookieFile,
 			CURLOPT_HEADER         => true,
 		]);
 		$response = curl_exec($ch);
-		$status   = curl_getinfo($ch, CURLINFO_HTTP_CODE);
-		$errno    = curl_errno($ch);
-		$error    = curl_error($ch);
+		$status  = curl_getinfo($ch, CURLINFO_HTTP_CODE);
+		$errno   = curl_errno($ch);
+		$error   = curl_error($ch);
 		curl_close($ch);
 
 		if ($errno) {
@@ -110,6 +72,17 @@ abstract class ApiTestCase extends TestCase {
 			}
 		}
 
-		return [$status, $body, $headers];
+		if ($parseJson) {
+			/** @var array<string, mixed>|null $decoded */
+			$decoded = json_decode($body, true);
+
+			if (isset($decoded["csrf_token"]))
+				$this->csrfToken = $decoded["csrf_token"];
+
+			return [$status, $decoded ?? [], $headers];
+
+		} else {
+			return [$status, $body, $headers];
+		}
 	}
 }
diff --git a/tests_integration/CoverHandlerTest.php b/tests_integration/CoverHandlerTest.php
index d0fc37c..e1c8c17 100644
--- a/tests_integration/CoverHandlerTest.php
+++ b/tests_integration/CoverHandlerTest.php
@@ -28,10 +28,10 @@ final class CoverHandlerTest extends ApiTestCase
 		]);
 		$this->assertEquals(200, $status);
 
-		[$status, $body, $headers] = $this->postRaw($this->apiUrl, [
+		[$status, $body, $headers] = $this->post($this->apiUrl, [
 			'op' => 'cover',
 			'id' => 2, // Adventures of Huckleberry Finn
-		]);
+		], false);
 
 		$this->assertEquals(200, $status);
 		$this->assertStringContainsString('image/jpeg', $headers['Content-type'] ?? '');
@@ -50,10 +50,10 @@ final class CoverHandlerTest extends ApiTestCase
 		]);
 		$this->assertEquals(200, $status);
 
-		[$status, $body, $headers] = $this->postRaw($this->apiUrl, [
+		[$status, $body, $headers] = $this->post($this->apiUrl, [
 			'op' => 'cover',
 			'id' => 3,
-		]);
+		], false);
 
 		print_r($headers);
 
@@ -116,10 +116,10 @@ final class CoverHandlerTest extends ApiTestCase
 		]);
 		$this->assertEquals(200, $status);
 
-		[$status, $body, $headers] = $this->postRaw($this->apiUrl, [
+		[$status, $body, $headers] = $this->post($this->apiUrl, [
 			'op' => 'cover',
 			'id' => 0,
-		]);
+		], false);
 
 		$this->assertEquals(404, $status);
 	}
@@ -134,10 +134,10 @@ final class CoverHandlerTest extends ApiTestCase
 		]);
 		$this->assertEquals(200, $status);
 
-		[$status, $body, $headers] = $this->postRaw($this->apiUrl, [
+		[$status, $body, $headers] = $this->post($this->apiUrl, [
 			'op' => 'cover',
 			'id' => -1,
-		]);
+		], false);
 
 		$this->assertEquals(404, $status);
 	}