Skip to content

Commit d4a5133

Browse files
committed
Added tests
1 parent c779149 commit d4a5133

File tree

2 files changed

+140
-3
lines changed

2 files changed

+140
-3
lines changed

spec/CachePluginSpec.php

Lines changed: 139 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace spec\Http\Client\Common\Plugin;
44

55
use Prophecy\Argument;
6-
use Prophecy\Comparator\Factory as ComparatorFactory;
76
use Http\Message\StreamFactory;
87
use Http\Promise\FulfilledPromise;
98
use PhpSpec\ObjectBehavior;
@@ -133,9 +132,147 @@ function it_calculate_age_from_response(CacheItemPoolInterface $pool, CacheItemI
133132
$this->handleRequest($request, $next, function () {});
134133
}
135134

135+
function it_saves_etag(CacheItemPoolInterface $pool, CacheItemInterface $item, RequestInterface $request, ResponseInterface $response, StreamInterface $stream)
136+
{
137+
$httpBody = 'body';
138+
$stream->__toString()->willReturn($httpBody);
139+
$stream->isSeekable()->willReturn(true);
140+
$stream->rewind()->shouldBeCalled();
141+
142+
$request->getMethod()->willReturn('GET');
143+
$request->getUri()->willReturn('/');
144+
$response->getStatusCode()->willReturn(200);
145+
$response->getBody()->willReturn($stream);
146+
$response->getHeader('Cache-Control')->willReturn(array());
147+
$response->getHeader('Expires')->willReturn(array());
148+
$response->getHeader('ETag')->willReturn(array('foo_etag'));
149+
150+
$pool->getItem('e3b717d5883a45ef9493d009741f7c64')->shouldBeCalled()->willReturn($item);
151+
$item->isHit()->willReturn(false);
152+
$item->expiresAfter(1060)->willReturn($item);
153+
154+
$item->set($this->getCacheItemMatcher([
155+
'response' => $response->getWrappedObject(),
156+
'body' => $httpBody,
157+
'expiresAt' => 0,
158+
'createdAt' => 0,
159+
'etag' => ['foo_etag']
160+
]))->willReturn($item)->shouldBeCalled();
161+
$pool->save(Argument::any())->shouldBeCalled();
162+
163+
$next = function (RequestInterface $request) use ($response) {
164+
return new FulfilledPromise($response->getWrappedObject());
165+
};
166+
167+
$this->handleRequest($request, $next, function () {});
168+
}
169+
170+
function it_adds_etag_and_modfied_since_to_request(CacheItemPoolInterface $pool, CacheItemInterface $item, RequestInterface $request, ResponseInterface $response, StreamInterface $stream)
171+
{
172+
$httpBody = 'body';
173+
174+
$request->getMethod()->willReturn('GET');
175+
$request->getUri()->willReturn('/');
176+
177+
$request->withHeader('If-Modified-Since', 'Thursday, 01-Jan-70 01:18:31 GMT')->shouldBeCalled()->willReturn($request);
178+
$request->withHeader('If-None-Match', 'foo_etag')->shouldBeCalled()->willReturn($request);
179+
180+
$response->getStatusCode()->willReturn(304);
181+
182+
$pool->getItem('e3b717d5883a45ef9493d009741f7c64')->shouldBeCalled()->willReturn($item);
183+
$item->isHit()->willReturn(true, false);
184+
$item->get()->willReturn([
185+
'response' => $response,
186+
'body' => $httpBody,
187+
'expiresAt' => 0,
188+
'createdAt' => 4711,
189+
'etag' => ['foo_etag']
190+
])->shouldBeCalled();
191+
192+
$next = function (RequestInterface $request) use ($response) {
193+
return new FulfilledPromise($response->getWrappedObject());
194+
};
195+
196+
$this->handleRequest($request, $next, function () {});
197+
}
198+
199+
function it_servces_a_cached_response(CacheItemPoolInterface $pool, CacheItemInterface $item, RequestInterface $request, ResponseInterface $response, StreamInterface $stream, StreamFactory $streamFactory)
200+
{
201+
$httpBody = 'body';
202+
203+
$request->getMethod()->willReturn('GET');
204+
$request->getUri()->willReturn('/');
205+
206+
$pool->getItem('e3b717d5883a45ef9493d009741f7c64')->shouldBeCalled()->willReturn($item);
207+
$item->isHit()->willReturn(true);
208+
$item->get()->willReturn([
209+
'response' => $response,
210+
'body' => $httpBody,
211+
'expiresAt' => time()+1000000, //It is in the future
212+
'createdAt' => 4711,
213+
'etag' => []
214+
])->shouldBeCalled();
215+
216+
// Make sure we add back the body
217+
$response->withBody($stream)->willReturn($response)->shouldBeCalled();
218+
$streamFactory->createStream($httpBody)->shouldBeCalled()->willReturn($stream);
219+
220+
$next = function (RequestInterface $request) use ($response) {
221+
return new FulfilledPromise($response->getWrappedObject());
222+
};
223+
224+
$this->handleRequest($request, $next, function () {});
225+
}
226+
227+
function it_serves_and_resaved_expired_response(CacheItemPoolInterface $pool, CacheItemInterface $item, RequestInterface $request, ResponseInterface $response, StreamInterface $stream, StreamFactory $streamFactory)
228+
{
229+
$httpBody = 'body';
230+
231+
$request->getMethod()->willReturn('GET');
232+
$request->getUri()->willReturn('/');
233+
234+
$request->withHeader(Argument::any(), Argument::any())->willReturn($request);
235+
$request->withHeader(Argument::any(), Argument::any())->willReturn($request);
236+
237+
$response->getStatusCode()->willReturn(304);
238+
$response->getHeader('Cache-Control')->willReturn(array());
239+
$response->getHeader('Expires')->willReturn(array())->shouldBeCalled();
240+
241+
// Make sure we add back the body
242+
$response->withBody($stream)->willReturn($response)->shouldBeCalled();
243+
244+
$pool->getItem('e3b717d5883a45ef9493d009741f7c64')->shouldBeCalled()->willReturn($item);
245+
$item->isHit()->willReturn(true, true);
246+
$item->expiresAfter(1060)->willReturn($item)->shouldBeCalled();
247+
$item->get()->willReturn([
248+
'response' => $response,
249+
'body' => $httpBody,
250+
'expiresAt' => 0,
251+
'createdAt' => 4711,
252+
'etag' => ['foo_etag']
253+
])->shouldBeCalled();
254+
255+
$item->set($this->getCacheItemMatcher([
256+
'response' => $response->getWrappedObject(),
257+
'body' => $httpBody,
258+
'expiresAt' => 0,
259+
'createdAt' => 0,
260+
'etag' => ['foo_etag']
261+
]))->willReturn($item)->shouldBeCalled();
262+
$pool->save(Argument::any())->shouldBeCalled();
263+
264+
$streamFactory->createStream($httpBody)->shouldBeCalled()->willReturn($stream);
265+
266+
$next = function (RequestInterface $request) use ($response) {
267+
return new FulfilledPromise($response->getWrappedObject());
268+
};
269+
270+
$this->handleRequest($request, $next, function () {});
271+
}
272+
136273

137274
/**
138-
* Private function to match requests
275+
* Private function to match cache item data.
139276
*
140277
* @param array $expectedData
141278
*

src/CachePlugin.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public function handleRequest(RequestInterface $request, callable $next, callabl
6969

7070
if ($cacheItem->isHit()) {
7171
$data = $cacheItem->get();
72-
if (isset($data['expiresAt']) && time() > $data['expiresAt']) {
72+
if (isset($data['expiresAt']) && time() < $data['expiresAt']) {
7373
// This item is still valid according to previous cache headers
7474
return new FulfilledPromise($this->createResponseFromCacheItem($cacheItem));
7575
}

0 commit comments

Comments
 (0)