-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathCachePluginSpec.php
More file actions
118 lines (94 loc) · 4.59 KB
/
CachePluginSpec.php
File metadata and controls
118 lines (94 loc) · 4.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
<?php
namespace spec\Http\Client\Plugin;
use Http\Message\StreamFactory;
use Http\Promise\FulfilledPromise;
use PhpSpec\ObjectBehavior;
use Psr\Cache\CacheItemInterface;
use Psr\Cache\CacheItemPoolInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\StreamInterface;
class CachePluginSpec extends ObjectBehavior
{
function let(CacheItemPoolInterface $pool, StreamFactory $streamFactory)
{
$this->beConstructedWith($pool, $streamFactory, ['default_ttl'=>60]);
}
function it_is_initializable(CacheItemPoolInterface $pool)
{
$this->shouldHaveType('Http\Client\Plugin\CachePlugin');
}
function it_is_a_plugin()
{
$this->shouldImplement('Http\Client\Plugin\Plugin');
}
function it_caches_responses(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([]);
$response->getHeader('Expires')->willReturn([]);
$pool->getItem('e3b717d5883a45ef9493d009741f7c64')->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();
$next = function (RequestInterface $request) use ($response) {
return new FulfilledPromise($response->getWrappedObject());
};
$this->handleRequest($request, $next, function () {});
}
function it_doesnt_store_failed_responses(CacheItemPoolInterface $pool, CacheItemInterface $item, RequestInterface $request, ResponseInterface $response)
{
$request->getMethod()->willReturn('GET');
$request->getUri()->willReturn('/');
$response->getStatusCode()->willReturn(400);
$response->getHeader('Cache-Control')->willReturn([]);
$response->getHeader('Expires')->willReturn([]);
$pool->getItem('e3b717d5883a45ef9493d009741f7c64')->shouldBeCalled()->willReturn($item);
$item->isHit()->willReturn(false);
$next = function (RequestInterface $request) use ($response) {
return new FulfilledPromise($response->getWrappedObject());
};
$this->handleRequest($request, $next, function () {});
}
function it_doesnt_store_post_requests(CacheItemPoolInterface $pool, CacheItemInterface $item, RequestInterface $request, ResponseInterface $response)
{
$request->getMethod()->willReturn('POST');
$request->getUri()->willReturn('/');
$next = function (RequestInterface $request) use ($response) {
return new FulfilledPromise($response->getWrappedObject());
};
$this->handleRequest($request, $next, function () {});
}
function it_calculate_age_from_response(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(['max-age=40']);
$response->getHeader('Age')->willReturn(['15']);
$response->getHeader('Expires')->willReturn([]);
$pool->getItem('e3b717d5883a45ef9493d009741f7c64')->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();
$pool->save($item)->shouldBeCalled();
$next = function (RequestInterface $request) use ($response) {
return new FulfilledPromise($response->getWrappedObject());
};
$this->handleRequest($request, $next, function () {});
}
}