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
Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 1–5why, one step back1import json2from urllib.parse import parse_qs34import pytest5Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 6why, one step back6from httpx import (Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
line 7why, one step back7 URL,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
line 8why, one step back8 AsyncClient,Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7
lines 9–11why, one step back9 AsyncDispatcher,10 AsyncRequest,11 AsyncResponse,Work on bringing API into parity with `requests`. (#76)Tom Christiebefore adoption9574041
line 12why, one step back12 CertTypes,Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 13–14why, one step back13 RedirectBodyUnavailable,14 RedirectLoop,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–21why, one step back18 codes,19)2021Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7
line 22why, one step back22class MockDispatch(AsyncDispatcher):Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 23–24why, one step back23 async def send(24 self,Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7
line 25why, one step back25 request: AsyncRequest,Work on bringing API into parity with `requests`. (#76)Tom Christiebefore adoption9574041
lines 26–28why, one step back26 verify: VerifyTypes = None,27 cert: CertTypes = None,28 timeout: TimeoutTypes = None,Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7
line 29why, one step back29 ) -> AsyncResponse:Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
line 30why, one step back30 if request.url.path == "/redirect_301":Merge pull request #59 from encode/use-stdlib-status-codesTom Christiebefore adoptionafc8369
line 31why, one step back31 status_code = codes.MOVED_PERMANENTLYMerge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
line 32why, one step back32 headers = {"location": "https://example.org/"}Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7
line 33why, one step back33 return AsyncResponse(status_code, headers=headers, request=request)Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 34–35why, one step back3435 elif request.url.path == "/redirect_302":Merge pull request #59 from encode/use-stdlib-status-codesTom Christiebefore adoptionafc8369
line 36why, one step back36 status_code = codes.FOUNDMerge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
line 37why, one step back37 headers = {"location": "https://example.org/"}Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7
line 38why, one step back38 return AsyncResponse(status_code, headers=headers, request=request)Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 39–40why, one step back3940 elif request.url.path == "/redirect_303":Merge pull request #59 from encode/use-stdlib-status-codesTom Christiebefore adoptionafc8369
line 41why, one step back41 status_code = codes.SEE_OTHERMerge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
line 42why, one step back42 headers = {"location": "https://example.org/"}Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7
line 43why, one step back43 return AsyncResponse(status_code, headers=headers, request=request)Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 44–46why, one step back4445 elif request.url.path == "/relative_redirect":46 headers = {"location": "/"}Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7
line 47why, one step back47 return AsyncResponse(codes.SEE_OTHER, headers=headers, request=request)Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 48–50why, one step back4849 elif request.url.path == "/no_scheme_redirect":50 headers = {"location": "//example.org/"}Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7
line 51why, one step back51 return AsyncResponse(codes.SEE_OTHER, headers=headers, request=request)Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 52–56why, one step back5253 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 - 1Merge pull request #59 from encode/use-stdlib-status-codesTom Christiebefore adoptionafc8369
line 57why, one step back57 code = codes.SEE_OTHER if count else codes.OKMerge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 58–61why, one step back58 location = "/multiple_redirects"59 if redirect_count:60 location += "?count=" + str(redirect_count)61 headers = {"location": location} if count else {}Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7
line 62why, one step back62 return AsyncResponse(code, headers=headers, request=request)Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 63–65why, one step back6364 if request.url.path == "/redirect_loop":65 headers = {"location": "/redirect_loop"}Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7
line 66why, one step back66 return AsyncResponse(codes.SEE_OTHER, headers=headers, request=request)Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 67–69why, one step back6768 elif request.url.path == "/cross_domain":69 headers = {"location": "https://example.org/cross_domain_target"}Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7
line 70why, one step back70 return AsyncResponse(codes.SEE_OTHER, headers=headers, request=request)Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 71–74why, one step back7172 elif request.url.path == "/cross_domain_target":73 headers = dict(request.headers.items())74 content = json.dumps({"headers": headers}).encode()Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7
line 75why, one step back75 return AsyncResponse(codes.OK, content=content, request=request)Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 76–79why, one step back7677 elif request.url.path == "/redirect_body":78 await request.read()79 headers = {"location": "/redirect_body_target"}Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7
lines 80–82why, one step back80 return AsyncResponse(81 codes.PERMANENT_REDIRECT, headers=headers, request=request82 )Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 83–86why, one step back8384 elif request.url.path == "/redirect_body_target":85 content = await request.read()86 body = json.dumps({"body": content.decode()}).encode()Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7
line 87why, one step back87 return AsyncResponse(codes.OK, content=body, request=request)Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
line 88why, one step back88Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7
line 89why, one step back89 return AsyncResponse(codes.OK, content=b"Hello, world!", request=request)Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 90–93why, one step back909192@pytest.mark.asyncio93async def test_redirect_301():Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
line 94why, one step back94 client = AsyncClient(dispatch=MockDispatch())Tidy up redirect testsTom Christiebefore adoptione40b0bf
line 95why, one step back95 response = await client.post("https://example.org/redirect_301")Merge pull request #59 from encode/use-stdlib-status-codesTom Christiebefore adoptionafc8369
line 96why, one step back96 assert response.status_code == codes.OKMerge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 97–102why, one step back97 assert response.url == URL("https://example.org/")98 assert len(response.history) == 199100101@pytest.mark.asyncio102async def test_redirect_302():Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
line 103why, one step back103 client = AsyncClient(dispatch=MockDispatch())Tidy up redirect testsTom Christiebefore adoptione40b0bf
line 104why, one step back104 response = await client.post("https://example.org/redirect_302")Merge pull request #59 from encode/use-stdlib-status-codesTom Christiebefore adoptionafc8369
line 105why, one step back105 assert response.status_code == codes.OKMerge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 106–111why, one step back106 assert response.url == URL("https://example.org/")107 assert len(response.history) == 1108109110@pytest.mark.asyncio111async def test_redirect_303():Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
line 112why, one step back112 client = AsyncClient(dispatch=MockDispatch())Tidy up redirect testsTom Christiebefore adoptione40b0bf
line 113why, one step back113 response = await client.get("https://example.org/redirect_303")Merge pull request #59 from encode/use-stdlib-status-codesTom Christiebefore adoptionafc8369
line 114why, one step back114 assert response.status_code == codes.OKMerge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 115–120why, one step back115 assert response.url == URL("https://example.org/")116 assert len(response.history) == 1117118119@pytest.mark.asyncio120async def test_disallow_redirects():Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
line 121why, one step back121 client = AsyncClient(dispatch=MockDispatch())Tidy up redirect testsTom Christiebefore adoptione40b0bf
lines 122–123why, one step back122 response = await client.post(123 "https://example.org/redirect_303", allow_redirects=FalseMerge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
line 124why, one step back124 )Merge pull request #59 from encode/use-stdlib-status-codesTom Christiebefore adoptionafc8369
line 125why, one step back125 assert response.status_code == codes.SEE_OTHERMerge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 126–129why, one step back126 assert response.url == URL("https://example.org/redirect_303")127 assert len(response.history) == 0128129 response = await response.next()Merge pull request #59 from encode/use-stdlib-status-codesTom Christiebefore adoptionafc8369
line 130why, one step back130 assert response.status_code == codes.OKMerge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 131–136why, one step back131 assert response.url == URL("https://example.org/")132 assert len(response.history) == 1133134135@pytest.mark.asyncio136async def test_relative_redirect():Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
line 137why, one step back137 client = AsyncClient(dispatch=MockDispatch())Tidy up redirect testsTom Christiebefore adoptione40b0bf
line 138why, one step back138 response = await client.get("https://example.org/relative_redirect")Merge pull request #59 from encode/use-stdlib-status-codesTom Christiebefore adoptionafc8369
line 139why, one step back139 assert response.status_code == codes.OKMerge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 140–145why, one step back140 assert response.url == URL("https://example.org/")141 assert len(response.history) == 1142143144@pytest.mark.asyncio145async def test_no_scheme_redirect():Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
line 146why, one step back146 client = AsyncClient(dispatch=MockDispatch())Tidy up redirect testsTom Christiebefore adoptione40b0bf
line 147why, one step back147 response = await client.get("https://example.org/no_scheme_redirect")Merge pull request #59 from encode/use-stdlib-status-codesTom Christiebefore adoptionafc8369
line 148why, one step back148 assert response.status_code == codes.OKMerge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 149–154why, one step back149 assert response.url == URL("https://example.org/")150 assert len(response.history) == 1151152153@pytest.mark.asyncio154async def test_fragment_redirect():Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
line 155why, one step back155 client = AsyncClient(dispatch=MockDispatch())Tidy up redirect testsTom Christiebefore adoptione40b0bf
line 156why, one step back156 response = await client.get("https://example.org/relative_redirect#fragment")Merge pull request #59 from encode/use-stdlib-status-codesTom Christiebefore adoptionafc8369
line 157why, one step back157 assert response.status_code == codes.OKMerge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 158–163why, one step back158 assert response.url == URL("https://example.org/#fragment")159 assert len(response.history) == 1160161162@pytest.mark.asyncio163async def test_multiple_redirects():Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
line 164why, one step back164 client = AsyncClient(dispatch=MockDispatch())Tidy up redirect testsTom Christiebefore adoptione40b0bf
line 165why, one step back165 response = await client.get("https://example.org/multiple_redirects?count=20")Merge pull request #59 from encode/use-stdlib-status-codesTom Christiebefore adoptionafc8369
line 166why, one step back166 assert response.status_code == codes.OKMerge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 167–168why, one step back167 assert response.url == URL("https://example.org/multiple_redirects")168 assert len(response.history) == 20Multipart support (#90)Tom Christiebefore adoption6a44b05
lines 169–174why, one step back169 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 )Tweaks (#89)Tom Christiebefore adoption7c6fb5c
lines 175–176why, one step back175 assert len(response.history[0].history) == 0176 assert len(response.history[1].history) == 1Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 177–180why, one step back177178179@pytest.mark.asyncio180async def test_too_many_redirects():Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
line 181why, one step back181 client = AsyncClient(dispatch=MockDispatch())Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
line 182why, one step back182 with pytest.raises(TooManyRedirects):Tidy up redirect testsTom Christiebefore adoptione40b0bf
line 183why, one step back183 await client.get("https://example.org/multiple_redirects?count=21")Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 184–187why, one step back184185186@pytest.mark.asyncio187async def test_too_many_redirects_calling_next():Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
line 188why, one step back188 client = AsyncClient(dispatch=MockDispatch())Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
line 189why, one step back189 url = "https://example.org/multiple_redirects?count=21"Tidy up redirect testsTom Christiebefore adoptione40b0bf
line 190why, one step back190 response = await client.get(url, allow_redirects=False)Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 191–197why, one step back191 with pytest.raises(TooManyRedirects):192 while response.is_redirect:193 response = await response.next()194195196@pytest.mark.asyncio197async def test_redirect_loop():Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
line 198why, one step back198 client = AsyncClient(dispatch=MockDispatch())Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
line 199why, one step back199 with pytest.raises(RedirectLoop):Tidy up redirect testsTom Christiebefore adoptione40b0bf
line 200why, one step back200 await client.get("https://example.org/redirect_loop")Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 201–204why, one step back201202203@pytest.mark.asyncio204async def test_redirect_loop_calling_next():Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
line 205why, one step back205 client = AsyncClient(dispatch=MockDispatch())Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
line 206why, one step back206 url = "https://example.org/redirect_loop"Tidy up redirect testsTom Christiebefore adoptione40b0bf
line 207why, one step back207 response = await client.get(url, allow_redirects=False)Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 208–214why, one step back208 with pytest.raises(RedirectLoop):209 while response.is_redirect:210 response = await response.next()211212213@pytest.mark.asyncio214async def test_cross_domain_redirect():Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
line 215why, one step back215 client = AsyncClient(dispatch=MockDispatch())Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 216–217why, one step back216 url = "https://example.com/cross_domain"217 headers = {"Authorization": "abc"}Tidy up redirect testsTom Christiebefore adoptione40b0bf
line 218why, one step back218 response = await client.get(url, headers=headers)Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
line 219why, one step back219 assert response.url == URL("https://example.org/cross_domain_target")Add response.json()Tom Christiebefore adoption2116d44
line 220why, one step back220 assert "authorization" not in response.json()["headers"]Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 221–224why, one step back221222223@pytest.mark.asyncio224async def test_same_domain_redirect():Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
line 225why, one step back225 client = AsyncClient(dispatch=MockDispatch())Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 226–227why, one step back226 url = "https://example.org/cross_domain"227 headers = {"Authorization": "abc"}Tidy up redirect testsTom Christiebefore adoptione40b0bf
line 228why, one step back228 response = await client.get(url, headers=headers)Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
line 229why, one step back229 assert response.url == URL("https://example.org/cross_domain_target")Add response.json()Tom Christiebefore adoption2116d44
line 230why, one step back230 assert response.json()["headers"]["authorization"] == "abc"Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 231–234why, one step back231232233@pytest.mark.asyncio234async def test_body_redirect():Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
line 235why, one step back235 client = AsyncClient(dispatch=MockDispatch())Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 236–237why, one step back236 url = "https://example.org/redirect_body"237 data = b"Example request body"Tidy up redirect testsTom Christiebefore adoptione40b0bf
line 238why, one step back238 response = await client.post(url, data=data)Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
line 239why, one step back239 assert response.url == URL("https://example.org/redirect_body_target")Add response.json()Tom Christiebefore adoption2116d44
line 240why, one step back240 assert response.json() == {"body": "Example request body"}Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 241–244why, one step back241242243@pytest.mark.asyncio244async def test_cannot_redirect_streaming_body():Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
line 245why, one step back245 client = AsyncClient(dispatch=MockDispatch())Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 246–251why, one step back246 url = "https://example.org/redirect_body"247248 async def streaming_body():249 yield b"Example request body"250251 with pytest.raises(RedirectBodyUnavailable):Tidy up redirect testsTom Christiebefore adoptione40b0bf
line 252why, one step back252 await client.post(url, data=streaming_body())