Skip to content
Myr

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 file

Take it

Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144

lines 1–5why, one step back
1import json
2from urllib.parse import parse_qs34import pytest5

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

line 6why, one step back
6from httpx import (

Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144

line 7why, one step back
7 URL,

Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0

line 8why, one step back
8 AsyncClient,

Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7

lines 9–11why, one step back
9 AsyncDispatcher,
10 AsyncRequest,11 AsyncResponse,

Work on bringing API into parity with `requests`. (#76)Tom Christiebefore adoption9574041

line 12why, one step back
12 CertTypes,

Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144

lines 13–14why, one step back
13 RedirectBodyUnavailable,
14 RedirectLoop,

Work on bringing API into parity with `requests`. (#76)Tom Christiebefore adoption9574041

line 15why, one step back
15 TimeoutTypes,

Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144

line 16why, one step back
16 TooManyRedirects,

Work on bringing API into parity with `requests`. (#76)Tom Christiebefore adoption9574041

line 17why, one step back
17 VerifyTypes,

Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144

lines 18–21why, one step back
18 codes,
19)2021

Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7

line 22why, one step back
22class MockDispatch(AsyncDispatcher):

Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144

lines 23–24why, one step back
23 async def send(
24 self,

Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7

line 25why, one step back
25 request: AsyncRequest,

Work on bringing API into parity with `requests`. (#76)Tom Christiebefore adoption9574041

lines 26–28why, one step back
26 verify: VerifyTypes = None,
27 cert: CertTypes = None,28 timeout: TimeoutTypes = None,

Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7

line 29why, one step back
29 ) -> AsyncResponse:

Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144

line 30why, one step back
30 if request.url.path == "/redirect_301":

Merge pull request #59 from encode/use-stdlib-status-codesTom Christiebefore adoptionafc8369

line 31why, one step back
31 status_code = codes.MOVED_PERMANENTLY

Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144

line 32why, one step back
32 headers = {"location": "https://example.org/"}

Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7

line 33why, one step back
33 return AsyncResponse(status_code, headers=headers, request=request)

Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144

lines 34–35why, one step back
34
35 elif request.url.path == "/redirect_302":

Merge pull request #59 from encode/use-stdlib-status-codesTom Christiebefore adoptionafc8369

line 36why, one step back
36 status_code = codes.FOUND

Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144

line 37why, one step back
37 headers = {"location": "https://example.org/"}

Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7

line 38why, one step back
38 return AsyncResponse(status_code, headers=headers, request=request)

Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144

lines 39–40why, one step back
39
40 elif request.url.path == "/redirect_303":

Merge pull request #59 from encode/use-stdlib-status-codesTom Christiebefore adoptionafc8369

line 41why, one step back
41 status_code = codes.SEE_OTHER

Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144

line 42why, one step back
42 headers = {"location": "https://example.org/"}

Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7

line 43why, one step back
43 return AsyncResponse(status_code, headers=headers, request=request)

Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144

lines 44–46why, one step back
44
45 elif request.url.path == "/relative_redirect":46 headers = {"location": "/"}

Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7

line 47why, one step back
47 return AsyncResponse(codes.SEE_OTHER, headers=headers, request=request)

Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144

lines 48–50why, one step back
48
49 elif request.url.path == "/no_scheme_redirect":50 headers = {"location": "//example.org/"}

Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7

line 51why, one step back
51 return AsyncResponse(codes.SEE_OTHER, headers=headers, request=request)

Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144

lines 52–56why, one step back
52
53 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 - 1

Merge pull request #59 from encode/use-stdlib-status-codesTom Christiebefore adoptionafc8369

line 57why, one step back
57 code = codes.SEE_OTHER if count else codes.OK

Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144

lines 58–61why, one step back
58 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 back
62 return AsyncResponse(code, headers=headers, request=request)

Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144

lines 63–65why, one step back
63
64 if request.url.path == "/redirect_loop":65 headers = {"location": "/redirect_loop"}

Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7

line 66why, one step back
66 return AsyncResponse(codes.SEE_OTHER, headers=headers, request=request)

Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144

lines 67–69why, one step back
67
68 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 back
70 return AsyncResponse(codes.SEE_OTHER, headers=headers, request=request)

Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144

lines 71–74why, one step back
71
72 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 back
75 return AsyncResponse(codes.OK, content=content, request=request)

Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144

lines 76–79why, one step back
76
77 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 back
80 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 back
83
84 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 back
87 return AsyncResponse(codes.OK, content=body, request=request)

Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144

line 88why, one step back
88

Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7

line 89why, one step back
89 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 back
90
9192@pytest.mark.asyncio93async def test_redirect_301():

Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0

line 94why, one step back
94 client = AsyncClient(dispatch=MockDispatch())

Tidy up redirect testsTom Christiebefore adoptione40b0bf

line 95why, one step back
95 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 back
96 assert response.status_code == codes.OK

Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144

lines 97–102why, one step back
97 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 back
103 client = AsyncClient(dispatch=MockDispatch())

Tidy up redirect testsTom Christiebefore adoptione40b0bf

line 104why, one step back
104 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 back
105 assert response.status_code == codes.OK

Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144

lines 106–111why, one step back
106 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 back
112 client = AsyncClient(dispatch=MockDispatch())

Tidy up redirect testsTom Christiebefore adoptione40b0bf

line 113why, one step back
113 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 back
114 assert response.status_code == codes.OK

Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144

lines 115–120why, one step back
115 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 back
121 client = AsyncClient(dispatch=MockDispatch())

Tidy up redirect testsTom Christiebefore adoptione40b0bf

lines 122–123why, one step back
122 response = await client.post(
123 "https://example.org/redirect_303", allow_redirects=False

Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144

line 124why, one step back
124 )

Merge pull request #59 from encode/use-stdlib-status-codesTom Christiebefore adoptionafc8369

line 125why, one step back
125 assert response.status_code == codes.SEE_OTHER

Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144

lines 126–129why, one step back
126 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 back
130 assert response.status_code == codes.OK

Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144

lines 131–136why, one step back
131 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 back
137 client = AsyncClient(dispatch=MockDispatch())

Tidy up redirect testsTom Christiebefore adoptione40b0bf

line 138why, one step back
138 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 back
139 assert response.status_code == codes.OK

Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144

lines 140–145why, one step back
140 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 back
146 client = AsyncClient(dispatch=MockDispatch())

Tidy up redirect testsTom Christiebefore adoptione40b0bf

line 147why, one step back
147 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 back
148 assert response.status_code == codes.OK

Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144

lines 149–154why, one step back
149 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 back
155 client = AsyncClient(dispatch=MockDispatch())

Tidy up redirect testsTom Christiebefore adoptione40b0bf

line 156why, one step back
156 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 back
157 assert response.status_code == codes.OK

Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144

lines 158–163why, one step back
158 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 back
164 client = AsyncClient(dispatch=MockDispatch())

Tidy up redirect testsTom Christiebefore adoptione40b0bf

line 165why, one step back
165 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 back
166 assert response.status_code == codes.OK

Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144

lines 167–168why, one step back
167 assert response.url == URL("https://example.org/multiple_redirects")
168 assert len(response.history) == 20

Multipart support (#90)Tom Christiebefore adoption6a44b05

lines 169–174why, one step back
169 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 back
175 assert len(response.history[0].history) == 0
176 assert len(response.history[1].history) == 1

Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144

lines 177–180why, one step back
177
178179@pytest.mark.asyncio180async def test_too_many_redirects():

Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0

line 181why, one step back
181 client = AsyncClient(dispatch=MockDispatch())

Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144

line 182why, one step back
182 with pytest.raises(TooManyRedirects):

Tidy up redirect testsTom Christiebefore adoptione40b0bf

line 183why, one step back
183 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 back
184
185186@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 back
188 client = AsyncClient(dispatch=MockDispatch())

Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144

line 189why, one step back
189 url = "https://example.org/multiple_redirects?count=21"

Tidy up redirect testsTom Christiebefore adoptione40b0bf

line 190why, one step back
190 response = await client.get(url, allow_redirects=False)

Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144

lines 191–197why, one step back
191 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 back
198 client = AsyncClient(dispatch=MockDispatch())

Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144

line 199why, one step back
199 with pytest.raises(RedirectLoop):

Tidy up redirect testsTom Christiebefore adoptione40b0bf

line 200why, one step back
200 await client.get("https://example.org/redirect_loop")

Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144

lines 201–204why, one step back
201
202203@pytest.mark.asyncio204async def test_redirect_loop_calling_next():

Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0

line 205why, one step back
205 client = AsyncClient(dispatch=MockDispatch())

Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144

line 206why, one step back
206 url = "https://example.org/redirect_loop"

Tidy up redirect testsTom Christiebefore adoptione40b0bf

line 207why, one step back
207 response = await client.get(url, allow_redirects=False)

Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144

lines 208–214why, one step back
208 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 back
215 client = AsyncClient(dispatch=MockDispatch())

Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144

lines 216–217why, one step back
216 url = "https://example.com/cross_domain"
217 headers = {"Authorization": "abc"}

Tidy up redirect testsTom Christiebefore adoptione40b0bf

line 218why, one step back
218 response = await client.get(url, headers=headers)

Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144

line 219why, one step back
219 assert response.url == URL("https://example.org/cross_domain_target")

Add response.json()Tom Christiebefore adoption2116d44

line 220why, one step back
220 assert "authorization" not in response.json()["headers"]

Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144

lines 221–224why, one step back
221
222223@pytest.mark.asyncio224async def test_same_domain_redirect():

Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0

line 225why, one step back
225 client = AsyncClient(dispatch=MockDispatch())

Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144

lines 226–227why, one step back
226 url = "https://example.org/cross_domain"
227 headers = {"Authorization": "abc"}

Tidy up redirect testsTom Christiebefore adoptione40b0bf

line 228why, one step back
228 response = await client.get(url, headers=headers)

Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144

line 229why, one step back
229 assert response.url == URL("https://example.org/cross_domain_target")

Add response.json()Tom Christiebefore adoption2116d44

line 230why, one step back
230 assert response.json()["headers"]["authorization"] == "abc"

Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144

lines 231–234why, one step back
231
232233@pytest.mark.asyncio234async def test_body_redirect():

Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0

line 235why, one step back
235 client = AsyncClient(dispatch=MockDispatch())

Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144

lines 236–237why, one step back
236 url = "https://example.org/redirect_body"
237 data = b"Example request body"

Tidy up redirect testsTom Christiebefore adoptione40b0bf

line 238why, one step back
238 response = await client.post(url, data=data)

Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144

line 239why, one step back
239 assert response.url == URL("https://example.org/redirect_body_target")

Add response.json()Tom Christiebefore adoption2116d44

line 240why, one step back
240 assert response.json() == {"body": "Example request body"}

Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144

lines 241–244why, one step back
241
242243@pytest.mark.asyncio244async def test_cannot_redirect_streaming_body():

Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0

line 245why, one step back
245 client = AsyncClient(dispatch=MockDispatch())

Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144

lines 246–251why, one step back
246 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 back
252 await client.post(url, data=streaming_body())