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:
<?php
namespace Guzzle\Tests\Plugin\Cache;
use Guzzle\Cache\DoctrineCacheAdapter;
use Guzzle\Http\Message\Request;
use Guzzle\Http\Message\RequestFactory;
use Guzzle\Http\Message\Response;
use Guzzle\Plugin\Cache\DefaultCacheStorage;
use Doctrine\Common\Cache\ArrayCache;
class DefaultCacheStorageTest extends \Guzzle\Tests\GuzzleTestCase
{
protected function getCache()
{
$a = new ArrayCache();
$c = new DoctrineCacheAdapter($a);
$s = new DefaultCacheStorage($c);
$request = new Request('GET', 'http://foo.com', array('Accept' => 'application/json'));
$response = new Response(200, array(
'Content-Type' => 'application/json',
'Connection' => 'close',
'X-Foo' => 'Bar',
'Vary' => 'Accept'
), 'test');
$s->cache($request, $response);
$data = $this->readAttribute($a, 'data');
return array(
'cache' => $a,
'adapter' => $c,
'storage' => $s,
'request' => $request,
'response' => $response,
'serialized' => end($data)
);
}
public function testReturnsNullForCacheMiss()
{
$cache = $this->getCache();
$this->assertNull($cache['storage']->fetch(new Request('GET', 'http://test.com')));
}
public function testCachesRequests()
{
$cache = $this->getCache();
$foundRequest = $foundBody = $bodyKey = false;
foreach ($this->readAttribute($cache['cache'], 'data') as $key => $v) {
if (strpos($v, 'foo.com')) {
$foundRequest = true;
$data = unserialize($v);
$bodyKey = $data[0][3];
$this->assertInternalType('integer', $data[0][4]);
$this->assertFalse(isset($data[0][0]['connection']));
$this->assertEquals('foo.com', $data[0][0]['host']);
} elseif ($v == 'test') {
$foundBody = $key;
}
}
$this->assertContains($bodyKey, $foundBody);
$this->assertTrue($foundRequest);
}
public function testFetchesResponse()
{
$cache = $this->getCache();
$response = $cache['storage']->fetch($cache['request']);
$this->assertEquals(200, $response->getStatusCode());
$this->assertFalse($response->hasHeader('Connection'));
$this->assertEquals('Bar', (string) $response->getHeader('X-Foo'));
$this->assertEquals('test', (string) $response->getBody());
$this->assertTrue(in_array($cache['serialized'], $this->readAttribute($cache['cache'], 'data')));
}
public function testDeletesRequestItemsAndBody()
{
$cache = $this->getCache();
$cache['storage']->delete($cache['request']);
$this->assertFalse(in_array('test', $this->readAttribute($cache['cache'], 'data')));
$this->assertFalse(in_array($cache['serialized'], $this->readAttribute($cache['cache'], 'data')));
}
public function testCachesMultipleRequestsWithVary()
{
$cache = $this->getCache();
$cache['request']->setHeader('Accept', 'application/xml');
$response = $cache['response']->setHeader('Content-Type', 'application/xml');
$response->setBody('123');
$cache['storage']->cache($cache['request'], $response);
$data = $this->readAttribute($cache['cache'], 'data');
foreach ($data as $v) {
if (strpos($v, 'foo.com')) {
$u = unserialize($v);
$this->assertEquals(2, count($u));
$this->assertEquals($u[0][0]['accept'], 'application/xml');
$this->assertEquals($u[0][1]['content-type'], 'application/xml');
$this->assertEquals($u[1][0]['accept'], 'application/json');
$this->assertEquals($u[1][1]['content-type'], 'application/json');
$this->assertNotSame($u[0][3], $u[1][3]);
break;
}
}
}
public function testPurgeRemovesAllMethodCaches()
{
$cache = $this->getCache();
foreach (array('HEAD', 'POST', 'PUT', 'DELETE') as $method) {
$request = RequestFactory::getInstance()->cloneRequestWithMethod($cache['request'], $method);
$cache['storage']->cache($request, $cache['response']);
}
$cache['storage']->purge('http://foo.com');
$this->assertFalse(in_array('test', $this->readAttribute($cache['cache'], 'data')));
$this->assertFalse(in_array($cache['serialized'], $this->readAttribute($cache['cache'], 'data')));
$this->assertEquals(
array('DoctrineNamespaceCacheKey[]'),
array_keys($this->readAttribute($cache['cache'], 'data'))
);
}
public function testRemovesExpiredResponses()
{
$cache = $this->getCache();
$request = new Request('GET', 'http://xyz.com');
$response = new Response(200, array('Age' => 1000, 'Cache-Control' => 'max-age=-10000'));
$cache['storage']->cache($request, $response);
$this->assertNull($cache['storage']->fetch($request));
$data = $this->readAttribute($cache['cache'], 'data');
$this->assertFalse(in_array('xyz.com', $data));
$this->assertTrue(in_array($cache['serialized'], $data));
}
public function testUsesVaryToDetermineResult()
{
$cache = $this->getCache();
$this->assertInstanceOf('Guzzle\Http\Message\Response', $cache['storage']->fetch($cache['request']));
$request = new Request('GET', 'http://foo.com', array('Accept' => 'application/xml'));
$this->assertNull($cache['storage']->fetch($request));
}
public function testEnsuresResponseIsStillPresent()
{
$cache = $this->getCache();
$data = $this->readAttribute($cache['cache'], 'data');
$key = array_search('test', $data);
$cache['cache']->delete(substr($key, 1, -4));
$this->assertNull($cache['storage']->fetch($cache['request']));
}
public function staleProvider()
{
return array(
array(
new Request('GET', 'http://foo.com', array('Accept' => 'foo')),
new Response(200, array('Cache-Control' => 'stale-if-error=100', 'Vary' => 'Accept'))
),
array(
new Request('GET', 'http://foo.com', array('Accept' => 'foo')),
new Response(200, array('Cache-Control' => 'stale-if-error', 'Vary' => 'Accept'))
)
);
}
public function testUsesStaleTimeDirectiveForTtd($request, $response)
{
$cache = $this->getCache();
$cache['storage']->cache($request, $response);
$data = $this->readAttribute($cache['cache'], 'data');
foreach ($data as $v) {
if (strpos($v, 'foo.com')) {
$u = unserialize($v);
$this->assertGreaterThan($u[1][4], $u[0][4]);
break;
}
}
}
public function testCanFilterCacheKeys()
{
$cache = $this->getCache();
$cache['request']->getQuery()->set('auth', 'foo');
$this->assertNull($cache['storage']->fetch($cache['request']));
$cache['request']->getParams()->set('cache.key_filter', 'auth');
$this->assertNotNull($cache['storage']->fetch($cache['request']));
}
}