-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathCachePluginSpec.php
380 lines (310 loc) · 14.8 KB
/
CachePluginSpec.php
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
<?php
namespace spec\Http\Client\Common\Plugin;
use Prophecy\Argument;
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;
use Symfony\Component\OptionsResolver\Exception\InvalidOptionsException;
class CachePluginSpec extends ObjectBehavior
{
function let(CacheItemPoolInterface $pool, StreamFactory $streamFactory)
{
$this->beConstructedWith($pool, $streamFactory, [
'default_ttl' => 60,
'cache_lifetime' => 1000
]);
}
function it_is_initializable(CacheItemPoolInterface $pool)
{
$this->shouldHaveType('Http\Client\Common\Plugin\CachePlugin');
}
function it_is_a_plugin()
{
$this->shouldImplement('Http\Client\Common\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('/');
$request->getBody()->shouldBeCalled();
$response->getStatusCode()->willReturn(200);
$response->getBody()->willReturn($stream);
$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->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());
};
$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('/');
$request->getBody()->shouldBeCalled();
$response->getStatusCode()->willReturn(400);
$response->getHeader('Cache-Control')->willReturn(array());
$response->getHeader('Expires')->willReturn(array());
$pool->getItem('d20f64acc6e70b6079845f2fe357732929550ae1')->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_by_default(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_stores_post_requests_when_allowed(
CacheItemPoolInterface $pool,
CacheItemInterface $item,
RequestInterface $request,
ResponseInterface $response,
StreamFactory $streamFactory,
StreamInterface $stream
) {
$this->beConstructedWith($pool, $streamFactory, [
'default_ttl' => 60,
'cache_lifetime' => 1000,
'methods' => ['GET', 'HEAD', 'POST']
]);
$httpBody = 'hello=world';
$stream->__toString()->willReturn($httpBody);
$stream->isSeekable()->willReturn(true);
$stream->rewind()->shouldBeCalled();
$request->getMethod()->willReturn('POST');
$request->getUri()->willReturn('/post');
$request->getBody()->willReturn($stream);
$response->getStatusCode()->willReturn(200);
$response->getBody()->willReturn($stream);
$response->getHeader('Cache-Control')->willReturn([])->shouldBeCalled();
$response->getHeader('Expires')->willReturn([])->shouldBeCalled();
$response->getHeader('ETag')->willReturn([])->shouldBeCalled();
$pool->getItem('e37195334979e7ca0dda534c48a02c7de8368d64')->shouldBeCalled()->willReturn($item);
$item->isHit()->willReturn(false);
$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());
};
$this->handleRequest($request, $next, function () {});
}
function it_does_not_allow_invalid_request_methods(
CacheItemPoolInterface $pool,
CacheItemInterface $item,
RequestInterface $request,
ResponseInterface $response,
StreamFactory $streamFactory,
StreamInterface $stream
) {
$this
->shouldThrow("Symfony\Component\OptionsResolver\Exception\InvalidOptionsException")
->during('__construct', [$pool, $streamFactory, ['methods' => ['GET', 'HEAD', 'POST ']]]);
$this
->shouldThrow("Symfony\Component\OptionsResolver\Exception\InvalidOptionsException")
->during('__construct', [$pool, $streamFactory, ['methods' => ['GET', 'HEAD"', 'POST']]]);
}
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('/');
$request->getBody()->shouldBeCalled();
$response->getStatusCode()->willReturn(200);
$response->getBody()->willReturn($stream);
$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);
$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) {
return new FulfilledPromise($response->getWrappedObject());
};
$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->getBody()->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->getBody()->shouldBeCalled();
$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('/');
$request->getBody()->shouldBeCalled();
$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->getBody()->shouldBeCalled();
$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;
});
}
}