Skip to content
Myr

test_redirects.py

This file

Path

tests/client/test_redirects.py

354 lines · 13.2 KiB · Python

Last changed by

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

History of this file

Take it

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

Added links property to BaseResponse (#211)euri10before adoptioncad9c03

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,

Added links property to BaseResponse (#211)euri10before adoptioncad9c03

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,

Added links property to BaseResponse (#211)euri10before adoptioncad9c03

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

Added links property to BaseResponse (#211)euri10before adoptioncad9c03

lines 19–20why, one step back
19
20

Drop sync (#544)Tom Christiebefore adoption206c537

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

Added links property to BaseResponse (#211)euri10before adoptioncad9c03

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,

Added links property to BaseResponse (#211)euri10before adoptioncad9c03

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":

Added links property to BaseResponse (#211)euri10before adoptioncad9c03

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)

Added links property to BaseResponse (#211)euri10before adoptioncad9c03

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)

Added links property to BaseResponse (#211)euri10before adoptioncad9c03

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)

Added links property to BaseResponse (#211)euri10before adoptioncad9c03

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)

Added links property to BaseResponse (#211)euri10before adoptioncad9c03

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)

Added links property to BaseResponse (#211)euri10before adoptioncad9c03

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)

Added links property to BaseResponse (#211)euri10before adoptioncad9c03

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)

Added links property to BaseResponse (#211)euri10before adoptioncad9c03

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)

Added links property to BaseResponse (#211)euri10before adoptioncad9c03

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)

Added links property to BaseResponse (#211)euri10before adoptioncad9c03

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

Drop sync (#544)Tom Christiebefore adoption206c537

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

Added links property to BaseResponse (#211)euri10before adoptioncad9c03

line 83why, one step back
83

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

lines 84–86why, one step back
84 elif request.url.path == "/redirect_no_body":
85 await request.read()86 headers = {"location": "/redirect_body_target"}

Drop sync (#544)Tom Christiebefore adoption206c537

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

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

line 88why, one step back
88

Added links property to BaseResponse (#211)euri10before adoptioncad9c03

lines 89–90why, one step back
89 elif request.url.path == "/redirect_body_target":
90 content = await request.read()

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

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

Drop sync (#544)Tom Christiebefore adoption206c537

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

Added links property to BaseResponse (#211)euri10before adoptioncad9c03

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

Drop sync (#544)Tom Christiebefore adoption206c537

line 98why, one step back
98 return Response(

Added links property to BaseResponse (#211)euri10before adoptioncad9c03

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

Drop sync (#544)Tom Christiebefore adoption206c537

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

Added links property to BaseResponse (#211)euri10before adoptioncad9c03

line 103why, one step back
103

Drop sync (#544)Tom Christiebefore adoption206c537

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

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

lines 105–107why, one step back
105
106107async def test_no_redirect(backend):

Drop sync (#544)Tom Christiebefore adoption206c537

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

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

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

Added links property to BaseResponse (#211)euri10before adoptioncad9c03

lines 114–115why, one step back
114
115

Parametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058

line 116why, one step back
116async def test_redirect_301(backend):

Drop sync (#544)Tom Christiebefore adoption206c537

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

Added links property to BaseResponse (#211)euri10before adoptioncad9c03

lines 118–123why, one step back
118 response = await client.post("https://example.org/redirect_301")
119 assert response.status_code == codes.OK120 assert response.url == URL("https://example.org/")121 assert len(response.history) == 1122123

Parametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058

line 124why, one step back
124async def test_redirect_302(backend):

Drop sync (#544)Tom Christiebefore adoption206c537

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

Added links property to BaseResponse (#211)euri10before adoptioncad9c03

lines 126–131why, one step back
126 response = await client.post("https://example.org/redirect_302")
127 assert response.status_code == codes.OK128 assert response.url == URL("https://example.org/")129 assert len(response.history) == 1130131

Parametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058

line 132why, one step back
132async def test_redirect_303(backend):

Drop sync (#544)Tom Christiebefore adoption206c537

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

Added links property to BaseResponse (#211)euri10before adoptioncad9c03

lines 134–139why, one step back
134 response = await client.get("https://example.org/redirect_303")
135 assert response.status_code == codes.OK136 assert response.url == URL("https://example.org/")137 assert len(response.history) == 1138139

Parametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058

line 140why, one step back
140async def test_disallow_redirects(backend):

Drop sync (#544)Tom Christiebefore adoption206c537

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

Added links property to BaseResponse (#211)euri10before adoptioncad9c03

lines 142–156why, one step back
142 response = await client.post(
143 "https://example.org/redirect_303", allow_redirects=False144 )145 assert response.status_code == codes.SEE_OTHER146 assert response.url == URL("https://example.org/redirect_303")147 assert response.is_redirect is True148 assert len(response.history) == 0149150 response = await response.next()151 assert response.status_code == codes.OK152 assert response.url == URL("https://example.org/")153 assert response.is_redirect is False154 assert len(response.history) == 1155156

Parametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058

line 157why, one step back
157async def test_relative_redirect(backend):

Drop sync (#544)Tom Christiebefore adoption206c537

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

Added links property to BaseResponse (#211)euri10before adoptioncad9c03

lines 159–164why, one step back
159 response = await client.get("https://example.org/relative_redirect")
160 assert response.status_code == codes.OK161 assert response.url == URL("https://example.org/")162 assert len(response.history) == 1163164

Parametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058

line 165why, one step back
165async def test_no_scheme_redirect(backend):

Drop sync (#544)Tom Christiebefore adoption206c537

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

Added links property to BaseResponse (#211)euri10before adoptioncad9c03

lines 167–172why, one step back
167 response = await client.get("https://example.org/no_scheme_redirect")
168 assert response.status_code == codes.OK169 assert response.url == URL("https://example.org/")170 assert len(response.history) == 1171172

Parametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058

line 173why, one step back
173async def test_fragment_redirect(backend):

Drop sync (#544)Tom Christiebefore adoption206c537

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

Added links property to BaseResponse (#211)euri10before adoptioncad9c03

lines 175–180why, one step back
175 response = await client.get("https://example.org/relative_redirect#fragment")
176 assert response.status_code == codes.OK177 assert response.url == URL("https://example.org/#fragment")178 assert len(response.history) == 1179180

Parametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058

line 181why, one step back
181async def test_multiple_redirects(backend):

Drop sync (#544)Tom Christiebefore adoption206c537

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

Added links property to BaseResponse (#211)euri10before adoptioncad9c03

lines 183–196why, one step back
183 response = await client.get("https://example.org/multiple_redirects?count=20")
184 assert response.status_code == codes.OK185 assert response.url == URL("https://example.org/multiple_redirects")186 assert len(response.history) == 20187 assert response.history[0].url == URL(188 "https://example.org/multiple_redirects?count=20"189 )190 assert response.history[1].url == URL(191 "https://example.org/multiple_redirects?count=19"192 )193 assert len(response.history[0].history) == 0194 assert len(response.history[1].history) == 1195196

Parametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058

line 197why, one step back
197async def test_too_many_redirects(backend):

Drop sync (#544)Tom Christiebefore adoption206c537

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

Added links property to BaseResponse (#211)euri10before adoptioncad9c03

lines 199–202why, one step back
199 with pytest.raises(TooManyRedirects):
200 await client.get("https://example.org/multiple_redirects?count=21")201202

Parametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058

line 203why, one step back
203async def test_too_many_redirects_calling_next(backend):

Drop sync (#544)Tom Christiebefore adoption206c537

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

Added links property to BaseResponse (#211)euri10before adoptioncad9c03

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

Parametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058

line 212why, one step back
212async def test_redirect_loop(backend):

Drop sync (#544)Tom Christiebefore adoption206c537

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

Added links property to BaseResponse (#211)euri10before adoptioncad9c03

lines 214–217why, one step back
214 with pytest.raises(RedirectLoop):
215 await client.get("https://example.org/redirect_loop")216217

Parametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058

line 218why, one step back
218async def test_redirect_loop_calling_next(backend):

Drop sync (#544)Tom Christiebefore adoption206c537

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

Added links property to BaseResponse (#211)euri10before adoptioncad9c03

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

Parametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058

line 227why, one step back
227async def test_cross_domain_redirect(backend):

Drop sync (#544)Tom Christiebefore adoption206c537

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

Added links property to BaseResponse (#211)euri10before adoptioncad9c03

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

Parametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058

line 236why, one step back
236async def test_same_domain_redirect(backend):

Drop sync (#544)Tom Christiebefore adoption206c537

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

Added links property to BaseResponse (#211)euri10before adoptioncad9c03

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

Parametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058

line 245why, one step back
245async def test_body_redirect(backend):

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

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

Drop sync (#544)Tom Christiebefore adoption206c537

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

Added links property to BaseResponse (#211)euri10before adoptioncad9c03

lines 250–253why, one step back
250 url = "https://example.org/redirect_body"
251 data = b"Example request body"252 response = await client.post(url, data=data)253 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 254–261why, one step back
254 assert response.json()["body"] == "Example request body"
255 assert "content-length" in response.json()["headers"]256257258async def test_no_body_redirect(backend):259 """260 A 303 redirect should remove the request body.261 """

Drop sync (#544)Tom Christiebefore adoption206c537

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

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

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

Added links property to BaseResponse (#211)euri10before adoptioncad9c03

lines 269–270why, one step back
269
270

Parametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058

line 271why, one step back
271async def test_cannot_redirect_streaming_body(backend):

Drop sync (#544)Tom Christiebefore adoption206c537

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

Added links property to BaseResponse (#211)euri10before adoptioncad9c03

lines 273–281why, one step back
273 url = "https://example.org/redirect_body"
274275 async def streaming_body():276 yield b"Example request body"277278 with pytest.raises(RedirectBodyUnavailable):279 await client.post(url, data=streaming_body())280281

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

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

Drop sync (#544)Tom Christiebefore adoption206c537

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

Added links property to BaseResponse (#211)euri10before adoptioncad9c03

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

Fix redirect cookie behavior (#529)Tom Christiebefore adoption5aca0c0

lines 287–288why, one step back
287
288

Drop sync (#544)Tom Christiebefore adoption206c537

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

Fix redirect cookie behavior (#529)Tom Christiebefore adoption5aca0c0

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

Drop sync (#544)Tom Christiebefore adoption206c537

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

Fix redirect cookie behavior (#529)Tom Christiebefore adoption5aca0c0

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

Drop sync (#544)Tom Christiebefore adoption206c537

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

Fix redirect cookie behavior (#529)Tom Christiebefore adoption5aca0c0

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

Drop sync (#544)Tom Christiebefore adoption206c537

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

Fix redirect cookie behavior (#529)Tom Christiebefore adoption5aca0c0

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

Drop sync (#544)Tom Christiebefore adoption206c537

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

Fix redirect cookie behavior (#529)Tom Christiebefore adoption5aca0c0

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

Drop sync (#544)Tom Christiebefore adoption206c537

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

Fix redirect cookie behavior (#529)Tom Christiebefore adoption5aca0c0

lines 326–328why, one step back
326
327328async def test_redirect_cookie_behavior(backend):

Drop sync (#544)Tom Christiebefore adoption206c537

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

Fix redirect cookie behavior (#529)Tom Christiebefore adoption5aca0c0

lines 330–354why, one step back
330
331 # The client is not logged in.332 response = await client.get("https://example.com/")333 assert response.url == "https://example.com/"334 assert response.text == "Not logged in"335336 # Login redirects to the homepage, setting a session cookie.337 response = await client.post("https://example.com/login")338 assert response.url == "https://example.com/"339 assert response.text == "Logged in"340341 # The client is logged in.342 response = await client.get("https://example.com/")343 assert response.url == "https://example.com/"344 assert response.text == "Logged in"345346 # Logout redirects to the homepage, expiring the session cookie.347 response = await client.post("https://example.com/logout")348 assert response.url == "https://example.com/"349 assert response.text == "Not logged in"350351 # The client is not logged in.352 response = await client.get("https://example.com/")353 assert response.url == "https://example.com/"354 assert response.text == "Not logged in"