Skip to content
Myr

test_redirects.py

This file

Path

tests/client/test_redirects.py

353 lines · 12.9 KiB · Python

Last changed by

Use Streams API for both requests and responses. (#648)Tom Christiebefore adoptioncee1fcc

History of this file

Take it

The walk stopped after 200 commits: lines older than 1f4b8dd point at it.

Allow per file running of tests (#287)Jt Miclatbefore adoption1f4b8dd

lines 1–7why, one step back
1import json
2from urllib.parse import parse_qs34import pytest56from httpx import (7 URL,

Drop sync (#544)Tom Christiebefore adoption206c537

line 8why, one step back
8 Client,

Tighten up response.next() (#297)Florimond Mancabefore adoptiona464627

line 9why, one step back
9 NotRedirectResponse,

Allow per file running of tests (#287)Jt Miclatbefore adoption1f4b8dd

lines 10–11why, one step back
10 RedirectBodyUnavailable,
11 RedirectLoop,

Drop sync (#544)Tom Christiebefore adoption206c537

lines 12–13why, one step back
12 Request,
13 Response,

Allow per file running of tests (#287)Jt Miclatbefore adoption1f4b8dd

lines 14–16why, one step back
14 TooManyRedirects,
15 codes,16)

Tighten up top-level API to only expose public API (#608)Tom Christiebefore adoptiond15dc0b

lines 17–18why, one step back
17from httpx.config import CertTypes, TimeoutTypes, VerifyTypes
18from httpx.dispatch.base import Dispatcher

Allow per file running of tests (#287)Jt Miclatbefore adoption1f4b8dd

lines 19–20why, one step back
19
20

Drop sync (#544)Tom Christiebefore adoption206c537

line 21why, one step back
21class MockDispatch(Dispatcher):

Allow per file running of tests (#287)Jt Miclatbefore adoption1f4b8dd

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

Drop sync (#544)Tom Christiebefore adoption206c537

line 24why, one step back
24 request: Request,

Allow per file running of tests (#287)Jt Miclatbefore adoption1f4b8dd

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

Drop sync (#544)Tom Christiebefore adoption206c537

line 28why, one step back
28 ) -> Response:

Tighten up response.next() (#297)Florimond Mancabefore adoptiona464627

line 29why, one step back
29 if request.url.path == "/no_redirect":

Drop sync (#544)Tom Christiebefore adoption206c537

line 30why, one step back
30 return Response(codes.OK, request=request)

Tighten up response.next() (#297)Florimond Mancabefore adoptiona464627

lines 31–32why, one step back
31
32 elif request.url.path == "/redirect_301":

Allow per file running of tests (#287)Jt Miclatbefore adoption1f4b8dd

lines 33–34why, one step back
33 status_code = codes.MOVED_PERMANENTLY
34 headers = {"location": "https://example.org/"}

Drop sync (#544)Tom Christiebefore adoption206c537

line 35why, one step back
35 return Response(status_code, headers=headers, request=request)

Allow per file running of tests (#287)Jt Miclatbefore adoption1f4b8dd

lines 36–39why, one step back
36
37 elif request.url.path == "/redirect_302":38 status_code = codes.FOUND39 headers = {"location": "https://example.org/"}

Drop sync (#544)Tom Christiebefore adoption206c537

line 40why, one step back
40 return Response(status_code, headers=headers, request=request)

Allow per file running of tests (#287)Jt Miclatbefore adoption1f4b8dd

lines 41–44why, one step back
41
42 elif request.url.path == "/redirect_303":43 status_code = codes.SEE_OTHER44 headers = {"location": "https://example.org/"}

Drop sync (#544)Tom Christiebefore adoption206c537

line 45why, one step back
45 return Response(status_code, headers=headers, request=request)

Allow per file running of tests (#287)Jt Miclatbefore adoption1f4b8dd

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

Drop sync (#544)Tom Christiebefore adoption206c537

line 49why, one step back
49 return Response(codes.SEE_OTHER, headers=headers, request=request)

Allow per file running of tests (#287)Jt Miclatbefore adoption1f4b8dd

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

Drop sync (#544)Tom Christiebefore adoption206c537

line 53why, one step back
53 return Response(codes.SEE_OTHER, headers=headers, request=request)

Allow per file running of tests (#287)Jt Miclatbefore adoption1f4b8dd

lines 54–63why, one step back
54
55 elif request.url.path == "/multiple_redirects":56 params = parse_qs(request.url.query)57 count = int(params.get("count", "0")[0])58 redirect_count = count - 159 code = codes.SEE_OTHER if count else codes.OK60 location = "/multiple_redirects"61 if redirect_count:62 location += "?count=" + str(redirect_count)63 headers = {"location": location} if count else {}

Drop sync (#544)Tom Christiebefore adoption206c537

line 64why, one step back
64 return Response(code, headers=headers, request=request)

Allow per file running of tests (#287)Jt Miclatbefore adoption1f4b8dd

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

Drop sync (#544)Tom Christiebefore adoption206c537

line 68why, one step back
68 return Response(codes.SEE_OTHER, headers=headers, request=request)

Allow per file running of tests (#287)Jt Miclatbefore adoption1f4b8dd

lines 69–71why, one step back
69
70 elif request.url.path == "/cross_domain":71 headers = {"location": "https://example.org/cross_domain_target"}

Drop sync (#544)Tom Christiebefore adoption206c537

line 72why, one step back
72 return Response(codes.SEE_OTHER, headers=headers, request=request)

Allow per file running of tests (#287)Jt Miclatbefore adoption1f4b8dd

lines 73–76why, one step back
73
74 elif request.url.path == "/cross_domain_target":75 headers = dict(request.headers.items())76 content = json.dumps({"headers": headers}).encode()

Drop sync (#544)Tom Christiebefore adoption206c537

line 77why, one step back
77 return Response(codes.OK, content=content, request=request)

Allow per file running of tests (#287)Jt Miclatbefore adoption1f4b8dd

lines 78–80why, one step back
78
79 elif request.url.path == "/redirect_body":80 headers = {"location": "/redirect_body_target"}

Drop sync (#544)Tom Christiebefore adoption206c537

line 81why, one step back
81 return Response(codes.PERMANENT_REDIRECT, headers=headers, request=request)

Allow per file running of tests (#287)Jt Miclatbefore adoption1f4b8dd

line 82why, one step back
82

Merge pull request #310 from encode/drop-content-length-on-get-redirectsTom Christiebefore adoption33c5fe0

line 83why, one step back
83 elif request.url.path == "/redirect_no_body":

Use Streams API for both requests and responses. (#648)Tom Christiebefore adoptioncee1fcc

line 84why, one step back
84 content = b"".join([part async for part in request.stream])

Merge pull request #310 from encode/drop-content-length-on-get-redirectsTom Christiebefore adoption33c5fe0

line 85why, one step back
85 headers = {"location": "/redirect_body_target"}

Drop sync (#544)Tom Christiebefore adoption206c537

line 86why, one step back
86 return Response(codes.SEE_OTHER, headers=headers, request=request)

Merge pull request #310 from encode/drop-content-length-on-get-redirectsTom Christiebefore adoption33c5fe0

line 87why, one step back
87

Allow per file running of tests (#287)Jt Miclatbefore adoption1f4b8dd

line 88why, one step back
88 elif request.url.path == "/redirect_body_target":

Use Streams API for both requests and responses. (#648)Tom Christiebefore adoptioncee1fcc

line 89why, one step back
89 content = b"".join([part async for part in request.stream])

Merge pull request #310 from encode/drop-content-length-on-get-redirectsTom Christiebefore adoption33c5fe0

lines 90–91why, one step back
90 headers = dict(request.headers.items())
91 body = json.dumps({"body": content.decode(), "headers": headers}).encode()

Drop sync (#544)Tom Christiebefore adoption206c537

line 92why, one step back
92 return Response(codes.OK, content=body, request=request)

Allow per file running of tests (#287)Jt Miclatbefore adoption1f4b8dd

lines 93–96why, one step back
93
94 elif request.url.path == "/cross_subdomain":95 if request.headers["host"] != "www.example.org":96 headers = {"location": "https://www.example.org/cross_subdomain"}

Drop sync (#544)Tom Christiebefore adoption206c537

line 97why, one step back
97 return Response(

Allow per file running of tests (#287)Jt Miclatbefore adoption1f4b8dd

lines 98–100why, one step back
98 codes.PERMANENT_REDIRECT, headers=headers, request=request
99 )100 else:

Drop sync (#544)Tom Christiebefore adoption206c537

line 101why, one step back
101 return Response(codes.OK, content=b"Hello, world!", request=request)

Allow per file running of tests (#287)Jt Miclatbefore adoption1f4b8dd

line 102why, one step back
102

Drop sync (#544)Tom Christiebefore adoption206c537

line 103why, one step back
103 return Response(codes.OK, content=b"Hello, world!", request=request)

Tighten up response.next() (#297)Florimond Mancabefore adoptiona464627

lines 104–106why, one step back
104
105106async def test_no_redirect(backend):

Refactor tests in the light of backend auto-detection (#615)Florimond Mancabefore adoptionab41a5d

line 107why, one step back
107 client = Client(dispatch=MockDispatch())

Tighten up response.next() (#297)Florimond Mancabefore adoptiona464627

lines 108–112why, one step back
108 url = "https://example.com/no_redirect"
109 response = await client.get(url)110 assert response.status_code == 200111 with pytest.raises(NotRedirectResponse):112 await response.next()

Allow per file running of tests (#287)Jt Miclatbefore adoption1f4b8dd

lines 113–115why, one step back
113
114115async def test_redirect_301(backend):

Refactor tests in the light of backend auto-detection (#615)Florimond Mancabefore adoptionab41a5d

line 116why, one step back
116 client = Client(dispatch=MockDispatch())

Allow per file running of tests (#287)Jt Miclatbefore adoption1f4b8dd

lines 117–123why, one step back
117 response = await client.post("https://example.org/redirect_301")
118 assert response.status_code == codes.OK119 assert response.url == URL("https://example.org/")120 assert len(response.history) == 1121122123async def test_redirect_302(backend):

Refactor tests in the light of backend auto-detection (#615)Florimond Mancabefore adoptionab41a5d

line 124why, one step back
124 client = Client(dispatch=MockDispatch())

Allow per file running of tests (#287)Jt Miclatbefore adoption1f4b8dd

lines 125–131why, one step back
125 response = await client.post("https://example.org/redirect_302")
126 assert response.status_code == codes.OK127 assert response.url == URL("https://example.org/")128 assert len(response.history) == 1129130131async def test_redirect_303(backend):

Refactor tests in the light of backend auto-detection (#615)Florimond Mancabefore adoptionab41a5d

line 132why, one step back
132 client = Client(dispatch=MockDispatch())

Allow per file running of tests (#287)Jt Miclatbefore adoption1f4b8dd

lines 133–139why, one step back
133 response = await client.get("https://example.org/redirect_303")
134 assert response.status_code == codes.OK135 assert response.url == URL("https://example.org/")136 assert len(response.history) == 1137138139async def test_disallow_redirects(backend):

Refactor tests in the light of backend auto-detection (#615)Florimond Mancabefore adoptionab41a5d

line 140why, one step back
140 client = Client(dispatch=MockDispatch())

Allow per file running of tests (#287)Jt Miclatbefore adoption1f4b8dd

lines 141–156why, one step back
141 response = await client.post(
142 "https://example.org/redirect_303", allow_redirects=False143 )144 assert response.status_code == codes.SEE_OTHER145 assert response.url == URL("https://example.org/redirect_303")146 assert response.is_redirect is True147 assert len(response.history) == 0148149 response = await response.next()150 assert response.status_code == codes.OK151 assert response.url == URL("https://example.org/")152 assert response.is_redirect is False153 assert len(response.history) == 1154155156async def test_relative_redirect(backend):

Refactor tests in the light of backend auto-detection (#615)Florimond Mancabefore adoptionab41a5d

line 157why, one step back
157 client = Client(dispatch=MockDispatch())

Allow per file running of tests (#287)Jt Miclatbefore adoption1f4b8dd

lines 158–164why, one step back
158 response = await client.get("https://example.org/relative_redirect")
159 assert response.status_code == codes.OK160 assert response.url == URL("https://example.org/")161 assert len(response.history) == 1162163164async def test_no_scheme_redirect(backend):

Refactor tests in the light of backend auto-detection (#615)Florimond Mancabefore adoptionab41a5d

line 165why, one step back
165 client = Client(dispatch=MockDispatch())

Allow per file running of tests (#287)Jt Miclatbefore adoption1f4b8dd

lines 166–172why, one step back
166 response = await client.get("https://example.org/no_scheme_redirect")
167 assert response.status_code == codes.OK168 assert response.url == URL("https://example.org/")169 assert len(response.history) == 1170171172async def test_fragment_redirect(backend):

Refactor tests in the light of backend auto-detection (#615)Florimond Mancabefore adoptionab41a5d

line 173why, one step back
173 client = Client(dispatch=MockDispatch())

Allow per file running of tests (#287)Jt Miclatbefore adoption1f4b8dd

lines 174–180why, one step back
174 response = await client.get("https://example.org/relative_redirect#fragment")
175 assert response.status_code == codes.OK176 assert response.url == URL("https://example.org/#fragment")177 assert len(response.history) == 1178179180async def test_multiple_redirects(backend):

Refactor tests in the light of backend auto-detection (#615)Florimond Mancabefore adoptionab41a5d

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

Allow per file running of tests (#287)Jt Miclatbefore adoption1f4b8dd

lines 182–196why, one step back
182 response = await client.get("https://example.org/multiple_redirects?count=20")
183 assert response.status_code == codes.OK184 assert response.url == URL("https://example.org/multiple_redirects")185 assert len(response.history) == 20186 assert response.history[0].url == URL(187 "https://example.org/multiple_redirects?count=20"188 )189 assert response.history[1].url == URL(190 "https://example.org/multiple_redirects?count=19"191 )192 assert len(response.history[0].history) == 0193 assert len(response.history[1].history) == 1194195196async def test_too_many_redirects(backend):

Refactor tests in the light of backend auto-detection (#615)Florimond Mancabefore adoptionab41a5d

line 197why, one step back
197 client = Client(dispatch=MockDispatch())

Allow per file running of tests (#287)Jt Miclatbefore adoption1f4b8dd

lines 198–202why, one step back
198 with pytest.raises(TooManyRedirects):
199 await client.get("https://example.org/multiple_redirects?count=21")200201202async def test_too_many_redirects_calling_next(backend):

Refactor tests in the light of backend auto-detection (#615)Florimond Mancabefore adoptionab41a5d

line 203why, one step back
203 client = Client(dispatch=MockDispatch())

Allow per file running of tests (#287)Jt Miclatbefore adoption1f4b8dd

lines 204–211why, one step back
204 url = "https://example.org/multiple_redirects?count=21"
205 response = await client.get(url, allow_redirects=False)206 with pytest.raises(TooManyRedirects):207 while response.is_redirect:208 response = await response.next()209210211async def test_redirect_loop(backend):

Refactor tests in the light of backend auto-detection (#615)Florimond Mancabefore adoptionab41a5d

line 212why, one step back
212 client = Client(dispatch=MockDispatch())

Allow per file running of tests (#287)Jt Miclatbefore adoption1f4b8dd

lines 213–217why, one step back
213 with pytest.raises(RedirectLoop):
214 await client.get("https://example.org/redirect_loop")215216217async def test_redirect_loop_calling_next(backend):

Refactor tests in the light of backend auto-detection (#615)Florimond Mancabefore adoptionab41a5d

line 218why, one step back
218 client = Client(dispatch=MockDispatch())

Allow per file running of tests (#287)Jt Miclatbefore adoption1f4b8dd

lines 219–226why, one step back
219 url = "https://example.org/redirect_loop"
220 response = await client.get(url, allow_redirects=False)221 with pytest.raises(RedirectLoop):222 while response.is_redirect:223 response = await response.next()224225226async def test_cross_domain_redirect(backend):

Refactor tests in the light of backend auto-detection (#615)Florimond Mancabefore adoptionab41a5d

line 227why, one step back
227 client = Client(dispatch=MockDispatch())

Allow per file running of tests (#287)Jt Miclatbefore adoption1f4b8dd

lines 228–235why, one step back
228 url = "https://example.com/cross_domain"
229 headers = {"Authorization": "abc"}230 response = await client.get(url, headers=headers)231 assert response.url == URL("https://example.org/cross_domain_target")232 assert "authorization" not in response.json()["headers"]233234235async def test_same_domain_redirect(backend):

Refactor tests in the light of backend auto-detection (#615)Florimond Mancabefore adoptionab41a5d

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

Allow per file running of tests (#287)Jt Miclatbefore adoption1f4b8dd

lines 237–244why, one step back
237 url = "https://example.org/cross_domain"
238 headers = {"Authorization": "abc"}239 response = await client.get(url, headers=headers)240 assert response.url == URL("https://example.org/cross_domain_target")241 assert response.json()["headers"]["authorization"] == "abc"242243244async def test_body_redirect(backend):

Merge pull request #310 from encode/drop-content-length-on-get-redirectsTom Christiebefore adoption33c5fe0

lines 245–247why, one step back
245 """
246 A 308 redirect should preserve the request body.247 """

Refactor tests in the light of backend auto-detection (#615)Florimond Mancabefore adoptionab41a5d

line 248why, one step back
248 client = Client(dispatch=MockDispatch())

Allow per file running of tests (#287)Jt Miclatbefore adoption1f4b8dd

lines 249–252why, one step back
249 url = "https://example.org/redirect_body"
250 data = b"Example request body"251 response = await client.post(url, data=data)252 assert response.url == URL("https://example.org/redirect_body_target")

Merge pull request #310 from encode/drop-content-length-on-get-redirectsTom Christiebefore adoption33c5fe0

lines 253–260why, one step back
253 assert response.json()["body"] == "Example request body"
254 assert "content-length" in response.json()["headers"]255256257async def test_no_body_redirect(backend):258 """259 A 303 redirect should remove the request body.260 """

Refactor tests in the light of backend auto-detection (#615)Florimond Mancabefore adoptionab41a5d

line 261why, one step back
261 client = Client(dispatch=MockDispatch())

Merge pull request #310 from encode/drop-content-length-on-get-redirectsTom Christiebefore adoption33c5fe0

lines 262–267why, one step back
262 url = "https://example.org/redirect_no_body"
263 data = b"Example request body"264 response = await client.post(url, data=data)265 assert response.url == URL("https://example.org/redirect_body_target")266 assert response.json()["body"] == ""267 assert "content-length" not in response.json()["headers"]

Allow per file running of tests (#287)Jt Miclatbefore adoption1f4b8dd

lines 268–270why, one step back
268
269270async def test_cannot_redirect_streaming_body(backend):

Refactor tests in the light of backend auto-detection (#615)Florimond Mancabefore adoptionab41a5d

line 271why, one step back
271 client = Client(dispatch=MockDispatch())

Allow per file running of tests (#287)Jt Miclatbefore adoption1f4b8dd

lines 272–280why, one step back
272 url = "https://example.org/redirect_body"
273274 async def streaming_body():275 yield b"Example request body"276277 with pytest.raises(RedirectBodyUnavailable):278 await client.post(url, data=streaming_body())279280

Refactor client functionality into middleware (#268)Florimond Mancabefore adoption40e6e8c

line 281why, one step back
281async def test_cross_subdomain_redirect(backend):

Refactor tests in the light of backend auto-detection (#615)Florimond Mancabefore adoptionab41a5d

line 282why, one step back
282 client = Client(dispatch=MockDispatch())

Allow per file running of tests (#287)Jt Miclatbefore adoption1f4b8dd

lines 283–285why, one step back
283 url = "https://example.com/cross_subdomain"
284 response = await client.get(url)285 assert response.url == URL("https://www.example.org/cross_subdomain")

Fix redirect cookie behavior (#529)Tom Christiebefore adoption5aca0c0

lines 286–287why, one step back
286
287

Drop sync (#544)Tom Christiebefore adoption206c537

line 288why, one step back
288class MockCookieDispatch(Dispatcher):

Fix redirect cookie behavior (#529)Tom Christiebefore adoption5aca0c0

lines 289–290why, one step back
289 async def send(
290 self,

Drop sync (#544)Tom Christiebefore adoption206c537

line 291why, one step back
291 request: Request,

Fix redirect cookie behavior (#529)Tom Christiebefore adoption5aca0c0

lines 292–294why, one step back
292 verify: VerifyTypes = None,
293 cert: CertTypes = None,294 timeout: TimeoutTypes = None,

Drop sync (#544)Tom Christiebefore adoption206c537

line 295why, one step back
295 ) -> Response:

Fix redirect cookie behavior (#529)Tom Christiebefore adoption5aca0c0

lines 296–300why, one step back
296 if request.url.path == "/":
297 if "cookie" in request.headers:298 content = b"Logged in"299 else:300 content = b"Not logged in"

Drop sync (#544)Tom Christiebefore adoption206c537

line 301why, one step back
301 return Response(codes.OK, content=content, request=request)

Fix redirect cookie behavior (#529)Tom Christiebefore adoption5aca0c0

lines 302–312why, one step back
302
303 elif request.url.path == "/login":304 status_code = codes.SEE_OTHER305 headers = {306 "location": "/",307 "set-cookie": (308 "session=eyJ1c2VybmFtZSI6ICJ0b21; path=/; Max-Age=1209600; "309 "httponly; samesite=lax"310 ),311 }312

Drop sync (#544)Tom Christiebefore adoption206c537

line 313why, one step back
313 return Response(status_code, headers=headers, request=request)

Fix redirect cookie behavior (#529)Tom Christiebefore adoption5aca0c0

lines 314–323why, one step back
314
315 elif request.url.path == "/logout":316 status_code = codes.SEE_OTHER317 headers = {318 "location": "/",319 "set-cookie": (320 "session=null; path=/; expires=Thu, 01 Jan 1970 00:00:00 GMT; "321 "httponly; samesite=lax"322 ),323 }

Drop sync (#544)Tom Christiebefore adoption206c537

line 324why, one step back
324 return Response(status_code, headers=headers, request=request)

Fix redirect cookie behavior (#529)Tom Christiebefore adoption5aca0c0

lines 325–327why, one step back
325
326327async def test_redirect_cookie_behavior(backend):

Refactor tests in the light of backend auto-detection (#615)Florimond Mancabefore adoptionab41a5d

line 328why, one step back
328 client = Client(dispatch=MockCookieDispatch())

Fix redirect cookie behavior (#529)Tom Christiebefore adoption5aca0c0

lines 329–353why, one step back
329
330 # The client is not logged in.331 response = await client.get("https://example.com/")332 assert response.url == "https://example.com/"333 assert response.text == "Not logged in"334335 # Login redirects to the homepage, setting a session cookie.336 response = await client.post("https://example.com/login")337 assert response.url == "https://example.com/"338 assert response.text == "Logged in"339340 # The client is logged in.341 response = await client.get("https://example.com/")342 assert response.url == "https://example.com/"343 assert response.text == "Logged in"344345 # Logout redirects to the homepage, expiring the session cookie.346 response = await client.post("https://example.com/logout")347 assert response.url == "https://example.com/"348 assert response.text == "Not logged in"349350 # The client is not logged in.351 response = await client.get("https://example.com/")352 assert response.url == "https://example.com/"353 assert response.text == "Not logged in"