Skip to content

Commit 7521597

Browse files
committed
feat: Added support for HTTP status in ResponseCache
1 parent fe1e944 commit 7521597

2 files changed

Lines changed: 27 additions & 1 deletion

File tree

system/Cache/ResponseCache.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,12 @@ public function make(CLIRequest|IncomingRequest $request, ResponseInterface $res
102102

103103
return $this->cache->save(
104104
$this->generateCacheKey($request),
105-
serialize(['headers' => $headers, 'output' => $response->getBody()]),
105+
serialize([
106+
'headers' => $headers,
107+
'output' => $response->getBody(),
108+
'statuscode' => $response->getStatusCode(),
109+
'reasonphrase' => $response->getReasonPhrase(),
110+
]),
106111
$this->ttl,
107112
);
108113
}
@@ -140,6 +145,10 @@ public function get(CLIRequest|IncomingRequest $request, ResponseInterface $resp
140145

141146
$response->setBody($output);
142147

148+
if (isset($cachedResponse['statuscode'])) {
149+
$response->setStatusCode($cachedResponse['statuscode'], $cachedResponse['reasonphrase'] ?? '');
150+
}
151+
143152
return $response;
144153
}
145154

tests/system/Cache/ResponseCacheTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,23 @@ public function testCachePageIncomingRequest(): void
107107
$this->assertNotInstanceOf(ResponseInterface::class, $cachedResponse);
108108
}
109109

110+
public function testCachePageIncomingRequestWithStatus(): void
111+
{
112+
$pageCache = $this->createResponseCache();
113+
114+
$response = new Response(new App());
115+
$response->setStatusCode(432, 'Foo Bar');
116+
$response->setBody('The response body.');
117+
118+
$this->assertTrue($pageCache->make($this->createIncomingRequest('foo/bar'), $response));
119+
120+
// Check cached response status
121+
$cachedResponse = $pageCache->get($this->createIncomingRequest('foo/bar'), new Response(new App()));
122+
$this->assertInstanceOf(ResponseInterface::class, $cachedResponse);
123+
$this->assertSame(432, $cachedResponse->getStatusCode());
124+
$this->assertSame('Foo Bar', $cachedResponse->getReasonPhrase());
125+
}
126+
110127
public function testCachePageIncomingRequestWithCacheQueryString(): void
111128
{
112129
$cache = new Cache();

0 commit comments

Comments
 (0)