Skip to content
Myr

test_redirects.py

This file

Path

tests/client/test_redirects.py

374 lines · 13.9 KiB · Python

Last changed by

Dispatcher -> AsyncDispatcher (#725)Tom Christiebefore adoptione19bd9b

History of this file

Take it

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

Document + recommend async with syntax for AsyncClient (#344)Florimond Mancabefore adoption4616031

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

Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84

line 8why, one step back
8 AsyncClient,

Document + recommend async with syntax for AsyncClient (#344)Florimond Mancabefore adoption4616031

lines 9–10why, one step back
9 NotRedirectResponse,
10 RedirectLoop,

Drop sync (#544)Tom Christiebefore adoption206c537

line 11why, one step back
11 Request,

Repurpose RedirectBodyUnavailable as RequestBodyUnavailable (#690)Florimond Mancabefore adoption910aa90

line 12why, one step back
12 RequestBodyUnavailable,

Drop sync (#544)Tom Christiebefore adoption206c537

line 13why, one step back
13 Response,

Document + recommend async with syntax for AsyncClient (#344)Florimond Mancabefore adoption4616031

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

line 17why, one step back
17from httpx.config import CertTypes, TimeoutTypes, VerifyTypes

Dispatcher -> AsyncDispatcher (#725)Tom Christiebefore adoptione19bd9b

line 18why, one step back
18from httpx.dispatch.base import AsyncDispatcher

Document + recommend async with syntax for AsyncClient (#344)Florimond Mancabefore adoption4616031

lines 19–20why, one step back
19
20

Dispatcher -> AsyncDispatcher (#725)Tom Christiebefore adoptione19bd9b

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

Document + recommend async with syntax for AsyncClient (#344)Florimond Mancabefore adoption4616031

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,

Document + recommend async with syntax for AsyncClient (#344)Florimond Mancabefore adoption4616031

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:

Document + recommend async with syntax for AsyncClient (#344)Florimond Mancabefore adoption4616031

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)

Document + recommend async with syntax for AsyncClient (#344)Florimond Mancabefore adoption4616031

lines 31–34why, one step back
31
32 elif request.url.path == "/redirect_301":33 status_code = codes.MOVED_PERMANENTLY34 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)

Document + recommend async with syntax for AsyncClient (#344)Florimond Mancabefore adoption4616031

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)

Document + recommend async with syntax for AsyncClient (#344)Florimond Mancabefore adoption4616031

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)

Document + recommend async with syntax for AsyncClient (#344)Florimond Mancabefore adoption4616031

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)

Document + recommend async with syntax for AsyncClient (#344)Florimond Mancabefore adoption4616031

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)

Document + recommend async with syntax for AsyncClient (#344)Florimond Mancabefore adoption4616031

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)

Document + recommend async with syntax for AsyncClient (#344)Florimond Mancabefore adoption4616031

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)

Document + recommend async with syntax for AsyncClient (#344)Florimond Mancabefore adoption4616031

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)

Document + recommend async with syntax for AsyncClient (#344)Florimond Mancabefore adoption4616031

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)

Document + recommend async with syntax for AsyncClient (#344)Florimond Mancabefore adoption4616031

lines 78–79why, one step back
78
79 elif request.url.path == "/redirect_body":

Bump coverage (#705)Tom Christiebefore adoptionb8394f1

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

Document + recommend async with syntax for AsyncClient (#344)Florimond Mancabefore adoption4616031

line 81why, one step back
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)

Document + recommend async with syntax for AsyncClient (#344)Florimond Mancabefore adoption4616031

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

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

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

Document + recommend async with syntax for AsyncClient (#344)Florimond Mancabefore adoption4616031

line 86why, one step back
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)

Document + recommend async with syntax for AsyncClient (#344)Florimond Mancabefore adoption4616031

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

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

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

Document + recommend async with syntax for AsyncClient (#344)Florimond Mancabefore adoption4616031

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)

Document + recommend async with syntax for AsyncClient (#344)Florimond Mancabefore adoption4616031

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(

Document + recommend async with syntax for AsyncClient (#344)Florimond Mancabefore adoption4616031

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)

Document + recommend async with syntax for AsyncClient (#344)Florimond Mancabefore adoption4616031

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)

Document + recommend async with syntax for AsyncClient (#344)Florimond Mancabefore adoption4616031

lines 105–106why, one step back
105
106

Clean up 'backend' fixture (#664)Florimond Mancabefore adoptiond0427be

lines 107–108why, one step back
107@pytest.mark.usefixtures("async_environment")
108async def test_no_redirect():

Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84

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

Document + recommend async with syntax for AsyncClient (#344)Florimond Mancabefore adoption4616031

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

Rename 'next' to 'anext' on Response (#676)Florimond Mancabefore adoption3462999

line 114why, one step back
114 await response.anext()

Document + recommend async with syntax for AsyncClient (#344)Florimond Mancabefore adoption4616031

lines 115–116why, one step back
115
116

Clean up 'backend' fixture (#664)Florimond Mancabefore adoptiond0427be

lines 117–118why, one step back
117@pytest.mark.usefixtures("async_environment")
118async def test_redirect_301():

Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84

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

Document + recommend async with syntax for AsyncClient (#344)Florimond Mancabefore adoption4616031

lines 120–125why, one step back
120 response = await client.post("https://example.org/redirect_301")
121 assert response.status_code == codes.OK122 assert response.url == URL("https://example.org/")123 assert len(response.history) == 1124125

Clean up 'backend' fixture (#664)Florimond Mancabefore adoptiond0427be

lines 126–127why, one step back
126@pytest.mark.usefixtures("async_environment")
127async def test_redirect_302():

Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84

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

Document + recommend async with syntax for AsyncClient (#344)Florimond Mancabefore adoption4616031

lines 129–134why, one step back
129 response = await client.post("https://example.org/redirect_302")
130 assert response.status_code == codes.OK131 assert response.url == URL("https://example.org/")132 assert len(response.history) == 1133134

Clean up 'backend' fixture (#664)Florimond Mancabefore adoptiond0427be

lines 135–136why, one step back
135@pytest.mark.usefixtures("async_environment")
136async def test_redirect_303():

Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84

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

Document + recommend async with syntax for AsyncClient (#344)Florimond Mancabefore adoption4616031

lines 138–143why, one step back
138 response = await client.get("https://example.org/redirect_303")
139 assert response.status_code == codes.OK140 assert response.url == URL("https://example.org/")141 assert len(response.history) == 1142143

Clean up 'backend' fixture (#664)Florimond Mancabefore adoptiond0427be

lines 144–145why, one step back
144@pytest.mark.usefixtures("async_environment")
145async def test_disallow_redirects():

Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84

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

Document + recommend async with syntax for AsyncClient (#344)Florimond Mancabefore adoption4616031

lines 147–154why, one step back
147 response = await client.post(
148 "https://example.org/redirect_303", allow_redirects=False149 )150 assert response.status_code == codes.SEE_OTHER151 assert response.url == URL("https://example.org/redirect_303")152 assert response.is_redirect is True153 assert len(response.history) == 0154

Rename 'next' to 'anext' on Response (#676)Florimond Mancabefore adoption3462999

line 155why, one step back
155 response = await response.anext()

Document + recommend async with syntax for AsyncClient (#344)Florimond Mancabefore adoption4616031

lines 156–161why, one step back
156 assert response.status_code == codes.OK
157 assert response.url == URL("https://example.org/")158 assert response.is_redirect is False159 assert len(response.history) == 1160161

Clean up 'backend' fixture (#664)Florimond Mancabefore adoptiond0427be

lines 162–163why, one step back
162@pytest.mark.usefixtures("async_environment")
163async def test_relative_redirect():

Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84

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

Document + recommend async with syntax for AsyncClient (#344)Florimond Mancabefore adoption4616031

lines 165–170why, one step back
165 response = await client.get("https://example.org/relative_redirect")
166 assert response.status_code == codes.OK167 assert response.url == URL("https://example.org/")168 assert len(response.history) == 1169170

Clean up 'backend' fixture (#664)Florimond Mancabefore adoptiond0427be

lines 171–172why, one step back
171@pytest.mark.usefixtures("async_environment")
172async def test_no_scheme_redirect():

Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84

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

Document + recommend async with syntax for AsyncClient (#344)Florimond Mancabefore adoption4616031

lines 174–179why, one step back
174 response = await client.get("https://example.org/no_scheme_redirect")
175 assert response.status_code == codes.OK176 assert response.url == URL("https://example.org/")177 assert len(response.history) == 1178179

Clean up 'backend' fixture (#664)Florimond Mancabefore adoptiond0427be

lines 180–181why, one step back
180@pytest.mark.usefixtures("async_environment")
181async def test_fragment_redirect():

Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84

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

Document + recommend async with syntax for AsyncClient (#344)Florimond Mancabefore adoption4616031

lines 183–188why, one step back
183 response = await client.get("https://example.org/relative_redirect#fragment")
184 assert response.status_code == codes.OK185 assert response.url == URL("https://example.org/#fragment")186 assert len(response.history) == 1187188

Clean up 'backend' fixture (#664)Florimond Mancabefore adoptiond0427be

lines 189–190why, one step back
189@pytest.mark.usefixtures("async_environment")
190async def test_multiple_redirects():

Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84

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

Document + recommend async with syntax for AsyncClient (#344)Florimond Mancabefore adoption4616031

lines 192–205why, one step back
192 response = await client.get("https://example.org/multiple_redirects?count=20")
193 assert response.status_code == codes.OK194 assert response.url == URL("https://example.org/multiple_redirects")195 assert len(response.history) == 20196 assert response.history[0].url == URL(197 "https://example.org/multiple_redirects?count=20"198 )199 assert response.history[1].url == URL(200 "https://example.org/multiple_redirects?count=19"201 )202 assert len(response.history[0].history) == 0203 assert len(response.history[1].history) == 1204205

Clean up 'backend' fixture (#664)Florimond Mancabefore adoptiond0427be

lines 206–207why, one step back
206@pytest.mark.usefixtures("async_environment")
207async def test_too_many_redirects():

Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84

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

Document + recommend async with syntax for AsyncClient (#344)Florimond Mancabefore adoption4616031

lines 209–212why, one step back
209 with pytest.raises(TooManyRedirects):
210 await client.get("https://example.org/multiple_redirects?count=21")211212

Clean up 'backend' fixture (#664)Florimond Mancabefore adoptiond0427be

lines 213–214why, one step back
213@pytest.mark.usefixtures("async_environment")
214async def test_too_many_redirects_calling_next():

Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84

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

Document + recommend async with syntax for AsyncClient (#344)Florimond Mancabefore adoption4616031

lines 216–219why, one step back
216 url = "https://example.org/multiple_redirects?count=21"
217 response = await client.get(url, allow_redirects=False)218 with pytest.raises(TooManyRedirects):219 while response.is_redirect:

Rename 'next' to 'anext' on Response (#676)Florimond Mancabefore adoption3462999

line 220why, one step back
220 response = await response.anext()

Document + recommend async with syntax for AsyncClient (#344)Florimond Mancabefore adoption4616031

lines 221–222why, one step back
221
222

Clean up 'backend' fixture (#664)Florimond Mancabefore adoptiond0427be

lines 223–224why, one step back
223@pytest.mark.usefixtures("async_environment")
224async def test_redirect_loop():

Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84

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

Document + recommend async with syntax for AsyncClient (#344)Florimond Mancabefore adoption4616031

lines 226–229why, one step back
226 with pytest.raises(RedirectLoop):
227 await client.get("https://example.org/redirect_loop")228229

Clean up 'backend' fixture (#664)Florimond Mancabefore adoptiond0427be

lines 230–231why, one step back
230@pytest.mark.usefixtures("async_environment")
231async def test_redirect_loop_calling_next():

Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84

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

Document + recommend async with syntax for AsyncClient (#344)Florimond Mancabefore adoption4616031

lines 233–236why, one step back
233 url = "https://example.org/redirect_loop"
234 response = await client.get(url, allow_redirects=False)235 with pytest.raises(RedirectLoop):236 while response.is_redirect:

Rename 'next' to 'anext' on Response (#676)Florimond Mancabefore adoption3462999

line 237why, one step back
237 response = await response.anext()

Document + recommend async with syntax for AsyncClient (#344)Florimond Mancabefore adoption4616031

lines 238–239why, one step back
238
239

Clean up 'backend' fixture (#664)Florimond Mancabefore adoptiond0427be

lines 240–241why, one step back
240@pytest.mark.usefixtures("async_environment")
241async def test_cross_domain_redirect():

Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84

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

Document + recommend async with syntax for AsyncClient (#344)Florimond Mancabefore adoption4616031

lines 243–249why, one step back
243 url = "https://example.com/cross_domain"
244 headers = {"Authorization": "abc"}245 response = await client.get(url, headers=headers)246 assert response.url == URL("https://example.org/cross_domain_target")247 assert "authorization" not in response.json()["headers"]248249

Clean up 'backend' fixture (#664)Florimond Mancabefore adoptiond0427be

lines 250–251why, one step back
250@pytest.mark.usefixtures("async_environment")
251async def test_same_domain_redirect():

Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84

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

Document + recommend async with syntax for AsyncClient (#344)Florimond Mancabefore adoption4616031

lines 253–259why, one step back
253 url = "https://example.org/cross_domain"
254 headers = {"Authorization": "abc"}255 response = await client.get(url, headers=headers)256 assert response.url == URL("https://example.org/cross_domain_target")257 assert response.json()["headers"]["authorization"] == "abc"258259

Clean up 'backend' fixture (#664)Florimond Mancabefore adoptiond0427be

lines 260–261why, one step back
260@pytest.mark.usefixtures("async_environment")
261async def test_body_redirect():

Document + recommend async with syntax for AsyncClient (#344)Florimond Mancabefore adoption4616031

lines 262–264why, one step back
262 """
263 A 308 redirect should preserve the request body.264 """

Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84

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

Document + recommend async with syntax for AsyncClient (#344)Florimond Mancabefore adoption4616031

lines 266–273why, one step back
266 url = "https://example.org/redirect_body"
267 data = b"Example request body"268 response = await client.post(url, data=data)269 assert response.url == URL("https://example.org/redirect_body_target")270 assert response.json()["body"] == "Example request body"271 assert "content-length" in response.json()["headers"]272273

Clean up 'backend' fixture (#664)Florimond Mancabefore adoptiond0427be

lines 274–275why, one step back
274@pytest.mark.usefixtures("async_environment")
275async def test_no_body_redirect():

Document + recommend async with syntax for AsyncClient (#344)Florimond Mancabefore adoption4616031

lines 276–278why, one step back
276 """
277 A 303 redirect should remove the request body.278 """

Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84

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

Document + recommend async with syntax for AsyncClient (#344)Florimond Mancabefore adoption4616031

lines 280–287why, one step back
280 url = "https://example.org/redirect_no_body"
281 data = b"Example request body"282 response = await client.post(url, data=data)283 assert response.url == URL("https://example.org/redirect_body_target")284 assert response.json()["body"] == ""285 assert "content-length" not in response.json()["headers"]286287

Clean up 'backend' fixture (#664)Florimond Mancabefore adoptiond0427be

lines 288–289why, one step back
288@pytest.mark.usefixtures("async_environment")
289async def test_cannot_redirect_streaming_body():

Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84

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

Document + recommend async with syntax for AsyncClient (#344)Florimond Mancabefore adoption4616031

lines 291–295why, one step back
291 url = "https://example.org/redirect_body"
292293 async def streaming_body():294 yield b"Example request body"295

Repurpose RedirectBodyUnavailable as RequestBodyUnavailable (#690)Florimond Mancabefore adoption910aa90

line 296why, one step back
296 with pytest.raises(RequestBodyUnavailable):

Document + recommend async with syntax for AsyncClient (#344)Florimond Mancabefore adoption4616031

lines 297–299why, one step back
297 await client.post(url, data=streaming_body())
298299

Clean up 'backend' fixture (#664)Florimond Mancabefore adoptiond0427be

lines 300–301why, one step back
300@pytest.mark.usefixtures("async_environment")
301async def test_cross_subdomain_redirect():

Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84

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

Document + recommend async with syntax for AsyncClient (#344)Florimond Mancabefore adoption4616031

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

Fix redirect cookie behavior (#529)Tom Christiebefore adoption5aca0c0

lines 306–307why, one step back
306
307

Dispatcher -> AsyncDispatcher (#725)Tom Christiebefore adoptione19bd9b

line 308why, one step back
308class MockCookieDispatch(AsyncDispatcher):

Fix redirect cookie behavior (#529)Tom Christiebefore adoption5aca0c0

lines 309–310why, one step back
309 async def send(
310 self,

Drop sync (#544)Tom Christiebefore adoption206c537

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

Fix redirect cookie behavior (#529)Tom Christiebefore adoption5aca0c0

lines 312–314why, one step back
312 verify: VerifyTypes = None,
313 cert: CertTypes = None,314 timeout: TimeoutTypes = None,

Drop sync (#544)Tom Christiebefore adoption206c537

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

Fix redirect cookie behavior (#529)Tom Christiebefore adoption5aca0c0

lines 316–320why, one step back
316 if request.url.path == "/":
317 if "cookie" in request.headers:318 content = b"Logged in"319 else:320 content = b"Not logged in"

Drop sync (#544)Tom Christiebefore adoption206c537

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

Fix redirect cookie behavior (#529)Tom Christiebefore adoption5aca0c0

lines 322–332why, one step back
322
323 elif request.url.path == "/login":324 status_code = codes.SEE_OTHER325 headers = {326 "location": "/",327 "set-cookie": (328 "session=eyJ1c2VybmFtZSI6ICJ0b21; path=/; Max-Age=1209600; "329 "httponly; samesite=lax"330 ),331 }332

Drop sync (#544)Tom Christiebefore adoption206c537

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

Fix redirect cookie behavior (#529)Tom Christiebefore adoption5aca0c0

lines 334–343why, one step back
334
335 elif request.url.path == "/logout":336 status_code = codes.SEE_OTHER337 headers = {338 "location": "/",339 "set-cookie": (340 "session=null; path=/; expires=Thu, 01 Jan 1970 00:00:00 GMT; "341 "httponly; samesite=lax"342 ),343 }

Drop sync (#544)Tom Christiebefore adoption206c537

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

Fix redirect cookie behavior (#529)Tom Christiebefore adoption5aca0c0

lines 345–346why, one step back
345
346

Clean up 'backend' fixture (#664)Florimond Mancabefore adoptiond0427be

lines 347–348why, one step back
347@pytest.mark.usefixtures("async_environment")
348async def test_redirect_cookie_behavior():

Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84

line 349why, one step back
349 client = AsyncClient(dispatch=MockCookieDispatch())

Fix redirect cookie behavior (#529)Tom Christiebefore adoption5aca0c0

lines 350–374why, one step back
350
351 # 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"355356 # Login redirects to the homepage, setting a session cookie.357 response = await client.post("https://example.com/login")358 assert response.url == "https://example.com/"359 assert response.text == "Logged in"360361 # The client is logged in.362 response = await client.get("https://example.com/")363 assert response.url == "https://example.com/"364 assert response.text == "Logged in"365366 # Logout redirects to the homepage, expiring the session cookie.367 response = await client.post("https://example.com/logout")368 assert response.url == "https://example.com/"369 assert response.text == "Not logged in"370371 # The client is not logged in.372 response = await client.get("https://example.com/")373 assert response.url == "https://example.com/"374 assert response.text == "Not logged in"