Skip to content
Myr

test_redirects.py

This file

Path

tests/client/test_redirects.py

416 lines · 15.4 KiB · Python

Last changed by

Version 0.15.0 (#1301)Tom Christiebefore adoptionf932af9

History of this file

Take it

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

Use URLLib3 configuration options for URLLib3 transport (#970)Tom Christiebefore adoption225f1e2

lines 1–3why, one step back
1import httpcore
2import pytest3

Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205

line 4why, one step back
4import httpx

Version 0.15.0 (#1301)Tom Christiebefore adoptionf932af9

line 5why, one step back
5from tests.utils import MockTransport

Use URLLib3 configuration options for URLLib3 transport (#970)Tom Christiebefore adoption225f1e2

lines 6–7why, one step back
6
7

Refactor tests to use `MockTransport(<handler_function>)` (#1281)Tom Christiebefore adoptionc2afd2d

lines 8–12why, one step back
8def redirects(request: httpx.Request) -> httpx.Response:
9 if request.url.scheme not in ("http", "https"):10 raise httpcore.UnsupportedProtocol(11 f"Scheme {request.url.scheme!r} not supported."12 )

Use URLLib3 configuration options for URLLib3 transport (#970)Tom Christiebefore adoption225f1e2

line 13why, one step back
13

Refactor tests to use `MockTransport(<handler_function>)` (#1281)Tom Christiebefore adoptionc2afd2d

lines 14–15why, one step back
14 if request.url.path == "/no_redirect":
15 return httpx.Response(200)

Use URLLib3 configuration options for URLLib3 transport (#970)Tom Christiebefore adoption225f1e2

line 16why, one step back
16

Refactor tests to use `MockTransport(<handler_function>)` (#1281)Tom Christiebefore adoptionc2afd2d

lines 17–21why, one step back
17 elif request.url.path == "/redirect_301":
18 status_code = httpx.codes.MOVED_PERMANENTLY19 content = b"<a href='https://example.org/'>here</a>"20 headers = {"location": "https://example.org/"}21 return httpx.Response(status_code, headers=headers, content=content)

Update to httpcore 0.10 (#1126)Tom Christiebefore adoption876e722

line 22why, one step back
22

Refactor tests to use `MockTransport(<handler_function>)` (#1281)Tom Christiebefore adoptionc2afd2d

lines 23–26why, one step back
23 elif request.url.path == "/redirect_302":
24 status_code = httpx.codes.FOUND25 headers = {"location": "https://example.org/"}26 return httpx.Response(status_code, headers=headers)

Use URLLib3 configuration options for URLLib3 transport (#970)Tom Christiebefore adoption225f1e2

line 27why, one step back
27

Refactor tests to use `MockTransport(<handler_function>)` (#1281)Tom Christiebefore adoptionc2afd2d

lines 28–31why, one step back
28 elif request.url.path == "/redirect_303":
29 status_code = httpx.codes.SEE_OTHER30 headers = {"location": "https://example.org/"}31 return httpx.Response(status_code, headers=headers)

Use URLLib3 configuration options for URLLib3 transport (#970)Tom Christiebefore adoption225f1e2

line 32why, one step back
32

Refactor tests to use `MockTransport(<handler_function>)` (#1281)Tom Christiebefore adoptionc2afd2d

lines 33–36why, one step back
33 elif request.url.path == "/relative_redirect":
34 status_code = httpx.codes.SEE_OTHER35 headers = {"location": "/"}36 return httpx.Response(status_code, headers=headers)

Use URLLib3 configuration options for URLLib3 transport (#970)Tom Christiebefore adoption225f1e2

line 37why, one step back
37

Refactor tests to use `MockTransport(<handler_function>)` (#1281)Tom Christiebefore adoptionc2afd2d

lines 38–41why, one step back
38 elif request.url.path == "/malformed_redirect":
39 status_code = httpx.codes.SEE_OTHER40 headers = {"location": "https://:443/"}41 return httpx.Response(status_code, headers=headers)

Use URLLib3 configuration options for URLLib3 transport (#970)Tom Christiebefore adoption225f1e2

line 42why, one step back
42

Refactor tests to use `MockTransport(<handler_function>)` (#1281)Tom Christiebefore adoptionc2afd2d

lines 43–46why, one step back
43 elif request.url.path == "/invalid_redirect":
44 status_code = httpx.codes.SEE_OTHER45 raw_headers = [(b"location", "https://😇/".encode("utf-8"))]46 return httpx.Response(status_code, headers=raw_headers)

Use URLLib3 configuration options for URLLib3 transport (#970)Tom Christiebefore adoption225f1e2

line 47why, one step back
47

Refactor tests to use `MockTransport(<handler_function>)` (#1281)Tom Christiebefore adoptionc2afd2d

lines 48–51why, one step back
48 elif request.url.path == "/no_scheme_redirect":
49 status_code = httpx.codes.SEE_OTHER50 headers = {"location": "//example.org/"}51 return httpx.Response(status_code, headers=headers)

Use URLLib3 configuration options for URLLib3 transport (#970)Tom Christiebefore adoption225f1e2

line 52why, one step back
52

Refactor tests to use `MockTransport(<handler_function>)` (#1281)Tom Christiebefore adoptionc2afd2d

lines 53–59why, one step back
53 elif request.url.path == "/multiple_redirects":
54 params = httpx.QueryParams(request.url.query)55 count = int(params.get("count", "0"))56 redirect_count = count - 157 status_code = httpx.codes.SEE_OTHER if count else httpx.codes.OK58 if count:59 location = "/multiple_redirects"

Use URLLib3 configuration options for URLLib3 transport (#970)Tom Christiebefore adoption225f1e2

line 60why, one step back
60 if redirect_count:

Refactor tests to use `MockTransport(<handler_function>)` (#1281)Tom Christiebefore adoptionc2afd2d

lines 61–65why, one step back
61 location += f"?count={redirect_count}"
62 headers = {"location": location}63 else:64 headers = {}65 return httpx.Response(status_code, headers=headers)

Use URLLib3 configuration options for URLLib3 transport (#970)Tom Christiebefore adoption225f1e2

line 66why, one step back
66

Refactor tests to use `MockTransport(<handler_function>)` (#1281)Tom Christiebefore adoptionc2afd2d

lines 67–70why, one step back
67 if request.url.path == "/redirect_loop":
68 status_code = httpx.codes.SEE_OTHER69 headers = {"location": "/redirect_loop"}70 return httpx.Response(status_code, headers=headers)

Use URLLib3 configuration options for URLLib3 transport (#970)Tom Christiebefore adoption225f1e2

line 71why, one step back
71

Refactor tests to use `MockTransport(<handler_function>)` (#1281)Tom Christiebefore adoptionc2afd2d

lines 72–75why, one step back
72 elif request.url.path == "/cross_domain":
73 status_code = httpx.codes.SEE_OTHER74 headers = {"location": "https://example.org/cross_domain_target"}75 return httpx.Response(status_code, headers=headers)

Use URLLib3 configuration options for URLLib3 transport (#970)Tom Christiebefore adoption225f1e2

line 76why, one step back
76

Refactor tests to use `MockTransport(<handler_function>)` (#1281)Tom Christiebefore adoptionc2afd2d

lines 77–78why, one step back
77 elif request.url.path == "/cross_domain_target":
78 status_code = httpx.codes.OK

Support Response(text=...), Response(html=...), Response(json=...) (#1297)Tom Christiebefore adoptionf3c2941

lines 79–83why, one step back
79 data = {
80 "body": request.content.decode("ascii"),81 "headers": dict(request.headers),82 }83 return httpx.Response(status_code, json=data)

Use URLLib3 configuration options for URLLib3 transport (#970)Tom Christiebefore adoption225f1e2

line 84why, one step back
84

Refactor tests to use `MockTransport(<handler_function>)` (#1281)Tom Christiebefore adoptionc2afd2d

lines 85–88why, one step back
85 elif request.url.path == "/redirect_body":
86 status_code = httpx.codes.PERMANENT_REDIRECT87 headers = {"location": "/redirect_body_target"}88 return httpx.Response(status_code, headers=headers)

Use URLLib3 configuration options for URLLib3 transport (#970)Tom Christiebefore adoption225f1e2

line 89why, one step back
89

Refactor tests to use `MockTransport(<handler_function>)` (#1281)Tom Christiebefore adoptionc2afd2d

lines 90–93why, one step back
90 elif request.url.path == "/redirect_no_body":
91 status_code = httpx.codes.SEE_OTHER92 headers = {"location": "/redirect_body_target"}93 return httpx.Response(status_code, headers=headers)

Use URLLib3 configuration options for URLLib3 transport (#970)Tom Christiebefore adoption225f1e2

line 94why, one step back
94

Refactor tests to use `MockTransport(<handler_function>)` (#1281)Tom Christiebefore adoptionc2afd2d

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

Support Response(text=...), Response(html=...), Response(json=...) (#1297)Tom Christiebefore adoptionf3c2941

lines 96–100why, one step back
96 data = {
97 "body": request.content.decode("ascii"),98 "headers": dict(request.headers),99 }100 return httpx.Response(200, json=data)

Use URLLib3 configuration options for URLLib3 transport (#970)Tom Christiebefore adoption225f1e2

line 101why, one step back
101

Refactor tests to use `MockTransport(<handler_function>)` (#1281)Tom Christiebefore adoptionc2afd2d

lines 102–107why, one step back
102 elif request.url.path == "/cross_subdomain":
103 if request.headers["Host"] != "www.example.org":104 status_code = httpx.codes.PERMANENT_REDIRECT105 headers = {"location": "https://www.example.org/cross_subdomain"}106 return httpx.Response(status_code, headers=headers)107 else:

Support Response(text=...), Response(html=...), Response(json=...) (#1297)Tom Christiebefore adoptionf3c2941

line 108why, one step back
108 return httpx.Response(200, text="Hello, world!")

Use URLLib3 configuration options for URLLib3 transport (#970)Tom Christiebefore adoption225f1e2

line 109why, one step back
109

Refactor tests to use `MockTransport(<handler_function>)` (#1281)Tom Christiebefore adoptionc2afd2d

lines 110–113why, one step back
110 elif request.url.path == "/redirect_custom_scheme":
111 status_code = httpx.codes.MOVED_PERMANENTLY112 headers = {"location": "market://details?id=42"}113 return httpx.Response(status_code, headers=headers)

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

line 114why, one step back
114

Refactor tests to use `MockTransport(<handler_function>)` (#1281)Tom Christiebefore adoptionc2afd2d

lines 115–116why, one step back
115 if request.method == "HEAD":
116 return httpx.Response(200)

Use URLLib3 configuration options for URLLib3 transport (#970)Tom Christiebefore adoption225f1e2

line 117why, one step back
117

Support Response(text=...), Response(html=...), Response(json=...) (#1297)Tom Christiebefore adoptionf3c2941

line 118why, one step back
118 return httpx.Response(200, html="<html><body>Hello, world!</body></html>")

Use URLLib3 configuration options for URLLib3 transport (#970)Tom Christiebefore adoption225f1e2

line 119why, one step back
119

Add test for HEAD redirect behavior (#1184)Florimond Mancabefore adoption09c3e90

line 120why, one step back
120

Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205

line 121why, one step back
121def test_no_redirect():

Refactor tests to use `MockTransport(<handler_function>)` (#1281)Tom Christiebefore adoptionc2afd2d

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

Use URLLib3 configuration options for URLLib3 transport (#970)Tom Christiebefore adoption225f1e2

line 123why, one step back
123 url = "https://example.com/no_redirect"

Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205

line 124why, one step back
124 response = client.get(url)

Use URLLib3 configuration options for URLLib3 transport (#970)Tom Christiebefore adoption225f1e2

line 125why, one step back
125 assert response.status_code == 200

Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205

lines 126–127why, one step back
126 with pytest.raises(httpx.NotRedirectResponse):
127 response.next()

Use URLLib3 configuration options for URLLib3 transport (#970)Tom Christiebefore adoption225f1e2

lines 128–129why, one step back
128
129

Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205

line 130why, one step back
130def test_redirect_301():

Refactor tests to use `MockTransport(<handler_function>)` (#1281)Tom Christiebefore adoptionc2afd2d

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

Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205

lines 132–133why, one step back
132 response = client.post("https://example.org/redirect_301")
133 assert response.status_code == httpx.codes.OK

Replace httpx.URL for str in tests (#1237)Eduardo Enriquezbefore adoptionaad8209

line 134why, one step back
134 assert response.url == "https://example.org/"

Use URLLib3 configuration options for URLLib3 transport (#970)Tom Christiebefore adoption225f1e2

lines 135–137why, one step back
135 assert len(response.history) == 1
136137

Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205

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

Refactor tests to use `MockTransport(<handler_function>)` (#1281)Tom Christiebefore adoptionc2afd2d

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

Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205

lines 140–141why, one step back
140 response = client.post("https://example.org/redirect_302")
141 assert response.status_code == httpx.codes.OK

Replace httpx.URL for str in tests (#1237)Eduardo Enriquezbefore adoptionaad8209

line 142why, one step back
142 assert response.url == "https://example.org/"

Use URLLib3 configuration options for URLLib3 transport (#970)Tom Christiebefore adoption225f1e2

lines 143–145why, one step back
143 assert len(response.history) == 1
144145

Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205

line 146why, one step back
146def test_redirect_303():

Refactor tests to use `MockTransport(<handler_function>)` (#1281)Tom Christiebefore adoptionc2afd2d

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

Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205

lines 148–149why, one step back
148 response = client.get("https://example.org/redirect_303")
149 assert response.status_code == httpx.codes.OK

Replace httpx.URL for str in tests (#1237)Eduardo Enriquezbefore adoptionaad8209

line 150why, one step back
150 assert response.url == "https://example.org/"

Use URLLib3 configuration options for URLLib3 transport (#970)Tom Christiebefore adoption225f1e2

lines 151–153why, one step back
151 assert len(response.history) == 1
152153

Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205

line 154why, one step back
154def test_disallow_redirects():

Refactor tests to use `MockTransport(<handler_function>)` (#1281)Tom Christiebefore adoptionc2afd2d

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

Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205

lines 156–157why, one step back
156 response = client.post("https://example.org/redirect_303", allow_redirects=False)
157 assert response.status_code == httpx.codes.SEE_OTHER

Replace httpx.URL for str in tests (#1237)Eduardo Enriquezbefore adoptionaad8209

line 158why, one step back
158 assert response.url == "https://example.org/redirect_303"

Use URLLib3 configuration options for URLLib3 transport (#970)Tom Christiebefore adoption225f1e2

lines 159–161why, one step back
159 assert response.is_redirect is True
160 assert len(response.history) == 0161

Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205

lines 162–163why, one step back
162 response = response.next()
163 assert response.status_code == httpx.codes.OK

Replace httpx.URL for str in tests (#1237)Eduardo Enriquezbefore adoptionaad8209

line 164why, one step back
164 assert response.url == "https://example.org/"

Use URLLib3 configuration options for URLLib3 transport (#970)Tom Christiebefore adoption225f1e2

lines 165–166why, one step back
165 assert response.is_redirect is False
166 assert len(response.history) == 1

Add test for HEAD redirect behavior (#1184)Florimond Mancabefore adoption09c3e90

lines 167–168why, one step back
167
168

Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205

line 169why, one step back
169def test_head_redirect():

Add test for HEAD redirect behavior (#1184)Florimond Mancabefore adoption09c3e90

lines 170–172why, one step back
170 """
171 Contrary to Requests, redirects remain enabled by default for HEAD requests.172 """

Refactor tests to use `MockTransport(<handler_function>)` (#1281)Tom Christiebefore adoptionc2afd2d

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

Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205

lines 174–175why, one step back
174 response = client.head("https://example.org/redirect_302")
175 assert response.status_code == httpx.codes.OK

Replace httpx.URL for str in tests (#1237)Eduardo Enriquezbefore adoptionaad8209

line 176why, one step back
176 assert response.url == "https://example.org/"

Add test for HEAD redirect behavior (#1184)Florimond Mancabefore adoption09c3e90

lines 177–179why, one step back
177 assert response.request.method == "HEAD"
178 assert len(response.history) == 1179 assert response.text == ""

Use URLLib3 configuration options for URLLib3 transport (#970)Tom Christiebefore adoption225f1e2

lines 180–181why, one step back
180
181

Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205

line 182why, one step back
182def test_relative_redirect():

Refactor tests to use `MockTransport(<handler_function>)` (#1281)Tom Christiebefore adoptionc2afd2d

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

Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205

lines 184–185why, one step back
184 response = client.get("https://example.org/relative_redirect")
185 assert response.status_code == httpx.codes.OK

Replace httpx.URL for str in tests (#1237)Eduardo Enriquezbefore adoptionaad8209

line 186why, one step back
186 assert response.url == "https://example.org/"

Use URLLib3 configuration options for URLLib3 transport (#970)Tom Christiebefore adoption225f1e2

lines 187–189why, one step back
187 assert len(response.history) == 1
188189

Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205

line 190why, one step back
190def test_malformed_redirect():

Use URLLib3 configuration options for URLLib3 transport (#970)Tom Christiebefore adoption225f1e2

line 191why, one step back
191 # https://github.com/encode/httpx/issues/771

Refactor tests to use `MockTransport(<handler_function>)` (#1281)Tom Christiebefore adoptionc2afd2d

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

Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205

lines 193–194why, one step back
193 response = client.get("http://example.org/malformed_redirect")
194 assert response.status_code == httpx.codes.OK

Replace httpx.URL for str in tests (#1237)Eduardo Enriquezbefore adoptionaad8209

line 195why, one step back
195 assert response.url == "https://example.org:443/"

Use URLLib3 configuration options for URLLib3 transport (#970)Tom Christiebefore adoption225f1e2

line 196why, one step back
196 assert len(response.history) == 1

Map rfc3986 exceptions (#1163)Joebefore adoption45de714

lines 197–198why, one step back
197
198

Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205

line 199why, one step back
199def test_invalid_redirect():

Refactor tests to use `MockTransport(<handler_function>)` (#1281)Tom Christiebefore adoptionc2afd2d

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

Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205

lines 201–202why, one step back
201 with pytest.raises(httpx.RemoteProtocolError):
202 client.get("http://example.org/invalid_redirect")

Use URLLib3 configuration options for URLLib3 transport (#970)Tom Christiebefore adoption225f1e2

lines 203–204why, one step back
203
204

Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205

line 205why, one step back
205def test_no_scheme_redirect():

Refactor tests to use `MockTransport(<handler_function>)` (#1281)Tom Christiebefore adoptionc2afd2d

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

Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205

lines 207–208why, one step back
207 response = client.get("https://example.org/no_scheme_redirect")
208 assert response.status_code == httpx.codes.OK

Replace httpx.URL for str in tests (#1237)Eduardo Enriquezbefore adoptionaad8209

line 209why, one step back
209 assert response.url == "https://example.org/"

Use URLLib3 configuration options for URLLib3 transport (#970)Tom Christiebefore adoption225f1e2

lines 210–212why, one step back
210 assert len(response.history) == 1
211212

Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205

line 213why, one step back
213def test_fragment_redirect():

Refactor tests to use `MockTransport(<handler_function>)` (#1281)Tom Christiebefore adoptionc2afd2d

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

Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205

lines 215–216why, one step back
215 response = client.get("https://example.org/relative_redirect#fragment")
216 assert response.status_code == httpx.codes.OK

Replace httpx.URL for str in tests (#1237)Eduardo Enriquezbefore adoptionaad8209

line 217why, one step back
217 assert response.url == "https://example.org/#fragment"

Use URLLib3 configuration options for URLLib3 transport (#970)Tom Christiebefore adoption225f1e2

lines 218–220why, one step back
218 assert len(response.history) == 1
219220

Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205

line 221why, one step back
221def test_multiple_redirects():

Refactor tests to use `MockTransport(<handler_function>)` (#1281)Tom Christiebefore adoptionc2afd2d

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

Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205

lines 223–224why, one step back
223 response = client.get("https://example.org/multiple_redirects?count=20")
224 assert response.status_code == httpx.codes.OK

Replace httpx.URL for str in tests (#1237)Eduardo Enriquezbefore adoptionaad8209

line 225why, one step back
225 assert response.url == "https://example.org/multiple_redirects"

Use URLLib3 configuration options for URLLib3 transport (#970)Tom Christiebefore adoption225f1e2

line 226why, one step back
226 assert len(response.history) == 20

Replace httpx.URL for str in tests (#1237)Eduardo Enriquezbefore adoptionaad8209

lines 227–228why, one step back
227 assert response.history[0].url == "https://example.org/multiple_redirects?count=20"
228 assert response.history[1].url == "https://example.org/multiple_redirects?count=19"

Use URLLib3 configuration options for URLLib3 transport (#970)Tom Christiebefore adoption225f1e2

lines 229–233why, one step back
229 assert len(response.history[0].history) == 0
230 assert len(response.history[1].history) == 1231232233@pytest.mark.usefixtures("async_environment")

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

line 234why, one step back
234async def test_async_too_many_redirects():

Version 0.15.0 (#1301)Tom Christiebefore adoptionf932af9

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

Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205

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

Use URLLib3 configuration options for URLLib3 transport (#970)Tom Christiebefore adoption225f1e2

lines 238–240why, one step back
238
239240@pytest.mark.usefixtures("async_environment")

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

line 241why, one step back
241async def test_async_too_many_redirects_calling_next():

Version 0.15.0 (#1301)Tom Christiebefore adoptionf932af9

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

Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205

lines 243–247why, one step back
243 url = "https://example.org/multiple_redirects?count=21"
244 response = await client.get(url, allow_redirects=False)245 with pytest.raises(httpx.TooManyRedirects):246 while response.is_redirect:247 response = await response.anext()

Use URLLib3 configuration options for URLLib3 transport (#970)Tom Christiebefore adoption225f1e2

lines 248–249why, one step back
248
249

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

line 250why, one step back
250def test_sync_too_many_redirects():

Refactor tests to use `MockTransport(<handler_function>)` (#1281)Tom Christiebefore adoptionc2afd2d

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

Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205

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

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

lines 253–256why, one step back
253 client.get("https://example.org/multiple_redirects?count=21")
254255256def test_sync_too_many_redirects_calling_next():

Refactor tests to use `MockTransport(<handler_function>)` (#1281)Tom Christiebefore adoptionc2afd2d

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

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

lines 258–259why, one step back
258 url = "https://example.org/multiple_redirects?count=21"
259 response = client.get(url, allow_redirects=False)

Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205

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

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

line 261why, one step back
261 while response.is_redirect:

Add missing `Response.next()` (#1055)Florimond Mancabefore adoption3230cb3

line 262why, one step back
262 response = response.next()

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

lines 263–264why, one step back
263
264

Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205

line 265why, one step back
265def test_redirect_loop():

Refactor tests to use `MockTransport(<handler_function>)` (#1281)Tom Christiebefore adoptionc2afd2d

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

Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205

lines 267–268why, one step back
267 with pytest.raises(httpx.TooManyRedirects):
268 client.get("https://example.org/redirect_loop")

Use URLLib3 configuration options for URLLib3 transport (#970)Tom Christiebefore adoption225f1e2

lines 269–270why, one step back
269
270

Swap auth/redirects ordering (#1267)Tom Christiebefore adoption4d950e5

line 271why, one step back
271def test_cross_domain_redirect_with_auth_header():

Refactor tests to use `MockTransport(<handler_function>)` (#1281)Tom Christiebefore adoptionc2afd2d

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

Use URLLib3 configuration options for URLLib3 transport (#970)Tom Christiebefore adoption225f1e2

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

Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205

line 275why, one step back
275 response = client.get(url, headers=headers)

Swap auth/redirects ordering (#1267)Tom Christiebefore adoption4d950e5

lines 276–280why, one step back
276 assert response.url == "https://example.org/cross_domain_target"
277 assert "authorization" not in response.json()["headers"]278279280def test_cross_domain_redirect_with_auth():

Refactor tests to use `MockTransport(<handler_function>)` (#1281)Tom Christiebefore adoptionc2afd2d

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

Swap auth/redirects ordering (#1267)Tom Christiebefore adoption4d950e5

lines 282–283why, one step back
282 url = "https://example.com/cross_domain"
283 response = client.get(url, auth=("user", "pass"))

Replace httpx.URL for str in tests (#1237)Eduardo Enriquezbefore adoptionaad8209

line 284why, one step back
284 assert response.url == "https://example.org/cross_domain_target"

Use URLLib3 configuration options for URLLib3 transport (#970)Tom Christiebefore adoption225f1e2

lines 285–287why, one step back
285 assert "authorization" not in response.json()["headers"]
286287

Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205

line 288why, one step back
288def test_same_domain_redirect():

Refactor tests to use `MockTransport(<handler_function>)` (#1281)Tom Christiebefore adoptionc2afd2d

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

Use URLLib3 configuration options for URLLib3 transport (#970)Tom Christiebefore adoption225f1e2

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

Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205

line 292why, one step back
292 response = client.get(url, headers=headers)

Replace httpx.URL for str in tests (#1237)Eduardo Enriquezbefore adoptionaad8209

line 293why, one step back
293 assert response.url == "https://example.org/cross_domain_target"

Use URLLib3 configuration options for URLLib3 transport (#970)Tom Christiebefore adoption225f1e2

lines 294–296why, one step back
294 assert response.json()["headers"]["authorization"] == "abc"
295296

Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205

line 297why, one step back
297def test_body_redirect():

Use URLLib3 configuration options for URLLib3 transport (#970)Tom Christiebefore adoption225f1e2

lines 298–300why, one step back
298 """
299 A 308 redirect should preserve the request body.300 """

Refactor tests to use `MockTransport(<handler_function>)` (#1281)Tom Christiebefore adoptionc2afd2d

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

Use URLLib3 configuration options for URLLib3 transport (#970)Tom Christiebefore adoption225f1e2

line 302why, one step back
302 url = "https://example.org/redirect_body"

Seperate `content=...` and `data=...` parameters (#1266)Tom Christiebefore adoptionfeb404f

lines 303–304why, one step back
303 content = b"Example request body"
304 response = client.post(url, content=content)

Replace httpx.URL for str in tests (#1237)Eduardo Enriquezbefore adoptionaad8209

line 305why, one step back
305 assert response.url == "https://example.org/redirect_body_target"

Use URLLib3 configuration options for URLLib3 transport (#970)Tom Christiebefore adoption225f1e2

lines 306–309why, one step back
306 assert response.json()["body"] == "Example request body"
307 assert "content-length" in response.json()["headers"]308309

Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205

line 310why, one step back
310def test_no_body_redirect():

Use URLLib3 configuration options for URLLib3 transport (#970)Tom Christiebefore adoption225f1e2

lines 311–313why, one step back
311 """
312 A 303 redirect should remove the request body.313 """

Refactor tests to use `MockTransport(<handler_function>)` (#1281)Tom Christiebefore adoptionc2afd2d

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

Use URLLib3 configuration options for URLLib3 transport (#970)Tom Christiebefore adoption225f1e2

line 315why, one step back
315 url = "https://example.org/redirect_no_body"

Seperate `content=...` and `data=...` parameters (#1266)Tom Christiebefore adoptionfeb404f

lines 316–317why, one step back
316 content = b"Example request body"
317 response = client.post(url, content=content)

Replace httpx.URL for str in tests (#1237)Eduardo Enriquezbefore adoptionaad8209

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

Use URLLib3 configuration options for URLLib3 transport (#970)Tom Christiebefore adoption225f1e2

lines 319–322why, one step back
319 assert response.json()["body"] == ""
320 assert "content-length" not in response.json()["headers"]321322

Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205

line 323why, one step back
323def test_can_stream_if_no_redirect():

Refactor tests to use `MockTransport(<handler_function>)` (#1281)Tom Christiebefore adoptionc2afd2d

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

Use URLLib3 configuration options for URLLib3 transport (#970)Tom Christiebefore adoption225f1e2

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

Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205

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

Use URLLib3 configuration options for URLLib3 transport (#970)Tom Christiebefore adoption225f1e2

line 327why, one step back
327 assert not response.is_closed

Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205

line 328why, one step back
328 assert response.status_code == httpx.codes.MOVED_PERMANENTLY

Use URLLib3 configuration options for URLLib3 transport (#970)Tom Christiebefore adoption225f1e2

lines 329–331why, one step back
329 assert response.headers["location"] == "https://example.org/"
330331

Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205

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

Refactor tests to use `MockTransport(<handler_function>)` (#1281)Tom Christiebefore adoptionc2afd2d

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

Use URLLib3 configuration options for URLLib3 transport (#970)Tom Christiebefore adoption225f1e2

lines 334–335why, one step back
334 url = "https://example.org/redirect_body"
335

Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205

line 336why, one step back
336 def streaming_body():

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

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

Use URLLib3 configuration options for URLLib3 transport (#970)Tom Christiebefore adoption225f1e2

line 338why, one step back
338

Drop ContentStream (#1295)Tom Christiebefore adoptionfbb21fb

line 339why, one step back
339 with pytest.raises(httpx.StreamConsumed):

Seperate `content=...` and `data=...` parameters (#1266)Tom Christiebefore adoptionfeb404f

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

Use URLLib3 configuration options for URLLib3 transport (#970)Tom Christiebefore adoption225f1e2

lines 341–342why, one step back
341
342

Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205

line 343why, one step back
343def test_cross_subdomain_redirect():

Refactor tests to use `MockTransport(<handler_function>)` (#1281)Tom Christiebefore adoptionc2afd2d

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

Use URLLib3 configuration options for URLLib3 transport (#970)Tom Christiebefore adoption225f1e2

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

Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205

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

Replace httpx.URL for str in tests (#1237)Eduardo Enriquezbefore adoptionaad8209

line 347why, one step back
347 assert response.url == "https://www.example.org/cross_subdomain"

Use URLLib3 configuration options for URLLib3 transport (#970)Tom Christiebefore adoption225f1e2

lines 348–349why, one step back
348
349

Refactor tests to use `MockTransport(<handler_function>)` (#1281)Tom Christiebefore adoptionc2afd2d

lines 350–357why, one step back
350def 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)

Use URLLib3 configuration options for URLLib3 transport (#970)Tom Christiebefore adoption225f1e2

line 358why, one step back
358

Refactor tests to use `MockTransport(<handler_function>)` (#1281)Tom Christiebefore adoptionc2afd2d

lines 359–368why, one step back
359 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)

Use URLLib3 configuration options for URLLib3 transport (#970)Tom Christiebefore adoption225f1e2

line 369why, one step back
369

Refactor tests to use `MockTransport(<handler_function>)` (#1281)Tom Christiebefore adoptionc2afd2d

lines 370–380why, one step back
370 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)

Use URLLib3 configuration options for URLLib3 transport (#970)Tom Christiebefore adoption225f1e2

lines 381–382why, one step back
381
382

Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205

line 383why, one step back
383def test_redirect_cookie_behavior():

Refactor tests to use `MockTransport(<handler_function>)` (#1281)Tom Christiebefore adoptionc2afd2d

line 384why, one step back
384 client = httpx.Client(transport=MockTransport(cookie_sessions))

Use URLLib3 configuration options for URLLib3 transport (#970)Tom Christiebefore adoption225f1e2

lines 385–386why, one step back
385
386 # The client is not logged in.

Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205

line 387why, one step back
387 response = client.get("https://example.com/")

Use URLLib3 configuration options for URLLib3 transport (#970)Tom Christiebefore adoption225f1e2

lines 388–391why, one step back
388 assert response.url == "https://example.com/"
389 assert response.text == "Not logged in"390391 # Login redirects to the homepage, setting a session cookie.

Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205

line 392why, one step back
392 response = client.post("https://example.com/login")

Use URLLib3 configuration options for URLLib3 transport (#970)Tom Christiebefore adoption225f1e2

lines 393–396why, one step back
393 assert response.url == "https://example.com/"
394 assert response.text == "Logged in"395396 # The client is logged in.

Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205

line 397why, one step back
397 response = client.get("https://example.com/")

Use URLLib3 configuration options for URLLib3 transport (#970)Tom Christiebefore adoption225f1e2

lines 398–401why, one step back
398 assert response.url == "https://example.com/"
399 assert response.text == "Logged in"400401 # Logout redirects to the homepage, expiring the session cookie.

Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205

line 402why, one step back
402 response = client.post("https://example.com/logout")

Use URLLib3 configuration options for URLLib3 transport (#970)Tom Christiebefore adoption225f1e2

lines 403–406why, one step back
403 assert response.url == "https://example.com/"
404 assert response.text == "Not logged in"405406 # The client is not logged in.

Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205

line 407why, one step back
407 response = client.get("https://example.com/")

Use URLLib3 configuration options for URLLib3 transport (#970)Tom Christiebefore adoption225f1e2

lines 408–409why, one step back
408 assert response.url == "https://example.com/"
409 assert response.text == "Not logged in"

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

lines 410–411why, one step back
410
411

Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205

line 412why, one step back
412def test_redirect_custom_scheme():

Refactor tests to use `MockTransport(<handler_function>)` (#1281)Tom Christiebefore adoptionc2afd2d

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

Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205

lines 414–415why, one step back
414 with pytest.raises(httpx.UnsupportedProtocol) as e:
415 client.post("https://example.org/redirect_custom_scheme")

Refactor tests to use `MockTransport(<handler_function>)` (#1281)Tom Christiebefore adoptionc2afd2d

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