/**
  * @dataProvider cacheRevalidationDataProvider
  */
 public function testRevalidatesResponsesAgainstOriginServer($can, $request, $response, $validate = null, $result = null)
 {
     // Send some responses to the test server for cache validation
     $server = $this->getServer();
     $server->flush();
     if ($validate) {
         $server->enqueue($validate);
     }
     $request = RequestFactory::getInstance()->fromMessage("GET / HTTP/1.1\r\nHost: 127.0.0.1:" . $server->getPort() . "\r\n" . $request);
     $response = Response::fromMessage($response);
     $request->setClient(new Client());
     $plugin = new CachePlugin(new DoctrineCacheAdapter(new ArrayCache()));
     $this->assertEquals($can, $plugin->canResponseSatisfyRequest($request, $response), '-> ' . $request . "\n" . $response);
     if ($result) {
         $result = Response::fromMessage($result);
         $result->removeHeader('Date');
         $request->getResponse()->removeHeader('Date');
         $request->getResponse()->removeHeader('Connection');
         // Get rid of dates
         $this->assertEquals((string) $result, (string) $request->getResponse());
     }
     if ($validate) {
         $this->assertEquals(1, count($server->getReceivedRequests()));
     }
 }
 /**
  * The following is a bit of an integration test to ensure that the CachePlugin honors a
  * custom can cache strategy.
  */
 public function testIntegrationWithCachePlugin()
 {
     $c = new CallbackCanCacheStrategy(function ($request) {
         return true;
     }, function ($response) {
         return true;
     });
     // Make a request and response that have no business being cached
     $request = new Request('DELETE', 'http://www.foo.com');
     $response = Response::fromMessage("HTTP/1.1 200 OK\r\n" . "Expires: Mon, 26 Jul 1997 05:00:00 GMT\r\n" . "Last-Modified: Wed, 09 Jan 2013 08:48:53 GMT\r\n" . "Content-Length: 2\r\n" . "Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0\r\n\r\n" . "hi");
     $this->assertTrue($c->canCacheRequest($request));
     $this->assertTrue($c->canCacheResponse($response));
     $s = $this->getMockBuilder('Guzzle\\Plugin\\Cache\\DefaultCacheStorage')->setConstructorArgs(array(new DoctrineCacheAdapter(new ArrayCache())))->setMethods(array('fetch'))->getMockForAbstractClass();
     $s->expects($this->once())->method('fetch')->will($this->returnValue(array(200, $response->getHeaders(), $response->getBody(true))));
     $plugin = new CachePlugin(array('can_cache' => $c, 'storage' => $s));
     $plugin->onRequestBeforeSend(new Event(array('request' => $request)));
     $this->assertEquals(200, $request->getResponse()->getStatusCode());
     $this->assertEquals('hi', $request->getResponse()->getBody(true));
 }
 public function revalidate(RequestInterface $request, Response $response)
 {
     try {
         $revalidate = $this->createRevalidationRequest($request, $response);
         $validateResponse = $revalidate->send();
         if ($validateResponse->getStatusCode() == 200) {
             return $this->handle200Response($request, $validateResponse);
         } elseif ($validateResponse->getStatusCode() == 304) {
             return $this->handle304Response($request, $validateResponse, $response);
         }
     } catch (CurlException $e) {
         return $this->plugin->canResponseSatisfyFailedRequest($request, $response);
     } catch (BadResponseException $e) {
         $this->handleBadResponse($e);
     }
     // Other exceptions encountered in the revalidation request are ignored
     // in hopes that sending a request to the origin server will fix it
     return false;
 }
 /**
  * This is a very hacked way to listen for a cURL timeout.
  * and serve a cached response from stale cache.
  *
  * The Guzzle\Plugin\Cache\CachePlugin::onBeforeRequestSend event listener
  * method is called manually to allow the plugin to set the cached response.
  *
  * @param Event $event
  */
 public function onRequestException(Event $event)
 {
     $exception = $event['exception'];
     /** @var \Guzzle\Http\Message\Request $request */
     $request =& $event['request'];
     if (!$request) {
         return;
     }
     $cacheControl = $request->getHeader('Cache-Control');
     // Make sure the response can satisfy a request manually before proceeding.
     if ($exception instanceof CurlException && $exception->getErrorNo() === CURLE_OPERATION_TIMEOUTED && $this->guzzleCachePlugin->canResponseSatisfyFailedRequest($event['request'], new Response(408))) {
         if ($cacheControl && $cacheControl->hasDirective('max-age') && $cacheControl->hasDirective('stale-if-error')) {
             // Hack the max-age so the CachePlugin will accept the request as cacheable.
             $cacheControl->addDirective('max-age', $this->getConfig('stale-if-error'));
             $this->guzzleCachePlugin->onRequestBeforeSend(new Event(array('request' => $request)));
         }
     }
 }
 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 testPurgesRequestsWithCustomMethods()
 {
     $storage = $this->getMockBuilder('Guzzle\\Plugin\\Cache\\CacheStorageInterface')->setMethods(array('delete'))->getMockForAbstractClass();
     $storage->expects($this->exactly(2))->method('delete');
     $plugin = new CachePlugin(array('storage' => $storage));
     $request = new Request('GET', 'http://foo.com', array('X-Foo' => 'Bar'));
     $request->getParams()->set('cache.purge_methods', array('FOO', 'BAR'));
     $plugin->purge($request);
 }
 public function testCachesResponsesWhenCacheable()
 {
     $cache = new ArrayCache();
     $adapter = new DoctrineCacheAdapter($cache);
     $plugin = new CachePlugin(array('adapter' => $adapter));
     $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);
     $data = end($data);
     $this->assertEquals(200, $data[0]);
     $this->assertInternalType('array', $data[1]);
     $this->assertEquals('Foo', $data[2]);
 }