async def test_same_domain_redirect(): client = AsyncClient(dispatch=MockDispatch()) url = "https://example.org/cross_domain" headers = {"Authorization": "abc"} response = await client.get(url, headers=headers) assert response.url == URL("https://example.org/cross_domain_target") assert response.json()["headers"]["authorization"] == "abc"
async def test_body_redirect(): client = AsyncClient(dispatch=MockDispatch()) url = "https://example.org/redirect_body" data = b"Example request body" response = await client.post(url, data=data) assert response.url == URL("https://example.org/redirect_body_target") assert response.json() == {"body": "Example request body"}
async def test_fragment_redirect(): client = AsyncClient(dispatch=MockDispatch()) response = await client.get( "https://example.org/relative_redirect#fragment") assert response.status_code == codes.OK assert response.url == URL("https://example.org/#fragment") assert len(response.history) == 1
async def test_redirect_loop_calling_next(): client = AsyncClient(dispatch=MockDispatch()) url = "https://example.org/redirect_loop" response = await client.get(url, allow_redirects=False) with pytest.raises(RedirectLoop): while response.is_redirect: response = await response.next()
async def test_too_many_redirects_calling_next(): client = AsyncClient(dispatch=MockDispatch()) url = "https://example.org/multiple_redirects?count=21" response = await client.get(url, allow_redirects=False) with pytest.raises(TooManyRedirects): while response.is_redirect: response = await response.next()
async def test_connect_timeout(server): timeout = TimeoutConfig(connect_timeout=0.000001) async with AsyncClient(timeout=timeout) as client: with pytest.raises(ConnectTimeout): # See https://stackoverflow.com/questions/100841/ await client.get("http://10.255.255.1/")
async def test_write_timeout(server): timeout = TimeoutConfig(write_timeout=0.000001) async with AsyncClient(timeout=timeout) as client: with pytest.raises(WriteTimeout): data = b"*" * 1024 * 1024 * 100 await client.put("http://127.0.0.1:8000/slow_response", data=data)
async def test_cannot_redirect_streaming_body(): client = AsyncClient(dispatch=MockDispatch()) url = "https://example.org/redirect_body" async def streaming_body(): yield b"Example request body" with pytest.raises(RedirectBodyUnavailable): await client.post(url, data=streaming_body())
async def test_pool_timeout(server): pool_limits = PoolLimits(hard_limit=1, pool_timeout=0.000001) async with AsyncClient(pool_limits=pool_limits) as client: response = await client.get("http://127.0.0.1:8000/", stream=True) with pytest.raises(PoolTimeout): await client.get("http://localhost:8000/") await response.read()
async def test_disallow_redirects(): client = AsyncClient(dispatch=MockDispatch()) response = await client.post("https://example.org/redirect_303", allow_redirects=False) assert response.status_code == codes.SEE_OTHER assert response.url == URL("https://example.org/redirect_303") assert len(response.history) == 0 response = await response.next() assert response.status_code == codes.OK assert response.url == URL("https://example.org/") assert len(response.history) == 1
async def test_multiple_redirects(): client = AsyncClient(dispatch=MockDispatch()) response = await client.get( "https://example.org/multiple_redirects?count=20") assert response.status_code == codes.OK assert response.url == URL("https://example.org/multiple_redirects") assert len(response.history) == 20 assert response.history[0].url == URL( "https://example.org/multiple_redirects?count=20") assert response.history[1].url == URL( "https://example.org/multiple_redirects?count=19") assert len(response.history[0].history) == 0 assert len(response.history[1].history) == 1
async def handle(path: str): request: quart.Request = quart.request embeds = "" with open(app_config['index_path'], 'r') as f: text: str = f.read() try: if path.startswith('i/') or path == '': pass else: async with AsyncClient() as http: headers = {} if 'sistoken' in request.cookies: headers['Authorization'] = request.cookies.get('sistoken') resp = await http.get(app_config['api'] + '/users/' + path, headers=headers) j = resp.json() success = j['success'] embeds += title( '@' + j['user']['username'] if success else 'User not found') embeds += meta( 'og:description', '@' + j['user']['username'] + ' is on CraftJobs!' if success else 'CraftJobs') j['target'] = path.lower() text = text.replace('"sister-preload"', json.dumps(j, separators=(',', ':'))) if success: embeds += meta('og:image', j['user']['avatarUrl']) embeds += meta('og:url', 'https://craftjobs.net/' + path) embeds += meta('theme-color', '#FFAAFF') text = text.replace('<sister-embeds></sister-embeds>', embeds) return text # Panic mode to stop 500s except Exception as e: print(e) return text
async def test_redirect_loop(): client = AsyncClient(dispatch=MockDispatch()) with pytest.raises(RedirectLoop): await client.get("https://example.org/redirect_loop")
async def test_too_many_redirects(): client = AsyncClient(dispatch=MockDispatch()) with pytest.raises(TooManyRedirects): await client.get("https://example.org/multiple_redirects?count=21")
async def test_no_scheme_redirect(): client = AsyncClient(dispatch=MockDispatch()) response = await client.get("https://example.org/no_scheme_redirect") assert response.status_code == codes.OK assert response.url == URL("https://example.org/") assert len(response.history) == 1
async def test_read_timeout(server): timeout = TimeoutConfig(read_timeout=0.000001) async with AsyncClient(timeout=timeout) as client: with pytest.raises(ReadTimeout): await client.get("http://127.0.0.1:8000/slow_response")