@@ -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