Skip to content
Myr

test_redirects.py

This file

Path

tests/client/test_redirects.py

427 lines · 16.3 KiB · Python

Last changed by

Transport API as plain `request -> response` method. (#1840)Tom Christiebefore adoptionff9813e

History of this file

Take it

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

Allow covariants of __enter__, __aenter__, and @classmethod (#1336)Changshengbefore adoption4b87740

lines 1–7why, one step back
1import pytest
23import httpx456def redirects(request: httpx.Request) -> httpx.Response:7 if request.url.scheme not in ("http", "https"):

Transport API (#1522)Tom Christiebefore adoption1a6e254

line 8why, one step back
8 raise httpx.UnsupportedProtocol(f"Scheme {request.url.scheme!r} not supported.")

Allow covariants of __enter__, __aenter__, and @classmethod (#1336)Changshengbefore adoption4b87740

line 9why, one step back
9

Drop `.next()`/`.anext()` in favour of `response.next_request` (#1339)Tom Christiebefore adoption0eed6a3

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

Allow covariants of __enter__, __aenter__, and @classmethod (#1336)Changshengbefore adoption4b87740

lines 11–114why, one step back
11 status_code = httpx.codes.MOVED_PERMANENTLY
12 content = b"<a href='https://example.org/'>here</a>"13 headers = {"location": "https://example.org/"}14 return httpx.Response(status_code, headers=headers, content=content)1516 elif request.url.path == "/redirect_302":17 status_code = httpx.codes.FOUND18 headers = {"location": "https://example.org/"}19 return httpx.Response(status_code, headers=headers)2021 elif request.url.path == "/redirect_303":22 status_code = httpx.codes.SEE_OTHER23 headers = {"location": "https://example.org/"}24 return httpx.Response(status_code, headers=headers)2526 elif request.url.path == "/relative_redirect":27 status_code = httpx.codes.SEE_OTHER28 headers = {"location": "/"}29 return httpx.Response(status_code, headers=headers)3031 elif request.url.path == "/malformed_redirect":32 status_code = httpx.codes.SEE_OTHER33 headers = {"location": "https://:443/"}34 return httpx.Response(status_code, headers=headers)3536 elif request.url.path == "/invalid_redirect":37 status_code = httpx.codes.SEE_OTHER38 raw_headers = [(b"location", "https://😇/".encode("utf-8"))]39 return httpx.Response(status_code, headers=raw_headers)4041 elif request.url.path == "/no_scheme_redirect":42 status_code = httpx.codes.SEE_OTHER43 headers = {"location": "//example.org/"}44 return httpx.Response(status_code, headers=headers)4546 elif request.url.path == "/multiple_redirects":47 params = httpx.QueryParams(request.url.query)48 count = int(params.get("count", "0"))49 redirect_count = count - 150 status_code = httpx.codes.SEE_OTHER if count else httpx.codes.OK51 if count:52 location = "/multiple_redirects"53 if redirect_count:54 location += f"?count={redirect_count}"55 headers = {"location": location}56 else:57 headers = {}58 return httpx.Response(status_code, headers=headers)5960 if request.url.path == "/redirect_loop":61 status_code = httpx.codes.SEE_OTHER62 headers = {"location": "/redirect_loop"}63 return httpx.Response(status_code, headers=headers)6465 elif request.url.path == "/cross_domain":66 status_code = httpx.codes.SEE_OTHER67 headers = {"location": "https://example.org/cross_domain_target"}68 return httpx.Response(status_code, headers=headers)6970 elif request.url.path == "/cross_domain_target":71 status_code = httpx.codes.OK72 data = {73 "body": request.content.decode("ascii"),74 "headers": dict(request.headers),75 }76 return httpx.Response(status_code, json=data)7778 elif request.url.path == "/redirect_body":79 status_code = httpx.codes.PERMANENT_REDIRECT80 headers = {"location": "/redirect_body_target"}81 return httpx.Response(status_code, headers=headers)8283 elif request.url.path == "/redirect_no_body":84 status_code = httpx.codes.SEE_OTHER85 headers = {"location": "/redirect_body_target"}86 return httpx.Response(status_code, headers=headers)8788 elif request.url.path == "/redirect_body_target":89 data = {90 "body": request.content.decode("ascii"),91 "headers": dict(request.headers),92 }93 return httpx.Response(200, json=data)9495 elif request.url.path == "/cross_subdomain":96 if request.headers["Host"] != "www.example.org":97 status_code = httpx.codes.PERMANENT_REDIRECT98 headers = {"location": "https://www.example.org/cross_subdomain"}99 return httpx.Response(status_code, headers=headers)100 else:101 return httpx.Response(200, text="Hello, world!")102103 elif request.url.path == "/redirect_custom_scheme":104 status_code = httpx.codes.MOVED_PERMANENTLY105 headers = {"location": "market://details?id=42"}106 return httpx.Response(status_code, headers=headers)107108 if request.method == "HEAD":109 return httpx.Response(200)110111 return httpx.Response(200, html="<html><body>Hello, world!</body></html>")112113114def test_redirect_301():

Add `httpx.MockTransport()` (#1401)Tom Christiebefore adoption9c7c2ac

line 115why, one step back
115 client = httpx.Client(transport=httpx.MockTransport(redirects))

Switch follow redirects default (#1808)Tom Christiebefore adoption47266d7

line 116why, one step back
116 response = client.post("https://example.org/redirect_301", follow_redirects=True)

Allow covariants of __enter__, __aenter__, and @classmethod (#1336)Changshengbefore adoption4b87740

lines 117–122why, one step back
117 assert response.status_code == httpx.codes.OK
118 assert response.url == "https://example.org/"119 assert len(response.history) == 1120121122def test_redirect_302():

Add `httpx.MockTransport()` (#1401)Tom Christiebefore adoption9c7c2ac

line 123why, one step back
123 client = httpx.Client(transport=httpx.MockTransport(redirects))

Switch follow redirects default (#1808)Tom Christiebefore adoption47266d7

line 124why, one step back
124 response = client.post("https://example.org/redirect_302", follow_redirects=True)

Allow covariants of __enter__, __aenter__, and @classmethod (#1336)Changshengbefore adoption4b87740

lines 125–130why, one step back
125 assert response.status_code == httpx.codes.OK
126 assert response.url == "https://example.org/"127 assert len(response.history) == 1128129130def test_redirect_303():

Add `httpx.MockTransport()` (#1401)Tom Christiebefore adoption9c7c2ac

line 131why, one step back
131 client = httpx.Client(transport=httpx.MockTransport(redirects))

Switch follow redirects default (#1808)Tom Christiebefore adoption47266d7

line 132why, one step back
132 response = client.get("https://example.org/redirect_303", follow_redirects=True)

Allow covariants of __enter__, __aenter__, and @classmethod (#1336)Changshengbefore adoption4b87740

lines 133–137why, one step back
133 assert response.status_code == httpx.codes.OK
134 assert response.url == "https://example.org/"135 assert len(response.history) == 1136137

Drop `.next()`/`.anext()` in favour of `response.next_request` (#1339)Tom Christiebefore adoption0eed6a3

line 138why, one step back
138def test_next_request():

Add `httpx.MockTransport()` (#1401)Tom Christiebefore adoption9c7c2ac

line 139why, one step back
139 client = httpx.Client(transport=httpx.MockTransport(redirects))

Drop `.next()`/`.anext()` in favour of `response.next_request` (#1339)Tom Christiebefore adoption0eed6a3

line 140why, one step back
140 request = client.build_request("POST", "https://example.org/redirect_303")

Switch follow redirects default (#1808)Tom Christiebefore adoption47266d7

line 141why, one step back
141 response = client.send(request, follow_redirects=False)

Allow covariants of __enter__, __aenter__, and @classmethod (#1336)Changshengbefore adoption4b87740

lines 142–143why, one step back
142 assert response.status_code == httpx.codes.SEE_OTHER
143 assert response.url == "https://example.org/redirect_303"

Drop `.next()`/`.anext()` in favour of `response.next_request` (#1339)Tom Christiebefore adoption0eed6a3

line 144why, one step back
144 assert response.next_request is not None

Allow covariants of __enter__, __aenter__, and @classmethod (#1336)Changshengbefore adoption4b87740

line 145why, one step back
145

Switch follow redirects default (#1808)Tom Christiebefore adoption47266d7

line 146why, one step back
146 response = client.send(response.next_request, follow_redirects=False)

Allow covariants of __enter__, __aenter__, and @classmethod (#1336)Changshengbefore adoption4b87740

lines 147–148why, one step back
147 assert response.status_code == httpx.codes.OK
148 assert response.url == "https://example.org/"

Drop `.next()`/`.anext()` in favour of `response.next_request` (#1339)Tom Christiebefore adoption0eed6a3

line 149why, one step back
149 assert response.next_request is None

Allow covariants of __enter__, __aenter__, and @classmethod (#1336)Changshengbefore adoption4b87740

lines 150–151why, one step back
150
151

Drop `.next()`/`.anext()` in favour of `response.next_request` (#1339)Tom Christiebefore adoption0eed6a3

lines 152–153why, one step back
152@pytest.mark.usefixtures("async_environment")
153async def test_async_next_request():

Add `httpx.MockTransport()` (#1401)Tom Christiebefore adoption9c7c2ac

line 154why, one step back
154 async with httpx.AsyncClient(transport=httpx.MockTransport(redirects)) as client:

Close AsyncClient properly in test_async_next_request (#1361)Tom Christiebefore adoption07229b8

line 155why, one step back
155 request = client.build_request("POST", "https://example.org/redirect_303")

Switch follow redirects default (#1808)Tom Christiebefore adoption47266d7

line 156why, one step back
156 response = await client.send(request, follow_redirects=False)

Close AsyncClient properly in test_async_next_request (#1361)Tom Christiebefore adoption07229b8

lines 157–159why, one step back
157 assert response.status_code == httpx.codes.SEE_OTHER
158 assert response.url == "https://example.org/redirect_303"159 assert response.next_request is not None

Allow covariants of __enter__, __aenter__, and @classmethod (#1336)Changshengbefore adoption4b87740

line 160why, one step back
160

Switch follow redirects default (#1808)Tom Christiebefore adoption47266d7

line 161why, one step back
161 response = await client.send(response.next_request, follow_redirects=False)

Close AsyncClient properly in test_async_next_request (#1361)Tom Christiebefore adoption07229b8

lines 162–164why, one step back
162 assert response.status_code == httpx.codes.OK
163 assert response.url == "https://example.org/"164 assert response.next_request is None

Allow covariants of __enter__, __aenter__, and @classmethod (#1336)Changshengbefore adoption4b87740

lines 165–170why, one step back
165
166167def test_head_redirect():168 """169 Contrary to Requests, redirects remain enabled by default for HEAD requests.170 """

Add `httpx.MockTransport()` (#1401)Tom Christiebefore adoption9c7c2ac

line 171why, one step back
171 client = httpx.Client(transport=httpx.MockTransport(redirects))

Switch follow redirects default (#1808)Tom Christiebefore adoption47266d7

line 172why, one step back
172 response = client.head("https://example.org/redirect_302", follow_redirects=True)

Allow covariants of __enter__, __aenter__, and @classmethod (#1336)Changshengbefore adoption4b87740

lines 173–180why, one step back
173 assert response.status_code == httpx.codes.OK
174 assert response.url == "https://example.org/"175 assert response.request.method == "HEAD"176 assert len(response.history) == 1177 assert response.text == ""178179180def test_relative_redirect():

Add `httpx.MockTransport()` (#1401)Tom Christiebefore adoption9c7c2ac

line 181why, one step back
181 client = httpx.Client(transport=httpx.MockTransport(redirects))

Switch follow redirects default (#1808)Tom Christiebefore adoption47266d7

lines 182–184why, one step back
182 response = client.get(
183 "https://example.org/relative_redirect", follow_redirects=True184 )

Allow covariants of __enter__, __aenter__, and @classmethod (#1336)Changshengbefore adoption4b87740

lines 185–191why, one step back
185 assert response.status_code == httpx.codes.OK
186 assert response.url == "https://example.org/"187 assert len(response.history) == 1188189190def test_malformed_redirect():191 # https://github.com/encode/httpx/issues/771

Add `httpx.MockTransport()` (#1401)Tom Christiebefore adoption9c7c2ac

line 192why, one step back
192 client = httpx.Client(transport=httpx.MockTransport(redirects))

Switch follow redirects default (#1808)Tom Christiebefore adoption47266d7

lines 193–195why, one step back
193 response = client.get(
194 "http://example.org/malformed_redirect", follow_redirects=True195 )

Allow covariants of __enter__, __aenter__, and @classmethod (#1336)Changshengbefore adoption4b87740

lines 196–201why, one step back
196 assert response.status_code == httpx.codes.OK
197 assert response.url == "https://example.org:443/"198 assert len(response.history) == 1199200201def test_invalid_redirect():

Add `httpx.MockTransport()` (#1401)Tom Christiebefore adoption9c7c2ac

line 202why, one step back
202 client = httpx.Client(transport=httpx.MockTransport(redirects))

Allow covariants of __enter__, __aenter__, and @classmethod (#1336)Changshengbefore adoption4b87740

line 203why, one step back
203 with pytest.raises(httpx.RemoteProtocolError):

Switch follow redirects default (#1808)Tom Christiebefore adoption47266d7

line 204why, one step back
204 client.get("http://example.org/invalid_redirect", follow_redirects=True)

Allow covariants of __enter__, __aenter__, and @classmethod (#1336)Changshengbefore adoption4b87740

lines 205–207why, one step back
205
206207def test_no_scheme_redirect():

Add `httpx.MockTransport()` (#1401)Tom Christiebefore adoption9c7c2ac

line 208why, one step back
208 client = httpx.Client(transport=httpx.MockTransport(redirects))

Switch follow redirects default (#1808)Tom Christiebefore adoption47266d7

lines 209–211why, one step back
209 response = client.get(
210 "https://example.org/no_scheme_redirect", follow_redirects=True211 )

Allow covariants of __enter__, __aenter__, and @classmethod (#1336)Changshengbefore adoption4b87740

lines 212–217why, one step back
212 assert response.status_code == httpx.codes.OK
213 assert response.url == "https://example.org/"214 assert len(response.history) == 1215216217def test_fragment_redirect():

Add `httpx.MockTransport()` (#1401)Tom Christiebefore adoption9c7c2ac

line 218why, one step back
218 client = httpx.Client(transport=httpx.MockTransport(redirects))

Switch follow redirects default (#1808)Tom Christiebefore adoption47266d7

lines 219–221why, one step back
219 response = client.get(
220 "https://example.org/relative_redirect#fragment", follow_redirects=True221 )

Allow covariants of __enter__, __aenter__, and @classmethod (#1336)Changshengbefore adoption4b87740

lines 222–227why, one step back
222 assert response.status_code == httpx.codes.OK
223 assert response.url == "https://example.org/#fragment"224 assert len(response.history) == 1225226227def test_multiple_redirects():

Add `httpx.MockTransport()` (#1401)Tom Christiebefore adoption9c7c2ac

line 228why, one step back
228 client = httpx.Client(transport=httpx.MockTransport(redirects))

Switch follow redirects default (#1808)Tom Christiebefore adoption47266d7

lines 229–231why, one step back
229 response = client.get(
230 "https://example.org/multiple_redirects?count=20", follow_redirects=True231 )

Allow covariants of __enter__, __aenter__, and @classmethod (#1336)Changshengbefore adoption4b87740

lines 232–242why, one step back
232 assert response.status_code == httpx.codes.OK
233 assert response.url == "https://example.org/multiple_redirects"234 assert len(response.history) == 20235 assert response.history[0].url == "https://example.org/multiple_redirects?count=20"236 assert response.history[1].url == "https://example.org/multiple_redirects?count=19"237 assert len(response.history[0].history) == 0238 assert len(response.history[1].history) == 1239240241@pytest.mark.usefixtures("async_environment")242async def test_async_too_many_redirects():

Add `httpx.MockTransport()` (#1401)Tom Christiebefore adoption9c7c2ac

line 243why, one step back
243 async with httpx.AsyncClient(transport=httpx.MockTransport(redirects)) as client:

Allow covariants of __enter__, __aenter__, and @classmethod (#1336)Changshengbefore adoption4b87740

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

Switch follow redirects default (#1808)Tom Christiebefore adoption47266d7

lines 245–247why, one step back
245 await client.get(
246 "https://example.org/multiple_redirects?count=21", follow_redirects=True247 )

Allow covariants of __enter__, __aenter__, and @classmethod (#1336)Changshengbefore adoption4b87740

lines 248–250why, one step back
248
249250def test_sync_too_many_redirects():

Add `httpx.MockTransport()` (#1401)Tom Christiebefore adoption9c7c2ac

line 251why, one step back
251 client = httpx.Client(transport=httpx.MockTransport(redirects))

Allow covariants of __enter__, __aenter__, and @classmethod (#1336)Changshengbefore adoption4b87740

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

Switch follow redirects default (#1808)Tom Christiebefore adoption47266d7

lines 253–255why, one step back
253 client.get(
254 "https://example.org/multiple_redirects?count=21", follow_redirects=True255 )

Allow covariants of __enter__, __aenter__, and @classmethod (#1336)Changshengbefore adoption4b87740

lines 256–258why, one step back
256
257258def test_redirect_loop():

Add `httpx.MockTransport()` (#1401)Tom Christiebefore adoption9c7c2ac

line 259why, one step back
259 client = httpx.Client(transport=httpx.MockTransport(redirects))

Allow covariants of __enter__, __aenter__, and @classmethod (#1336)Changshengbefore adoption4b87740

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

Switch follow redirects default (#1808)Tom Christiebefore adoption47266d7

line 261why, one step back
261 client.get("https://example.org/redirect_loop", follow_redirects=True)

Allow covariants of __enter__, __aenter__, and @classmethod (#1336)Changshengbefore adoption4b87740

lines 262–264why, one step back
262
263264def test_cross_domain_redirect_with_auth_header():

Add `httpx.MockTransport()` (#1401)Tom Christiebefore adoption9c7c2ac

line 265why, one step back
265 client = httpx.Client(transport=httpx.MockTransport(redirects))

Allow covariants of __enter__, __aenter__, and @classmethod (#1336)Changshengbefore adoption4b87740

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

Switch follow redirects default (#1808)Tom Christiebefore adoption47266d7

line 268why, one step back
268 response = client.get(url, headers=headers, follow_redirects=True)

Allow covariants of __enter__, __aenter__, and @classmethod (#1336)Changshengbefore adoption4b87740

lines 269–273why, one step back
269 assert response.url == "https://example.org/cross_domain_target"
270 assert "authorization" not in response.json()["headers"]271272273def test_cross_domain_redirect_with_auth():

Add `httpx.MockTransport()` (#1401)Tom Christiebefore adoption9c7c2ac

line 274why, one step back
274 client = httpx.Client(transport=httpx.MockTransport(redirects))

Allow covariants of __enter__, __aenter__, and @classmethod (#1336)Changshengbefore adoption4b87740

line 275why, one step back
275 url = "https://example.com/cross_domain"

Switch follow redirects default (#1808)Tom Christiebefore adoption47266d7

line 276why, one step back
276 response = client.get(url, auth=("user", "pass"), follow_redirects=True)

Allow covariants of __enter__, __aenter__, and @classmethod (#1336)Changshengbefore adoption4b87740

lines 277–281why, one step back
277 assert response.url == "https://example.org/cross_domain_target"
278 assert "authorization" not in response.json()["headers"]279280281def test_same_domain_redirect():

Add `httpx.MockTransport()` (#1401)Tom Christiebefore adoption9c7c2ac

line 282why, one step back
282 client = httpx.Client(transport=httpx.MockTransport(redirects))

Allow covariants of __enter__, __aenter__, and @classmethod (#1336)Changshengbefore adoption4b87740

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

Switch follow redirects default (#1808)Tom Christiebefore adoption47266d7

line 285why, one step back
285 response = client.get(url, headers=headers, follow_redirects=True)

Allow covariants of __enter__, __aenter__, and @classmethod (#1336)Changshengbefore adoption4b87740

lines 286–293why, one step back
286 assert response.url == "https://example.org/cross_domain_target"
287 assert response.json()["headers"]["authorization"] == "abc"288289290def test_body_redirect():291 """292 A 308 redirect should preserve the request body.293 """

Add `httpx.MockTransport()` (#1401)Tom Christiebefore adoption9c7c2ac

line 294why, one step back
294 client = httpx.Client(transport=httpx.MockTransport(redirects))

Allow covariants of __enter__, __aenter__, and @classmethod (#1336)Changshengbefore adoption4b87740

lines 295–296why, one step back
295 url = "https://example.org/redirect_body"
296 content = b"Example request body"

Switch follow redirects default (#1808)Tom Christiebefore adoption47266d7

line 297why, one step back
297 response = client.post(url, content=content, follow_redirects=True)

Allow covariants of __enter__, __aenter__, and @classmethod (#1336)Changshengbefore adoption4b87740

lines 298–306why, one step back
298 assert response.url == "https://example.org/redirect_body_target"
299 assert response.json()["body"] == "Example request body"300 assert "content-length" in response.json()["headers"]301302303def test_no_body_redirect():304 """305 A 303 redirect should remove the request body.306 """

Add `httpx.MockTransport()` (#1401)Tom Christiebefore adoption9c7c2ac

line 307why, one step back
307 client = httpx.Client(transport=httpx.MockTransport(redirects))

Allow covariants of __enter__, __aenter__, and @classmethod (#1336)Changshengbefore adoption4b87740

lines 308–309why, one step back
308 url = "https://example.org/redirect_no_body"
309 content = b"Example request body"

Switch follow redirects default (#1808)Tom Christiebefore adoption47266d7

line 310why, one step back
310 response = client.post(url, content=content, follow_redirects=True)

Allow covariants of __enter__, __aenter__, and @classmethod (#1336)Changshengbefore adoption4b87740

lines 311–316why, one step back
311 assert response.url == "https://example.org/redirect_body_target"
312 assert response.json()["body"] == ""313 assert "content-length" not in response.json()["headers"]314315316def test_can_stream_if_no_redirect():

Add `httpx.MockTransport()` (#1401)Tom Christiebefore adoption9c7c2ac

line 317why, one step back
317 client = httpx.Client(transport=httpx.MockTransport(redirects))

Allow covariants of __enter__, __aenter__, and @classmethod (#1336)Changshengbefore adoption4b87740

line 318why, one step back
318 url = "https://example.org/redirect_301"

Switch follow redirects default (#1808)Tom Christiebefore adoption47266d7

line 319why, one step back
319 with client.stream("GET", url, follow_redirects=False) as response:

Transport API as plain `request -> response` method. (#1840)Tom Christiebefore adoptionff9813e

line 320why, one step back
320 pass

Allow covariants of __enter__, __aenter__, and @classmethod (#1336)Changshengbefore adoption4b87740

lines 321–324why, one step back
321 assert response.status_code == httpx.codes.MOVED_PERMANENTLY
322 assert response.headers["location"] == "https://example.org/"323324

Transport API as plain `request -> response` method. (#1840)Tom Christiebefore adoptionff9813e

lines 325–331why, one step back
325class ConsumeBodyTransport(httpx.MockTransport):
326 def handle_request(self, request: httpx.Request) -> httpx.Response:327 assert isinstance(request.stream, httpx.SyncByteStream)328 [_ for _ in request.stream]329 return self.handler(request)330331

Allow covariants of __enter__, __aenter__, and @classmethod (#1336)Changshengbefore adoption4b87740

line 332why, one step back
332def test_cannot_redirect_streaming_body():

Transport API as plain `request -> response` method. (#1840)Tom Christiebefore adoptionff9813e

line 333why, one step back
333 client = httpx.Client(transport=ConsumeBodyTransport(redirects))

Allow covariants of __enter__, __aenter__, and @classmethod (#1336)Changshengbefore adoption4b87740

lines 334–339why, one step back
334 url = "https://example.org/redirect_body"
335336 def streaming_body():337 yield b"Example request body" # pragma: nocover338339 with pytest.raises(httpx.StreamConsumed):

Switch follow redirects default (#1808)Tom Christiebefore adoption47266d7

line 340why, one step back
340 client.post(url, content=streaming_body(), follow_redirects=True)

Allow covariants of __enter__, __aenter__, and @classmethod (#1336)Changshengbefore adoption4b87740

lines 341–343why, one step back
341
342343def test_cross_subdomain_redirect():

Add `httpx.MockTransport()` (#1401)Tom Christiebefore adoption9c7c2ac

line 344why, one step back
344 client = httpx.Client(transport=httpx.MockTransport(redirects))

Allow covariants of __enter__, __aenter__, and @classmethod (#1336)Changshengbefore adoption4b87740

line 345why, one step back
345 url = "https://example.com/cross_subdomain"

Switch follow redirects default (#1808)Tom Christiebefore adoption47266d7

line 346why, one step back
346 response = client.get(url, follow_redirects=True)

Allow covariants of __enter__, __aenter__, and @classmethod (#1336)Changshengbefore adoption4b87740

lines 347–383why, one step back
347 assert response.url == "https://www.example.org/cross_subdomain"
348349350def cookie_sessions(request: httpx.Request) -> httpx.Response:351 if request.url.path == "/":352 cookie = request.headers.get("Cookie")353 if cookie is not None:354 content = b"Logged in"355 else:356 content = b"Not logged in"357 return httpx.Response(200, content=content)358359 elif request.url.path == "/login":360 status_code = httpx.codes.SEE_OTHER361 headers = {362 "location": "/",363 "set-cookie": (364 "session=eyJ1c2VybmFtZSI6ICJ0b21; path=/; Max-Age=1209600; "365 "httponly; samesite=lax"366 ),367 }368 return httpx.Response(status_code, headers=headers)369370 else:371 assert request.url.path == "/logout"372 status_code = httpx.codes.SEE_OTHER373 headers = {374 "location": "/",375 "set-cookie": (376 "session=null; path=/; expires=Thu, 01 Jan 1970 00:00:00 GMT; "377 "httponly; samesite=lax"378 ),379 }380 return httpx.Response(status_code, headers=headers)381382383def test_redirect_cookie_behavior():

Switch follow redirects default (#1808)Tom Christiebefore adoption47266d7

lines 384–386why, one step back
384 client = httpx.Client(
385 transport=httpx.MockTransport(cookie_sessions), follow_redirects=True386 )

Allow covariants of __enter__, __aenter__, and @classmethod (#1336)Changshengbefore adoption4b87740

lines 387–414why, one step back
387
388 # The client is not logged in.389 response = client.get("https://example.com/")390 assert response.url == "https://example.com/"391 assert response.text == "Not logged in"392393 # Login redirects to the homepage, setting a session cookie.394 response = client.post("https://example.com/login")395 assert response.url == "https://example.com/"396 assert response.text == "Logged in"397398 # The client is logged in.399 response = client.get("https://example.com/")400 assert response.url == "https://example.com/"401 assert response.text == "Logged in"402403 # Logout redirects to the homepage, expiring the session cookie.404 response = client.post("https://example.com/logout")405 assert response.url == "https://example.com/"406 assert response.text == "Not logged in"407408 # The client is not logged in.409 response = client.get("https://example.com/")410 assert response.url == "https://example.com/"411 assert response.text == "Not logged in"412413414def test_redirect_custom_scheme():

Add `httpx.MockTransport()` (#1401)Tom Christiebefore adoption9c7c2ac

line 415why, one step back
415 client = httpx.Client(transport=httpx.MockTransport(redirects))

Allow covariants of __enter__, __aenter__, and @classmethod (#1336)Changshengbefore adoption4b87740

line 416why, one step back
416 with pytest.raises(httpx.UnsupportedProtocol) as e:

Switch follow redirects default (#1808)Tom Christiebefore adoption47266d7

line 417why, one step back
417 client.post("https://example.org/redirect_custom_scheme", follow_redirects=True)

Allow covariants of __enter__, __aenter__, and @classmethod (#1336)Changshengbefore adoption4b87740

line 418why, one step back
418 assert str(e.value) == "Scheme 'market' not supported."

Transport API (#1522)Tom Christiebefore adoption1a6e254

lines 419–424why, one step back
419
420421@pytest.mark.usefixtures("async_environment")422async def test_async_invalid_redirect():423 async with httpx.AsyncClient(transport=httpx.MockTransport(redirects)) as client:424 with pytest.raises(httpx.RemoteProtocolError):

Switch follow redirects default (#1808)Tom Christiebefore adoption47266d7

lines 425–427why, one step back
425 await client.get(
426 "http://example.org/invalid_redirect", follow_redirects=True427 )