Skip to content

Added support for Last-Modified and ETag #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 19 commits into from
Aug 2, 2016
Merged
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
5 changes: 5 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@
"Http\\Client\\Common\\Plugin\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"spec\\Http\\Client\\Common\\Plugin\\": "spec/"
}
},
"scripts": {
"test": "vendor/bin/phpspec run",
"test-ci": "vendor/bin/phpspec run -c phpspec.ci.yml"
Expand Down
204 changes: 195 additions & 9 deletions spec/CachePluginSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace spec\Http\Client\Common\Plugin;

use Prophecy\Argument;
use Http\Message\StreamFactory;
use Http\Promise\FulfilledPromise;
use PhpSpec\ObjectBehavior;
Expand All @@ -15,7 +16,10 @@ class CachePluginSpec extends ObjectBehavior
{
function let(CacheItemPoolInterface $pool, StreamFactory $streamFactory)
{
$this->beConstructedWith($pool, $streamFactory, ['default_ttl'=>60]);
$this->beConstructedWith($pool, $streamFactory, [
'default_ttl' => 60,
'cache_lifetime' => 1000
]);
}

function it_is_initializable(CacheItemPoolInterface $pool)
Expand All @@ -39,14 +43,22 @@ function it_caches_responses(CacheItemPoolInterface $pool, CacheItemInterface $i
$request->getUri()->willReturn('/');
$response->getStatusCode()->willReturn(200);
$response->getBody()->willReturn($stream);
$response->getHeader('Cache-Control')->willReturn(array());
$response->getHeader('Expires')->willReturn(array());
$response->getHeader('Cache-Control')->willReturn(array())->shouldBeCalled();
$response->getHeader('Expires')->willReturn(array())->shouldBeCalled();
$response->getHeader('ETag')->willReturn(array())->shouldBeCalled();

$pool->getItem('d20f64acc6e70b6079845f2fe357732929550ae1')->shouldBeCalled()->willReturn($item);
$item->isHit()->willReturn(false);
$item->set(['response' => $response, 'body' => $httpBody])->willReturn($item)->shouldBeCalled();
$item->expiresAfter(60)->willReturn($item)->shouldBeCalled();
$pool->save($item)->shouldBeCalled();
$item->expiresAfter(1060)->willReturn($item)->shouldBeCalled();

$item->set($this->getCacheItemMatcher([
'response' => $response->getWrappedObject(),
'body' => $httpBody,
'expiresAt' => 0,
'createdAt' => 0,
'etag' => []
]))->willReturn($item)->shouldBeCalled();
$pool->save(Argument::any())->shouldBeCalled();

$next = function (RequestInterface $request) use ($response) {
return new FulfilledPromise($response->getWrappedObject());
Expand Down Expand Up @@ -100,13 +112,20 @@ function it_calculate_age_from_response(CacheItemPoolInterface $pool, CacheItemI
$response->getHeader('Cache-Control')->willReturn(array('max-age=40'));
$response->getHeader('Age')->willReturn(array('15'));
$response->getHeader('Expires')->willReturn(array());
$response->getHeader('ETag')->willReturn(array());

$pool->getItem('d20f64acc6e70b6079845f2fe357732929550ae1')->shouldBeCalled()->willReturn($item);
$item->isHit()->willReturn(false);

// 40-15 should be 25
$item->set(['response' => $response, 'body' => $httpBody])->willReturn($item)->shouldBeCalled();
$item->expiresAfter(25)->willReturn($item)->shouldBeCalled();
$item->set($this->getCacheItemMatcher([
'response' => $response->getWrappedObject(),
'body' => $httpBody,
'expiresAt' => 0,
'createdAt' => 0,
'etag' => []
]))->willReturn($item)->shouldBeCalled();
// 40-15 should be 25 + the default 1000
$item->expiresAfter(1025)->willReturn($item)->shouldBeCalled();
$pool->save($item)->shouldBeCalled();

$next = function (RequestInterface $request) use ($response) {
Expand All @@ -115,4 +134,171 @@ function it_calculate_age_from_response(CacheItemPoolInterface $pool, CacheItemI

$this->handleRequest($request, $next, function () {});
}

function it_saves_etag(CacheItemPoolInterface $pool, CacheItemInterface $item, RequestInterface $request, ResponseInterface $response, StreamInterface $stream)
{
$httpBody = 'body';
$stream->__toString()->willReturn($httpBody);
$stream->isSeekable()->willReturn(true);
$stream->rewind()->shouldBeCalled();

$request->getMethod()->willReturn('GET');
$request->getUri()->willReturn('/');
$response->getStatusCode()->willReturn(200);
$response->getBody()->willReturn($stream);
$response->getHeader('Cache-Control')->willReturn(array());
$response->getHeader('Expires')->willReturn(array());
$response->getHeader('ETag')->willReturn(array('foo_etag'));

$pool->getItem('d20f64acc6e70b6079845f2fe357732929550ae1')->shouldBeCalled()->willReturn($item);
$item->isHit()->willReturn(false);
$item->expiresAfter(1060)->willReturn($item);

$item->set($this->getCacheItemMatcher([
'response' => $response->getWrappedObject(),
'body' => $httpBody,
'expiresAt' => 0,
'createdAt' => 0,
'etag' => ['foo_etag']
]))->willReturn($item)->shouldBeCalled();
$pool->save(Argument::any())->shouldBeCalled();

$next = function (RequestInterface $request) use ($response) {
return new FulfilledPromise($response->getWrappedObject());
};

$this->handleRequest($request, $next, function () {});
}

function it_adds_etag_and_modfied_since_to_request(CacheItemPoolInterface $pool, CacheItemInterface $item, RequestInterface $request, ResponseInterface $response, StreamInterface $stream)
{
$httpBody = 'body';

$request->getMethod()->willReturn('GET');
$request->getUri()->willReturn('/');

$request->withHeader('If-Modified-Since', 'Thursday, 01-Jan-70 01:18:31 GMT')->shouldBeCalled()->willReturn($request);
$request->withHeader('If-None-Match', 'foo_etag')->shouldBeCalled()->willReturn($request);

$response->getStatusCode()->willReturn(304);

$pool->getItem('d20f64acc6e70b6079845f2fe357732929550ae1')->shouldBeCalled()->willReturn($item);
$item->isHit()->willReturn(true, false);
$item->get()->willReturn([
'response' => $response,
'body' => $httpBody,
'expiresAt' => 0,
'createdAt' => 4711,
'etag' => ['foo_etag']
])->shouldBeCalled();

$next = function (RequestInterface $request) use ($response) {
return new FulfilledPromise($response->getWrappedObject());
};

$this->handleRequest($request, $next, function () {});
}

function it_servces_a_cached_response(CacheItemPoolInterface $pool, CacheItemInterface $item, RequestInterface $request, ResponseInterface $response, StreamInterface $stream, StreamFactory $streamFactory)
{
$httpBody = 'body';

$request->getMethod()->willReturn('GET');
$request->getUri()->willReturn('/');

$pool->getItem('d20f64acc6e70b6079845f2fe357732929550ae1')->shouldBeCalled()->willReturn($item);
$item->isHit()->willReturn(true);
$item->get()->willReturn([
'response' => $response,
'body' => $httpBody,
'expiresAt' => time()+1000000, //It is in the future
'createdAt' => 4711,
'etag' => []
])->shouldBeCalled();

// Make sure we add back the body
$response->withBody($stream)->willReturn($response)->shouldBeCalled();
$streamFactory->createStream($httpBody)->shouldBeCalled()->willReturn($stream);

$next = function (RequestInterface $request) use ($response) {
return new FulfilledPromise($response->getWrappedObject());
};

$this->handleRequest($request, $next, function () {});
}

function it_serves_and_resaved_expired_response(CacheItemPoolInterface $pool, CacheItemInterface $item, RequestInterface $request, ResponseInterface $response, StreamInterface $stream, StreamFactory $streamFactory)
{
$httpBody = 'body';

$request->getMethod()->willReturn('GET');
$request->getUri()->willReturn('/');

$request->withHeader(Argument::any(), Argument::any())->willReturn($request);
$request->withHeader(Argument::any(), Argument::any())->willReturn($request);

$response->getStatusCode()->willReturn(304);
$response->getHeader('Cache-Control')->willReturn(array());
$response->getHeader('Expires')->willReturn(array())->shouldBeCalled();

// Make sure we add back the body
$response->withBody($stream)->willReturn($response)->shouldBeCalled();

$pool->getItem('d20f64acc6e70b6079845f2fe357732929550ae1')->shouldBeCalled()->willReturn($item);
$item->isHit()->willReturn(true, true);
$item->expiresAfter(1060)->willReturn($item)->shouldBeCalled();
$item->get()->willReturn([
'response' => $response,
'body' => $httpBody,
'expiresAt' => 0,
'createdAt' => 4711,
'etag' => ['foo_etag']
])->shouldBeCalled();

$item->set($this->getCacheItemMatcher([
'response' => $response->getWrappedObject(),
'body' => $httpBody,
'expiresAt' => 0,
'createdAt' => 0,
'etag' => ['foo_etag']
]))->willReturn($item)->shouldBeCalled();
$pool->save(Argument::any())->shouldBeCalled();

$streamFactory->createStream($httpBody)->shouldBeCalled()->willReturn($stream);

$next = function (RequestInterface $request) use ($response) {
return new FulfilledPromise($response->getWrappedObject());
};

$this->handleRequest($request, $next, function () {});
}


/**
* Private function to match cache item data.
*
* @param array $expectedData
*
* @return \Closure
*/
private function getCacheItemMatcher(array $expectedData)
{
return Argument::that(function(array $actualData) use ($expectedData) {
foreach ($expectedData as $key => $value) {
if (!isset($actualData[$key])) {
return false;
}

if ($key === 'expiresAt' || $key === 'createdAt') {
// We do not need to validate the value of these fields.
continue;
}

if ($actualData[$key] !== $value) {
return false;
}
}
return true;
});
}
}
Loading