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 testStoresResponsesWithCustomTtl() { $c = $this->getMockBuilder('Guzzle\\Cache\\CacheAdapterInterface')->setMethods(array('save'))->getMockForAbstractClass(); $that = $this; $c->expects($this->once())->method('save')->will($this->returnCallback(function ($a, $b, $c) use($that) { $that->assertArrayHasKey('Date', $b[1]); $that->assertFalse(array_key_exists('Connection', $b[1])); $that->assertEquals(50, $c); })); $s = new DefaultCacheStorage($c, 100); $response = new Response(200, array('foo' => 'bar', 'Connection' => 'close'), 'baz'); $s->cache('foo', $response, 50); }
protected function getCacheKey(\Guzzle\Http\Message\RequestInterface $request) { if (!$this->_cacheKeyGenerator) { return parent::getCacheKey($request); } $generator = $this->_cacheKeyGenerator; return md5(parent::getCacheKey($request) . $generator($request)); }
public function testHandles404RevalidationResponses() { $request = new Request('GET', 'http://foo.com'); $request->setClient(new Client()); $badResponse = new Response(404, array(), 'Oh no!'); $badRequest = clone $request; $badRequest->setResponse($badResponse, true); $response = new Response(200, array(), 'foo'); // Seed the cache $s = new DefaultCacheStorage(new DoctrineCacheAdapter(new ArrayCache())); $s->cache($request, $response); $this->assertNotNull($s->fetch($request)); $rev = $this->getMockBuilder('Guzzle\\Plugin\\Cache\\DefaultRevalidation')->setConstructorArgs(array($s))->setMethods(array('createRevalidationRequest'))->getMock(); $rev->expects($this->once())->method('createRevalidationRequest')->will($this->returnValue($badRequest)); try { $rev->revalidate($request, $response); $this->fail('Should have thrown an exception'); } catch (BadResponseException $e) { $this->assertSame($badResponse, $e->getResponse()); $this->assertNull($s->fetch($request)); } }