def test_cache_conditional_get_last_modified_expired(self, mock_requests): """ Tests that the cache performs a conditional GET request when asked to update the response for a URL with a Last-Modified header, which has since expired. """ last_modified = http_date(time.time() - 3600) self.set_mock_response(mock_requests, headers={ 'Last-Modified': last_modified }) cache = HttpCache(self.cache_directory) url = 'http://example.com' cache.update(url) # Set a new Last-Modified and content value new_last_modified = http_date(time.time()) self.response_content = b'Response' self.set_mock_response(mock_requests, headers={ 'Last-Modified': new_last_modified }) # Run the update again response, updated = cache.update(url) self.assertTrue(updated) self.assertEqual(200, response.status_code) # The new content is found in the cache self.assertEqual(self.response_content, cache.get_content(url)) # The new Last-Modified is found in the headers cache self.assertEqual( new_last_modified, cache.get_headers(url)['Last-Modified'] )
def test_conditional_get_etag_expired(self, mock_requests): """ Tests that the cache performs a conditional GET request when asked to update the response for a URL with an ETag header, which has since expired. """ etag = '"466010a-11bf9-4e17efa8afb81"' self.set_mock_response(mock_requests, headers={ 'ETag': etag, }) cache = HttpCache(self.cache_directory) url = 'http://example.com' cache.update(url) # Set a new ETag and content value new_etag = '"57ngfhty11bf9-9t831116kn1qw1' self.response_content = b'Response' self.set_mock_response(mock_requests, headers={ 'ETag': new_etag }) # Run the update again response, updated = cache.update(url) self.assertTrue(updated) self.assertEqual(200, response.status_code) # The new content is found in the cache self.assertEqual(self.response_content, cache.get_content(url)) # The new Last-Modified is found in the headers cache self.assertEqual( new_etag, cache.get_headers(url)['ETag'] )
def test_update_cache_new_item(self, mock_requests): """ Tests the simple case of updating the cache with a new URL's response. """ headers = { 'Connection': 'Keep-Alive', 'Content-Type': 'text/plain', } self.set_mock_response(mock_requests, headers=headers) cache = HttpCache(self.cache_directory) url = 'http://example.com' # The URL cannot be found in the cache at this point self.assertFalse(url in cache) response, updated = cache.update(url) # The returned response is correct self.assertEqual(self.response_content, response.content) self.assertEqual(200, response.status_code) # The return value indicates the cache has been updated self.assertTrue(updated) # The URL is now found in the cache self.assertTrue(url in cache) # The content is accessible through the cache self.assertEqual(self.response_content, cache.get_content(url)) # The returned headers are accessible through the cache cached_headers = cache.get_headers(url) for key, value in headers.items(): self.assertIn(key, cached_headers) self.assertEqual(value, cached_headers[key])