def test_url__params(): url = URL("https://example.org:123/path/to/somewhere", params={"a": "123"}) assert str(url) == "https://example.org:123/path/to/somewhere?a=123" url = URL("https://example.org:123/path/to/somewhere?b=456", params={"a": "123"}) assert str(url) == "https://example.org:123/path/to/somewhere?a=123"
def test_url_set(): urls = ( URL("http://example.org:123/path/to/somewhere"), URL("http://example.org:123/path/to/somewhere/else"), ) url_set = set(urls) assert all(url in urls for url in url_set)
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_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
def test_url(): url = URL("https://example.org:123/path/to/somewhere?abc=123#anchor") assert url.scheme == "https" assert url.host == "example.org" assert url.port == 123 assert url.authority == "example.org:123" assert url.path == "/path/to/somewhere" assert url.query == "abc=123" assert url.fragment == "anchor" assert (repr(url) == "URL('https://example.org:123/path/to/somewhere?abc=123#anchor')") new = url.copy_with(scheme="http") assert new == URL( "http://example.org:123/path/to/somewhere?abc=123#anchor") assert new.scheme == "http"
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 = 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_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_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
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_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"}}
def test_idna_url(): url = URL("http://中国.icom.museum:80/") assert url == URL("http://xn--fiqs8s.icom.museum:80/") assert url.host == "xn--fiqs8s.icom.museum"
async def test_redirect_301(): client = AsyncClient(dispatch=MockDispatch()) response = await client.post("https://example.org/redirect_301") assert response.status_code == codes.OK assert response.url == URL("https://example.org/") assert len(response.history) == 1