Skip to content

Commit 1d99639

Browse files
committed
[HttpClient] Consider cached responses without expiration as immediately stale
1 parent 47ed7b5 commit 1d99639

File tree

2 files changed

+52
-5
lines changed

2 files changed

+52
-5
lines changed

CachingHttpClient.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -559,7 +559,7 @@ private function evaluateCacheFreshness(array $data): Freshness
559559
$now = time();
560560
$expires = $data['expires_at'];
561561

562-
if (null === $expires || $now <= $expires) {
562+
if (null !== $expires && $now <= $expires) {
563563
return Freshness::Fresh;
564564
}
565565

Tests/CachingHttpClientTest.php

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,9 @@ public function testItServesResponseFromCache()
9292
$mockClient = new MockHttpClient([
9393
new MockResponse('foo', [
9494
'http_code' => 200,
95+
'response_headers' => [
96+
'Cache-Control' => 'max-age=300',
97+
],
9598
]),
9699
new MockResponse('should not be served'),
97100
]);
@@ -119,6 +122,7 @@ public function testItSupportsVaryHeader()
119122
new MockResponse('foo', [
120123
'http_code' => 200,
121124
'response_headers' => [
125+
'Cache-Control' => 'max-age=300',
122126
'Vary' => 'Foo, Bar',
123127
],
124128
]),
@@ -183,6 +187,34 @@ public function testItDoesntServeAStaleResponse()
183187
self::assertSame('bar', $response->getContent());
184188
}
185189

190+
public function testAResponseWithoutExpirationAsStale()
191+
{
192+
$mockClient = new MockHttpClient([
193+
new MockResponse('foo', [
194+
'http_code' => 200,
195+
'response_headers' => [
196+
'Cache-Control' => 'public',
197+
],
198+
]),
199+
new MockResponse('bar'),
200+
]);
201+
202+
$client = new CachingHttpClient(
203+
$mockClient,
204+
$this->cacheAdapter,
205+
);
206+
207+
// The first request returns "foo".
208+
$response = $client->request('GET', 'http://example.com/foo-bar');
209+
self::assertSame(200, $response->getStatusCode());
210+
self::assertSame('foo', $response->getContent());
211+
212+
// After an extra second the cache expires, so a new response is served.
213+
$response = $client->request('GET', 'http://example.com/foo-bar');
214+
self::assertSame(200, $response->getStatusCode());
215+
self::assertSame('bar', $response->getContent());
216+
}
217+
186218
public function testItRevalidatesAResponseWithNoCacheDirective()
187219
{
188220
$mockClient = new MockHttpClient([
@@ -329,7 +361,7 @@ public function testASharedCacheStoresAResponseWithPublicDirectiveFromRequestWit
329361
new MockResponse('foo', [
330362
'http_code' => 200,
331363
'response_headers' => [
332-
'Cache-Control' => 'public',
364+
'Cache-Control' => 'public, max-age=300',
333365
],
334366
]),
335367
new MockResponse('should not be served'),
@@ -580,7 +612,12 @@ public function testItCanStreamAsyncResponse()
580612
public function testItCanStreamCachedResponse()
581613
{
582614
$mockClient = new MockHttpClient([
583-
new MockResponse('foo', ['http_code' => 200]),
615+
new MockResponse('foo', [
616+
'http_code' => 200,
617+
'response_headers' => [
618+
'Cache-Control' => 'max-age=300',
619+
],
620+
]),
584621
]);
585622

586623
$client = new CachingHttpClient(
@@ -604,7 +641,12 @@ public function testItCanStreamCachedResponse()
604641
public function testItCanStreamBoth()
605642
{
606643
$mockClient = new MockHttpClient([
607-
new MockResponse('foo', ['http_code' => 200]),
644+
new MockResponse('foo', [
645+
'http_code' => 200,
646+
'response_headers' => [
647+
'Cache-Control' => 'max-age=300',
648+
],
649+
]),
608650
new MockResponse('bar', ['http_code' => 200]),
609651
]);
610652

@@ -873,7 +915,12 @@ public function testHeuristicFreshnessWithLastModified()
873915
public function testResponseInfluencingHeadersAffectCacheKey()
874916
{
875917
$mockClient = new MockHttpClient([
876-
new MockResponse('response for en', ['http_code' => 200]),
918+
new MockResponse('response for en', [
919+
'http_code' => 200,
920+
'response_headers' => [
921+
'Cache-Control' => 'max-age=300',
922+
],
923+
]),
877924
new MockResponse('response for fr', ['http_code' => 200]),
878925
]);
879926

0 commit comments

Comments
 (0)