def test_get_non_async(basic_test_server, loop): with pytest.warns(DeprecationWarning): # throws DeprecationWarning b.c. session not create in async func session = ClientSession() resp_coro = session.get(basic_test_server["uri"]) resp = loop.run_until_complete(resp_coro) assert loop.run_until_complete( resp.json()) == basic_test_server["data"]
async def test_get_check_cache(basic_test_server): uri, data = basic_test_server import tempfile from aiohttp_client_cache import SQLiteBackend temp_cachefile = tempfile.NamedTemporaryFile(suffix=".sqlite") temp_cachefile_name = temp_cachefile.name cache = SQLiteBackend( cache_name=temp_cachefile_name, expire_after=-1, allowed_codes=[200], allowed_methods=["GET"], ) async with ClientSession(cache=cache) as client: r = await client.get(basic_test_server["uri"]) r_json = await r.json() r2 = await client.get(basic_test_server["uri"]) r2_json = await r2.json() assert r_json == r2_json # first not from cache, second is from cache assert r.from_cache is False assert r2.from_cache is True
async def test_variadic_get(mirror_server): params = {"hey": ["there"], "this": [Variadic(["that", "the", "other"])]} # mirror_server is type: yarl.URL which does not subclass str. requires cast url = Url(str(mirror_server)) + params async with ClientSession() as session: async with session.get(url) as req: assert await req.json() == params
async def test_variadic_get(mirror_server): params = {"hey": ["there"], "this": [Variadic(["that", "the", "other"])]} async with ClientSession() as session: async with session.get( mirror_server, params=params, ) as req: assert await req.json() == params
async def test_get(basic_test_server): async with ClientSession() as client: async with client.get(basic_test_server["uri"]) as resp: assert await resp.json() == basic_test_server["data"]
async def test_backoff_server_n_retries_2_in_init(backoff_server): async with ClientSession(n_retries=1) as client: async with client.get(backoff_server["uri"]) as resp: data = await resp.json() assert data["called"] == 2 # 1 standard get, 1 backoff
async def test_backoff_server_turnoff_retry_in_init(backoff_server): async with ClientSession(retry=False) as client: async with client.get(backoff_server["uri"]) as resp: data = await resp.json() assert data["called"] == 1 # 1 standard get
async def test_backoff_server(backoff_server): async with ClientSession() as client: async with client.get(backoff_server["uri"]) as resp: data = await resp.json() assert data["called"] == 3 # 1 standard get, 2 backoff