post($this->apiUrl, [ 'op' => 'cover', 'id' => 2, ]); $this->assertEquals(401, $status); $this->assertArrayHasKey('error', $body); } // ── Valid cover ────────────────────────────────────────────────────────── public function test_cover_returns_200_for_existing_cover(): void { // Login [$status, $body] = $this->post($this->apiUrl, [ 'op' => 'perform-login', 'user' => 'test', 'password' => 'test', ]); $this->assertEquals(200, $status); [$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'] ?? ''); $this->assertNotEmpty($body); // JPEG magic bytes $this->assertEquals('ffd8', bin2hex(substr($body, 0, 2))); } public function test_cover_returns_cache_headers(): void { // Login [$status, $body] = $this->post($this->apiUrl, [ 'op' => 'perform-login', 'user' => 'test', 'password' => 'test', ]); $this->assertEquals(200, $status); [$status, $body, $headers] = $this->post($this->apiUrl, [ 'op' => 'cover', 'id' => 3, ], false); $this->assertEquals(200, $status); $this->assertArrayHasKey('Cache-control', $headers); $this->assertStringContainsString('max-age=', $headers['Cache-control'] ?? ''); $this->assertArrayHasKey('Expires', $headers); $this->assertArrayHasKey('Last-Modified', $headers); } // ── Missing cover file ─────────────────────────────────────────────────── public function test_cover_returns_404_for_missing_cover_file(): void { // Login [$status, $body] = $this->post($this->apiUrl, [ 'op' => 'perform-login', 'user' => 'test', 'password' => 'test', ]); $this->assertEquals(200, $status); // Book ID 1 doesn't exist in the Calibre DB, so the handler returns 404 [$status, $body] = $this->post($this->apiUrl, [ 'op' => 'cover', 'id' => 1, ]); $this->assertEquals(404, $status); $this->assertEquals('Not found.', $body['error']); } // ── Missing/invalid ID ─────────────────────────────────────────────────── public function test_cover_without_id(): void { // Login [$status, $body] = $this->post($this->apiUrl, [ 'op' => 'perform-login', 'user' => 'test', 'password' => 'test', ]); $this->assertEquals(200, $status); [$status, $body] = $this->post($this->apiUrl, [ 'op' => 'cover', ]); $this->assertEquals(404, $status); $this->assertEquals('Not found.', $body['error']); } public function test_cover_with_zero_id(): void { // Login [$status, $body] = $this->post($this->apiUrl, [ 'op' => 'perform-login', 'user' => 'test', 'password' => 'test', ]); $this->assertEquals(200, $status); [$status, $body, $headers] = $this->post($this->apiUrl, [ 'op' => 'cover', 'id' => 0, ], false); $this->assertEquals(404, $status); } public function test_cover_with_negative_id(): void { // Login [$status, $body] = $this->post($this->apiUrl, [ 'op' => 'perform-login', 'user' => 'test', 'password' => 'test', ]); $this->assertEquals(200, $status); [$status, $body, $headers] = $this->post($this->apiUrl, [ 'op' => 'cover', 'id' => -1, ], false); $this->assertEquals(404, $status); } }