diff --git a/system/Cache/ResponseCache.php b/system/Cache/ResponseCache.php index 2c92dc5058be..7f0ea9e1da94 100644 --- a/system/Cache/ResponseCache.php +++ b/system/Cache/ResponseCache.php @@ -102,7 +102,12 @@ public function make(CLIRequest|IncomingRequest $request, ResponseInterface $res return $this->cache->save( $this->generateCacheKey($request), - serialize(['headers' => $headers, 'output' => $response->getBody()]), + serialize([ + 'headers' => $headers, + 'output' => $response->getBody(), + 'status' => $response->getStatusCode(), + 'reason' => $response->getReasonPhrase(), + ]), $this->ttl, ); } @@ -127,6 +132,8 @@ public function get(CLIRequest|IncomingRequest $request, ResponseInterface $resp $headers = $cachedResponse['headers']; $output = $cachedResponse['output']; + $status = $cachedResponse['status'] ?? 200; + $reason = $cachedResponse['reason'] ?? ''; // Clear all default headers foreach (array_keys($response->headers()) as $key) { @@ -140,6 +147,8 @@ public function get(CLIRequest|IncomingRequest $request, ResponseInterface $resp $response->setBody($output); + $response->setStatusCode($status, $reason); + return $response; } diff --git a/tests/system/Cache/ResponseCacheTest.php b/tests/system/Cache/ResponseCacheTest.php index b3f0ec832905..198caefbc015 100644 --- a/tests/system/Cache/ResponseCacheTest.php +++ b/tests/system/Cache/ResponseCacheTest.php @@ -107,6 +107,23 @@ public function testCachePageIncomingRequest(): void $this->assertNotInstanceOf(ResponseInterface::class, $cachedResponse); } + public function testCachePageIncomingRequestWithStatus(): void + { + $pageCache = $this->createResponseCache(); + + $response = new Response(new App()); + $response->setStatusCode(432, 'Foo Bar'); + $response->setBody('The response body.'); + + $this->assertTrue($pageCache->make($this->createIncomingRequest('foo/bar'), $response)); + + // Check cached response status + $cachedResponse = $pageCache->get($this->createIncomingRequest('foo/bar'), new Response(new App())); + $this->assertInstanceOf(ResponseInterface::class, $cachedResponse); + $this->assertSame(432, $cachedResponse->getStatusCode()); + $this->assertSame('Foo Bar', $cachedResponse->getReasonPhrase()); + } + public function testCachePageIncomingRequestWithCacheQueryString(): void { $cache = new Cache(); diff --git a/user_guide_src/source/changelogs/v4.7.0.rst b/user_guide_src/source/changelogs/v4.7.0.rst index 5cfbbf6815f5..bb33537fe556 100644 --- a/user_guide_src/source/changelogs/v4.7.0.rst +++ b/user_guide_src/source/changelogs/v4.7.0.rst @@ -162,6 +162,7 @@ Libraries - **CLI:** Added ``SignalTrait`` to provide unified handling of operating system signals in CLI commands. - **Cache:** Added ``async`` and ``persistent`` config item to Predis handler. - **Cache:** Added ``persistent`` config item to Redis handler. +- **Cache:** Added support for HTTP status in ``ResponseCache``. - **CURLRequest:** Added ``shareConnection`` config item to change default share connection. - **CURLRequest:** Added ``dns_cache_timeout`` option to change default DNS cache timeout. - **CURLRequest:** Added ``fresh_connect`` options to enable/disable request fresh connection.