async def test_head(): async with AsyncCacheControl() as cached_sess: async with cached_sess.head("http://example.com") as resp: resp_text = await resp.text() assert resp.status == 200 assert resp_text == "" assert resp.headers.get("Cache-Control") == "max-age=604800"
async def test_no_hit_cache(): # response from given url contains 'no-cache' directive url = "https://my-json-server.typicode.com/typicode/demo/posts" expected_json = [ { "id": 1, "title": "Post 1" }, { "id": 2, "title": "Post 2" }, { "id": 3, "title": "Post 3" }, ] cache_observer = CacheObserver() async with AsyncCacheControl(cache=cache_observer) as cached_sess: async with cached_sess.get(url) as resp: resp_json = await resp.json() assert resp.status == 200 assert resp_json == expected_json async with cached_sess.get(url) as resp: resp_json = await resp.json() assert resp.status == 200 assert resp_json == expected_json assert len(cache_observer.get_calls) == 0
async def test_clear_cashe(mocker): """Verify AsyncCacheControl.clear_cache works correctly.""" mock_RCM = mocker.Mock() mock_cache = mocker.Mock() async with AsyncCacheControl(request_context_manager_cls=mock_RCM, cache=mock_cache) as cached_sess: cached_sess.clear_cache() mock_cache.clear_cache.assert_called_once()
async def fire_request(self, uri, **params): """Perform http(s) request within AsyncCacheControl session. Return response in json-format. """ async with AsyncCacheControl(cache=self.cache) as cached_sess: async with cached_sess.get(uri, **params) as resp: resp_json = await resp.json() return resp_json
async def test_post(mocker): """Verify AsyncCacheControl.post works correctly.""" mock_RCM = mocker.Mock() async with AsyncCacheControl( request_context_manager=mock_RCM) as cached_sess: _ = cached_sess.post('http://example.com', data={'key': 'value'}) mock_RCM.assert_called_with(cached_sess._async_session, cached_sess.cache, 'POST', 'http://example.com', data={'key': 'value'})
async def test_get(mocker): """Verify AsyncCacheControl.get works correctly.""" mock_RCM = mocker.Mock() async with AsyncCacheControl( request_context_manager=mock_RCM) as cached_sess: _ = cached_sess.get('http://example.com') mock_RCM.assert_called_with(cached_sess._async_session, cached_sess.cache, 'GET', 'http://example.com', allow_redirects=True)
async def test_hit_cache(): cache_observer = CacheObserver() async with AsyncCacheControl(cache=cache_observer) as cached_sess: async with cached_sess.request('GET', 'http://example.com') as resp: resp_text = await resp.text() assert 'Example Domain' in resp_text async with cached_sess.request('GET', 'http://example.com') as resp: resp_text = await resp.text() assert 'Example Domain' in resp_text assert ('GET', 'http://example.com', {}) in cache_observer.get_calls
async def test_patch(mocker): """Verify AsyncCacheControl.patch works correctly.""" mock_RCM = mocker.Mock() async with AsyncCacheControl( request_context_manager_cls=mock_RCM) as cached_sess: _ = cached_sess.patch("http://example.com", data={"key": "value"}) mock_RCM.assert_called_with( cached_sess._async_client_session, cached_sess.cache, "PATCH", "http://example.com", data={"key": "value"}, )
async def test_get(mocker): """Verify AsyncCacheControl.get works correctly.""" mock_RCM = mocker.Mock() async with AsyncCacheControl( request_context_manager_cls=mock_RCM) as cached_sess: _ = cached_sess.get("http://example.com") mock_RCM.assert_called_with( cached_sess._async_client_session, cached_sess.cache, "GET", "http://example.com", allow_redirects=True, )
async def test_hit_cache(): cache_observer = CacheObserver() async with AsyncCacheControl(cache=cache_observer) as cached_sess: async with cached_sess.get("http://example.com") as resp: resp_text = await resp.text() assert resp.status == 200 assert "Example Domain" in resp_text async with cached_sess.get("http://example.com") as resp: resp_text = await resp.text() assert resp.status == 200 assert "Example Domain" in resp_text assert ("GET", "http://example.com") in cache_observer.get_calls
async def test_hit_cache_json(): url = 'https://my-json-server.typicode.com/typicode/demo/posts' expected_json = [{ "id": 1, "title": "Post 1" }, { "id": 2, "title": "Post 2" }, { "id": 3, "title": "Post 3" }] async with AsyncCacheControl() as cached_sess: async with cached_sess.request('GET', url) as resp: resp_json = await resp.json() assert resp_json == expected_json async with cached_sess.request('GET', url) as resp: resp_json = await resp.json() assert resp_json == expected_json
async def test_request(mocker): """Verify AsyncCacheControl.request works correctly.""" mock_RCM = mocker.Mock() async with AsyncCacheControl( cache=AsyncCache(), request_context_manager_cls=mock_RCM) as cached_sess: _ = cached_sess.request("GET", "http://example.com") mock_RCM.assert_called_with( cached_sess._async_client_session, cached_sess.cache, "GET", "http://example.com", ) _ = cached_sess.request("POST", "http://example.com", data={"key": "value"}) mock_RCM.assert_called_with( cached_sess._async_client_session, cached_sess.cache, "POST", "http://example.com", data={"key": "value"}, )
async def test_request(): async with AsyncCacheControl() as cached_sess: async with cached_sess.request('GET', 'http://example.com') as resp: resp_text = await resp.text() assert 'Example Domain' in resp_text
async def test_get(): async with AsyncCacheControl() as cached_sess: async with cached_sess.get("http://example.com") as resp: resp_text = await resp.text() assert resp.status == 200 assert "Example Domain" in resp_text