def test_session_is_a_class_with_original_attributes(self): aiohttp_client_cache.install_cache(name=CACHE_NAME, backend=CACHE_BACKEND) self.assertTrue(isinstance(requests.Session, type)) for attribute in dir(OriginalSession): self.assertTrue(hasattr(requests.Session, attribute)) self.assertTrue(isinstance(requests.Session(), CachedSession))
def test_requests_from_cache(self): aiohttp_client_cache.install_cache(name=CACHE_NAME, backend=CACHE_BACKEND) r = requests.get('http://httpbin.org/get') self.assertFalse(r.from_cache) r = requests.get('http://httpbin.org/get') self.assertTrue(r.from_cache)
def test_passing_backend_instance_support(self): class MyCache(BaseCache): pass backend = MyCache() aiohttp_client_cache.install_cache(name=CACHE_NAME, backend=backend) self.assertIs(requests.Session().cache, backend) session = CachedSession(backend=backend) self.assertIs(session.cache, backend)
def test_install_uninstall(self): for _ in range(2): aiohttp_client_cache.install_cache(name=CACHE_NAME, backend=CACHE_BACKEND) self.assertTrue(isinstance(requests.Session(), CachedSession)) self.assertTrue( isinstance(requests.sessions.Session(), CachedSession)) self.assertTrue(isinstance(requests.session(), CachedSession)) aiohttp_client_cache.uninstall_cache() self.assertFalse(isinstance(requests.Session(), CachedSession)) self.assertFalse( isinstance(requests.sessions.Session(), CachedSession)) self.assertFalse(isinstance(requests.session(), CachedSession))
def test_inheritance_after_monkey_patch(self): aiohttp_client_cache.install_cache(name=CACHE_NAME, backend=CACHE_BACKEND) class FooSession(requests.Session): __attrs__ = requests.Session.__attrs__ + ["new_one"] def __init__(self, param): self.param = param super(FooSession, self).__init__() s = FooSession(1) self.assertEquals(s.param, 1) self.assertIn("new_one", s.__attrs__) self.assertTrue(isinstance(s, CachedSession))
def test_disabled(self): url = httpbin('get') aiohttp_client_cache.install_cache(CACHE_NAME, backend=CACHE_BACKEND, fast_save=FAST_SAVE) requests.get(url) with aiohttp_client_cache.disabled(): for i in range(2): r = requests.get(url) self.assertFalse(getattr(r, 'from_cache', False)) with self.s.cache_disabled(): for i in range(2): r = self.s.get(url) self.assertFalse(getattr(r, 'from_cache', False)) r = self.s.get(url) self.assertTrue(getattr(r, 'from_cache', False))
def test_content_and_cookies(self): aiohttp_client_cache.install_cache(CACHE_NAME, CACHE_BACKEND) s = requests.session() def js(url): return json.loads(s.get(url).text) r1 = js(httpbin('cookies/set/test1/test2')) with aiohttp_client_cache.disabled(): r2 = js(httpbin('cookies')) self.assertEqual(r1, r2) r3 = js(httpbin('cookies')) with aiohttp_client_cache.disabled(): r4 = js(httpbin('cookies/set/test3/test4')) # from cache self.assertEqual(r3, js(httpbin('cookies'))) # updated with aiohttp_client_cache.disabled(): self.assertEqual(r4, js(httpbin('cookies')))
def setUp(self): aiohttp_client_cache.install_cache(name=CACHE_NAME, backend=CACHE_BACKEND) requests.Session().cache.clear() aiohttp_client_cache.uninstall_cache()