Skip to content

Commit e31a287

Browse files
committed
Make sure we cache the message body
1 parent 01ea431 commit e31a287

File tree

3 files changed

+33
-10
lines changed

3 files changed

+33
-10
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
"require": {
1414
"php": ">=5.4",
1515
"php-http/httplug": "1.0.0-alpha3",
16-
"php-http/client-tools": "^0.1@dev"
16+
"php-http/client-tools": "^0.1@dev",
17+
"php-http/message-factory": "^1.0"
1718
},
1819
"require-dev": {
1920
"phpspec/phpspec": "^2.4-alpha",

spec/CachePluginSpec.php

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,19 @@
33
namespace spec\Http\Client\Plugin;
44

55
use Http\Client\Tools\Promise\FulfilledPromise;
6+
use Http\Message\StreamFactory;
67
use PhpSpec\ObjectBehavior;
78
use Psr\Cache\CacheItemInterface;
89
use Psr\Cache\CacheItemPoolInterface;
910
use Psr\Http\Message\RequestInterface;
1011
use Psr\Http\Message\ResponseInterface;
12+
use Psr\Http\Message\StreamInterface;
1113

1214
class CachePluginSpec extends ObjectBehavior
1315
{
14-
function let(CacheItemPoolInterface $pool)
16+
function let(CacheItemPoolInterface $pool, StreamFactory $streamFactory)
1517
{
16-
$this->beConstructedWith($pool, ['default_ttl'=>60]);
18+
$this->beConstructedWith($pool, $streamFactory, ['default_ttl'=>60]);
1719
}
1820

1921
function it_is_initializable(CacheItemPoolInterface $pool)
@@ -26,17 +28,21 @@ function it_is_a_plugin()
2628
$this->shouldImplement('Http\Client\Plugin\Plugin');
2729
}
2830

29-
function it_caches_responses(CacheItemPoolInterface $pool, CacheItemInterface $item, RequestInterface $request, ResponseInterface $response)
31+
function it_caches_responses(CacheItemPoolInterface $pool, CacheItemInterface $item, RequestInterface $request, ResponseInterface $response, StreamInterface $stream)
3032
{
33+
$httpBody = 'body';
34+
$stream->__toString()->willReturn($httpBody);
35+
3136
$request->getMethod()->willReturn('GET');
3237
$request->getUri()->willReturn('/');
3338
$response->getStatusCode()->willReturn(200);
39+
$response->getBody()->willReturn($stream);
3440
$response->getHeader('Cache-Control')->willReturn(array());
3541
$response->getHeader('Expires')->willReturn(array());
3642

3743
$pool->getItem('e3b717d5883a45ef9493d009741f7c64')->shouldBeCalled()->willReturn($item);
3844
$item->isHit()->willReturn(false);
39-
$item->set($response)->willReturn($item)->shouldBeCalled();
45+
$item->set(['response' => $response, 'body' => $httpBody])->willReturn($item)->shouldBeCalled();
4046
$item->expiresAfter(60)->willReturn($item)->shouldBeCalled();
4147
$pool->save($item)->shouldBeCalled();
4248

@@ -78,11 +84,15 @@ function it_doesnt_store_post_requests(CacheItemPoolInterface $pool, CacheItemIn
7884
}
7985

8086

81-
function it_calculate_age_from_response(CacheItemPoolInterface $pool, CacheItemInterface $item, RequestInterface $request, ResponseInterface $response)
87+
function it_calculate_age_from_response(CacheItemPoolInterface $pool, CacheItemInterface $item, RequestInterface $request, ResponseInterface $response, StreamInterface $stream)
8288
{
89+
$httpBody = 'body';
90+
$stream->__toString()->willReturn($httpBody);
91+
8392
$request->getMethod()->willReturn('GET');
8493
$request->getUri()->willReturn('/');
8594
$response->getStatusCode()->willReturn(200);
95+
$response->getBody()->willReturn($stream);
8696
$response->getHeader('Cache-Control')->willReturn(array('max-age=40'));
8797
$response->getHeader('Age')->willReturn(array('15'));
8898
$response->getHeader('Expires')->willReturn(array());
@@ -91,7 +101,7 @@ function it_calculate_age_from_response(CacheItemPoolInterface $pool, CacheItemI
91101
$item->isHit()->willReturn(false);
92102

93103
// 40-15 should be 25
94-
$item->set($response)->willReturn($item)->shouldBeCalled();
104+
$item->set(['response' => $response, 'body' => $httpBody])->willReturn($item)->shouldBeCalled();
95105
$item->expiresAfter(25)->willReturn($item)->shouldBeCalled();
96106
$pool->save($item)->shouldBeCalled();
97107

src/CachePlugin.php

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

55
use Http\Client\Tools\Promise\FulfilledPromise;
6+
use Http\Message\StreamFactory;
67
use Psr\Cache\CacheItemPoolInterface;
78
use Psr\Http\Message\RequestInterface;
89
use Psr\Http\Message\ResponseInterface;
@@ -19,6 +20,11 @@ class CachePlugin implements Plugin
1920
*/
2021
private $pool;
2122

23+
/**
24+
* @var StreamFactory
25+
*/
26+
private $streamFactory;
27+
2228
/**
2329
* Default time to store object in cache. This value is used if CachePlugin::respectCacheHeaders is false or
2430
* if cache headers are missing.
@@ -36,11 +42,13 @@ class CachePlugin implements Plugin
3642

3743
/**
3844
* @param CacheItemPoolInterface $pool
45+
* @param StreamFactory $streamFactory
3946
* @param array $options
4047
*/
41-
public function __construct(CacheItemPoolInterface $pool, array $options = [])
48+
public function __construct(CacheItemPoolInterface $pool, StreamFactory $streamFactory, array $options = [])
4249
{
4350
$this->pool = $pool;
51+
$this->streamFactory = $streamFactory;
4452
$this->defaultTtl = isset($options['default_ttl']) ? $options['default_ttl'] : null;
4553
$this->respectCacheHeaders = isset($options['respect_cache_headers']) ? $options['respect_cache_headers'] : true;
4654
}
@@ -63,12 +71,16 @@ public function handleRequest(RequestInterface $request, callable $next, callabl
6371

6472
if ($cacheItem->isHit()) {
6573
// return cached response
66-
return new FulfilledPromise($cacheItem->get());
74+
$data = $cacheItem->get();
75+
$response = $data['response'];
76+
$response = $response->withBody($this->streamFactory->createStream($data['body']));
77+
78+
return new FulfilledPromise($response);
6779
}
6880

6981
return $next($request)->then(function (ResponseInterface $response) use ($cacheItem) {
7082
if ($this->isCacheable($response)) {
71-
$cacheItem->set($response)
83+
$cacheItem->set(['response' => $response, 'body' => $response->getBody()->__toString()])
7284
->expiresAfter($this->getMaxAge($response));
7385
$this->pool->save($cacheItem);
7486
}

0 commit comments

Comments
 (0)