Skip to content
Myr

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 file

Take it

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

lines 1–7why, one step back
1import json
2from 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 back
8 AsyncClient,

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

line 9why, one step back
9 CertTypes,

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

lines 10–14why, one step back
10 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 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–26why, one step back
18 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 back
27 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 back
30 ) -> Response:
31 if request.url.path == "/redirect_301":

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

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

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

lines 33–36why, one step back
33 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 back
37 status_code = codes.FOUND

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

lines 38–41why, one step back
38 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 back
42 status_code = codes.SEE_OTHER

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

lines 43–47why, one step back
43 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 back
48 return Response(codes.SEE_OTHER, headers=headers, request=request)

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

lines 49–51why, one step back
49
50 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 back
52 return Response(codes.SEE_OTHER, headers=headers, request=request)

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

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

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

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

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

lines 59–66why, one step back
59 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 back
67 return Response(codes.SEE_OTHER, headers=headers, request=request)

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

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

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

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

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

lines 77–80why, one step back
77
78 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 back
81 return Response(codes.PERMANENT_REDIRECT, headers=headers, request=request)

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

lines 82–85why, one step back
82
83 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 back
86 return Response(codes.OK, content=body, request=request)

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

line 87why, one step back
87

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

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

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

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

Tidy up redirect testsTom Christiebefore adoptione40b0bf

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

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

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

Tidy up redirect testsTom Christiebefore adoptione40b0bf

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

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

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

Tidy up redirect testsTom Christiebefore adoptione40b0bf

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

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

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

Tidy up redirect testsTom Christiebefore adoptione40b0bf

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

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

line 123why, one step back
123 )

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

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

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

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

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

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

Tidy up redirect testsTom Christiebefore adoptione40b0bf

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

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

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

Tidy up redirect testsTom Christiebefore adoptione40b0bf

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

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

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

Tidy up redirect testsTom Christiebefore adoptione40b0bf

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

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

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

Tidy up redirect testsTom Christiebefore adoptione40b0bf

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

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

lines 166–171why, one step back
166 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 back
172 client = AsyncClient(dispatch=MockDispatch())

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

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

Tidy up redirect testsTom Christiebefore adoptione40b0bf

line 174why, one step back
174 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 back
175
176177@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 back
179 client = AsyncClient(dispatch=MockDispatch())

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

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

Tidy up redirect testsTom Christiebefore adoptione40b0bf

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

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

lines 182–188why, one step back
182 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 back
189 client = AsyncClient(dispatch=MockDispatch())

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

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

Tidy up redirect testsTom Christiebefore adoptione40b0bf

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

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

lines 192–195why, one step back
192
193194@pytest.mark.asyncio195async def test_redirect_loop_calling_next():

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

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

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

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

Tidy up redirect testsTom Christiebefore adoptione40b0bf

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

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

lines 199–205why, one step back
199 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 back
206 client = AsyncClient(dispatch=MockDispatch())

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

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

Tidy up redirect testsTom Christiebefore adoptione40b0bf

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

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

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

Add response.json()Tom Christiebefore adoption2116d44

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

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

lines 212–215why, one step back
212
213214@pytest.mark.asyncio215async def test_same_domain_redirect():

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

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

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

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

Tidy up redirect testsTom Christiebefore adoptione40b0bf

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

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

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

Add response.json()Tom Christiebefore adoption2116d44

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

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

lines 222–225why, one step back
222
223224@pytest.mark.asyncio225async def test_body_redirect():

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

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

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

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

Tidy up redirect testsTom Christiebefore adoptione40b0bf

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

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

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

Add response.json()Tom Christiebefore adoption2116d44

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

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

lines 232–235why, one step back
232
233234@pytest.mark.asyncio235async def test_cannot_redirect_streaming_body():

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

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

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

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