Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion system/Cache/ResponseCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);
}
Expand All @@ -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) {
Expand All @@ -140,6 +147,8 @@ public function get(CLIRequest|IncomingRequest $request, ResponseInterface $resp

$response->setBody($output);

$response->setStatusCode($status, $reason);

return $response;
}

Expand Down
17 changes: 17 additions & 0 deletions tests/system/Cache/ResponseCacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
1 change: 1 addition & 0 deletions user_guide_src/source/changelogs/v4.7.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Loading