test_redirects.py
This file
Path
tests/client/test_redirects.py
269 lines · 10.2 KiB · Python
Last changed by
Refactor client functionality into middleware (#268)Florimond Mancabefore adoption40e6e8c
History of this fileTake it
Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 1–5why, one step back1import json2from urllib.parse import parse_qs34import pytest5Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 6why, one step back6from httpx import (Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
line 7why, one step back7 URL,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
line 8why, one step back8 AsyncClient,Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7
lines 9–11why, one step back9 AsyncDispatcher,10 AsyncRequest,11 AsyncResponse,Work on bringing API into parity with `requests`. (#76)Tom Christiebefore adoption9574041
line 12why, one step back12 CertTypes,Tighten up response.next() (#297)Florimond Mancabefore adoptiona464627
line 13why, one step back13 NotRedirectResponse,Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 14–15why, one step back14 RedirectBodyUnavailable,15 RedirectLoop,Work on bringing API into parity with `requests`. (#76)Tom Christiebefore adoption9574041
line 16why, one step back16 TimeoutTypes,Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
line 17why, one step back17 TooManyRedirects,Work on bringing API into parity with `requests`. (#76)Tom Christiebefore adoption9574041
line 18why, one step back18 VerifyTypes,Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 19–22why, one step back19 codes,20)2122Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7
line 23why, one step back23class MockDispatch(AsyncDispatcher):Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 24–25why, one step back24 async def send(25 self,Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7
line 26why, one step back26 request: AsyncRequest,Work on bringing API into parity with `requests`. (#76)Tom Christiebefore adoption9574041
lines 27–29why, one step back27 verify: VerifyTypes = None,28 cert: CertTypes = None,29 timeout: TimeoutTypes = None,Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7
line 30why, one step back30 ) -> AsyncResponse:Tighten up response.next() (#297)Florimond Mancabefore adoptiona464627
lines 31–34why, one step back31 if request.url.path == "/no_redirect":32 return AsyncResponse(codes.OK, request=request)3334 elif request.url.path == "/redirect_301":Merge pull request #59 from encode/use-stdlib-status-codesTom Christiebefore adoptionafc8369
line 35why, one step back35 status_code = codes.MOVED_PERMANENTLYMerge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
line 36why, one step back36 headers = {"location": "https://example.org/"}Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7
line 37why, one step back37 return AsyncResponse(status_code, headers=headers, request=request)Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 38–39why, one step back3839 elif request.url.path == "/redirect_302":Merge pull request #59 from encode/use-stdlib-status-codesTom Christiebefore adoptionafc8369
line 40why, one step back40 status_code = codes.FOUNDMerge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
line 41why, one step back41 headers = {"location": "https://example.org/"}Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7
line 42why, one step back42 return AsyncResponse(status_code, headers=headers, request=request)Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 43–44why, one step back4344 elif request.url.path == "/redirect_303":Merge pull request #59 from encode/use-stdlib-status-codesTom Christiebefore adoptionafc8369
line 45why, one step back45 status_code = codes.SEE_OTHERMerge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
line 46why, one step back46 headers = {"location": "https://example.org/"}Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7
line 47why, one step back47 return AsyncResponse(status_code, headers=headers, request=request)Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 48–50why, one step back4849 elif request.url.path == "/relative_redirect":50 headers = {"location": "/"}Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7
line 51why, one step back51 return AsyncResponse(codes.SEE_OTHER, headers=headers, request=request)Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 52–54why, one step back5253 elif request.url.path == "/no_scheme_redirect":54 headers = {"location": "//example.org/"}Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7
line 55why, one step back55 return AsyncResponse(codes.SEE_OTHER, headers=headers, request=request)Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 56–60why, one step back5657 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 - 1Merge pull request #59 from encode/use-stdlib-status-codesTom Christiebefore adoptionafc8369
line 61why, one step back61 code = codes.SEE_OTHER if count else codes.OKMerge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 62–65why, one step back62 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 back66 return AsyncResponse(code, headers=headers, request=request)Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 67–69why, one step back6768 if request.url.path == "/redirect_loop":69 headers = {"location": "/redirect_loop"}Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7
line 70why, one step back70 return AsyncResponse(codes.SEE_OTHER, headers=headers, request=request)Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 71–73why, one step back7172 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 back74 return AsyncResponse(codes.SEE_OTHER, headers=headers, request=request)Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 75–78why, one step back7576 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 back79 return AsyncResponse(codes.OK, content=content, request=request)Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 80–83why, one step back8081 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 back84 return AsyncResponse(85 codes.PERMANENT_REDIRECT, headers=headers, request=request86 )Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 87–90why, one step back8788 elif request.url.path == "/redirect_body_target":89 content = await request.read()90 body = json.dumps({"body": content.decode()}).encode()Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7
line 91why, one step back91 return AsyncResponse(codes.OK, content=body, request=request)Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
line 92why, one step back92Remove "Host" header on redirects if URL origin has changed (#199)Yeray Diaz Diazbefore adoption95dad24
lines 93–103why, one step back93 elif request.url.path == "/cross_subdomain":94 if request.headers["host"] != "www.example.org":95 headers = {"location": "https://www.example.org/cross_subdomain"}96 return AsyncResponse(97 codes.PERMANENT_REDIRECT, headers=headers, request=request98 )99 else:100 return AsyncResponse(101 codes.OK, content=b"Hello, world!", request=request102 )103Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7
line 104why, one step back104 return AsyncResponse(codes.OK, content=b"Hello, world!", request=request)Tighten up response.next() (#297)Florimond Mancabefore adoptiona464627
lines 105–113why, one step back105106107async def test_no_redirect(backend):108 client = AsyncClient(dispatch=MockDispatch(), backend=backend)109 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()Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 114–115why, one step back114115Parametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058
lines 116–117why, one step back116async def test_redirect_301(backend):117 client = AsyncClient(dispatch=MockDispatch(), backend=backend)Tidy up redirect testsTom Christiebefore adoptione40b0bf
line 118why, one step back118 response = await client.post("https://example.org/redirect_301")Merge pull request #59 from encode/use-stdlib-status-codesTom Christiebefore adoptionafc8369
line 119why, one step back119 assert response.status_code == codes.OKMerge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 120–123why, one step back120 assert response.url == URL("https://example.org/")121 assert len(response.history) == 1122123Parametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058
lines 124–125why, one step back124async def test_redirect_302(backend):125 client = AsyncClient(dispatch=MockDispatch(), backend=backend)Tidy up redirect testsTom Christiebefore adoptione40b0bf
line 126why, one step back126 response = await client.post("https://example.org/redirect_302")Merge pull request #59 from encode/use-stdlib-status-codesTom Christiebefore adoptionafc8369
line 127why, one step back127 assert response.status_code == codes.OKMerge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 128–131why, one step back128 assert response.url == URL("https://example.org/")129 assert len(response.history) == 1130131Parametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058
lines 132–133why, one step back132async def test_redirect_303(backend):133 client = AsyncClient(dispatch=MockDispatch(), backend=backend)Tidy up redirect testsTom Christiebefore adoptione40b0bf
line 134why, one step back134 response = await client.get("https://example.org/redirect_303")Merge pull request #59 from encode/use-stdlib-status-codesTom Christiebefore adoptionafc8369
line 135why, one step back135 assert response.status_code == codes.OKMerge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 136–139why, one step back136 assert response.url == URL("https://example.org/")137 assert len(response.history) == 1138139Parametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058
lines 140–141why, one step back140async def test_disallow_redirects(backend):141 client = AsyncClient(dispatch=MockDispatch(), backend=backend)Tidy up redirect testsTom Christiebefore adoptione40b0bf
lines 142–143why, one step back142 response = await client.post(143 "https://example.org/redirect_303", allow_redirects=FalseMerge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
line 144why, one step back144 )Merge pull request #59 from encode/use-stdlib-status-codesTom Christiebefore adoptionafc8369
line 145why, one step back145 assert response.status_code == codes.SEE_OTHERMerge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
line 146why, one step back146 assert response.url == URL("https://example.org/redirect_303")Explicitly check is_redirect in simple test case (#208)Tammo Behrendsbefore adoptionbebedcb
line 147why, one step back147 assert response.is_redirect is TrueMerge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 148–150why, one step back148 assert len(response.history) == 0149150 response = await response.next()Merge pull request #59 from encode/use-stdlib-status-codesTom Christiebefore adoptionafc8369
line 151why, one step back151 assert response.status_code == codes.OKMerge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
line 152why, one step back152 assert response.url == URL("https://example.org/")Explicitly check is_redirect in simple test case (#208)Tammo Behrendsbefore adoptionbebedcb
line 153why, one step back153 assert response.is_redirect is FalseMerge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 154–156why, one step back154 assert len(response.history) == 1155156Parametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058
lines 157–158why, one step back157async def test_relative_redirect(backend):158 client = AsyncClient(dispatch=MockDispatch(), backend=backend)Tidy up redirect testsTom Christiebefore adoptione40b0bf
line 159why, one step back159 response = await client.get("https://example.org/relative_redirect")Merge pull request #59 from encode/use-stdlib-status-codesTom Christiebefore adoptionafc8369
line 160why, one step back160 assert response.status_code == codes.OKMerge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 161–164why, one step back161 assert response.url == URL("https://example.org/")162 assert len(response.history) == 1163164Parametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058
lines 165–166why, one step back165async def test_no_scheme_redirect(backend):166 client = AsyncClient(dispatch=MockDispatch(), backend=backend)Tidy up redirect testsTom Christiebefore adoptione40b0bf
line 167why, one step back167 response = await client.get("https://example.org/no_scheme_redirect")Merge pull request #59 from encode/use-stdlib-status-codesTom Christiebefore adoptionafc8369
line 168why, one step back168 assert response.status_code == codes.OKMerge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 169–172why, one step back169 assert response.url == URL("https://example.org/")170 assert len(response.history) == 1171172Parametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058
lines 173–174why, one step back173async def test_fragment_redirect(backend):174 client = AsyncClient(dispatch=MockDispatch(), backend=backend)Tidy up redirect testsTom Christiebefore adoptione40b0bf
line 175why, one step back175 response = await client.get("https://example.org/relative_redirect#fragment")Merge pull request #59 from encode/use-stdlib-status-codesTom Christiebefore adoptionafc8369
line 176why, one step back176 assert response.status_code == codes.OKMerge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 177–180why, one step back177 assert response.url == URL("https://example.org/#fragment")178 assert len(response.history) == 1179180Parametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058
lines 181–182why, one step back181async def test_multiple_redirects(backend):182 client = AsyncClient(dispatch=MockDispatch(), backend=backend)Tidy up redirect testsTom Christiebefore adoptione40b0bf
line 183why, one step back183 response = await client.get("https://example.org/multiple_redirects?count=20")Merge pull request #59 from encode/use-stdlib-status-codesTom Christiebefore adoptionafc8369
line 184why, one step back184 assert response.status_code == codes.OKMerge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 185–186why, one step back185 assert response.url == URL("https://example.org/multiple_redirects")186 assert len(response.history) == 20Multipart support (#90)Tom Christiebefore adoption6a44b05
lines 187–192why, one step back187 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 )Tweaks (#89)Tom Christiebefore adoption7c6fb5c
lines 193–194why, one step back193 assert len(response.history[0].history) == 0194 assert len(response.history[1].history) == 1Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 195–196why, one step back195196Parametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058
lines 197–198why, one step back197async def test_too_many_redirects(backend):198 client = AsyncClient(dispatch=MockDispatch(), backend=backend)Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
line 199why, one step back199 with pytest.raises(TooManyRedirects):Tidy up redirect testsTom Christiebefore adoptione40b0bf
line 200why, one step back200 await client.get("https://example.org/multiple_redirects?count=21")Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 201–202why, one step back201202Parametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058
lines 203–204why, one step back203async def test_too_many_redirects_calling_next(backend):204 client = AsyncClient(dispatch=MockDispatch(), backend=backend)Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
line 205why, one step back205 url = "https://example.org/multiple_redirects?count=21"Tidy up redirect testsTom Christiebefore adoptione40b0bf
line 206why, one step back206 response = await client.get(url, allow_redirects=False)Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 207–211why, one step back207 with pytest.raises(TooManyRedirects):208 while response.is_redirect:209 response = await response.next()210211Parametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058
lines 212–213why, one step back212async def test_redirect_loop(backend):213 client = AsyncClient(dispatch=MockDispatch(), backend=backend)Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
line 214why, one step back214 with pytest.raises(RedirectLoop):Tidy up redirect testsTom Christiebefore adoptione40b0bf
line 215why, one step back215 await client.get("https://example.org/redirect_loop")Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 216–217why, one step back216217Parametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058
lines 218–219why, one step back218async def test_redirect_loop_calling_next(backend):219 client = AsyncClient(dispatch=MockDispatch(), backend=backend)Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
line 220why, one step back220 url = "https://example.org/redirect_loop"Tidy up redirect testsTom Christiebefore adoptione40b0bf
line 221why, one step back221 response = await client.get(url, allow_redirects=False)Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 222–226why, one step back222 with pytest.raises(RedirectLoop):223 while response.is_redirect:224 response = await response.next()225226Parametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058
lines 227–228why, one step back227async def test_cross_domain_redirect(backend):228 client = AsyncClient(dispatch=MockDispatch(), backend=backend)Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 229–230why, one step back229 url = "https://example.com/cross_domain"230 headers = {"Authorization": "abc"}Tidy up redirect testsTom Christiebefore adoptione40b0bf
line 231why, one step back231 response = await client.get(url, headers=headers)Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
line 232why, one step back232 assert response.url == URL("https://example.org/cross_domain_target")Add response.json()Tom Christiebefore adoption2116d44
line 233why, one step back233 assert "authorization" not in response.json()["headers"]Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 234–235why, one step back234235Parametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058
lines 236–237why, one step back236async def test_same_domain_redirect(backend):237 client = AsyncClient(dispatch=MockDispatch(), backend=backend)Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 238–239why, one step back238 url = "https://example.org/cross_domain"239 headers = {"Authorization": "abc"}Tidy up redirect testsTom Christiebefore adoptione40b0bf
line 240why, one step back240 response = await client.get(url, headers=headers)Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
line 241why, one step back241 assert response.url == URL("https://example.org/cross_domain_target")Add response.json()Tom Christiebefore adoption2116d44
line 242why, one step back242 assert response.json()["headers"]["authorization"] == "abc"Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 243–244why, one step back243244Parametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058
lines 245–246why, one step back245async def test_body_redirect(backend):246 client = AsyncClient(dispatch=MockDispatch(), backend=backend)Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 247–248why, one step back247 url = "https://example.org/redirect_body"248 data = b"Example request body"Tidy up redirect testsTom Christiebefore adoptione40b0bf
line 249why, one step back249 response = await client.post(url, data=data)Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
line 250why, one step back250 assert response.url == URL("https://example.org/redirect_body_target")Add response.json()Tom Christiebefore adoption2116d44
line 251why, one step back251 assert response.json() == {"body": "Example request body"}Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 252–253why, one step back252253Parametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058
lines 254–255why, one step back254async def test_cannot_redirect_streaming_body(backend):255 client = AsyncClient(dispatch=MockDispatch(), backend=backend)Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 256–261why, one step back256 url = "https://example.org/redirect_body"257258 async def streaming_body():259 yield b"Example request body"260261 with pytest.raises(RedirectBodyUnavailable):Tidy up redirect testsTom Christiebefore adoptione40b0bf
line 262why, one step back262 await client.post(url, data=streaming_body())Remove "Host" header on redirects if URL origin has changed (#199)Yeray Diaz Diazbefore adoption95dad24
lines 263–264why, one step back263264Refactor client functionality into middleware (#268)Florimond Mancabefore adoption40e6e8c
line 265why, one step back265async def test_cross_subdomain_redirect(backend):Parametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058
line 266why, one step back266 client = AsyncClient(dispatch=MockDispatch(), backend=backend)Remove "Host" header on redirects if URL origin has changed (#199)Yeray Diaz Diazbefore adoption95dad24
lines 267–269why, one step back267 url = "https://example.com/cross_subdomain"268 response = await client.get(url)269 assert response.url == URL("https://www.example.org/cross_subdomain")