public function testMustRevalidate() { $now = time(); // Expires 1 year in the future, and contains the must-revalidate directive. // Don't revalidate. must-revalidate only applies to expired entries. $future = $now + 365 * 24 * 60 * 60; $resp = new apiHttpRequest('http://localhost', 'GET'); $resp->setResponseHttpCode('200'); $resp->setResponseHeaders(array('Cache-Control' => 'max-age=3600, must-revalidate', 'Expires' => gmdate('D, d M Y H:i:s', $future) . ' GMT', 'Date' => gmdate('D, d M Y H:i:s', $now) . ' GMT')); $this->assertFalse(apiCacheParser::mustRevalidate($resp)); // Contains the max-age=3600 directive, but was created 2 hours ago. // Must revalidate. $past = $now - 2 * 60 * 60; $resp = new apiHttpRequest('http://localhost', 'GET'); $resp->setResponseHttpCode('200'); $resp->setResponseHeaders(array('Cache-Control' => 'max-age=3600', 'Expires' => gmdate('D, d M Y H:i:s', $future) . ' GMT', 'Date' => gmdate('D, d M Y H:i:s', $past) . ' GMT')); $this->assertTrue(apiCacheParser::mustRevalidate($resp)); // Contains the max-age=3600 directive, and was created 600 seconds ago. // No need to revalidate, regardless of the expires header. $past = $now - 600; $resp = new apiHttpRequest('http://localhost', 'GET'); $resp->setResponseHttpCode('200'); $resp->setResponseHeaders(array('Cache-Control' => 'max-age=3600', 'Expires' => gmdate('D, d M Y H:i:s', $past) . ' GMT', 'Date' => gmdate('D, d M Y H:i:s', $past) . ' GMT')); $this->assertFalse(apiCacheParser::mustRevalidate($resp)); }
/** * @visible for testing. * @param apiHttpRequest $request * @return apiHttpRequest|bool Returns the cached object or * false if the operation was unsuccessful. */ public function getCachedRequest(apiHttpRequest $request) { if (false == apiCacheParser::isRequestCacheable($request)) { false; } return apiClient::$cache->get($request->getCacheKey()); }
public function testIsExpired() { $now = time(); $future = $now + 365 * 24 * 60 * 60; // Expires 1 year in the future. Response is fresh. $resp = new apiHttpRequest('GET'); $resp->setResponseHttpCode('200'); $resp->setResponseHeaders(array('Expires' => gmdate('D, d M Y H:i:s', $future) . ' GMT', 'Date' => gmdate('D, d M Y H:i:s', $now) . ' GMT')); $this->assertFalse(apiCacheParser::isExpired($resp)); // The response expires soon. Response is fresh. $resp = new apiHttpRequest('GET'); $resp->setResponseHttpCode('200'); $resp->setResponseHeaders(array('Expires' => gmdate('D, d M Y H:i:s', $now + 2), 'Date' => gmdate('D, d M Y H:i:s', $now) . ' GMT')); $this->assertFalse(apiCacheParser::isExpired($resp)); // Expired 1 year ago. Response is stale. $future = $now - 365 * 24 * 60 * 60; $resp = new apiHttpRequest('GET'); $resp->setResponseHttpCode('200'); $resp->setResponseHeaders(array('Expires' => gmdate('D, d M Y H:i:s', $future) . ' GMT', 'Date' => gmdate('D, d M Y H:i:s', $now) . ' GMT')); $this->assertTrue(apiCacheParser::isExpired($resp)); // Invalid expires header. Response is stale. $resp = new apiHttpRequest('GET'); $resp->setResponseHttpCode('200'); $resp->setResponseHeaders(array('Expires' => '-1', 'Date' => gmdate('D, d M Y H:i:s', $now) . ' GMT')); $this->assertTrue(apiCacheParser::isExpired($resp)); // The response expires immediately. G+ APIs do this. Response is stale. $resp = new apiHttpRequest('GET'); $resp->setResponseHttpCode('200'); $resp->setResponseHeaders(array('Expires' => gmdate('D, d M Y H:i:s', $now), 'Date' => gmdate('D, d M Y H:i:s', $now) . ' GMT')); $this->assertTrue(apiCacheParser::isExpired($resp)); }