Skip to content
Myr

test_redirects.py

This file

Path

tests/client/test_redirects.py

292 lines · 11.1 KiB · Python

Last changed by

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

History of this file

Take it

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

Update README.mdTom Christiebefore adoption53a3e55

lines 1–5why, one step back
1import json
2from urllib.parse import parse_qs34import pytest5

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

line 6why, one step back
6from httpx import (

Update README.mdTom Christiebefore adoption53a3e55

lines 7–8why, one step back
7 URL,
8 AsyncClient,

Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7

lines 9–11why, one step back
9 AsyncDispatcher,
10 AsyncRequest,11 AsyncResponse,

Update README.mdTom Christiebefore adoption53a3e55

line 12why, one step back
12 CertTypes,

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

line 13why, one step back
13 NotRedirectResponse,

Update README.mdTom Christiebefore adoption53a3e55

lines 14–22why, one step back
14 RedirectBodyUnavailable,
15 RedirectLoop,16 TimeoutTypes,17 TooManyRedirects,18 VerifyTypes,19 codes,20)2122

Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7

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

Update README.mdTom Christiebefore adoption53a3e55

lines 24–25why, one step back
24 async def send(
25 self,

Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7

line 26why, one step back
26 request: AsyncRequest,

Update README.mdTom Christiebefore adoption53a3e55

lines 27–29why, one step back
27 verify: VerifyTypes = None,
28 cert: CertTypes = None,29 timeout: TimeoutTypes = None,

Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7

line 30why, one step back
30 ) -> AsyncResponse:

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

lines 31–34why, one step back
31 if request.url.path == "/no_redirect":
32 return AsyncResponse(codes.OK, request=request)3334 elif request.url.path == "/redirect_301":

Update README.mdTom Christiebefore adoption53a3e55

lines 35–36why, one step back
35 status_code = codes.MOVED_PERMANENTLY
36 headers = {"location": "https://example.org/"}

Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7

line 37why, one step back
37 return AsyncResponse(status_code, headers=headers, request=request)

Update README.mdTom Christiebefore adoption53a3e55

lines 38–41why, one step back
38
39 elif request.url.path == "/redirect_302":40 status_code = codes.FOUND41 headers = {"location": "https://example.org/"}

Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7

line 42why, one step back
42 return AsyncResponse(status_code, headers=headers, request=request)

Update README.mdTom Christiebefore adoption53a3e55

lines 43–46why, one step back
43
44 elif request.url.path == "/redirect_303":45 status_code = codes.SEE_OTHER46 headers = {"location": "https://example.org/"}

Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7

line 47why, one step back
47 return AsyncResponse(status_code, headers=headers, request=request)

Update README.mdTom Christiebefore adoption53a3e55

lines 48–50why, one step back
48
49 elif request.url.path == "/relative_redirect":50 headers = {"location": "/"}

Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7

line 51why, one step back
51 return AsyncResponse(codes.SEE_OTHER, headers=headers, request=request)

Update README.mdTom Christiebefore adoption53a3e55

lines 52–54why, one step back
52
53 elif request.url.path == "/no_scheme_redirect":54 headers = {"location": "//example.org/"}

Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7

line 55why, one step back
55 return AsyncResponse(codes.SEE_OTHER, headers=headers, request=request)

Update README.mdTom Christiebefore adoption53a3e55

lines 56–65why, one step back
56
57 elif request.url.path == "/multiple_redirects":58 params = parse_qs(request.url.query)59 count = int(params.get("count", "0")[0])60 redirect_count = count - 161 code = codes.SEE_OTHER if count else codes.OK62 location = "/multiple_redirects"63 if redirect_count:64 location += "?count=" + str(redirect_count)65 headers = {"location": location} if count else {}

Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7

line 66why, one step back
66 return AsyncResponse(code, headers=headers, request=request)

Update README.mdTom Christiebefore adoption53a3e55

lines 67–69why, one step back
67
68 if request.url.path == "/redirect_loop":69 headers = {"location": "/redirect_loop"}

Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7

line 70why, one step back
70 return AsyncResponse(codes.SEE_OTHER, headers=headers, request=request)

Update README.mdTom Christiebefore adoption53a3e55

lines 71–73why, one step back
71
72 elif request.url.path == "/cross_domain":73 headers = {"location": "https://example.org/cross_domain_target"}

Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7

line 74why, one step back
74 return AsyncResponse(codes.SEE_OTHER, headers=headers, request=request)

Update README.mdTom Christiebefore adoption53a3e55

lines 75–78why, one step back
75
76 elif request.url.path == "/cross_domain_target":77 headers = dict(request.headers.items())78 content = json.dumps({"headers": headers}).encode()

Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7

line 79why, one step back
79 return AsyncResponse(codes.OK, content=content, request=request)

Update README.mdTom Christiebefore adoption53a3e55

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

Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7

lines 84–86why, one step back
84 return AsyncResponse(
85 codes.PERMANENT_REDIRECT, headers=headers, request=request86 )

Update README.mdTom Christiebefore adoption53a3e55

line 87why, one step back
87

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

lines 88–92why, one step back
88 elif request.url.path == "/redirect_no_body":
89 await request.read()90 headers = {"location": "/redirect_body_target"}91 return AsyncResponse(codes.SEE_OTHER, headers=headers, request=request)92

Update README.mdTom Christiebefore adoption53a3e55

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

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

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

Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7

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

Update README.mdTom Christiebefore adoption53a3e55

line 98why, one step back
98

Remove "Host" header on redirects if URL origin has changed (#199)Yeray Diaz Diazbefore adoption95dad24

lines 99–109why, one step back
99 elif request.url.path == "/cross_subdomain":
100 if request.headers["host"] != "www.example.org":101 headers = {"location": "https://www.example.org/cross_subdomain"}102 return AsyncResponse(103 codes.PERMANENT_REDIRECT, headers=headers, request=request104 )105 else:106 return AsyncResponse(107 codes.OK, content=b"Hello, world!", request=request108 )109

Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7

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

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

lines 111–119why, one step back
111
112113async def test_no_redirect(backend):114 client = AsyncClient(dispatch=MockDispatch(), backend=backend)115 url = "https://example.com/no_redirect"116 response = await client.get(url)117 assert response.status_code == 200118 with pytest.raises(NotRedirectResponse):119 await response.next()

Update README.mdTom Christiebefore adoption53a3e55

lines 120–121why, one step back
120
121

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

lines 122–123why, one step back
122async def test_redirect_301(backend):
123 client = AsyncClient(dispatch=MockDispatch(), backend=backend)

Update README.mdTom Christiebefore adoption53a3e55

lines 124–129why, one step back
124 response = await client.post("https://example.org/redirect_301")
125 assert response.status_code == codes.OK126 assert response.url == URL("https://example.org/")127 assert len(response.history) == 1128129

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

lines 130–131why, one step back
130async def test_redirect_302(backend):
131 client = AsyncClient(dispatch=MockDispatch(), backend=backend)

Update README.mdTom Christiebefore adoption53a3e55

lines 132–137why, one step back
132 response = await client.post("https://example.org/redirect_302")
133 assert response.status_code == codes.OK134 assert response.url == URL("https://example.org/")135 assert len(response.history) == 1136137

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

lines 138–139why, one step back
138async def test_redirect_303(backend):
139 client = AsyncClient(dispatch=MockDispatch(), backend=backend)

Update README.mdTom Christiebefore adoption53a3e55

lines 140–145why, one step back
140 response = await client.get("https://example.org/redirect_303")
141 assert response.status_code == codes.OK142 assert response.url == URL("https://example.org/")143 assert len(response.history) == 1144145

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

lines 146–147why, one step back
146async def test_disallow_redirects(backend):
147 client = AsyncClient(dispatch=MockDispatch(), backend=backend)

Update README.mdTom Christiebefore adoption53a3e55

lines 148–152why, one step back
148 response = await client.post(
149 "https://example.org/redirect_303", allow_redirects=False150 )151 assert response.status_code == codes.SEE_OTHER152 assert response.url == URL("https://example.org/redirect_303")

Explicitly check is_redirect in simple test case (#208)Tammo Behrendsbefore adoptionbebedcb

line 153why, one step back
153 assert response.is_redirect is True

Update README.mdTom Christiebefore adoption53a3e55

lines 154–158why, one step back
154 assert len(response.history) == 0
155156 response = await response.next()157 assert response.status_code == codes.OK158 assert response.url == URL("https://example.org/")

Explicitly check is_redirect in simple test case (#208)Tammo Behrendsbefore adoptionbebedcb

line 159why, one step back
159 assert response.is_redirect is False

Update README.mdTom Christiebefore adoption53a3e55

lines 160–162why, one step back
160 assert len(response.history) == 1
161162

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

lines 163–164why, one step back
163async def test_relative_redirect(backend):
164 client = AsyncClient(dispatch=MockDispatch(), backend=backend)

Update README.mdTom Christiebefore adoption53a3e55

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

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

lines 171–172why, one step back
171async def test_no_scheme_redirect(backend):
172 client = AsyncClient(dispatch=MockDispatch(), backend=backend)

Update README.mdTom Christiebefore adoption53a3e55

lines 173–178why, one step back
173 response = await client.get("https://example.org/no_scheme_redirect")
174 assert response.status_code == codes.OK175 assert response.url == URL("https://example.org/")176 assert len(response.history) == 1177178

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

lines 179–180why, one step back
179async def test_fragment_redirect(backend):
180 client = AsyncClient(dispatch=MockDispatch(), backend=backend)

Update README.mdTom Christiebefore adoption53a3e55

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

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

lines 187–188why, one step back
187async def test_multiple_redirects(backend):
188 client = AsyncClient(dispatch=MockDispatch(), backend=backend)

Update README.mdTom Christiebefore adoption53a3e55

lines 189–192why, one step back
189 response = await client.get("https://example.org/multiple_redirects?count=20")
190 assert response.status_code == codes.OK191 assert response.url == URL("https://example.org/multiple_redirects")192 assert len(response.history) == 20

Multipart support (#90)Tom Christiebefore adoption6a44b05

lines 193–198why, one step back
193 assert response.history[0].url == URL(
194 "https://example.org/multiple_redirects?count=20"195 )196 assert response.history[1].url == URL(197 "https://example.org/multiple_redirects?count=19"198 )

Tweaks (#89)Tom Christiebefore adoption7c6fb5c

lines 199–200why, one step back
199 assert len(response.history[0].history) == 0
200 assert len(response.history[1].history) == 1

Update README.mdTom Christiebefore adoption53a3e55

lines 201–202why, one step back
201
202

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

lines 203–204why, one step back
203async def test_too_many_redirects(backend):
204 client = AsyncClient(dispatch=MockDispatch(), backend=backend)

Update README.mdTom Christiebefore adoption53a3e55

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

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

lines 209–210why, one step back
209async def test_too_many_redirects_calling_next(backend):
210 client = AsyncClient(dispatch=MockDispatch(), backend=backend)

Update README.mdTom Christiebefore adoption53a3e55

lines 211–217why, one step back
211 url = "https://example.org/multiple_redirects?count=21"
212 response = await client.get(url, allow_redirects=False)213 with pytest.raises(TooManyRedirects):214 while response.is_redirect:215 response = await response.next()216217

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

lines 218–219why, one step back
218async def test_redirect_loop(backend):
219 client = AsyncClient(dispatch=MockDispatch(), backend=backend)

Update README.mdTom Christiebefore adoption53a3e55

lines 220–223why, one step back
220 with pytest.raises(RedirectLoop):
221 await client.get("https://example.org/redirect_loop")222223

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

lines 224–225why, one step back
224async def test_redirect_loop_calling_next(backend):
225 client = AsyncClient(dispatch=MockDispatch(), backend=backend)

Update README.mdTom Christiebefore adoption53a3e55

lines 226–232why, one step back
226 url = "https://example.org/redirect_loop"
227 response = await client.get(url, allow_redirects=False)228 with pytest.raises(RedirectLoop):229 while response.is_redirect:230 response = await response.next()231232

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

lines 233–234why, one step back
233async def test_cross_domain_redirect(backend):
234 client = AsyncClient(dispatch=MockDispatch(), backend=backend)

Update README.mdTom Christiebefore adoption53a3e55

lines 235–241why, one step back
235 url = "https://example.com/cross_domain"
236 headers = {"Authorization": "abc"}237 response = await client.get(url, headers=headers)238 assert response.url == URL("https://example.org/cross_domain_target")239 assert "authorization" not in response.json()["headers"]240241

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

lines 242–243why, one step back
242async def test_same_domain_redirect(backend):
243 client = AsyncClient(dispatch=MockDispatch(), backend=backend)

Update README.mdTom Christiebefore adoption53a3e55

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

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

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

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

lines 252–254why, one step back
252 """
253 A 308 redirect should preserve the request body.254 """

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

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

Update README.mdTom Christiebefore adoption53a3e55

lines 256–259why, one step back
256 url = "https://example.org/redirect_body"
257 data = b"Example request body"258 response = await client.post(url, data=data)259 assert response.url == URL("https://example.org/redirect_body_target")

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

lines 260–274why, one step back
260 assert response.json()["body"] == "Example request body"
261 assert "content-length" in response.json()["headers"]262263264async def test_no_body_redirect(backend):265 """266 A 303 redirect should remove the request body.267 """268 client = AsyncClient(dispatch=MockDispatch(), backend=backend)269 url = "https://example.org/redirect_no_body"270 data = b"Example request body"271 response = await client.post(url, data=data)272 assert response.url == URL("https://example.org/redirect_body_target")273 assert response.json()["body"] == ""274 assert "content-length" not in response.json()["headers"]

Update README.mdTom Christiebefore adoption53a3e55

lines 275–276why, one step back
275
276

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

lines 277–278why, one step back
277async def test_cannot_redirect_streaming_body(backend):
278 client = AsyncClient(dispatch=MockDispatch(), backend=backend)

Update README.mdTom Christiebefore adoption53a3e55

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

Remove "Host" header on redirects if URL origin has changed (#199)Yeray Diaz Diazbefore adoption95dad24

lines 286–287why, one step back
286
287

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

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

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

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

Remove "Host" header on redirects if URL origin has changed (#199)Yeray Diaz Diazbefore adoption95dad24

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