test_redirects.py
This file
Path
tests/client/test_redirects.py
243 lines · 8.5 KiB · Python
Last changed by
Work on bringing API into parity with `requests`. (#76)Tom Christiebefore adoption9574041
History of this fileTake it
Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 1–7why, one step back1import json2from urllib.parse import parse_qs34import pytest56from httpcore import (7 URL,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
line 8why, one step back8 AsyncClient,Work on bringing API into parity with `requests`. (#76)Tom Christiebefore adoption9574041
line 9why, one step back9 CertTypes,Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 10–14why, one step back10 Dispatcher,11 RedirectBodyUnavailable,12 RedirectLoop,13 Request,14 Response,Work on bringing API into parity with `requests`. (#76)Tom Christiebefore adoption9574041
line 15why, one step back15 TimeoutTypes,Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
line 16why, one step back16 TooManyRedirects,Work on bringing API into parity with `requests`. (#76)Tom Christiebefore adoption9574041
line 17why, one step back17 VerifyTypes,Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 18–26why, one step back18 codes,19)202122class MockDispatch(Dispatcher):23 async def send(24 self,25 request: Request,26 stream: bool = False,Work on bringing API into parity with `requests`. (#76)Tom Christiebefore adoption9574041
lines 27–29why, one step back27 verify: VerifyTypes = None,28 cert: CertTypes = None,29 timeout: TimeoutTypes = None,Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 30–31why, one step back30 ) -> Response:31 if request.url.path == "/redirect_301":Merge pull request #59 from encode/use-stdlib-status-codesTom Christiebefore adoptionafc8369
line 32why, one step back32 status_code = codes.MOVED_PERMANENTLYMerge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 33–36why, one step back33 headers = {"location": "https://example.org/"}34 return Response(status_code, headers=headers, request=request)3536 elif request.url.path == "/redirect_302":Merge pull request #59 from encode/use-stdlib-status-codesTom Christiebefore adoptionafc8369
line 37why, one step back37 status_code = codes.FOUNDMerge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 38–41why, one step back38 headers = {"location": "https://example.org/"}39 return Response(status_code, headers=headers, request=request)4041 elif request.url.path == "/redirect_303":Merge pull request #59 from encode/use-stdlib-status-codesTom Christiebefore adoptionafc8369
line 42why, one step back42 status_code = codes.SEE_OTHERMerge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 43–47why, one step back43 headers = {"location": "https://example.org/"}44 return Response(status_code, headers=headers, request=request)4546 elif request.url.path == "/relative_redirect":47 headers = {"location": "/"}Merge pull request #59 from encode/use-stdlib-status-codesTom Christiebefore adoptionafc8369
line 48why, one step back48 return Response(codes.SEE_OTHER, headers=headers, request=request)Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 49–51why, one step back4950 elif request.url.path == "/no_scheme_redirect":51 headers = {"location": "//example.org/"}Merge pull request #59 from encode/use-stdlib-status-codesTom Christiebefore adoptionafc8369
line 52why, one step back52 return Response(codes.SEE_OTHER, headers=headers, request=request)Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 53–57why, one step back5354 elif request.url.path == "/multiple_redirects":55 params = parse_qs(request.url.query)56 count = int(params.get("count", "0")[0])57 redirect_count = count - 1Merge pull request #59 from encode/use-stdlib-status-codesTom Christiebefore adoptionafc8369
line 58why, one step back58 code = codes.SEE_OTHER if count else codes.OKMerge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 59–66why, one step back59 location = "/multiple_redirects"60 if redirect_count:61 location += "?count=" + str(redirect_count)62 headers = {"location": location} if count else {}63 return Response(code, headers=headers, request=request)6465 if request.url.path == "/redirect_loop":66 headers = {"location": "/redirect_loop"}Merge pull request #59 from encode/use-stdlib-status-codesTom Christiebefore adoptionafc8369
line 67why, one step back67 return Response(codes.SEE_OTHER, headers=headers, request=request)Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 68–70why, one step back6869 elif request.url.path == "/cross_domain":70 headers = {"location": "https://example.org/cross_domain_target"}Merge pull request #59 from encode/use-stdlib-status-codesTom Christiebefore adoptionafc8369
line 71why, one step back71 return Response(codes.SEE_OTHER, headers=headers, request=request)Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 72–75why, one step back7273 elif request.url.path == "/cross_domain_target":74 headers = dict(request.headers.items())75 content = json.dumps({"headers": headers}).encode()Merge pull request #59 from encode/use-stdlib-status-codesTom Christiebefore adoptionafc8369
line 76why, one step back76 return Response(codes.OK, content=content, request=request)Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 77–80why, one step back7778 elif request.url.path == "/redirect_body":79 await request.read()80 headers = {"location": "/redirect_body_target"}Merge pull request #59 from encode/use-stdlib-status-codesTom Christiebefore adoptionafc8369
line 81why, one step back81 return Response(codes.PERMANENT_REDIRECT, headers=headers, request=request)Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 82–85why, one step back8283 elif request.url.path == "/redirect_body_target":84 content = await request.read()85 body = json.dumps({"body": content.decode()}).encode()Merge pull request #59 from encode/use-stdlib-status-codesTom Christiebefore adoptionafc8369
line 86why, one step back86 return Response(codes.OK, content=body, request=request)Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
line 87why, one step back87Merge pull request #59 from encode/use-stdlib-status-codesTom Christiebefore adoptionafc8369
line 88why, one step back88 return Response(codes.OK, content=b"Hello, world!", request=request)Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 89–92why, one step back899091@pytest.mark.asyncio92async def test_redirect_301():Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
line 93why, one step back93 client = AsyncClient(dispatch=MockDispatch())Tidy up redirect testsTom Christiebefore adoptione40b0bf
line 94why, one step back94 response = await client.post("https://example.org/redirect_301")Merge pull request #59 from encode/use-stdlib-status-codesTom Christiebefore adoptionafc8369
line 95why, one step back95 assert response.status_code == codes.OKMerge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 96–101why, one step back96 assert response.url == URL("https://example.org/")97 assert len(response.history) == 19899100@pytest.mark.asyncio101async def test_redirect_302():Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
line 102why, one step back102 client = AsyncClient(dispatch=MockDispatch())Tidy up redirect testsTom Christiebefore adoptione40b0bf
line 103why, one step back103 response = await client.post("https://example.org/redirect_302")Merge pull request #59 from encode/use-stdlib-status-codesTom Christiebefore adoptionafc8369
line 104why, one step back104 assert response.status_code == codes.OKMerge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 105–110why, one step back105 assert response.url == URL("https://example.org/")106 assert len(response.history) == 1107108109@pytest.mark.asyncio110async def test_redirect_303():Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
line 111why, one step back111 client = AsyncClient(dispatch=MockDispatch())Tidy up redirect testsTom Christiebefore adoptione40b0bf
line 112why, one step back112 response = await client.get("https://example.org/redirect_303")Merge pull request #59 from encode/use-stdlib-status-codesTom Christiebefore adoptionafc8369
line 113why, one step back113 assert response.status_code == codes.OKMerge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 114–119why, one step back114 assert response.url == URL("https://example.org/")115 assert len(response.history) == 1116117118@pytest.mark.asyncio119async def test_disallow_redirects():Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
line 120why, one step back120 client = AsyncClient(dispatch=MockDispatch())Tidy up redirect testsTom Christiebefore adoptione40b0bf
lines 121–122why, one step back121 response = await client.post(122 "https://example.org/redirect_303", allow_redirects=FalseMerge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
line 123why, one step back123 )Merge pull request #59 from encode/use-stdlib-status-codesTom Christiebefore adoptionafc8369
line 124why, one step back124 assert response.status_code == codes.SEE_OTHERMerge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 125–128why, one step back125 assert response.url == URL("https://example.org/redirect_303")126 assert len(response.history) == 0127128 response = await response.next()Merge pull request #59 from encode/use-stdlib-status-codesTom Christiebefore adoptionafc8369
line 129why, one step back129 assert response.status_code == codes.OKMerge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 130–135why, one step back130 assert response.url == URL("https://example.org/")131 assert len(response.history) == 1132133134@pytest.mark.asyncio135async def test_relative_redirect():Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
line 136why, one step back136 client = AsyncClient(dispatch=MockDispatch())Tidy up redirect testsTom Christiebefore adoptione40b0bf
line 137why, one step back137 response = await client.get("https://example.org/relative_redirect")Merge pull request #59 from encode/use-stdlib-status-codesTom Christiebefore adoptionafc8369
line 138why, one step back138 assert response.status_code == codes.OKMerge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 139–144why, one step back139 assert response.url == URL("https://example.org/")140 assert len(response.history) == 1141142143@pytest.mark.asyncio144async def test_no_scheme_redirect():Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
line 145why, one step back145 client = AsyncClient(dispatch=MockDispatch())Tidy up redirect testsTom Christiebefore adoptione40b0bf
line 146why, one step back146 response = await client.get("https://example.org/no_scheme_redirect")Merge pull request #59 from encode/use-stdlib-status-codesTom Christiebefore adoptionafc8369
line 147why, one step back147 assert response.status_code == codes.OKMerge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 148–153why, one step back148 assert response.url == URL("https://example.org/")149 assert len(response.history) == 1150151152@pytest.mark.asyncio153async def test_fragment_redirect():Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
line 154why, one step back154 client = AsyncClient(dispatch=MockDispatch())Tidy up redirect testsTom Christiebefore adoptione40b0bf
line 155why, one step back155 response = await client.get("https://example.org/relative_redirect#fragment")Merge pull request #59 from encode/use-stdlib-status-codesTom Christiebefore adoptionafc8369
line 156why, one step back156 assert response.status_code == codes.OKMerge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 157–162why, one step back157 assert response.url == URL("https://example.org/#fragment")158 assert len(response.history) == 1159160161@pytest.mark.asyncio162async def test_multiple_redirects():Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
line 163why, one step back163 client = AsyncClient(dispatch=MockDispatch())Tidy up redirect testsTom Christiebefore adoptione40b0bf
line 164why, one step back164 response = await client.get("https://example.org/multiple_redirects?count=20")Merge pull request #59 from encode/use-stdlib-status-codesTom Christiebefore adoptionafc8369
line 165why, one step back165 assert response.status_code == codes.OKMerge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 166–171why, one step back166 assert response.url == URL("https://example.org/multiple_redirects")167 assert len(response.history) == 20168169170@pytest.mark.asyncio171async def test_too_many_redirects():Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
line 172why, one step back172 client = AsyncClient(dispatch=MockDispatch())Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
line 173why, one step back173 with pytest.raises(TooManyRedirects):Tidy up redirect testsTom Christiebefore adoptione40b0bf
line 174why, one step back174 await client.get("https://example.org/multiple_redirects?count=21")Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 175–178why, one step back175176177@pytest.mark.asyncio178async def test_too_many_redirects_calling_next():Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
line 179why, one step back179 client = AsyncClient(dispatch=MockDispatch())Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
line 180why, one step back180 url = "https://example.org/multiple_redirects?count=21"Tidy up redirect testsTom Christiebefore adoptione40b0bf
line 181why, one step back181 response = await client.get(url, allow_redirects=False)Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 182–188why, one step back182 with pytest.raises(TooManyRedirects):183 while response.is_redirect:184 response = await response.next()185186187@pytest.mark.asyncio188async def test_redirect_loop():Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
line 189why, one step back189 client = AsyncClient(dispatch=MockDispatch())Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
line 190why, one step back190 with pytest.raises(RedirectLoop):Tidy up redirect testsTom Christiebefore adoptione40b0bf
line 191why, one step back191 await client.get("https://example.org/redirect_loop")Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 192–195why, one step back192193194@pytest.mark.asyncio195async def test_redirect_loop_calling_next():Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
line 196why, one step back196 client = AsyncClient(dispatch=MockDispatch())Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
line 197why, one step back197 url = "https://example.org/redirect_loop"Tidy up redirect testsTom Christiebefore adoptione40b0bf
line 198why, one step back198 response = await client.get(url, allow_redirects=False)Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 199–205why, one step back199 with pytest.raises(RedirectLoop):200 while response.is_redirect:201 response = await response.next()202203204@pytest.mark.asyncio205async def test_cross_domain_redirect():Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
line 206why, one step back206 client = AsyncClient(dispatch=MockDispatch())Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 207–208why, one step back207 url = "https://example.com/cross_domain"208 headers = {"Authorization": "abc"}Tidy up redirect testsTom Christiebefore adoptione40b0bf
line 209why, one step back209 response = await client.get(url, headers=headers)Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
line 210why, one step back210 assert response.url == URL("https://example.org/cross_domain_target")Add response.json()Tom Christiebefore adoption2116d44
line 211why, one step back211 assert "authorization" not in response.json()["headers"]Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 212–215why, one step back212213214@pytest.mark.asyncio215async def test_same_domain_redirect():Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
line 216why, one step back216 client = AsyncClient(dispatch=MockDispatch())Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 217–218why, one step back217 url = "https://example.org/cross_domain"218 headers = {"Authorization": "abc"}Tidy up redirect testsTom Christiebefore adoptione40b0bf
line 219why, one step back219 response = await client.get(url, headers=headers)Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
line 220why, one step back220 assert response.url == URL("https://example.org/cross_domain_target")Add response.json()Tom Christiebefore adoption2116d44
line 221why, one step back221 assert response.json()["headers"]["authorization"] == "abc"Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 222–225why, one step back222223224@pytest.mark.asyncio225async def test_body_redirect():Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
line 226why, one step back226 client = AsyncClient(dispatch=MockDispatch())Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 227–228why, one step back227 url = "https://example.org/redirect_body"228 data = b"Example request body"Tidy up redirect testsTom Christiebefore adoptione40b0bf
line 229why, one step back229 response = await client.post(url, data=data)Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
line 230why, one step back230 assert response.url == URL("https://example.org/redirect_body_target")Add response.json()Tom Christiebefore adoption2116d44
line 231why, one step back231 assert response.json() == {"body": "Example request body"}Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 232–235why, one step back232233234@pytest.mark.asyncio235async def test_cannot_redirect_streaming_body():Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
line 236why, one step back236 client = AsyncClient(dispatch=MockDispatch())Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 237–242why, one step back237 url = "https://example.org/redirect_body"238239 async def streaming_body():240 yield b"Example request body"241242 with pytest.raises(RedirectBodyUnavailable):Tidy up redirect testsTom Christiebefore adoptione40b0bf
line 243why, one step back243 await client.post(url, data=streaming_body())