test_redirects.py
This file
Path
tests/client/test_redirects.py
354 lines · 12.9 KiB · Python
Last changed by
Refactor tests in the light of backend auto-detection (#615)Florimond Mancabefore adoptionab41a5d
History of this fileTake it
The walk stopped after 200 commits: lines older than cad9c03 point at it.
Added links property to BaseResponse (#211)euri10before adoptioncad9c03
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,Added links property to BaseResponse (#211)euri10before adoptioncad9c03
lines 10–11why, one step back10 RedirectBodyUnavailable,11 RedirectLoop,Drop sync (#544)Tom Christiebefore adoption206c537
lines 12–13why, one step back12 Request,13 Response,Added links property to BaseResponse (#211)euri10before adoptioncad9c03
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 DispatcherAdded links property to BaseResponse (#211)euri10before adoptioncad9c03
lines 19–20why, one step back1920Drop sync (#544)Tom Christiebefore adoption206c537
line 21why, one step back21class MockDispatch(Dispatcher):Added links property to BaseResponse (#211)euri10before adoptioncad9c03
lines 22–23why, one step back22 async def send(23 self,Drop sync (#544)Tom Christiebefore adoption206c537
line 24why, one step back24 request: Request,Added links property to BaseResponse (#211)euri10before adoptioncad9c03
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":Added links property to BaseResponse (#211)euri10before adoptioncad9c03
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)Added links property to BaseResponse (#211)euri10before adoptioncad9c03
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)Added links property to BaseResponse (#211)euri10before adoptioncad9c03
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)Added links property to BaseResponse (#211)euri10before adoptioncad9c03
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)Added links property to BaseResponse (#211)euri10before adoptioncad9c03
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)Added links property to BaseResponse (#211)euri10before adoptioncad9c03
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)Added links property to BaseResponse (#211)euri10before adoptioncad9c03
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)Added links property to BaseResponse (#211)euri10before adoptioncad9c03
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)Added links property to BaseResponse (#211)euri10before adoptioncad9c03
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)Added links property to BaseResponse (#211)euri10before adoptioncad9c03
lines 78–81why, one step back7879 elif request.url.path == "/redirect_body":80 await request.read()81 headers = {"location": "/redirect_body_target"}Drop sync (#544)Tom Christiebefore adoption206c537
line 82why, one step back82 return Response(codes.PERMANENT_REDIRECT, headers=headers, request=request)Added links property to BaseResponse (#211)euri10before adoptioncad9c03
line 83why, one step back83Merge pull request #310 from encode/drop-content-length-on-get-redirectsTom Christiebefore adoption33c5fe0
lines 84–86why, one step back84 elif request.url.path == "/redirect_no_body":85 await request.read()86 headers = {"location": "/redirect_body_target"}Drop sync (#544)Tom Christiebefore adoption206c537
line 87why, one step back87 return Response(codes.SEE_OTHER, headers=headers, request=request)Merge pull request #310 from encode/drop-content-length-on-get-redirectsTom Christiebefore adoption33c5fe0
line 88why, one step back88Added links property to BaseResponse (#211)euri10before adoptioncad9c03
lines 89–90why, one step back89 elif request.url.path == "/redirect_body_target":90 content = await request.read()Merge pull request #310 from encode/drop-content-length-on-get-redirectsTom Christiebefore adoption33c5fe0
lines 91–92why, one step back91 headers = dict(request.headers.items())92 body = json.dumps({"body": content.decode(), "headers": headers}).encode()Drop sync (#544)Tom Christiebefore adoption206c537
line 93why, one step back93 return Response(codes.OK, content=body, request=request)Added links property to BaseResponse (#211)euri10before adoptioncad9c03
lines 94–97why, one step back9495 elif request.url.path == "/cross_subdomain":96 if request.headers["host"] != "www.example.org":97 headers = {"location": "https://www.example.org/cross_subdomain"}Drop sync (#544)Tom Christiebefore adoption206c537
line 98why, one step back98 return Response(Added links property to BaseResponse (#211)euri10before adoptioncad9c03
lines 99–101why, one step back99 codes.PERMANENT_REDIRECT, headers=headers, request=request100 )101 else:Drop sync (#544)Tom Christiebefore adoption206c537
line 102why, one step back102 return Response(codes.OK, content=b"Hello, world!", request=request)Added links property to BaseResponse (#211)euri10before adoptioncad9c03
line 103why, one step back103Drop sync (#544)Tom Christiebefore adoption206c537
line 104why, one step back104 return Response(codes.OK, content=b"Hello, world!", request=request)Tighten up response.next() (#297)Florimond Mancabefore adoptiona464627
lines 105–107why, one step back105106107async def test_no_redirect(backend):Refactor tests in the light of backend auto-detection (#615)Florimond Mancabefore adoptionab41a5d
line 108why, one step back108 client = Client(dispatch=MockDispatch())Tighten up response.next() (#297)Florimond Mancabefore adoptiona464627
lines 109–113why, one step back109 url = "https://example.com/no_redirect"110 response = await client.get(url)111 assert response.status_code == 200112 with pytest.raises(NotRedirectResponse):113 await response.next()Added links property to BaseResponse (#211)euri10before adoptioncad9c03
lines 114–115why, one step back114115Parametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058
line 116why, one step back116async def test_redirect_301(backend):Refactor tests in the light of backend auto-detection (#615)Florimond Mancabefore adoptionab41a5d
line 117why, one step back117 client = Client(dispatch=MockDispatch())Added links property to BaseResponse (#211)euri10before adoptioncad9c03
lines 118–123why, one step back118 response = await client.post("https://example.org/redirect_301")119 assert response.status_code == codes.OK120 assert response.url == URL("https://example.org/")121 assert len(response.history) == 1122123Parametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058
line 124why, one step back124async def test_redirect_302(backend):Refactor tests in the light of backend auto-detection (#615)Florimond Mancabefore adoptionab41a5d
line 125why, one step back125 client = Client(dispatch=MockDispatch())Added links property to BaseResponse (#211)euri10before adoptioncad9c03
lines 126–131why, one step back126 response = await client.post("https://example.org/redirect_302")127 assert response.status_code == codes.OK128 assert response.url == URL("https://example.org/")129 assert len(response.history) == 1130131Parametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058
line 132why, one step back132async def test_redirect_303(backend):Refactor tests in the light of backend auto-detection (#615)Florimond Mancabefore adoptionab41a5d
line 133why, one step back133 client = Client(dispatch=MockDispatch())Added links property to BaseResponse (#211)euri10before adoptioncad9c03
lines 134–139why, one step back134 response = await client.get("https://example.org/redirect_303")135 assert response.status_code == codes.OK136 assert response.url == URL("https://example.org/")137 assert len(response.history) == 1138139Parametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058
line 140why, one step back140async def test_disallow_redirects(backend):Refactor tests in the light of backend auto-detection (#615)Florimond Mancabefore adoptionab41a5d
line 141why, one step back141 client = Client(dispatch=MockDispatch())Added links property to BaseResponse (#211)euri10before adoptioncad9c03
lines 142–156why, one step back142 response = await client.post(143 "https://example.org/redirect_303", allow_redirects=False144 )145 assert response.status_code == codes.SEE_OTHER146 assert response.url == URL("https://example.org/redirect_303")147 assert response.is_redirect is True148 assert len(response.history) == 0149150 response = await response.next()151 assert response.status_code == codes.OK152 assert response.url == URL("https://example.org/")153 assert response.is_redirect is False154 assert len(response.history) == 1155156Parametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058
line 157why, one step back157async def test_relative_redirect(backend):Refactor tests in the light of backend auto-detection (#615)Florimond Mancabefore adoptionab41a5d
line 158why, one step back158 client = Client(dispatch=MockDispatch())Added links property to BaseResponse (#211)euri10before adoptioncad9c03
lines 159–164why, one step back159 response = await client.get("https://example.org/relative_redirect")160 assert response.status_code == codes.OK161 assert response.url == URL("https://example.org/")162 assert len(response.history) == 1163164Parametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058
line 165why, one step back165async def test_no_scheme_redirect(backend):Refactor tests in the light of backend auto-detection (#615)Florimond Mancabefore adoptionab41a5d
line 166why, one step back166 client = Client(dispatch=MockDispatch())Added links property to BaseResponse (#211)euri10before adoptioncad9c03
lines 167–172why, one step back167 response = await client.get("https://example.org/no_scheme_redirect")168 assert response.status_code == codes.OK169 assert response.url == URL("https://example.org/")170 assert len(response.history) == 1171172Parametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058
line 173why, one step back173async def test_fragment_redirect(backend):Refactor tests in the light of backend auto-detection (#615)Florimond Mancabefore adoptionab41a5d
line 174why, one step back174 client = Client(dispatch=MockDispatch())Added links property to BaseResponse (#211)euri10before adoptioncad9c03
lines 175–180why, one step back175 response = await client.get("https://example.org/relative_redirect#fragment")176 assert response.status_code == codes.OK177 assert response.url == URL("https://example.org/#fragment")178 assert len(response.history) == 1179180Parametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058
line 181why, one step back181async def test_multiple_redirects(backend):Refactor tests in the light of backend auto-detection (#615)Florimond Mancabefore adoptionab41a5d
line 182why, one step back182 client = Client(dispatch=MockDispatch())Added links property to BaseResponse (#211)euri10before adoptioncad9c03
lines 183–196why, one step back183 response = await client.get("https://example.org/multiple_redirects?count=20")184 assert response.status_code == codes.OK185 assert response.url == URL("https://example.org/multiple_redirects")186 assert len(response.history) == 20187 assert response.history[0].url == URL(188 "https://example.org/multiple_redirects?count=20"189 )190 assert response.history[1].url == URL(191 "https://example.org/multiple_redirects?count=19"192 )193 assert len(response.history[0].history) == 0194 assert len(response.history[1].history) == 1195196Parametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058
line 197why, one step back197async def test_too_many_redirects(backend):Refactor tests in the light of backend auto-detection (#615)Florimond Mancabefore adoptionab41a5d
line 198why, one step back198 client = Client(dispatch=MockDispatch())Added links property to BaseResponse (#211)euri10before adoptioncad9c03
lines 199–202why, one step back199 with pytest.raises(TooManyRedirects):200 await client.get("https://example.org/multiple_redirects?count=21")201202Parametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058
line 203why, one step back203async def test_too_many_redirects_calling_next(backend):Refactor tests in the light of backend auto-detection (#615)Florimond Mancabefore adoptionab41a5d
line 204why, one step back204 client = Client(dispatch=MockDispatch())Added links property to BaseResponse (#211)euri10before adoptioncad9c03
lines 205–211why, one step back205 url = "https://example.org/multiple_redirects?count=21"206 response = await client.get(url, allow_redirects=False)207 with pytest.raises(TooManyRedirects):208 while response.is_redirect:209 response = await response.next()210211Parametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058
line 212why, one step back212async def test_redirect_loop(backend):Refactor tests in the light of backend auto-detection (#615)Florimond Mancabefore adoptionab41a5d
line 213why, one step back213 client = Client(dispatch=MockDispatch())Added links property to BaseResponse (#211)euri10before adoptioncad9c03
lines 214–217why, one step back214 with pytest.raises(RedirectLoop):215 await client.get("https://example.org/redirect_loop")216217Parametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058
line 218why, one step back218async def test_redirect_loop_calling_next(backend):Refactor tests in the light of backend auto-detection (#615)Florimond Mancabefore adoptionab41a5d
line 219why, one step back219 client = Client(dispatch=MockDispatch())Added links property to BaseResponse (#211)euri10before adoptioncad9c03
lines 220–226why, one step back220 url = "https://example.org/redirect_loop"221 response = await client.get(url, allow_redirects=False)222 with pytest.raises(RedirectLoop):223 while response.is_redirect:224 response = await response.next()225226Parametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058
line 227why, one step back227async def test_cross_domain_redirect(backend):Refactor tests in the light of backend auto-detection (#615)Florimond Mancabefore adoptionab41a5d
line 228why, one step back228 client = Client(dispatch=MockDispatch())Added links property to BaseResponse (#211)euri10before adoptioncad9c03
lines 229–235why, one step back229 url = "https://example.com/cross_domain"230 headers = {"Authorization": "abc"}231 response = await client.get(url, headers=headers)232 assert response.url == URL("https://example.org/cross_domain_target")233 assert "authorization" not in response.json()["headers"]234235Parametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058
line 236why, one step back236async def test_same_domain_redirect(backend):Refactor tests in the light of backend auto-detection (#615)Florimond Mancabefore adoptionab41a5d
line 237why, one step back237 client = Client(dispatch=MockDispatch())Added links property to BaseResponse (#211)euri10before adoptioncad9c03
lines 238–244why, one step back238 url = "https://example.org/cross_domain"239 headers = {"Authorization": "abc"}240 response = await client.get(url, headers=headers)241 assert response.url == URL("https://example.org/cross_domain_target")242 assert response.json()["headers"]["authorization"] == "abc"243244Parametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058
line 245why, one step back245async def test_body_redirect(backend):Merge pull request #310 from encode/drop-content-length-on-get-redirectsTom Christiebefore adoption33c5fe0
lines 246–248why, one step back246 """247 A 308 redirect should preserve the request body.248 """Refactor tests in the light of backend auto-detection (#615)Florimond Mancabefore adoptionab41a5d
line 249why, one step back249 client = Client(dispatch=MockDispatch())Added links property to BaseResponse (#211)euri10before adoptioncad9c03
lines 250–253why, one step back250 url = "https://example.org/redirect_body"251 data = b"Example request body"252 response = await client.post(url, data=data)253 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 254–261why, one step back254 assert response.json()["body"] == "Example request body"255 assert "content-length" in response.json()["headers"]256257258async def test_no_body_redirect(backend):259 """260 A 303 redirect should remove the request body.261 """Refactor tests in the light of backend auto-detection (#615)Florimond Mancabefore adoptionab41a5d
line 262why, one step back262 client = Client(dispatch=MockDispatch())Merge pull request #310 from encode/drop-content-length-on-get-redirectsTom Christiebefore adoption33c5fe0
lines 263–268why, one step back263 url = "https://example.org/redirect_no_body"264 data = b"Example request body"265 response = await client.post(url, data=data)266 assert response.url == URL("https://example.org/redirect_body_target")267 assert response.json()["body"] == ""268 assert "content-length" not in response.json()["headers"]Added links property to BaseResponse (#211)euri10before adoptioncad9c03
lines 269–270why, one step back269270Parametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058
line 271why, one step back271async def test_cannot_redirect_streaming_body(backend):Refactor tests in the light of backend auto-detection (#615)Florimond Mancabefore adoptionab41a5d
line 272why, one step back272 client = Client(dispatch=MockDispatch())Added links property to BaseResponse (#211)euri10before adoptioncad9c03
lines 273–281why, one step back273 url = "https://example.org/redirect_body"274275 async def streaming_body():276 yield b"Example request body"277278 with pytest.raises(RedirectBodyUnavailable):279 await client.post(url, data=streaming_body())280281Refactor client functionality into middleware (#268)Florimond Mancabefore adoption40e6e8c
line 282why, one step back282async def test_cross_subdomain_redirect(backend):Refactor tests in the light of backend auto-detection (#615)Florimond Mancabefore adoptionab41a5d
line 283why, one step back283 client = Client(dispatch=MockDispatch())Added links property to BaseResponse (#211)euri10before adoptioncad9c03
lines 284–286why, one step back284 url = "https://example.com/cross_subdomain"285 response = await client.get(url)286 assert response.url == URL("https://www.example.org/cross_subdomain")Fix redirect cookie behavior (#529)Tom Christiebefore adoption5aca0c0
lines 287–288why, one step back287288Drop sync (#544)Tom Christiebefore adoption206c537
line 289why, one step back289class MockCookieDispatch(Dispatcher):Fix redirect cookie behavior (#529)Tom Christiebefore adoption5aca0c0
lines 290–291why, one step back290 async def send(291 self,Drop sync (#544)Tom Christiebefore adoption206c537
line 292why, one step back292 request: Request,Fix redirect cookie behavior (#529)Tom Christiebefore adoption5aca0c0
lines 293–295why, one step back293 verify: VerifyTypes = None,294 cert: CertTypes = None,295 timeout: TimeoutTypes = None,Drop sync (#544)Tom Christiebefore adoption206c537
line 296why, one step back296 ) -> Response:Fix redirect cookie behavior (#529)Tom Christiebefore adoption5aca0c0
lines 297–301why, one step back297 if request.url.path == "/":298 if "cookie" in request.headers:299 content = b"Logged in"300 else:301 content = b"Not logged in"Drop sync (#544)Tom Christiebefore adoption206c537
line 302why, one step back302 return Response(codes.OK, content=content, request=request)Fix redirect cookie behavior (#529)Tom Christiebefore adoption5aca0c0
lines 303–313why, one step back303304 elif request.url.path == "/login":305 status_code = codes.SEE_OTHER306 headers = {307 "location": "/",308 "set-cookie": (309 "session=eyJ1c2VybmFtZSI6ICJ0b21; path=/; Max-Age=1209600; "310 "httponly; samesite=lax"311 ),312 }313Drop sync (#544)Tom Christiebefore adoption206c537
line 314why, one step back314 return Response(status_code, headers=headers, request=request)Fix redirect cookie behavior (#529)Tom Christiebefore adoption5aca0c0
lines 315–324why, one step back315316 elif request.url.path == "/logout":317 status_code = codes.SEE_OTHER318 headers = {319 "location": "/",320 "set-cookie": (321 "session=null; path=/; expires=Thu, 01 Jan 1970 00:00:00 GMT; "322 "httponly; samesite=lax"323 ),324 }Drop sync (#544)Tom Christiebefore adoption206c537
line 325why, one step back325 return Response(status_code, headers=headers, request=request)Fix redirect cookie behavior (#529)Tom Christiebefore adoption5aca0c0
lines 326–328why, one step back326327328async def test_redirect_cookie_behavior(backend):Refactor tests in the light of backend auto-detection (#615)Florimond Mancabefore adoptionab41a5d
line 329why, one step back329 client = Client(dispatch=MockCookieDispatch())Fix redirect cookie behavior (#529)Tom Christiebefore adoption5aca0c0
lines 330–354why, one step back330331 # The client is not logged in.332 response = await client.get("https://example.com/")333 assert response.url == "https://example.com/"334 assert response.text == "Not logged in"335336 # Login redirects to the homepage, setting a session cookie.337 response = await client.post("https://example.com/login")338 assert response.url == "https://example.com/"339 assert response.text == "Logged in"340341 # The client is logged in.342 response = await client.get("https://example.com/")343 assert response.url == "https://example.com/"344 assert response.text == "Logged in"345346 # Logout redirects to the homepage, expiring the session cookie.347 response = await client.post("https://example.com/logout")348 assert response.url == "https://example.com/"349 assert response.text == "Not logged in"350351 # The client is not logged in.352 response = await client.get("https://example.com/")353 assert response.url == "https://example.com/"354 assert response.text == "Not logged in"