Skip to content
Myr

test_redirects.py

This file

Path

tests/client/test_redirects.py

485 lines · 17.9 KiB · Python

Last changed by

Increase test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067

History of this file

Take it

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

Warn if cert / verify / trust_env are passed to client.request() (#597)Tom Christiebefore adoption1e23855

line 1why, one step back
1import json

Httpcore interface (#804)Tom Christiebefore adoption3046e92

line 2why, one step back
2import typing

Warn if cert / verify / trust_env are passed to client.request() (#597)Tom Christiebefore adoption1e23855

lines 3–4why, one step back
3from urllib.parse import parse_qs
4

Httpcore interface (#804)Tom Christiebefore adoption3046e92

line 5why, one step back
5import httpcore

Warn if cert / verify / trust_env are passed to client.request() (#597)Tom Christiebefore adoption1e23855

lines 6–9why, one step back
6import pytest
78from httpx import (9 URL,

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

line 10why, one step back
10 AsyncClient,

Increase test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067

line 11why, one step back
11 Client,

Improve error when redirecting with custom schemes (#1002)Josep Cugatbefore adoption093cb42

line 12why, one step back
12 InvalidURL,

Warn if cert / verify / trust_env are passed to client.request() (#597)Tom Christiebefore adoption1e23855

line 13why, one step back
13 NotRedirectResponse,

Repurpose RedirectBodyUnavailable as RequestBodyUnavailable (#690)Florimond Mancabefore adoption910aa90

line 14why, one step back
14 RequestBodyUnavailable,

Warn if cert / verify / trust_env are passed to client.request() (#597)Tom Christiebefore adoption1e23855

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

Httpcore interface (#804)Tom Christiebefore adoption3046e92

line 18why, one step back
18from httpx._content_streams import AsyncIteratorStream, ByteStream, ContentStream

Warn if cert / verify / trust_env are passed to client.request() (#597)Tom Christiebefore adoption1e23855

lines 19–20why, one step back
19
20

Httpcore interface (#804)Tom Christiebefore adoption3046e92

lines 21–28why, one step back
21def get_header_value(headers, key, default=None):
22 lookup = key.encode("ascii").lower()23 for header_key, header_value in headers:24 if header_key.lower() == lookup:25 return header_value.decode("ascii")26 return default2728

Increase test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067

lines 29–30why, one step back
29class MockTransport:
30 def _request(

Warn if cert / verify / trust_env are passed to client.request() (#597)Tom Christiebefore adoption1e23855

line 31why, one step back
31 self,

Httpcore interface (#804)Tom Christiebefore adoption3046e92

lines 32–43why, one step back
32 method: bytes,
33 url: typing.Tuple[bytes, bytes, int, bytes],34 headers: typing.List[typing.Tuple[bytes, bytes]],35 stream: ContentStream,36 timeout: typing.Dict[str, typing.Optional[float]] = None,37 ) -> typing.Tuple[38 bytes, int, bytes, typing.List[typing.Tuple[bytes, bytes]], ContentStream39 ]:40 scheme, host, port, path = url41 path, _, query = path.partition(b"?")42 if path == b"/no_redirect":43 return b"HTTP/1.1", codes.OK, b"OK", [], ByteStream(b"")

Warn if cert / verify / trust_env are passed to client.request() (#597)Tom Christiebefore adoption1e23855

line 44why, one step back
44

Httpcore interface (#804)Tom Christiebefore adoption3046e92

line 45why, one step back
45 elif path == b"/redirect_301":

Fix for streaming a redirect response body with allow_redirects=False (#766)Tom Christiebefore adoption5a63540

lines 46–49why, one step back
46
47 async def body():48 yield b"<a href='https://example.org/'>here</a>"49

Warn if cert / verify / trust_env are passed to client.request() (#597)Tom Christiebefore adoption1e23855

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

Httpcore interface (#804)Tom Christiebefore adoption3046e92

line 51why, one step back
51 headers = [(b"location", b"https://example.org/")]

Fix for streaming a redirect response body with allow_redirects=False (#766)Tom Christiebefore adoption5a63540

line 52why, one step back
52 stream = AsyncIteratorStream(aiterator=body())

Httpcore interface (#804)Tom Christiebefore adoption3046e92

line 53why, one step back
53 return b"HTTP/1.1", status_code, b"Moved Permanently", headers, stream

Warn if cert / verify / trust_env are passed to client.request() (#597)Tom Christiebefore adoption1e23855

line 54why, one step back
54

Httpcore interface (#804)Tom Christiebefore adoption3046e92

line 55why, one step back
55 elif path == b"/redirect_302":

Warn if cert / verify / trust_env are passed to client.request() (#597)Tom Christiebefore adoption1e23855

line 56why, one step back
56 status_code = codes.FOUND

Httpcore interface (#804)Tom Christiebefore adoption3046e92

lines 57–58why, one step back
57 headers = [(b"location", b"https://example.org/")]
58 return b"HTTP/1.1", status_code, b"Found", headers, ByteStream(b"")

Warn if cert / verify / trust_env are passed to client.request() (#597)Tom Christiebefore adoption1e23855

line 59why, one step back
59

Httpcore interface (#804)Tom Christiebefore adoption3046e92

line 60why, one step back
60 elif path == b"/redirect_303":

Warn if cert / verify / trust_env are passed to client.request() (#597)Tom Christiebefore adoption1e23855

line 61why, one step back
61 status_code = codes.SEE_OTHER

Httpcore interface (#804)Tom Christiebefore adoption3046e92

lines 62–63why, one step back
62 headers = [(b"location", b"https://example.org/")]
63 return b"HTTP/1.1", status_code, b"See Other", headers, ByteStream(b"")

Warn if cert / verify / trust_env are passed to client.request() (#597)Tom Christiebefore adoption1e23855

line 64why, one step back
64

Httpcore interface (#804)Tom Christiebefore adoption3046e92

lines 65–68why, one step back
65 elif path == b"/relative_redirect":
66 status_code = codes.SEE_OTHER67 headers = [(b"location", b"/")]68 return b"HTTP/1.1", status_code, b"See Other", headers, ByteStream(b"")

Warn if cert / verify / trust_env are passed to client.request() (#597)Tom Christiebefore adoption1e23855

line 69why, one step back
69

Httpcore interface (#804)Tom Christiebefore adoption3046e92

lines 70–73why, one step back
70 elif path == b"/malformed_redirect":
71 status_code = codes.SEE_OTHER72 headers = [(b"location", b"https://:443/")]73 return b"HTTP/1.1", status_code, b"See Other", headers, ByteStream(b"")

Handle redirect with malformed Location headers missing host. (#774)Tom Christiebefore adoption1f8fb28

line 74why, one step back
74

Httpcore interface (#804)Tom Christiebefore adoption3046e92

lines 75–78why, one step back
75 elif path == b"/no_scheme_redirect":
76 status_code = codes.SEE_OTHER77 headers = [(b"location", b"//example.org/")]78 return b"HTTP/1.1", status_code, b"See Other", headers, ByteStream(b"")

Warn if cert / verify / trust_env are passed to client.request() (#597)Tom Christiebefore adoption1e23855

line 79why, one step back
79

Httpcore interface (#804)Tom Christiebefore adoption3046e92

lines 80–81why, one step back
80 elif path == b"/multiple_redirects":
81 params = parse_qs(query.decode("ascii"))

Warn if cert / verify / trust_env are passed to client.request() (#597)Tom Christiebefore adoption1e23855

lines 82–84why, one step back
82 count = int(params.get("count", "0")[0])
83 redirect_count = count - 184 code = codes.SEE_OTHER if count else codes.OK

Httpcore interface (#804)Tom Christiebefore adoption3046e92

lines 85–86why, one step back
85 phrase = b"See Other" if count else b"OK"
86 location = b"/multiple_redirects"

Warn if cert / verify / trust_env are passed to client.request() (#597)Tom Christiebefore adoption1e23855

line 87why, one step back
87 if redirect_count:

Httpcore interface (#804)Tom Christiebefore adoption3046e92

lines 88–90why, one step back
88 location += b"?count=" + str(redirect_count).encode("ascii")
89 headers = [(b"location", location)] if count else []90 return b"HTTP/1.1", code, phrase, headers, ByteStream(b"")

Warn if cert / verify / trust_env are passed to client.request() (#597)Tom Christiebefore adoption1e23855

line 91why, one step back
91

Httpcore interface (#804)Tom Christiebefore adoption3046e92

lines 92–95why, one step back
92 if path == b"/redirect_loop":
93 code = codes.SEE_OTHER94 headers = [(b"location", b"/redirect_loop")]95 return b"HTTP/1.1", code, b"See Other", headers, ByteStream(b"")

Warn if cert / verify / trust_env are passed to client.request() (#597)Tom Christiebefore adoption1e23855

line 96why, one step back
96

Httpcore interface (#804)Tom Christiebefore adoption3046e92

lines 97–100why, one step back
97 elif path == b"/cross_domain":
98 code = codes.SEE_OTHER99 headers = [(b"location", b"https://example.org/cross_domain_target")]100 return b"HTTP/1.1", code, b"See Other", headers, ByteStream(b"")

Warn if cert / verify / trust_env are passed to client.request() (#597)Tom Christiebefore adoption1e23855

line 101why, one step back
101

Httpcore interface (#804)Tom Christiebefore adoption3046e92

lines 102–107why, one step back
102 elif path == b"/cross_domain_target":
103 headers_dict = dict(104 [(key.decode("ascii"), value.decode("ascii")) for key, value in headers]105 )106 content = ByteStream(json.dumps({"headers": headers_dict}).encode())107 return b"HTTP/1.1", 200, b"OK", [], content

Warn if cert / verify / trust_env are passed to client.request() (#597)Tom Christiebefore adoption1e23855

line 108why, one step back
108

Httpcore interface (#804)Tom Christiebefore adoption3046e92

lines 109–112why, one step back
109 elif path == b"/redirect_body":
110 code = codes.PERMANENT_REDIRECT111 headers = [(b"location", b"/redirect_body_target")]112 return b"HTTP/1.1", code, b"Permanent Redirect", headers, ByteStream(b"")

Warn if cert / verify / trust_env are passed to client.request() (#597)Tom Christiebefore adoption1e23855

line 113why, one step back
113

Httpcore interface (#804)Tom Christiebefore adoption3046e92

lines 114–117why, one step back
114 elif path == b"/redirect_no_body":
115 code = codes.SEE_OTHER116 headers = [(b"location", b"/redirect_body_target")]117 return b"HTTP/1.1", code, b"See Other", headers, ByteStream(b"")

Warn if cert / verify / trust_env are passed to client.request() (#597)Tom Christiebefore adoption1e23855

line 118why, one step back
118

Httpcore interface (#804)Tom Christiebefore adoption3046e92

line 119why, one step back
119 elif path == b"/redirect_body_target":

Increase test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067

line 120why, one step back
120 content = b"".join(stream)

Httpcore interface (#804)Tom Christiebefore adoption3046e92

lines 121–127why, one step back
121 headers_dict = dict(
122 [(key.decode("ascii"), value.decode("ascii")) for key, value in headers]123 )124 body = ByteStream(125 json.dumps({"body": content.decode(), "headers": headers_dict}).encode()126 )127 return b"HTTP/1.1", 200, b"OK", [], body

Warn if cert / verify / trust_env are passed to client.request() (#597)Tom Christiebefore adoption1e23855

line 128why, one step back
128

Httpcore interface (#804)Tom Christiebefore adoption3046e92

lines 129–138why, one step back
129 elif path == b"/cross_subdomain":
130 host = get_header_value(headers, "host")131 if host != "www.example.org":132 headers = [(b"location", b"https://www.example.org/cross_subdomain")]133 return (134 b"HTTP/1.1",135 codes.PERMANENT_REDIRECT,136 b"Permanent Redirect",137 headers,138 ByteStream(b""),

Warn if cert / verify / trust_env are passed to client.request() (#597)Tom Christiebefore adoption1e23855

lines 139–140why, one step back
139 )
140 else:

Httpcore interface (#804)Tom Christiebefore adoption3046e92

line 141why, one step back
141 return b"HTTP/1.1", 200, b"OK", [], ByteStream(b"Hello, world!")

Warn if cert / verify / trust_env are passed to client.request() (#597)Tom Christiebefore adoption1e23855

line 142why, one step back
142

Improve error when redirecting with custom schemes (#1002)Josep Cugatbefore adoption093cb42

lines 143–153why, one step back
143 elif path == b"/redirect_custom_scheme":
144 status_code = codes.MOVED_PERMANENTLY145 headers = [(b"location", b"market://details?id=42")]146 return (147 b"HTTP/1.1",148 status_code,149 b"Moved Permanently",150 headers,151 ByteStream(b""),152 )153

Httpcore interface (#804)Tom Christiebefore adoption3046e92

line 154why, one step back
154 return b"HTTP/1.1", 200, b"OK", [], ByteStream(b"Hello, world!")

Warn if cert / verify / trust_env are passed to client.request() (#597)Tom Christiebefore adoption1e23855

lines 155–156why, one step back
155
156

Increase test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067

lines 157–174why, one step back
157class AsyncMockTransport(MockTransport, httpcore.AsyncHTTPTransport):
158 async def request(159 self, *args, **kwargs160 ) -> typing.Tuple[161 bytes, int, bytes, typing.List[typing.Tuple[bytes, bytes]], ContentStream162 ]:163 return self._request(*args, **kwargs)164165166class SyncMockTransport(MockTransport, httpcore.SyncHTTPTransport):167 def request(168 self, *args, **kwargs169 ) -> typing.Tuple[170 bytes, int, bytes, typing.List[typing.Tuple[bytes, bytes]], ContentStream171 ]:172 return self._request(*args, **kwargs)173174

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

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

Increase test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067

line 177why, one step back
177 client = AsyncClient(transport=AsyncMockTransport())

Warn if cert / verify / trust_env are passed to client.request() (#597)Tom Christiebefore adoption1e23855

lines 178–181why, one step back
178 url = "https://example.com/no_redirect"
179 response = await client.get(url)180 assert response.status_code == 200181 with pytest.raises(NotRedirectResponse):

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

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

Warn if cert / verify / trust_env are passed to client.request() (#597)Tom Christiebefore adoption1e23855

lines 183–184why, one step back
183
184

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

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

Increase test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067

line 187why, one step back
187 client = AsyncClient(transport=AsyncMockTransport())

Warn if cert / verify / trust_env are passed to client.request() (#597)Tom Christiebefore adoption1e23855

lines 188–193why, one step back
188 response = await client.post("https://example.org/redirect_301")
189 assert response.status_code == codes.OK190 assert response.url == URL("https://example.org/")191 assert len(response.history) == 1192193

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

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

Increase test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067

line 196why, one step back
196 client = AsyncClient(transport=AsyncMockTransport())

Warn if cert / verify / trust_env are passed to client.request() (#597)Tom Christiebefore adoption1e23855

lines 197–202why, one step back
197 response = await client.post("https://example.org/redirect_302")
198 assert response.status_code == codes.OK199 assert response.url == URL("https://example.org/")200 assert len(response.history) == 1201202

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

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

Increase test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067

line 205why, one step back
205 client = AsyncClient(transport=AsyncMockTransport())

Warn if cert / verify / trust_env are passed to client.request() (#597)Tom Christiebefore adoption1e23855

lines 206–211why, one step back
206 response = await client.get("https://example.org/redirect_303")
207 assert response.status_code == codes.OK208 assert response.url == URL("https://example.org/")209 assert len(response.history) == 1210211

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

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

Increase test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067

line 214why, one step back
214 client = AsyncClient(transport=AsyncMockTransport())

Warn if cert / verify / trust_env are passed to client.request() (#597)Tom Christiebefore adoption1e23855

lines 215–222why, one step back
215 response = await client.post(
216 "https://example.org/redirect_303", allow_redirects=False217 )218 assert response.status_code == codes.SEE_OTHER219 assert response.url == URL("https://example.org/redirect_303")220 assert response.is_redirect is True221 assert len(response.history) == 0222

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

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

Warn if cert / verify / trust_env are passed to client.request() (#597)Tom Christiebefore adoption1e23855

lines 224–229why, one step back
224 assert response.status_code == codes.OK
225 assert response.url == URL("https://example.org/")226 assert response.is_redirect is False227 assert len(response.history) == 1228229

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

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

Increase test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067

line 232why, one step back
232 client = AsyncClient(transport=AsyncMockTransport())

Warn if cert / verify / trust_env are passed to client.request() (#597)Tom Christiebefore adoption1e23855

line 233why, one step back
233 response = await client.get("https://example.org/relative_redirect")

Handle redirect with malformed Location headers missing host. (#774)Tom Christiebefore adoption1f8fb28

lines 234–241why, one step back
234 assert response.status_code == codes.OK
235 assert response.url == URL("https://example.org/")236 assert len(response.history) == 1237238239@pytest.mark.usefixtures("async_environment")240async def test_malformed_redirect():241 # https://github.com/encode/httpx/issues/771

Increase test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067

line 242why, one step back
242 client = AsyncClient(transport=AsyncMockTransport())

Handle redirect with malformed Location headers missing host. (#774)Tom Christiebefore adoption1f8fb28

line 243why, one step back
243 response = await client.get("http://example.org/malformed_redirect")

Warn if cert / verify / trust_env are passed to client.request() (#597)Tom Christiebefore adoption1e23855

lines 244–248why, one step back
244 assert response.status_code == codes.OK
245 assert response.url == URL("https://example.org/")246 assert len(response.history) == 1247248

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

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

Increase test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067

line 251why, one step back
251 client = AsyncClient(transport=AsyncMockTransport())

Warn if cert / verify / trust_env are passed to client.request() (#597)Tom Christiebefore adoption1e23855

lines 252–257why, one step back
252 response = await client.get("https://example.org/no_scheme_redirect")
253 assert response.status_code == codes.OK254 assert response.url == URL("https://example.org/")255 assert len(response.history) == 1256257

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

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

Increase test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067

line 260why, one step back
260 client = AsyncClient(transport=AsyncMockTransport())

Warn if cert / verify / trust_env are passed to client.request() (#597)Tom Christiebefore adoption1e23855

lines 261–266why, one step back
261 response = await client.get("https://example.org/relative_redirect#fragment")
262 assert response.status_code == codes.OK263 assert response.url == URL("https://example.org/#fragment")264 assert len(response.history) == 1265266

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

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

Increase test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067

line 269why, one step back
269 client = AsyncClient(transport=AsyncMockTransport())

Warn if cert / verify / trust_env are passed to client.request() (#597)Tom Christiebefore adoption1e23855

lines 270–283why, one step back
270 response = await client.get("https://example.org/multiple_redirects?count=20")
271 assert response.status_code == codes.OK272 assert response.url == URL("https://example.org/multiple_redirects")273 assert len(response.history) == 20274 assert response.history[0].url == URL(275 "https://example.org/multiple_redirects?count=20"276 )277 assert response.history[1].url == URL(278 "https://example.org/multiple_redirects?count=19"279 )280 assert len(response.history[0].history) == 0281 assert len(response.history[1].history) == 1282283

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

line 284why, one step back
284@pytest.mark.usefixtures("async_environment")

Increase test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067

lines 285–286why, one step back
285async def test_async_too_many_redirects():
286 client = AsyncClient(transport=AsyncMockTransport())

Warn if cert / verify / trust_env are passed to client.request() (#597)Tom Christiebefore adoption1e23855

lines 287–290why, one step back
287 with pytest.raises(TooManyRedirects):
288 await client.get("https://example.org/multiple_redirects?count=21")289290

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

line 291why, one step back
291@pytest.mark.usefixtures("async_environment")

Increase test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067

lines 292–293why, one step back
292async def test_async_too_many_redirects_calling_next():
293 client = AsyncClient(transport=AsyncMockTransport())

Warn if cert / verify / trust_env are passed to client.request() (#597)Tom Christiebefore adoption1e23855

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

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

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

Warn if cert / verify / trust_env are passed to client.request() (#597)Tom Christiebefore adoption1e23855

lines 299–300why, one step back
299
300

Increase test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067

lines 301–315why, one step back
301def test_sync_too_many_redirects():
302 client = Client(transport=SyncMockTransport())303 with pytest.raises(TooManyRedirects):304 client.get("https://example.org/multiple_redirects?count=21")305306307def test_sync_too_many_redirects_calling_next():308 client = Client(transport=SyncMockTransport())309 url = "https://example.org/multiple_redirects?count=21"310 response = client.get(url, allow_redirects=False)311 with pytest.raises(TooManyRedirects):312 while response.is_redirect:313 response = response.call_next()314315

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

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

Increase test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067

line 318why, one step back
318 client = AsyncClient(transport=AsyncMockTransport())

Drop RedirectLoop exception (#819)Piotr Staroszczykbefore adoptionefe9f61

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

Warn if cert / verify / trust_env are passed to client.request() (#597)Tom Christiebefore adoption1e23855

lines 320–322why, one step back
320 await client.get("https://example.org/redirect_loop")
321322

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

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

Increase test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067

line 325why, one step back
325 client = AsyncClient(transport=AsyncMockTransport())

Warn if cert / verify / trust_env are passed to client.request() (#597)Tom Christiebefore adoption1e23855

lines 326–332why, one step back
326 url = "https://example.com/cross_domain"
327 headers = {"Authorization": "abc"}328 response = await client.get(url, headers=headers)329 assert response.url == URL("https://example.org/cross_domain_target")330 assert "authorization" not in response.json()["headers"]331332

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

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

Increase test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067

line 335why, one step back
335 client = AsyncClient(transport=AsyncMockTransport())

Warn if cert / verify / trust_env are passed to client.request() (#597)Tom Christiebefore adoption1e23855

lines 336–342why, one step back
336 url = "https://example.org/cross_domain"
337 headers = {"Authorization": "abc"}338 response = await client.get(url, headers=headers)339 assert response.url == URL("https://example.org/cross_domain_target")340 assert response.json()["headers"]["authorization"] == "abc"341342

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

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

Warn if cert / verify / trust_env are passed to client.request() (#597)Tom Christiebefore adoption1e23855

lines 345–347why, one step back
345 """
346 A 308 redirect should preserve the request body.347 """

Increase test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067

line 348why, one step back
348 client = AsyncClient(transport=AsyncMockTransport())

Warn if cert / verify / trust_env are passed to client.request() (#597)Tom Christiebefore adoption1e23855

lines 349–353why, one step back
349 url = "https://example.org/redirect_body"
350 data = b"Example request body"351 response = await client.post(url, data=data)352 assert response.url == URL("https://example.org/redirect_body_target")353 assert response.json()["body"] == "Example request body"

Revert "Add content-length header for empty bytestream (#866)" (#898)Tom Christiebefore adoption631ba97

line 354why, one step back
354 assert "content-length" in response.json()["headers"]

Warn if cert / verify / trust_env are passed to client.request() (#597)Tom Christiebefore adoption1e23855

lines 355–356why, one step back
355
356

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

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

Warn if cert / verify / trust_env are passed to client.request() (#597)Tom Christiebefore adoption1e23855

lines 359–361why, one step back
359 """
360 A 303 redirect should remove the request body.361 """

Increase test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067

line 362why, one step back
362 client = AsyncClient(transport=AsyncMockTransport())

Warn if cert / verify / trust_env are passed to client.request() (#597)Tom Christiebefore adoption1e23855

lines 363–367why, one step back
363 url = "https://example.org/redirect_no_body"
364 data = b"Example request body"365 response = await client.post(url, data=data)366 assert response.url == URL("https://example.org/redirect_body_target")367 assert response.json()["body"] == ""

Revert "Add content-length header for empty bytestream (#866)" (#898)Tom Christiebefore adoption631ba97

line 368why, one step back
368 assert "content-length" not in response.json()["headers"]

Fix for streaming a redirect response body with allow_redirects=False (#766)Tom Christiebefore adoption5a63540

lines 369–372why, one step back
369
370371@pytest.mark.usefixtures("async_environment")372async def test_can_stream_if_no_redirect():

Increase test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067

line 373why, one step back
373 client = AsyncClient(transport=AsyncMockTransport())

Fix for streaming a redirect response body with allow_redirects=False (#766)Tom Christiebefore adoption5a63540

lines 374–378why, one step back
374 url = "https://example.org/redirect_301"
375 async with client.stream("GET", url, allow_redirects=False) as response:376 assert not response.is_closed377 assert response.status_code == codes.MOVED_PERMANENTLY378 assert response.headers["location"] == "https://example.org/"

Warn if cert / verify / trust_env are passed to client.request() (#597)Tom Christiebefore adoption1e23855

lines 379–380why, one step back
379
380

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

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

Increase test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067

line 383why, one step back
383 client = AsyncClient(transport=AsyncMockTransport())

Warn if cert / verify / trust_env are passed to client.request() (#597)Tom Christiebefore adoption1e23855

lines 384–386why, one step back
384 url = "https://example.org/redirect_body"
385386 async def streaming_body():

Increase test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067

line 387why, one step back
387 yield b"Example request body" # pragma: nocover

Warn if cert / verify / trust_env are passed to client.request() (#597)Tom Christiebefore adoption1e23855

line 388why, one step back
388

Repurpose RedirectBodyUnavailable as RequestBodyUnavailable (#690)Florimond Mancabefore adoption910aa90

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

Warn if cert / verify / trust_env are passed to client.request() (#597)Tom Christiebefore adoption1e23855

lines 390–392why, one step back
390 await client.post(url, data=streaming_body())
391392

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

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

Increase test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067

line 395why, one step back
395 client = AsyncClient(transport=AsyncMockTransport())

Warn if cert / verify / trust_env are passed to client.request() (#597)Tom Christiebefore adoption1e23855

lines 396–400why, one step back
396 url = "https://example.com/cross_subdomain"
397 response = await client.get(url)398 assert response.url == URL("https://www.example.org/cross_subdomain")399400

Replace remaining occurrences of dispatch with transport (#1010)Josep Cugatbefore adoption89a8100

line 401why, one step back
401class MockCookieTransport(httpcore.AsyncHTTPTransport):

Httpcore interface (#804)Tom Christiebefore adoption3046e92

line 402why, one step back
402 async def request(

Warn if cert / verify / trust_env are passed to client.request() (#597)Tom Christiebefore adoption1e23855

line 403why, one step back
403 self,

Httpcore interface (#804)Tom Christiebefore adoption3046e92

lines 404–415why, one step back
404 method: bytes,
405 url: typing.Tuple[bytes, bytes, int, bytes],406 headers: typing.List[typing.Tuple[bytes, bytes]],407 stream: ContentStream,408 timeout: typing.Dict[str, typing.Optional[float]] = None,409 ) -> typing.Tuple[410 bytes, int, bytes, typing.List[typing.Tuple[bytes, bytes]], ContentStream411 ]:412 scheme, host, port, path = url413 if path == b"/":414 cookie = get_header_value(headers, "Cookie")415 if cookie is not None:

Warn if cert / verify / trust_env are passed to client.request() (#597)Tom Christiebefore adoption1e23855

lines 416–418why, one step back
416 content = b"Logged in"
417 else:418 content = b"Not logged in"

Httpcore interface (#804)Tom Christiebefore adoption3046e92

line 419why, one step back
419 return b"HTTP/1.1", 200, b"OK", [], ByteStream(content)

Warn if cert / verify / trust_env are passed to client.request() (#597)Tom Christiebefore adoption1e23855

line 420why, one step back
420

Httpcore interface (#804)Tom Christiebefore adoption3046e92

line 421why, one step back
421 elif path == b"/login":

Warn if cert / verify / trust_env are passed to client.request() (#597)Tom Christiebefore adoption1e23855

line 422why, one step back
422 status_code = codes.SEE_OTHER

Httpcore interface (#804)Tom Christiebefore adoption3046e92

lines 423–430why, one step back
423 headers = [
424 (b"location", b"/"),425 (426 b"set-cookie",427 (428 b"session=eyJ1c2VybmFtZSI6ICJ0b21; path=/; Max-Age=1209600; "429 b"httponly; samesite=lax"430 ),

Warn if cert / verify / trust_env are passed to client.request() (#597)Tom Christiebefore adoption1e23855

line 431why, one step back
431 ),

Httpcore interface (#804)Tom Christiebefore adoption3046e92

lines 432–433why, one step back
432 ]
433 return b"HTTP/1.1", status_code, b"See Other", headers, ByteStream(b"")

Warn if cert / verify / trust_env are passed to client.request() (#597)Tom Christiebefore adoption1e23855

line 434why, one step back
434

Httpcore interface (#804)Tom Christiebefore adoption3046e92

line 435why, one step back
435 elif path == b"/logout":

Warn if cert / verify / trust_env are passed to client.request() (#597)Tom Christiebefore adoption1e23855

line 436why, one step back
436 status_code = codes.SEE_OTHER

Httpcore interface (#804)Tom Christiebefore adoption3046e92

lines 437–444why, one step back
437 headers = [
438 (b"location", b"/"),439 (440 b"set-cookie",441 (442 b"session=null; path=/; expires=Thu, 01 Jan 1970 00:00:00 GMT; "443 b"httponly; samesite=lax"444 ),

Warn if cert / verify / trust_env are passed to client.request() (#597)Tom Christiebefore adoption1e23855

line 445why, one step back
445 ),

Httpcore interface (#804)Tom Christiebefore adoption3046e92

lines 446–447why, one step back
446 ]
447 return b"HTTP/1.1", status_code, b"See Other", headers, ByteStream(b"")

Warn if cert / verify / trust_env are passed to client.request() (#597)Tom Christiebefore adoption1e23855

lines 448–449why, one step back
448
449

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

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

Replace remaining occurrences of dispatch with transport (#1010)Josep Cugatbefore adoption89a8100

line 452why, one step back
452 client = AsyncClient(transport=MockCookieTransport())

Warn if cert / verify / trust_env are passed to client.request() (#597)Tom Christiebefore adoption1e23855

lines 453–477why, one step back
453
454 # The client is not logged in.455 response = await client.get("https://example.com/")456 assert response.url == "https://example.com/"457 assert response.text == "Not logged in"458459 # Login redirects to the homepage, setting a session cookie.460 response = await client.post("https://example.com/login")461 assert response.url == "https://example.com/"462 assert response.text == "Logged in"463464 # The client is logged in.465 response = await client.get("https://example.com/")466 assert response.url == "https://example.com/"467 assert response.text == "Logged in"468469 # Logout redirects to the homepage, expiring the session cookie.470 response = await client.post("https://example.com/logout")471 assert response.url == "https://example.com/"472 assert response.text == "Not logged in"473474 # The client is not logged in.475 response = await client.get("https://example.com/")476 assert response.url == "https://example.com/"477 assert response.text == "Not logged in"

Improve error when redirecting with custom schemes (#1002)Josep Cugatbefore adoption093cb42

lines 478–481why, one step back
478
479480@pytest.mark.usefixtures("async_environment")481async def test_redirect_custom_scheme():

Increase test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067

line 482why, one step back
482 client = AsyncClient(transport=AsyncMockTransport())

Improve error when redirecting with custom schemes (#1002)Josep Cugatbefore adoption093cb42

lines 483–485why, one step back
483 with pytest.raises(InvalidURL) as e:
484 await client.post("https://example.org/redirect_custom_scheme")485 assert str(e.value) == 'Scheme "market" not supported.'