test_redirects.py
This file
Path
tests/client/test_redirects.py
353 lines · 12.9 KiB · Python
Last changed by
Use Streams API for both requests and responses. (#648)Tom Christiebefore adoptioncee1fcc
History of this fileTake it
The walk stopped after 200 commits: lines older than 1f4b8dd point at it.
Allow per file running of tests (#287)Jt Miclatbefore adoption1f4b8dd
lines 1–7why, one step back1import json2from urllib.parse import parse_qs34import pytest56from httpx import (7 URL,Drop sync (#544)Tom Christiebefore adoption206c537
line 8why, one step back8 Client,Tighten up response.next() (#297)Florimond Mancabefore adoptiona464627
line 9why, one step back9 NotRedirectResponse,Allow per file running of tests (#287)Jt Miclatbefore adoption1f4b8dd
lines 10–11why, one step back10 RedirectBodyUnavailable,11 RedirectLoop,Drop sync (#544)Tom Christiebefore adoption206c537
lines 12–13why, one step back12 Request,13 Response,Allow per file running of tests (#287)Jt Miclatbefore adoption1f4b8dd
lines 14–16why, one step back14 TooManyRedirects,15 codes,16)Tighten up top-level API to only expose public API (#608)Tom Christiebefore adoptiond15dc0b
lines 17–18why, one step back17from httpx.config import CertTypes, TimeoutTypes, VerifyTypes18from httpx.dispatch.base import DispatcherAllow per file running of tests (#287)Jt Miclatbefore adoption1f4b8dd
lines 19–20why, one step back1920Drop sync (#544)Tom Christiebefore adoption206c537
line 21why, one step back21class MockDispatch(Dispatcher):Allow per file running of tests (#287)Jt Miclatbefore adoption1f4b8dd
lines 22–23why, one step back22 async def send(23 self,Drop sync (#544)Tom Christiebefore adoption206c537
line 24why, one step back24 request: Request,Allow per file running of tests (#287)Jt Miclatbefore adoption1f4b8dd
lines 25–27why, one step back25 verify: VerifyTypes = None,26 cert: CertTypes = None,27 timeout: TimeoutTypes = None,Drop sync (#544)Tom Christiebefore adoption206c537
line 28why, one step back28 ) -> Response:Tighten up response.next() (#297)Florimond Mancabefore adoptiona464627
line 29why, one step back29 if request.url.path == "/no_redirect":Drop sync (#544)Tom Christiebefore adoption206c537
line 30why, one step back30 return Response(codes.OK, request=request)Tighten up response.next() (#297)Florimond Mancabefore adoptiona464627
lines 31–32why, one step back3132 elif request.url.path == "/redirect_301":Allow per file running of tests (#287)Jt Miclatbefore adoption1f4b8dd
lines 33–34why, one step back33 status_code = codes.MOVED_PERMANENTLY34 headers = {"location": "https://example.org/"}Drop sync (#544)Tom Christiebefore adoption206c537
line 35why, one step back35 return Response(status_code, headers=headers, request=request)Allow per file running of tests (#287)Jt Miclatbefore adoption1f4b8dd
lines 36–39why, one step back3637 elif request.url.path == "/redirect_302":38 status_code = codes.FOUND39 headers = {"location": "https://example.org/"}Drop sync (#544)Tom Christiebefore adoption206c537
line 40why, one step back40 return Response(status_code, headers=headers, request=request)Allow per file running of tests (#287)Jt Miclatbefore adoption1f4b8dd
lines 41–44why, one step back4142 elif request.url.path == "/redirect_303":43 status_code = codes.SEE_OTHER44 headers = {"location": "https://example.org/"}Drop sync (#544)Tom Christiebefore adoption206c537
line 45why, one step back45 return Response(status_code, headers=headers, request=request)Allow per file running of tests (#287)Jt Miclatbefore adoption1f4b8dd
lines 46–48why, one step back4647 elif request.url.path == "/relative_redirect":48 headers = {"location": "/"}Drop sync (#544)Tom Christiebefore adoption206c537
line 49why, one step back49 return Response(codes.SEE_OTHER, headers=headers, request=request)Allow per file running of tests (#287)Jt Miclatbefore adoption1f4b8dd
lines 50–52why, one step back5051 elif request.url.path == "/no_scheme_redirect":52 headers = {"location": "//example.org/"}Drop sync (#544)Tom Christiebefore adoption206c537
line 53why, one step back53 return Response(codes.SEE_OTHER, headers=headers, request=request)Allow per file running of tests (#287)Jt Miclatbefore adoption1f4b8dd
lines 54–63why, one step back5455 elif request.url.path == "/multiple_redirects":56 params = parse_qs(request.url.query)57 count = int(params.get("count", "0")[0])58 redirect_count = count - 159 code = codes.SEE_OTHER if count else codes.OK60 location = "/multiple_redirects"61 if redirect_count:62 location += "?count=" + str(redirect_count)63 headers = {"location": location} if count else {}Drop sync (#544)Tom Christiebefore adoption206c537
line 64why, one step back64 return Response(code, headers=headers, request=request)Allow per file running of tests (#287)Jt Miclatbefore adoption1f4b8dd
lines 65–67why, one step back6566 if request.url.path == "/redirect_loop":67 headers = {"location": "/redirect_loop"}Drop sync (#544)Tom Christiebefore adoption206c537
line 68why, one step back68 return Response(codes.SEE_OTHER, headers=headers, request=request)Allow per file running of tests (#287)Jt Miclatbefore adoption1f4b8dd
lines 69–71why, one step back6970 elif request.url.path == "/cross_domain":71 headers = {"location": "https://example.org/cross_domain_target"}Drop sync (#544)Tom Christiebefore adoption206c537
line 72why, one step back72 return Response(codes.SEE_OTHER, headers=headers, request=request)Allow per file running of tests (#287)Jt Miclatbefore adoption1f4b8dd
lines 73–76why, one step back7374 elif request.url.path == "/cross_domain_target":75 headers = dict(request.headers.items())76 content = json.dumps({"headers": headers}).encode()Drop sync (#544)Tom Christiebefore adoption206c537
line 77why, one step back77 return Response(codes.OK, content=content, request=request)Allow per file running of tests (#287)Jt Miclatbefore adoption1f4b8dd
lines 78–80why, one step back7879 elif request.url.path == "/redirect_body":80 headers = {"location": "/redirect_body_target"}Drop sync (#544)Tom Christiebefore adoption206c537
line 81why, one step back81 return Response(codes.PERMANENT_REDIRECT, headers=headers, request=request)Allow per file running of tests (#287)Jt Miclatbefore adoption1f4b8dd
line 82why, one step back82Merge pull request #310 from encode/drop-content-length-on-get-redirectsTom Christiebefore adoption33c5fe0
line 83why, one step back83 elif request.url.path == "/redirect_no_body":Use Streams API for both requests and responses. (#648)Tom Christiebefore adoptioncee1fcc
line 84why, one step back84 content = b"".join([part async for part in request.stream])Merge pull request #310 from encode/drop-content-length-on-get-redirectsTom Christiebefore adoption33c5fe0
line 85why, one step back85 headers = {"location": "/redirect_body_target"}Drop sync (#544)Tom Christiebefore adoption206c537
line 86why, one step back86 return Response(codes.SEE_OTHER, headers=headers, request=request)Merge pull request #310 from encode/drop-content-length-on-get-redirectsTom Christiebefore adoption33c5fe0
line 87why, one step back87Allow per file running of tests (#287)Jt Miclatbefore adoption1f4b8dd
line 88why, one step back88 elif request.url.path == "/redirect_body_target":Use Streams API for both requests and responses. (#648)Tom Christiebefore adoptioncee1fcc
line 89why, one step back89 content = b"".join([part async for part in request.stream])Merge pull request #310 from encode/drop-content-length-on-get-redirectsTom Christiebefore adoption33c5fe0
lines 90–91why, one step back90 headers = dict(request.headers.items())91 body = json.dumps({"body": content.decode(), "headers": headers}).encode()Drop sync (#544)Tom Christiebefore adoption206c537
line 92why, one step back92 return Response(codes.OK, content=body, request=request)Allow per file running of tests (#287)Jt Miclatbefore adoption1f4b8dd
lines 93–96why, one step back9394 elif request.url.path == "/cross_subdomain":95 if request.headers["host"] != "www.example.org":96 headers = {"location": "https://www.example.org/cross_subdomain"}Drop sync (#544)Tom Christiebefore adoption206c537
line 97why, one step back97 return Response(Allow per file running of tests (#287)Jt Miclatbefore adoption1f4b8dd
lines 98–100why, one step back98 codes.PERMANENT_REDIRECT, headers=headers, request=request99 )100 else:Drop sync (#544)Tom Christiebefore adoption206c537
line 101why, one step back101 return Response(codes.OK, content=b"Hello, world!", request=request)Allow per file running of tests (#287)Jt Miclatbefore adoption1f4b8dd
line 102why, one step back102Drop sync (#544)Tom Christiebefore adoption206c537
line 103why, one step back103 return Response(codes.OK, content=b"Hello, world!", request=request)Tighten up response.next() (#297)Florimond Mancabefore adoptiona464627
lines 104–106why, one step back104105106async def test_no_redirect(backend):Refactor tests in the light of backend auto-detection (#615)Florimond Mancabefore adoptionab41a5d
line 107why, one step back107 client = Client(dispatch=MockDispatch())Tighten up response.next() (#297)Florimond Mancabefore adoptiona464627
lines 108–112why, one step back108 url = "https://example.com/no_redirect"109 response = await client.get(url)110 assert response.status_code == 200111 with pytest.raises(NotRedirectResponse):112 await response.next()Allow per file running of tests (#287)Jt Miclatbefore adoption1f4b8dd
lines 113–115why, one step back113114115async def test_redirect_301(backend):Refactor tests in the light of backend auto-detection (#615)Florimond Mancabefore adoptionab41a5d
line 116why, one step back116 client = Client(dispatch=MockDispatch())Allow per file running of tests (#287)Jt Miclatbefore adoption1f4b8dd
lines 117–123why, one step back117 response = await client.post("https://example.org/redirect_301")118 assert response.status_code == codes.OK119 assert response.url == URL("https://example.org/")120 assert len(response.history) == 1121122123async def test_redirect_302(backend):Refactor tests in the light of backend auto-detection (#615)Florimond Mancabefore adoptionab41a5d
line 124why, one step back124 client = Client(dispatch=MockDispatch())Allow per file running of tests (#287)Jt Miclatbefore adoption1f4b8dd
lines 125–131why, one step back125 response = await client.post("https://example.org/redirect_302")126 assert response.status_code == codes.OK127 assert response.url == URL("https://example.org/")128 assert len(response.history) == 1129130131async def test_redirect_303(backend):Refactor tests in the light of backend auto-detection (#615)Florimond Mancabefore adoptionab41a5d
line 132why, one step back132 client = Client(dispatch=MockDispatch())Allow per file running of tests (#287)Jt Miclatbefore adoption1f4b8dd
lines 133–139why, one step back133 response = await client.get("https://example.org/redirect_303")134 assert response.status_code == codes.OK135 assert response.url == URL("https://example.org/")136 assert len(response.history) == 1137138139async def test_disallow_redirects(backend):Refactor tests in the light of backend auto-detection (#615)Florimond Mancabefore adoptionab41a5d
line 140why, one step back140 client = Client(dispatch=MockDispatch())Allow per file running of tests (#287)Jt Miclatbefore adoption1f4b8dd
lines 141–156why, one step back141 response = await client.post(142 "https://example.org/redirect_303", allow_redirects=False143 )144 assert response.status_code == codes.SEE_OTHER145 assert response.url == URL("https://example.org/redirect_303")146 assert response.is_redirect is True147 assert len(response.history) == 0148149 response = await response.next()150 assert response.status_code == codes.OK151 assert response.url == URL("https://example.org/")152 assert response.is_redirect is False153 assert len(response.history) == 1154155156async def test_relative_redirect(backend):Refactor tests in the light of backend auto-detection (#615)Florimond Mancabefore adoptionab41a5d
line 157why, one step back157 client = Client(dispatch=MockDispatch())Allow per file running of tests (#287)Jt Miclatbefore adoption1f4b8dd
lines 158–164why, one step back158 response = await client.get("https://example.org/relative_redirect")159 assert response.status_code == codes.OK160 assert response.url == URL("https://example.org/")161 assert len(response.history) == 1162163164async def test_no_scheme_redirect(backend):Refactor tests in the light of backend auto-detection (#615)Florimond Mancabefore adoptionab41a5d
line 165why, one step back165 client = Client(dispatch=MockDispatch())Allow per file running of tests (#287)Jt Miclatbefore adoption1f4b8dd
lines 166–172why, one step back166 response = await client.get("https://example.org/no_scheme_redirect")167 assert response.status_code == codes.OK168 assert response.url == URL("https://example.org/")169 assert len(response.history) == 1170171172async def test_fragment_redirect(backend):Refactor tests in the light of backend auto-detection (#615)Florimond Mancabefore adoptionab41a5d
line 173why, one step back173 client = Client(dispatch=MockDispatch())Allow per file running of tests (#287)Jt Miclatbefore adoption1f4b8dd
lines 174–180why, one step back174 response = await client.get("https://example.org/relative_redirect#fragment")175 assert response.status_code == codes.OK176 assert response.url == URL("https://example.org/#fragment")177 assert len(response.history) == 1178179180async def test_multiple_redirects(backend):Refactor tests in the light of backend auto-detection (#615)Florimond Mancabefore adoptionab41a5d
line 181why, one step back181 client = Client(dispatch=MockDispatch())Allow per file running of tests (#287)Jt Miclatbefore adoption1f4b8dd
lines 182–196why, one step back182 response = await client.get("https://example.org/multiple_redirects?count=20")183 assert response.status_code == codes.OK184 assert response.url == URL("https://example.org/multiple_redirects")185 assert len(response.history) == 20186 assert response.history[0].url == URL(187 "https://example.org/multiple_redirects?count=20"188 )189 assert response.history[1].url == URL(190 "https://example.org/multiple_redirects?count=19"191 )192 assert len(response.history[0].history) == 0193 assert len(response.history[1].history) == 1194195196async def test_too_many_redirects(backend):Refactor tests in the light of backend auto-detection (#615)Florimond Mancabefore adoptionab41a5d
line 197why, one step back197 client = Client(dispatch=MockDispatch())Allow per file running of tests (#287)Jt Miclatbefore adoption1f4b8dd
lines 198–202why, one step back198 with pytest.raises(TooManyRedirects):199 await client.get("https://example.org/multiple_redirects?count=21")200201202async def test_too_many_redirects_calling_next(backend):Refactor tests in the light of backend auto-detection (#615)Florimond Mancabefore adoptionab41a5d
line 203why, one step back203 client = Client(dispatch=MockDispatch())Allow per file running of tests (#287)Jt Miclatbefore adoption1f4b8dd
lines 204–211why, one step back204 url = "https://example.org/multiple_redirects?count=21"205 response = await client.get(url, allow_redirects=False)206 with pytest.raises(TooManyRedirects):207 while response.is_redirect:208 response = await response.next()209210211async def test_redirect_loop(backend):Refactor tests in the light of backend auto-detection (#615)Florimond Mancabefore adoptionab41a5d
line 212why, one step back212 client = Client(dispatch=MockDispatch())Allow per file running of tests (#287)Jt Miclatbefore adoption1f4b8dd
lines 213–217why, one step back213 with pytest.raises(RedirectLoop):214 await client.get("https://example.org/redirect_loop")215216217async def test_redirect_loop_calling_next(backend):Refactor tests in the light of backend auto-detection (#615)Florimond Mancabefore adoptionab41a5d
line 218why, one step back218 client = Client(dispatch=MockDispatch())Allow per file running of tests (#287)Jt Miclatbefore adoption1f4b8dd
lines 219–226why, one step back219 url = "https://example.org/redirect_loop"220 response = await client.get(url, allow_redirects=False)221 with pytest.raises(RedirectLoop):222 while response.is_redirect:223 response = await response.next()224225226async def test_cross_domain_redirect(backend):Refactor tests in the light of backend auto-detection (#615)Florimond Mancabefore adoptionab41a5d
line 227why, one step back227 client = Client(dispatch=MockDispatch())Allow per file running of tests (#287)Jt Miclatbefore adoption1f4b8dd
lines 228–235why, one step back228 url = "https://example.com/cross_domain"229 headers = {"Authorization": "abc"}230 response = await client.get(url, headers=headers)231 assert response.url == URL("https://example.org/cross_domain_target")232 assert "authorization" not in response.json()["headers"]233234235async def test_same_domain_redirect(backend):Refactor tests in the light of backend auto-detection (#615)Florimond Mancabefore adoptionab41a5d
line 236why, one step back236 client = Client(dispatch=MockDispatch())Allow per file running of tests (#287)Jt Miclatbefore adoption1f4b8dd
lines 237–244why, one step back237 url = "https://example.org/cross_domain"238 headers = {"Authorization": "abc"}239 response = await client.get(url, headers=headers)240 assert response.url == URL("https://example.org/cross_domain_target")241 assert response.json()["headers"]["authorization"] == "abc"242243244async def test_body_redirect(backend):Merge pull request #310 from encode/drop-content-length-on-get-redirectsTom Christiebefore adoption33c5fe0
lines 245–247why, one step back245 """246 A 308 redirect should preserve the request body.247 """Refactor tests in the light of backend auto-detection (#615)Florimond Mancabefore adoptionab41a5d
line 248why, one step back248 client = Client(dispatch=MockDispatch())Allow per file running of tests (#287)Jt Miclatbefore adoption1f4b8dd
lines 249–252why, one step back249 url = "https://example.org/redirect_body"250 data = b"Example request body"251 response = await client.post(url, data=data)252 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 253–260why, one step back253 assert response.json()["body"] == "Example request body"254 assert "content-length" in response.json()["headers"]255256257async def test_no_body_redirect(backend):258 """259 A 303 redirect should remove the request body.260 """Refactor tests in the light of backend auto-detection (#615)Florimond Mancabefore adoptionab41a5d
line 261why, one step back261 client = Client(dispatch=MockDispatch())Merge pull request #310 from encode/drop-content-length-on-get-redirectsTom Christiebefore adoption33c5fe0
lines 262–267why, one step back262 url = "https://example.org/redirect_no_body"263 data = b"Example request body"264 response = await client.post(url, data=data)265 assert response.url == URL("https://example.org/redirect_body_target")266 assert response.json()["body"] == ""267 assert "content-length" not in response.json()["headers"]Allow per file running of tests (#287)Jt Miclatbefore adoption1f4b8dd
lines 268–270why, one step back268269270async def test_cannot_redirect_streaming_body(backend):Refactor tests in the light of backend auto-detection (#615)Florimond Mancabefore adoptionab41a5d
line 271why, one step back271 client = Client(dispatch=MockDispatch())Allow per file running of tests (#287)Jt Miclatbefore adoption1f4b8dd
lines 272–280why, one step back272 url = "https://example.org/redirect_body"273274 async def streaming_body():275 yield b"Example request body"276277 with pytest.raises(RedirectBodyUnavailable):278 await client.post(url, data=streaming_body())279280Refactor client functionality into middleware (#268)Florimond Mancabefore adoption40e6e8c
line 281why, one step back281async def test_cross_subdomain_redirect(backend):Refactor tests in the light of backend auto-detection (#615)Florimond Mancabefore adoptionab41a5d
line 282why, one step back282 client = Client(dispatch=MockDispatch())Allow per file running of tests (#287)Jt Miclatbefore adoption1f4b8dd
lines 283–285why, one step back283 url = "https://example.com/cross_subdomain"284 response = await client.get(url)285 assert response.url == URL("https://www.example.org/cross_subdomain")Fix redirect cookie behavior (#529)Tom Christiebefore adoption5aca0c0
lines 286–287why, one step back286287Drop sync (#544)Tom Christiebefore adoption206c537
line 288why, one step back288class MockCookieDispatch(Dispatcher):Fix redirect cookie behavior (#529)Tom Christiebefore adoption5aca0c0
lines 289–290why, one step back289 async def send(290 self,Drop sync (#544)Tom Christiebefore adoption206c537
line 291why, one step back291 request: Request,Fix redirect cookie behavior (#529)Tom Christiebefore adoption5aca0c0
lines 292–294why, one step back292 verify: VerifyTypes = None,293 cert: CertTypes = None,294 timeout: TimeoutTypes = None,Drop sync (#544)Tom Christiebefore adoption206c537
line 295why, one step back295 ) -> Response:Fix redirect cookie behavior (#529)Tom Christiebefore adoption5aca0c0
lines 296–300why, one step back296 if request.url.path == "/":297 if "cookie" in request.headers:298 content = b"Logged in"299 else:300 content = b"Not logged in"Drop sync (#544)Tom Christiebefore adoption206c537
line 301why, one step back301 return Response(codes.OK, content=content, request=request)Fix redirect cookie behavior (#529)Tom Christiebefore adoption5aca0c0
lines 302–312why, one step back302303 elif request.url.path == "/login":304 status_code = codes.SEE_OTHER305 headers = {306 "location": "/",307 "set-cookie": (308 "session=eyJ1c2VybmFtZSI6ICJ0b21; path=/; Max-Age=1209600; "309 "httponly; samesite=lax"310 ),311 }312Drop sync (#544)Tom Christiebefore adoption206c537
line 313why, one step back313 return Response(status_code, headers=headers, request=request)Fix redirect cookie behavior (#529)Tom Christiebefore adoption5aca0c0
lines 314–323why, one step back314315 elif request.url.path == "/logout":316 status_code = codes.SEE_OTHER317 headers = {318 "location": "/",319 "set-cookie": (320 "session=null; path=/; expires=Thu, 01 Jan 1970 00:00:00 GMT; "321 "httponly; samesite=lax"322 ),323 }Drop sync (#544)Tom Christiebefore adoption206c537
line 324why, one step back324 return Response(status_code, headers=headers, request=request)Fix redirect cookie behavior (#529)Tom Christiebefore adoption5aca0c0
lines 325–327why, one step back325326327async def test_redirect_cookie_behavior(backend):Refactor tests in the light of backend auto-detection (#615)Florimond Mancabefore adoptionab41a5d
line 328why, one step back328 client = Client(dispatch=MockCookieDispatch())Fix redirect cookie behavior (#529)Tom Christiebefore adoption5aca0c0
lines 329–353why, one step back329330 # The client is not logged in.331 response = await client.get("https://example.com/")332 assert response.url == "https://example.com/"333 assert response.text == "Not logged in"334335 # Login redirects to the homepage, setting a session cookie.336 response = await client.post("https://example.com/login")337 assert response.url == "https://example.com/"338 assert response.text == "Logged in"339340 # The client is logged in.341 response = await client.get("https://example.com/")342 assert response.url == "https://example.com/"343 assert response.text == "Logged in"344345 # Logout redirects to the homepage, expiring the session cookie.346 response = await client.post("https://example.com/logout")347 assert response.url == "https://example.com/"348 assert response.text == "Not logged in"349350 # The client is not logged in.351 response = await client.get("https://example.com/")352 assert response.url == "https://example.com/"353 assert response.text == "Not logged in"