14 use Doctrine\Common\Cache\ArrayCache;
26 $this->assertInstanceOf(
'Guzzle\Plugin\Cache\CacheStorageInterface', $this->readAttribute($plugin,
'storage'));
33 'storage' => $this->getMockBuilder(
'Guzzle\Plugin\Cache\CacheStorageInterface')->getMockForAbstractClass()
35 $this->assertInstanceOf(
'Guzzle\Plugin\Cache\CacheStorageInterface', $this->readAttribute($plugin,
'storage'));
36 $this->assertInstanceOf(
37 'Guzzle\Plugin\Cache\CanCacheStrategyInterface',
38 $this->readAttribute($plugin,
'canCache')
40 $this->assertInstanceOf(
41 'Guzzle\Plugin\Cache\RevalidationInterface',
42 $this->readAttribute($plugin,
'revalidation')
49 $plugin =
new CachePlugin(array(
'can_cache' =>
function () {}));
50 $this->assertInstanceOf(
51 'Guzzle\Plugin\Cache\CallbackCanCacheStrategy',
52 $this->readAttribute($plugin,
'canCache')
65 'adapter' => $this->getMockBuilder(
'Guzzle\Cache\CacheAdapterInterface')->getMockForAbstractClass()
67 $this->assertInstanceOf(
'Guzzle\Plugin\Cache\CacheStorageInterface', $this->readAttribute($plugin,
'storage'));
72 $can = $this->getMockBuilder(
'Guzzle\Plugin\Cache\CanCacheStrategyInterface')->getMockForAbstractClass();
73 $revalidate = $this->getMockBuilder(
'Guzzle\Plugin\Cache\RevalidationInterface')->getMockForAbstractClass();
75 'storage' => $this->getMockBuilder(
'Guzzle\Plugin\Cache\CacheStorageInterface')->getMockForAbstractClass(),
77 'revalidation' => $revalidate
79 $this->assertSame($can, $this->readAttribute($plugin,
'canCache'));
80 $this->assertSame($revalidate, $this->readAttribute($plugin,
'revalidation'));
85 $req1 =
new Request(
'GET',
'http://foo.com', array(
'Cache-Control' =>
'no-cache'));
89 array(
new Request(
'GET',
'http://foo.com', array(
'Cache-Control' =>
'max-age=20')),
new Response(200, array(
'Age' => 100)),
false,
false),
91 array(
new Request(
'GET',
'http://foo.com'),
new Response(200, array(
'Cache-Control' =>
'max-age=10',
'Age' => 100)),
false,
false),
93 array(
new Request(
'GET',
'http://foo.com', array(
'Cache-Control' =>
'max-stale=15')),
new Response(200, array(
'Cache-Control' =>
'max-age=90',
'Age' => 100)),
true,
false),
95 array(
new Request(
'GET',
'http://foo.com', array(
'Cache-Control' =>
'max-stale=5')),
new Response(200, array(
'Cache-Control' =>
'max-age=90',
'Age' => 100)),
false,
false),
97 array($req1,
new Response(200),
true,
true),
101 'Expires' => date(
'c', strtotime(
'+1 year'))
111 $didRevalidate =
false;
112 $storage = $this->getMockBuilder(
'Guzzle\Plugin\Cache\CacheStorageInterface')->getMockForAbstractClass();
113 $revalidate = $this->getMockBuilder(
'Guzzle\Plugin\Cache\DefaultRevalidation')
114 ->setMethods(array(
'revalidate'))
115 ->setConstructorArgs(array($storage))
116 ->getMockForAbstractClass();
118 $revalidate->expects($this->any())
119 ->method(
'revalidate')
120 ->will($this->returnCallback(
function () use (&$didRevalidate) {
121 $didRevalidate =
true;
126 'storage' => $storage,
127 'revalidation' => $revalidate
130 $this->assertEquals($can, $plugin->canResponseSatisfyRequest($request, $response));
131 $this->assertEquals($didRevalidate, $revalidates);
138 array(
new Request(
'GET',
'http://foo.com', array()),
new Response(200, array(
'Age' => 100)),
false),
140 array(
new Request(
'GET',
'http://foo.com', array(
'Cache-Control' =>
'stale-if-error')),
new Response(200, array(
'Age' => 100,
'Cache-Control' =>
'max-age=50')),
true),
142 array(
new Request(
'GET',
'http://foo.com', array(
'Cache-Control' =>
'stale-if-error=50')),
new Response(200, array(
'Age' => 100,
'Cache-Control' =>
'max-age=50')),
true),
144 array(
new Request(
'GET',
'http://foo.com', array(
'Cache-Control' =>
'stale-if-error=20')),
new Response(200, array(
'Age' => 100,
'Cache-Control' =>
'max-age=50')),
false),
146 array(
new Request(
'GET',
'http://foo.com', array()),
new Response(200, array(
'Age' => 100,
'Cache-Control' =>
'max-age=50, stale-if-error', )),
true),
148 array(
new Request(
'GET',
'http://foo.com', array()),
new Response(200, array(
'Age' => 100,
'Cache-Control' =>
'max-age=50, stale-if-error=50')),
true),
150 array(
new Request(
'GET',
'http://foo.com', array()),
new Response(200, array(
'Age' => 100,
'Cache-Control' =>
'max-age=50, stale-if-error=20')),
false),
152 array(
new Request(
'GET',
'http://foo.com', array(
'Cache-Control' =>
'stale-if-error=50')),
new Response(200, array(
'Age' => 100,
'Cache-Control' =>
'max-age=50, stale-if-error=20')),
false),
154 array(
new Request(
'GET',
'http://foo.com', array(
'Cache-Control' =>
'stale-if-error=20')),
new Response(200, array(
'Age' => 100,
'Cache-Control' =>
'max-age=50, stale-if-error=50')),
false),
165 $this->assertEquals($can, $plugin->canResponseSatisfyFailedRequest($request, $response));
170 $storage = $this->getMockBuilder(
'Guzzle\Plugin\Cache\CacheStorageInterface')
171 ->setMethods(array(
'fetch'))
172 ->getMockForAbstractClass();
173 $storage->expects($this->never())->method(
'fetch');
176 'storage' => $storage,
180 $plugin->onRequestBeforeSend(
new Event(array(
181 'request' =>
new Request(
'GET',
'http://foo.com')
187 $date = new \DateTime(
'-10 seconds');
191 array(
new Response(200, array(),
'foo')),
193 array(
new Response(200, array(
'Date' => $date->format(
'c'),
'Cache-Control' =>
'max-age=5'),
'foo'))
202 $storage = $this->getMockBuilder(
'Guzzle\Plugin\Cache\CacheStorageInterface')
203 ->setMethods(array(
'fetch'))
204 ->getMockForAbstractClass();
206 $storage->expects($this->once())->method(
'fetch')->will($this->returnValue($response));
207 $plugin =
new CachePlugin(array(
'storage' => $storage));
208 $request =
new Request(
'GET',
'http://foo.com', array(
'Cache-Control' =>
'max-stale'));
209 $plugin->onRequestBeforeSend(
new Event(array(
'request' => $request)));
210 $plugin->onRequestSent(
new Event(array(
'request' => $request,
'response' => $request->getResponse())));
211 $this->assertEquals($response->getStatusCode(), $request->getResponse()->getStatusCode());
212 $this->assertEquals((
string) $response->getBody(), (
string) $request->getResponse()->getBody());
213 $this->assertTrue($request->getResponse()->hasHeader(
'Age'));
214 if ($request->getResponse()->isFresh() ===
false) {
215 $this->assertContains(
'110', (
string) $request->getResponse()->getHeader(
'Warning'));
218 sprintf(
'%s GuzzleCache/%s', $request->getProtocolVersion(),
Version::VERSION),
219 (
string) $request->getHeader(
'Via')
222 sprintf(
'%s GuzzleCache/%s',$request->getProtocolVersion(),
Version::VERSION),
223 (
string) $request->getResponse()->getHeader(
'Via')
225 $this->assertTrue($request->getParams()->get(
'cache.lookup'));
226 $this->assertTrue($request->getParams()->get(
'cache.hit'));
227 $this->assertTrue($request->getResponse()->hasHeader(
'X-Cache-Lookup'));
228 $this->assertTrue($request->getResponse()->hasHeader(
'X-Cache'));
229 $this->assertEquals(
'HIT from GuzzleCache', (
string) $request->getResponse()->getHeader(
'X-Cache'));
230 $this->assertEquals(
'HIT from GuzzleCache', (
string) $request->getResponse()->getHeader(
'X-Cache-Lookup'));
235 $date = new \DateTime(
'-10 seconds');
239 'Date' => $date->format(
'c'),
240 'Cache-Control' =>
'max-age=5, stale-if-error'
251 $storage = $this->getMockBuilder(
'Guzzle\Plugin\Cache\CacheStorageInterface')
252 ->setMethods(array(
'fetch'))
253 ->getMockForAbstractClass();
254 $storage->expects($this->exactly(2))->method(
'fetch')->will($this->returnValue($cacheResponse));
255 $plugin =
new CachePlugin(array(
'storage' => $storage));
256 $request =
new Request(
'GET',
'http://foo.com', array(
'Cache-Control' =>
'max-stale'));
257 $plugin->onRequestBeforeSend(
new Event(array(
'request' => $request)));
258 $plugin->onRequestError(
259 $event =
new Event(array(
260 'request' => $request,
261 'response' => $request->getResponse(),
264 $response = $event[
'response'];
265 $this->assertEquals($cacheResponse->getStatusCode(), $response->getStatusCode());
266 $this->assertEquals((
string) $cacheResponse->getBody(), (
string) $response->getBody());
267 $this->assertTrue($response->hasHeader(
'Age'));
268 if ($response->isFresh() ===
false) {
269 $this->assertContains(
'110', (
string) $response->getHeader(
'Warning'));
271 $this->assertSame(sprintf(
'%s GuzzleCache/%s', $request->getProtocolVersion(),
Version::VERSION), (
string) $request->getHeader(
'Via'));
272 $this->assertSame(sprintf(
'%s GuzzleCache/%s', $request->getProtocolVersion(),
Version::VERSION), (
string) $response->getHeader(
'Via'));
273 $this->assertTrue($request->getParams()->get(
'cache.lookup'));
274 $this->assertSame(
'error', $request->getParams()->get(
'cache.hit'));
275 $this->assertTrue($response->hasHeader(
'X-Cache-Lookup'));
276 $this->assertTrue($response->hasHeader(
'X-Cache'));
277 $this->assertEquals(
'HIT from GuzzleCache', (
string) $response->getHeader(
'X-Cache-Lookup'));
278 $this->assertEquals(
'HIT_ERROR from GuzzleCache', (
string) $response->getHeader(
'X-Cache'));
286 $storage = $this->getMockBuilder(
'Guzzle\Plugin\Cache\CacheStorageInterface')
287 ->setMethods(array(
'fetch'))
288 ->getMockForAbstractClass();
289 $storage->expects($this->exactly(2))->method(
'fetch')->will($this->returnValue($cacheResponse));
290 $plugin =
new CachePlugin(array(
'storage' => $storage));
291 $request =
new Request(
'GET',
'http://foo.com', array(
'Cache-Control' =>
'max-stale'));
292 $plugin->onRequestBeforeSend(
new Event(array(
293 'request' => $request
295 $plugin->onRequestException(
297 'request' => $request,
298 'response' => $request->getResponse(),
299 'exception' => $this->getMock(
'Guzzle\Http\Exception\CurlException'),
302 $plugin->onRequestSent(
304 'request' => $request,
305 'response' => $response = $request->getResponse(),
308 $this->assertEquals($cacheResponse->getStatusCode(), $response->getStatusCode());
309 $this->assertEquals((
string) $cacheResponse->getBody(), (
string) $response->getBody());
310 $this->assertTrue($response->hasHeader(
'Age'));
311 if ($response->isFresh() ===
false) {
312 $this->assertContains(
'110', (
string) $response->getHeader(
'Warning'));
314 $this->assertSame(sprintf(
'%s GuzzleCache/%s', $request->getProtocolVersion(),
Version::VERSION), (
string) $request->getHeader(
'Via'));
315 $this->assertSame(sprintf(
'%s GuzzleCache/%s', $request->getProtocolVersion(),
Version::VERSION), (
string) $response->getHeader(
'Via'));
316 $this->assertTrue($request->getParams()->get(
'cache.lookup'));
317 $this->assertSame(
'error', $request->getParams()->get(
'cache.hit'));
318 $this->assertTrue($response->hasHeader(
'X-Cache-Lookup'));
319 $this->assertTrue($response->hasHeader(
'X-Cache'));
320 $this->assertEquals(
'HIT from GuzzleCache', (
string) $response->getHeader(
'X-Cache-Lookup'));
321 $this->assertEquals(
'HIT_ERROR from GuzzleCache', (
string) $response->getHeader(
'X-Cache'));
326 $date = new \DateTime(
'-10 seconds');
332 array(
'Cache-Control' =>
'no-store'),
333 new Response(200, array(
'Date' => $date->format(
'D, d M Y H:i:s T'),
'Cache-Control' =>
'max-age=5, stale-if-error'),
'foo'),
338 array(
'Cache-Control' =>
'stale-if-error=4'),
339 new Response(200, array(
'Date' => $date->format(
'D, d M Y H:i:s T'),
'Cache-Control' =>
'max-age=5, stale-if-error'),
'foo'),
344 array(
'Cache-Control' =>
'stale-if-error'),
345 new Response(200, array(
'Date' => $date->format(
'D, d M Y H:i:s T'),
'Cache-Control' =>
'max-age=5, stale-if-error=4'),
'foo'),
355 $storage = $this->getMockBuilder(
'Guzzle\Plugin\Cache\CacheStorageInterface')
356 ->setMethods(array(
'fetch'))
357 ->getMockForAbstractClass();
358 $storage->expects($this->exactly($requestCanCache ? 2 : 0))->method(
'fetch')->will($this->returnValue($cacheResponse));
359 $plugin =
new CachePlugin(array(
'storage' => $storage));
360 $request =
new Request(
'GET',
'http://foo.com', $requestHeaders);
361 $plugin->onRequestBeforeSend(
new Event(array(
362 'request' => $request
364 $plugin->onRequestError(
365 $event =
new Event(array(
366 'request' => $request,
367 'response' => $response = $request->getResponse(),
371 $this->assertSame($response, $event[
'response']);
379 $storage = $this->getMockBuilder(
'Guzzle\Plugin\Cache\CacheStorageInterface')
380 ->setMethods(array(
'fetch'))
381 ->getMockForAbstractClass();
382 $storage->expects($this->exactly($requestCanCache ? 2 : 0))->method(
'fetch')->will($this->returnValue($responseParts));
383 $plugin =
new CachePlugin(array(
'storage' => $storage));
384 $request =
new Request(
'GET',
'http://foo.com', $requestHeaders);
385 $plugin->onRequestBeforeSend(
new Event(array(
386 'request' => $request
388 $plugin->onRequestException(
389 $event =
new Event(array(
390 'request' => $request,
391 'response' => $response = $request->getResponse(),
392 'exception' => $this->getMock(
'Guzzle\Http\Exception\CurlException'),
396 $this->assertSame($response, $request->getResponse());
401 $cache =
new ArrayCache();
404 $request =
new Request(
'GET',
'http://foo.com');
405 $response =
new Response(200, array(),
'Foo');
406 $plugin->onRequestBeforeSend(
new Event(array(
407 'request' => $request
409 $plugin->onRequestSent(
new Event(array(
410 'request' => $request,
411 'response' => $response
413 $data = $this->readAttribute($cache,
'data');
414 $this->assertNotEmpty($data);
419 $storage = $this->getMockBuilder(
'Guzzle\Plugin\Cache\CacheStorageInterface')
420 ->setMethods(array(
'purge'))
421 ->getMockForAbstractClass();
422 $storage->expects($this->atLeastOnce())->method(
'purge');
423 $plugin =
new CachePlugin(array(
'storage' => $storage));
424 $request =
new Request(
'GET',
'http://foo.com', array(
'X-Foo' =>
'Bar'));
425 $plugin->purge($request);
430 $storage = $this->getMockBuilder(
'Guzzle\Plugin\Cache\CacheStorageInterface')
431 ->setMethods(array(
'purge'))
432 ->getMockForAbstractClass();
433 $storage->expects($this->atLeastOnce())->method(
'purge');
434 $plugin =
new CachePlugin(array(
'storage' => $storage,
'auto_purge' =>
true));
436 $request = $client->put(
'http://foo.com', array(
'X-Foo' =>
'Bar'));
437 $request->addSubscriber($plugin);
438 $request->setResponse(
new Response(200),
true);