test_redirects.py
This file
Path
tests/client/test_redirects.py
252 lines · 8.9 KiB · Python
Last changed by
Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
History of this fileTake it
1import json2from urllib.parse import parse_qs34import pytest56from httpx import (7 URL,8 AsyncClient,9 AsyncDispatcher,10 AsyncRequest,11 AsyncResponse,12 CertTypes,13 RedirectBodyUnavailable,14 RedirectLoop,15 TimeoutTypes,16 TooManyRedirects,17 VerifyTypes,18 codes,19)202122class MockDispatch(AsyncDispatcher):23 async def send(24 self,25 request: AsyncRequest,26 verify: VerifyTypes = None,27 cert: CertTypes = None,28 timeout: TimeoutTypes = None,29 ) -> AsyncResponse:30 if request.url.path == "/redirect_301":31 status_code = codes.MOVED_PERMANENTLY32 headers = {"location": "https://example.org/"}33 return AsyncResponse(status_code, headers=headers, request=request)3435 elif request.url.path == "/redirect_302":36 status_code = codes.FOUND37 headers = {"location": "https://example.org/"}38 return AsyncResponse(status_code, headers=headers, request=request)3940 elif request.url.path == "/redirect_303":41 status_code = codes.SEE_OTHER42 headers = {"location": "https://example.org/"}43 return AsyncResponse(status_code, headers=headers, request=request)4445 elif request.url.path == "/relative_redirect":46 headers = {"location": "/"}47 return AsyncResponse(codes.SEE_OTHER, headers=headers, request=request)4849 elif request.url.path == "/no_scheme_redirect":50 headers = {"location": "//example.org/"}51 return AsyncResponse(codes.SEE_OTHER, headers=headers, request=request)5253 elif request.url.path == "/multiple_redirects":54 params = parse_qs(request.url.query)55 count = int(params.get("count", "0")[0])56 redirect_count = count - 157 code = codes.SEE_OTHER if count else codes.OK58 location = "/multiple_redirects"59 if redirect_count:60 location += "?count=" + str(redirect_count)61 headers = {"location": location} if count else {}62 return AsyncResponse(code, headers=headers, request=request)6364 if request.url.path == "/redirect_loop":65 headers = {"location": "/redirect_loop"}66 return AsyncResponse(codes.SEE_OTHER, headers=headers, request=request)6768 elif request.url.path == "/cross_domain":69 headers = {"location": "https://example.org/cross_domain_target"}70 return AsyncResponse(codes.SEE_OTHER, headers=headers, request=request)7172 elif request.url.path == "/cross_domain_target":73 headers = dict(request.headers.items())74 content = json.dumps({"headers": headers}).encode()75 return AsyncResponse(codes.OK, content=content, request=request)7677 elif request.url.path == "/redirect_body":78 await request.read()79 headers = {"location": "/redirect_body_target"}80 return AsyncResponse(81 codes.PERMANENT_REDIRECT, headers=headers, request=request82 )8384 elif request.url.path == "/redirect_body_target":85 content = await request.read()86 body = json.dumps({"body": content.decode()}).encode()87 return AsyncResponse(codes.OK, content=body, request=request)8889 return AsyncResponse(codes.OK, content=b"Hello, world!", request=request)909192@pytest.mark.asyncio93async def test_redirect_301():94 client = AsyncClient(dispatch=MockDispatch())95 response = await client.post("https://example.org/redirect_301")96 assert response.status_code == codes.OK97 assert response.url == URL("https://example.org/")98 assert len(response.history) == 199100101@pytest.mark.asyncio102async def test_redirect_302():103 client = AsyncClient(dispatch=MockDispatch())104 response = await client.post("https://example.org/redirect_302")105 assert response.status_code == codes.OK106 assert response.url == URL("https://example.org/")107 assert len(response.history) == 1108109110@pytest.mark.asyncio111async def test_redirect_303():112 client = AsyncClient(dispatch=MockDispatch())113 response = await client.get("https://example.org/redirect_303")114 assert response.status_code == codes.OK115 assert response.url == URL("https://example.org/")116 assert len(response.history) == 1117118119@pytest.mark.asyncio120async def test_disallow_redirects():121 client = AsyncClient(dispatch=MockDispatch())122 response = await client.post(123 "https://example.org/redirect_303", allow_redirects=False124 )125 assert response.status_code == codes.SEE_OTHER126 assert response.url == URL("https://example.org/redirect_303")127 assert len(response.history) == 0128129 response = await response.next()130 assert response.status_code == codes.OK131 assert response.url == URL("https://example.org/")132 assert len(response.history) == 1133134135@pytest.mark.asyncio136async def test_relative_redirect():137 client = AsyncClient(dispatch=MockDispatch())138 response = await client.get("https://example.org/relative_redirect")139 assert response.status_code == codes.OK140 assert response.url == URL("https://example.org/")141 assert len(response.history) == 1142143144@pytest.mark.asyncio145async def test_no_scheme_redirect():146 client = AsyncClient(dispatch=MockDispatch())147 response = await client.get("https://example.org/no_scheme_redirect")148 assert response.status_code == codes.OK149 assert response.url == URL("https://example.org/")150 assert len(response.history) == 1151152153@pytest.mark.asyncio154async def test_fragment_redirect():155 client = AsyncClient(dispatch=MockDispatch())156 response = await client.get("https://example.org/relative_redirect#fragment")157 assert response.status_code == codes.OK158 assert response.url == URL("https://example.org/#fragment")159 assert len(response.history) == 1160161162@pytest.mark.asyncio163async def test_multiple_redirects():164 client = AsyncClient(dispatch=MockDispatch())165 response = await client.get("https://example.org/multiple_redirects?count=20")166 assert response.status_code == codes.OK167 assert response.url == URL("https://example.org/multiple_redirects")168 assert len(response.history) == 20169 assert response.history[0].url == URL(170 "https://example.org/multiple_redirects?count=20"171 )172 assert response.history[1].url == URL(173 "https://example.org/multiple_redirects?count=19"174 )175 assert len(response.history[0].history) == 0176 assert len(response.history[1].history) == 1177178179@pytest.mark.asyncio180async def test_too_many_redirects():181 client = AsyncClient(dispatch=MockDispatch())182 with pytest.raises(TooManyRedirects):183 await client.get("https://example.org/multiple_redirects?count=21")184185186@pytest.mark.asyncio187async def test_too_many_redirects_calling_next():188 client = AsyncClient(dispatch=MockDispatch())189 url = "https://example.org/multiple_redirects?count=21"190 response = await client.get(url, allow_redirects=False)191 with pytest.raises(TooManyRedirects):192 while response.is_redirect:193 response = await response.next()194195196@pytest.mark.asyncio197async def test_redirect_loop():198 client = AsyncClient(dispatch=MockDispatch())199 with pytest.raises(RedirectLoop):200 await client.get("https://example.org/redirect_loop")201202203@pytest.mark.asyncio204async def test_redirect_loop_calling_next():205 client = AsyncClient(dispatch=MockDispatch())206 url = "https://example.org/redirect_loop"207 response = await client.get(url, allow_redirects=False)208 with pytest.raises(RedirectLoop):209 while response.is_redirect:210 response = await response.next()211212213@pytest.mark.asyncio214async def test_cross_domain_redirect():215 client = AsyncClient(dispatch=MockDispatch())216 url = "https://example.com/cross_domain"217 headers = {"Authorization": "abc"}218 response = await client.get(url, headers=headers)219 assert response.url == URL("https://example.org/cross_domain_target")220 assert "authorization" not in response.json()["headers"]221222223@pytest.mark.asyncio224async def test_same_domain_redirect():225 client = AsyncClient(dispatch=MockDispatch())226 url = "https://example.org/cross_domain"227 headers = {"Authorization": "abc"}228 response = await client.get(url, headers=headers)229 assert response.url == URL("https://example.org/cross_domain_target")230 assert response.json()["headers"]["authorization"] == "abc"231232233@pytest.mark.asyncio234async def test_body_redirect():235 client = AsyncClient(dispatch=MockDispatch())236 url = "https://example.org/redirect_body"237 data = b"Example request body"238 response = await client.post(url, data=data)239 assert response.url == URL("https://example.org/redirect_body_target")240 assert response.json() == {"body": "Example request body"}241242243@pytest.mark.asyncio244async def test_cannot_redirect_streaming_body():245 client = AsyncClient(dispatch=MockDispatch())246 url = "https://example.org/redirect_body"247248 async def streaming_body():249 yield b"Example request body"250251 with pytest.raises(RedirectBodyUnavailable):252 await client.post(url, data=streaming_body())