async def test_redirect_loop_calling_next(): client = RedirectAdapter(MockDispatch()) url = "https://example.org/redirect_loop" response = await client.request("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 = RedirectAdapter(MockDispatch()) url = "https://example.org/multiple_redirects?count=21" response = await client.request("GET", url, allow_redirects=False) with pytest.raises(TooManyRedirects): while response.is_redirect: response = await response.next()
async def test_multiple_redirects(): client = RedirectAdapter(MockDispatch()) url = "https://example.org/multiple_redirects?count=20" response = await client.request("GET", url) assert response.status_code == codes.ok assert response.url == URL("https://example.org/multiple_redirects") assert len(response.history) == 20
async def test_fragment_redirect(): client = RedirectAdapter(MockDispatch()) url = "https://example.org/relative_redirect#fragment" response = await client.request("GET", url) assert response.status_code == codes.ok assert response.url == URL("https://example.org/#fragment") assert len(response.history) == 1
async def test_no_scheme_redirect(): client = RedirectAdapter(MockDispatch()) response = await client.request("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_body_redirect(): client = RedirectAdapter(MockDispatch()) url = "https://example.org/redirect_body" data = b"Example request body" response = await client.request("POST", url, data=data) data = json.loads(response.content.decode()) assert response.url == URL("https://example.org/redirect_body_target") assert data == {"body": "Example request body"}
async def test_same_domain_redirect(): client = RedirectAdapter(MockDispatch()) url = "https://example.org/cross_domain" headers = {"Authorization": "abc"} response = await client.request("GET", url, headers=headers) data = json.loads(response.content.decode()) assert response.url == URL("https://example.org/cross_domain_target") assert data == {"headers": {"authorization": "abc"}}
async def test_cannot_redirect_streaming_body(): client = RedirectAdapter(MockDispatch()) url = "https://example.org/redirect_body" async def streaming_body(): yield b"Example request body" with pytest.raises(RedirectBodyUnavailable): await client.request("POST", url, data=streaming_body())
async def test_disallow_redirects(): client = RedirectAdapter(MockDispatch()) response = await client.request("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_redirect_loop(): client = RedirectAdapter(MockDispatch()) with pytest.raises(RedirectLoop): await client.request("GET", "https://example.org/redirect_loop")
async def test_too_many_redirects(): client = RedirectAdapter(MockDispatch()) with pytest.raises(TooManyRedirects): await client.request( "GET", "https://example.org/multiple_redirects?count=21")