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: 381: 382: 383: 384: 385: 386: 387: 388: 389: 390: 391: 392: 393: 394: 395: 396: 397: 398: 399: 400: 401: 402: 403: 404: 405: 406: 407: 408: 409: 410: 411: 412: 413: 414: 415: 416: 417: 418: 419: 420: 421: 422: 423: 424: 425: 426: 427: 428: 429: 430: 431: 432: 433: 434: 435: 436: 437: 438: 439: 440: 441: 442:
<?php
namespace Guzzle\Tests\Plugin\Cache;
use Guzzle\Common\Event;
use Guzzle\Common\Version;
use Guzzle\Cache\DoctrineCacheAdapter;
use Guzzle\Http\Client;
use Guzzle\Http\Message\Request;
use Guzzle\Http\Message\Response;
use Guzzle\Plugin\Cache\CachePlugin;
use Guzzle\Plugin\Cache\DefaultCacheStorage;
use Guzzle\Plugin\Cache\CallbackCanCacheStrategy;
use Doctrine\Common\Cache\ArrayCache;
class CachePluginTest extends \Guzzle\Tests\GuzzleTestCase
{
public function testAddsDefaultStorage()
{
$plugin = new CachePlugin();
$this->assertInstanceOf('Guzzle\Plugin\Cache\CacheStorageInterface', $this->readAttribute($plugin, 'storage'));
}
public function testAddsDefaultCollaborators()
{
$this->assertNotEmpty(CachePlugin::getSubscribedEvents());
$plugin = new CachePlugin(array(
'storage' => $this->getMockBuilder('Guzzle\Plugin\Cache\CacheStorageInterface')->getMockForAbstractClass()
));
$this->assertInstanceOf('Guzzle\Plugin\Cache\CacheStorageInterface', $this->readAttribute($plugin, 'storage'));
$this->assertInstanceOf(
'Guzzle\Plugin\Cache\CanCacheStrategyInterface',
$this->readAttribute($plugin, 'canCache')
);
$this->assertInstanceOf(
'Guzzle\Plugin\Cache\RevalidationInterface',
$this->readAttribute($plugin, 'revalidation')
);
}
public function testAddsCallbackCollaborators()
{
$this->assertNotEmpty(CachePlugin::getSubscribedEvents());
$plugin = new CachePlugin(array('can_cache' => function () {}));
$this->assertInstanceOf(
'Guzzle\Plugin\Cache\CallbackCanCacheStrategy',
$this->readAttribute($plugin, 'canCache')
);
}
public function testCanPassCacheAsOnlyArgumentToConstructor()
{
$p = new CachePlugin(new DoctrineCacheAdapter(new ArrayCache()));
$p = new CachePlugin(new DefaultCacheStorage(new DoctrineCacheAdapter(new ArrayCache())));
}
public function testUsesCreatedCacheStorage()
{
$plugin = new CachePlugin(array(
'adapter' => $this->getMockBuilder('Guzzle\Cache\CacheAdapterInterface')->getMockForAbstractClass()
));
$this->assertInstanceOf('Guzzle\Plugin\Cache\CacheStorageInterface', $this->readAttribute($plugin, 'storage'));
}
public function testUsesProvidedOptions()
{
$can = $this->getMockBuilder('Guzzle\Plugin\Cache\CanCacheStrategyInterface')->getMockForAbstractClass();
$revalidate = $this->getMockBuilder('Guzzle\Plugin\Cache\RevalidationInterface')->getMockForAbstractClass();
$plugin = new CachePlugin(array(
'storage' => $this->getMockBuilder('Guzzle\Plugin\Cache\CacheStorageInterface')->getMockForAbstractClass(),
'can_cache' => $can,
'revalidation' => $revalidate
));
$this->assertSame($can, $this->readAttribute($plugin, 'canCache'));
$this->assertSame($revalidate, $this->readAttribute($plugin, 'revalidation'));
}
public function satisfyProvider()
{
$req1 = new Request('GET', 'http://foo.com', array('Cache-Control' => 'no-cache'));
return array(
array(new Request('GET', 'http://foo.com', array('Cache-Control' => 'max-age=20')), new Response(200, array('Age' => 100)), false, false),
array(new Request('GET', 'http://foo.com'), new Response(200, array('Cache-Control' => 'max-age=10', 'Age' => 100)), false, false),
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),
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),
array($req1, new Response(200), true, true),
array(new Request('GET', 'http://foo.com'), new Response(200, array(
'ETag' => 'ABC',
'Expires' => date('c', strtotime('+1 year'))
)), true, true),
);
}
public function testChecksIfResponseCanSatisfyRequest($request, $response, $can, $revalidates)
{
$didRevalidate = false;
$storage = $this->getMockBuilder('Guzzle\Plugin\Cache\CacheStorageInterface')->getMockForAbstractClass();
$revalidate = $this->getMockBuilder('Guzzle\Plugin\Cache\DefaultRevalidation')
->setMethods(array('revalidate'))
->setConstructorArgs(array($storage))
->getMockForAbstractClass();
$revalidate->expects($this->any())
->method('revalidate')
->will($this->returnCallback(function () use (&$didRevalidate) {
$didRevalidate = true;
return true;
}));
$plugin = new CachePlugin(array(
'storage' => $storage,
'revalidation' => $revalidate
));
$this->assertEquals($can, $plugin->canResponseSatisfyRequest($request, $response));
$this->assertEquals($didRevalidate, $revalidates);
}
public function satisfyFailedProvider()
{
return array(
array(new Request('GET', 'http://foo.com', array()), new Response(200, array('Age' => 100)), false),
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),
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),
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),
array(new Request('GET', 'http://foo.com', array()), new Response(200, array('Age' => 100, 'Cache-Control' => 'max-age=50, stale-if-error', )), true),
array(new Request('GET', 'http://foo.com', array()), new Response(200, array('Age' => 100, 'Cache-Control' => 'max-age=50, stale-if-error=50')), true),
array(new Request('GET', 'http://foo.com', array()), new Response(200, array('Age' => 100, 'Cache-Control' => 'max-age=50, stale-if-error=20')), false),
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),
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),
);
}
public function testChecksIfResponseCanSatisfyFailedRequest($request, $response, $can)
{
$plugin = new CachePlugin();
$this->assertEquals($can, $plugin->canResponseSatisfyFailedRequest($request, $response));
}
public function testDoesNothingWhenRequestIsNotCacheable()
{
$storage = $this->getMockBuilder('Guzzle\Plugin\Cache\CacheStorageInterface')
->setMethods(array('fetch'))
->getMockForAbstractClass();
$storage->expects($this->never())->method('fetch');
$plugin = new CachePlugin(array(
'storage' => $storage,
'can_cache' => new CallbackCanCacheStrategy(function () { return false; })
));
$plugin->onRequestBeforeSend(new Event(array(
'request' => new Request('GET', 'http://foo.com')
)));
}
public function satisfiableProvider()
{
$date = new \DateTime('-10 seconds');
return array(
array(new Response(200, array(), 'foo')),
array(new Response(200, array('Date' => $date->format('c'), 'Cache-Control' => 'max-age=5'), 'foo'))
);
}
public function testInjectsSatisfiableResponses($response)
{
$storage = $this->getMockBuilder('Guzzle\Plugin\Cache\CacheStorageInterface')
->setMethods(array('fetch'))
->getMockForAbstractClass();
$storage->expects($this->once())->method('fetch')->will($this->returnValue($response));
$plugin = new CachePlugin(array('storage' => $storage));
$request = new Request('GET', 'http://foo.com', array('Cache-Control' => 'max-stale'));
$plugin->onRequestBeforeSend(new Event(array('request' => $request)));
$plugin->onRequestSent(new Event(array('request' => $request, 'response' => $request->getResponse())));
$this->assertEquals($response->getStatusCode(), $request->getResponse()->getStatusCode());
$this->assertEquals((string) $response->getBody(), (string) $request->getResponse()->getBody());
$this->assertTrue($request->getResponse()->hasHeader('Age'));
if ($request->getResponse()->isFresh() === false) {
$this->assertContains('110', (string) $request->getResponse()->getHeader('Warning'));
}
$this->assertSame(
sprintf('%s GuzzleCache/%s', $request->getProtocolVersion(), Version::VERSION),
(string) $request->getHeader('Via')
);
$this->assertSame(
sprintf('%s GuzzleCache/%s',$request->getProtocolVersion(), Version::VERSION),
(string) $request->getResponse()->getHeader('Via')
);
$this->assertTrue($request->getParams()->get('cache.lookup'));
$this->assertTrue($request->getParams()->get('cache.hit'));
$this->assertTrue($request->getResponse()->hasHeader('X-Cache-Lookup'));
$this->assertTrue($request->getResponse()->hasHeader('X-Cache'));
$this->assertEquals('HIT from GuzzleCache', (string) $request->getResponse()->getHeader('X-Cache'));
$this->assertEquals('HIT from GuzzleCache', (string) $request->getResponse()->getHeader('X-Cache-Lookup'));
}
public function satisfiableOnErrorProvider()
{
$date = new \DateTime('-10 seconds');
return array(
array(
new Response(200, array(
'Date' => $date->format('c'),
'Cache-Control' => 'max-age=5, stale-if-error'
), 'foo'),
)
);
}
public function testInjectsSatisfiableResponsesOnError($cacheResponse)
{
$storage = $this->getMockBuilder('Guzzle\Plugin\Cache\CacheStorageInterface')
->setMethods(array('fetch'))
->getMockForAbstractClass();
$storage->expects($this->exactly(2))->method('fetch')->will($this->returnValue($cacheResponse));
$plugin = new CachePlugin(array('storage' => $storage));
$request = new Request('GET', 'http://foo.com', array('Cache-Control' => 'max-stale'));
$plugin->onRequestBeforeSend(new Event(array('request' => $request)));
$plugin->onRequestError(
$event = new Event(array(
'request' => $request,
'response' => $request->getResponse(),
))
);
$response = $event['response'];
$this->assertEquals($cacheResponse->getStatusCode(), $response->getStatusCode());
$this->assertEquals((string) $cacheResponse->getBody(), (string) $response->getBody());
$this->assertTrue($response->hasHeader('Age'));
if ($response->isFresh() === false) {
$this->assertContains('110', (string) $response->getHeader('Warning'));
}
$this->assertSame(sprintf('%s GuzzleCache/%s', $request->getProtocolVersion(), Version::VERSION), (string) $request->getHeader('Via'));
$this->assertSame(sprintf('%s GuzzleCache/%s', $request->getProtocolVersion(), Version::VERSION), (string) $response->getHeader('Via'));
$this->assertTrue($request->getParams()->get('cache.lookup'));
$this->assertSame('error', $request->getParams()->get('cache.hit'));
$this->assertTrue($response->hasHeader('X-Cache-Lookup'));
$this->assertTrue($response->hasHeader('X-Cache'));
$this->assertEquals('HIT from GuzzleCache', (string) $response->getHeader('X-Cache-Lookup'));
$this->assertEquals('HIT_ERROR from GuzzleCache', (string) $response->getHeader('X-Cache'));
}
public function testInjectsSatisfiableResponsesOnException($cacheResponse)
{
$storage = $this->getMockBuilder('Guzzle\Plugin\Cache\CacheStorageInterface')
->setMethods(array('fetch'))
->getMockForAbstractClass();
$storage->expects($this->exactly(2))->method('fetch')->will($this->returnValue($cacheResponse));
$plugin = new CachePlugin(array('storage' => $storage));
$request = new Request('GET', 'http://foo.com', array('Cache-Control' => 'max-stale'));
$plugin->onRequestBeforeSend(new Event(array(
'request' => $request
)));
$plugin->onRequestException(
new Event(array(
'request' => $request,
'response' => $request->getResponse(),
'exception' => $this->getMock('Guzzle\Http\Exception\CurlException'),
))
);
$plugin->onRequestSent(
new Event(array(
'request' => $request,
'response' => $response = $request->getResponse(),
))
);
$this->assertEquals($cacheResponse->getStatusCode(), $response->getStatusCode());
$this->assertEquals((string) $cacheResponse->getBody(), (string) $response->getBody());
$this->assertTrue($response->hasHeader('Age'));
if ($response->isFresh() === false) {
$this->assertContains('110', (string) $response->getHeader('Warning'));
}
$this->assertSame(sprintf('%s GuzzleCache/%s', $request->getProtocolVersion(), Version::VERSION), (string) $request->getHeader('Via'));
$this->assertSame(sprintf('%s GuzzleCache/%s', $request->getProtocolVersion(), Version::VERSION), (string) $response->getHeader('Via'));
$this->assertTrue($request->getParams()->get('cache.lookup'));
$this->assertSame('error', $request->getParams()->get('cache.hit'));
$this->assertTrue($response->hasHeader('X-Cache-Lookup'));
$this->assertTrue($response->hasHeader('X-Cache'));
$this->assertEquals('HIT from GuzzleCache', (string) $response->getHeader('X-Cache-Lookup'));
$this->assertEquals('HIT_ERROR from GuzzleCache', (string) $response->getHeader('X-Cache'));
}
public function unsatisfiableOnErrorProvider()
{
$date = new \DateTime('-10 seconds');
return array(
array(
false,
array('Cache-Control' => 'no-store'),
new Response(200, array('Date' => $date->format('D, d M Y H:i:s T'), 'Cache-Control' => 'max-age=5, stale-if-error'), 'foo'),
),
array(
true,
array('Cache-Control' => 'stale-if-error=4'),
new Response(200, array('Date' => $date->format('D, d M Y H:i:s T'), 'Cache-Control' => 'max-age=5, stale-if-error'), 'foo'),
),
array(
true,
array('Cache-Control' => 'stale-if-error'),
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'),
),
);
}
public function testDoesNotInjectUnsatisfiableResponsesOnError($requestCanCache, $requestHeaders, $cacheResponse)
{
$storage = $this->getMockBuilder('Guzzle\Plugin\Cache\CacheStorageInterface')
->setMethods(array('fetch'))
->getMockForAbstractClass();
$storage->expects($this->exactly($requestCanCache ? 2 : 0))->method('fetch')->will($this->returnValue($cacheResponse));
$plugin = new CachePlugin(array('storage' => $storage));
$request = new Request('GET', 'http://foo.com', $requestHeaders);
$plugin->onRequestBeforeSend(new Event(array(
'request' => $request
)));
$plugin->onRequestError(
$event = new Event(array(
'request' => $request,
'response' => $response = $request->getResponse(),
))
);
$this->assertSame($response, $event['response']);
}
public function testDoesNotInjectUnsatisfiableResponsesOnException($requestCanCache, $requestHeaders, $responseParts)
{
$storage = $this->getMockBuilder('Guzzle\Plugin\Cache\CacheStorageInterface')
->setMethods(array('fetch'))
->getMockForAbstractClass();
$storage->expects($this->exactly($requestCanCache ? 2 : 0))->method('fetch')->will($this->returnValue($responseParts));
$plugin = new CachePlugin(array('storage' => $storage));
$request = new Request('GET', 'http://foo.com', $requestHeaders);
$plugin->onRequestBeforeSend(new Event(array(
'request' => $request
)));
$plugin->onRequestException(
$event = new Event(array(
'request' => $request,
'response' => $response = $request->getResponse(),
'exception' => $this->getMock('Guzzle\Http\Exception\CurlException'),
))
);
$this->assertSame($response, $request->getResponse());
}
public function testCachesResponsesWhenCacheable()
{
$cache = new ArrayCache();
$plugin = new CachePlugin($cache);
$request = new Request('GET', 'http://foo.com');
$response = new Response(200, array(), 'Foo');
$plugin->onRequestBeforeSend(new Event(array(
'request' => $request
)));
$plugin->onRequestSent(new Event(array(
'request' => $request,
'response' => $response
)));
$data = $this->readAttribute($cache, 'data');
$this->assertNotEmpty($data);
}
public function testPurgesRequests()
{
$storage = $this->getMockBuilder('Guzzle\Plugin\Cache\CacheStorageInterface')
->setMethods(array('purge'))
->getMockForAbstractClass();
$storage->expects($this->atLeastOnce())->method('purge');
$plugin = new CachePlugin(array('storage' => $storage));
$request = new Request('GET', 'http://foo.com', array('X-Foo' => 'Bar'));
$plugin->purge($request);
}
public function testAutoPurgesRequests()
{
$storage = $this->getMockBuilder('Guzzle\Plugin\Cache\CacheStorageInterface')
->setMethods(array('purge'))
->getMockForAbstractClass();
$storage->expects($this->atLeastOnce())->method('purge');
$plugin = new CachePlugin(array('storage' => $storage, 'auto_purge' => true));
$client = new Client();
$request = $client->put('http://foo.com', array('X-Foo' => 'Bar'));
$request->addSubscriber($plugin);
$request->setResponse(new Response(200), true);
$request->send();
}
}