def test_vary_header_different_requests(self): """Testing the cache with the Vary header and different requests""" first_request = Request('http://vary', headers={'User-agent': 'foo'}, method='GET') second_request = Request('http://vary', headers={'User-agent': 'bar'}, method='GET') first_resp = self.cache.make_request(first_request) second_resp = self.cache.make_request(second_request) self.assertEqual(self.urlopener.get_hit_count('http://vary'), 2) self.assertFalse(isinstance(first_resp, CachedHTTPResponse)) self.assertFalse(isinstance(second_resp, CachedHTTPResponse))
def test_expires_header_future(self): """Testing the cache with the Expires header in the future""" # We generate the future date in the C locale so that it is properly # formatted. locale.setlocale(locale.LC_TIME, str('C')) future_date = datetime.datetime.utcnow() + datetime.timedelta(days=1) future_date = future_date.strftime(APICache.EXPIRES_FORMAT) + 'UTC' locale.resetlocale(locale.LC_TIME) self.urlopener.endpoints['http://expires_future'] = { 'hit_count': 0, 'headers': { 'Expires': future_date, }, } request = Request('http://expires_future', method='GET') first_resp = self.cache.make_request(request) second_resp = self.cache.make_request(request) self.assertEqual(self.urlopener.get_hit_count('http://expires_future'), 1) self.assertFalse(isinstance(first_resp, CachedHTTPResponse)) self.assertTrue(isinstance(second_resp, CachedHTTPResponse))
def test_cache_control_header_nocache_with_last_modified_updated(self): """Testing the cache with the no-cache control and an updated Last-Modified header """ endpoint = 'http://no_cache_lastmodified_updated' future_date = datetime.datetime.utcnow() + datetime.timedelta(days=1) self.urlopener.endpoints[endpoint] = { 'hit_count': 0, 'headers': { 'Cache-Control': 'no-cache', 'Last-Modified': '1999-12-31T00:00:00' }, } request = Request(endpoint, method='GET') first_resp = self.cache.make_request(request) self.urlopener.endpoints[endpoint]['headers']['Last-Modified'] = ( future_date.strftime(CacheEntry.DATE_FORMAT)) second_resp = self.cache.make_request(request) third_resp = self.cache.make_request(request) self.assertFalse(isinstance(first_resp, CachedHTTPResponse)) self.assertFalse(isinstance(second_resp, CachedHTTPResponse)) self.assertTrue(isinstance(third_resp, CachedHTTPResponse)) self.assertEqual(self.urlopener.get_hit_count(endpoint), 3)
def test_expires_header_expired(self): """Testing the cache with the Expires header in the past""" request = Request('http://expired', method='GET') first_resp = self.cache.make_request(request) second_resp = self.cache.make_request(request) self.assertEqual(self.urlopener.get_hit_count('http://expired'), 2) self.assertFalse(isinstance(first_resp, CachedHTTPResponse)) self.assertFalse(isinstance(second_resp, CachedHTTPResponse))
def test_pragma_header(self): """Testing the cache with the Pragma: no-cache header""" request = Request('http://pragma', method='GET') first_resp = self.cache.make_request(request) second_resp = self.cache.make_request(request) self.assertEqual(self.urlopener.get_hit_count('http://pragma'), 2) self.assertFalse(isinstance(first_resp, CachedHTTPResponse)) self.assertFalse(isinstance(second_resp, CachedHTTPResponse))
def test_cache_control_header_no_store(self): """Testing the cache with the no-store control""" request = Request('http://no_store', method='GET') first_resp = self.cache.make_request(request) second_resp = self.cache.make_request(request) self.assertEqual(self.urlopener.get_hit_count('http://no_store'), 2) self.assertFalse(isinstance(first_resp, CachedHTTPResponse)) self.assertFalse(isinstance(second_resp, CachedHTTPResponse))
def test_expires_header_overriden_by_max_age(self): """Testing the cache with an Expires header that is overridden""" request = Request('http://expires_override', method='GET') first_resp = self.cache.make_request(request) second_resp = self.cache.make_request(request) self.assertEqual( self.urlopener.get_hit_count('http://expires_override'), 1) self.assertFalse(isinstance(first_resp, CachedHTTPResponse)) self.assertTrue(isinstance(second_resp, CachedHTTPResponse))
def test_cache_control_header_nocache_with_etag(self): """Testing the cache with the no-cache control and a specified ETag""" request = Request('http://no_cache_etag', method='GET') first_resp = self.cache.make_request(request) second_resp = self.cache.make_request(request) self.assertEqual(self.urlopener.get_hit_count('http://no_cache_etag'), 2) self.assertFalse(isinstance(first_resp, CachedHTTPResponse)) self.assertTrue(isinstance(second_resp, CachedHTTPResponse))
def test_cache_control_header_max_age_zero(self): """Testing the cache with a zero max-age value""" request = Request('http://zero_max_age', method='GET') first_resp = self.cache.make_request(request) second_resp = self.cache.make_request(request) self.assertEqual(self.urlopener.get_hit_count('http://zero_max_age'), 2) self.assertFalse(isinstance(first_resp, CachedHTTPResponse)) self.assertFalse(isinstance(second_resp, CachedHTTPResponse))
def test_cache_control_header_nocache_with_etag_updated(self): """Testing the cache with the no-cache control and an updated ETag""" request = Request('http://no_cache_etag', method='GET') first_resp = self.cache.make_request(request) # Pretend the end point has been updated since the last request. self.urlopener.endpoints['http://no_cache_etag']['headers']['ETag'] = ( 'new-etag') second_resp = self.cache.make_request(request) third_resp = self.cache.make_request(request) self.assertFalse(isinstance(first_resp, CachedHTTPResponse)) self.assertFalse(isinstance(second_resp, CachedHTTPResponse)) self.assertTrue(isinstance(third_resp, CachedHTTPResponse)) self.assertEqual(self.urlopener.get_hit_count('http://no_cache_etag'), 3)