public function testAllowsCustomCacheFilterStrategies() { $plugin = new CachePlugin($this->adapter); $client = new Client($this->getServer()->getUrl(), array('params.cache.filter_strategy' => function ($request) { return true; })); $plugin->onRequestBeforeSend(new Event(array('request' => $client->post('/')))); $this->assertEquals(1, count($this->readAttribute($plugin, 'cached'))); $client->getConfig()->set('params.cache.filter_strategy', function ($request) { return false; }); $plugin->onRequestBeforeSend(new Event(array('request' => $client->get('/')))); $this->assertEquals(1, count($this->readAttribute($plugin, 'cached'))); }
/** * @covers Guzzle\Http\Plugin\CachePlugin::canResponseSatisfyRequest * @covers Guzzle\Http\Plugin\CachePlugin::revalidate * @dataProvider cacheRevalidationDataProvider */ public function testRevalidatesResponsesAgainstOriginServer($can, $request, $response, $validate = null, $result = null, $param = 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()); if ($param) { $request->getParams()->set('cache.revalidate', $param); } $plugin = new CachePlugin($this->adapter, true); $this->assertEquals($can, $plugin->canResponseSatisfyRequest($request, $response), '-> ' . $request . "\n" . $response); if ($result) { // Get rid of dates $this->assertEquals(preg_replace('/(Date:\\s)(.*)(\\r\\n)/', '$1$3', (string) $result), preg_replace('/(Date:\\s)(.*)(\\r\\n)/', '$1$3', (string) $request->getResponse())); } if ($validate) { $this->assertEquals(1, count($server->getReceivedRequests())); } }