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 fileTake it
The walk stopped after 200 commits: lines older than 53a3e55 point at it.
Update README.mdTom Christiebefore adoption53a3e55
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 (Update README.mdTom Christiebefore adoption53a3e55
lines 7–8why, one step back7 URL,8 AsyncClient,Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7
lines 9–11why, one step back9 AsyncDispatcher,10 AsyncRequest,11 AsyncResponse,Update README.mdTom Christiebefore adoption53a3e55
line 12why, one step back12 CertTypes,Tighten up response.next() (#297)Florimond Mancabefore adoptiona464627
line 13why, one step back13 NotRedirectResponse,Update README.mdTom Christiebefore adoption53a3e55
lines 14–22why, one step back14 RedirectBodyUnavailable,15 RedirectLoop,16 TimeoutTypes,17 TooManyRedirects,18 VerifyTypes,19 codes,20)2122Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7
line 23why, one step back23class MockDispatch(AsyncDispatcher):Update README.mdTom Christiebefore adoption53a3e55
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,Update README.mdTom Christiebefore adoption53a3e55
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":Update README.mdTom Christiebefore adoption53a3e55
lines 35–36why, one step back35 status_code = codes.MOVED_PERMANENTLY36 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)Update README.mdTom Christiebefore adoption53a3e55
lines 38–41why, one step back3839 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 back42 return AsyncResponse(status_code, headers=headers, request=request)Update README.mdTom Christiebefore adoption53a3e55
lines 43–46why, one step back4344 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 back47 return AsyncResponse(status_code, headers=headers, request=request)Update README.mdTom Christiebefore adoption53a3e55
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)Update README.mdTom Christiebefore adoption53a3e55
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)Update README.mdTom Christiebefore adoption53a3e55
lines 56–65why, 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 - 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 back66 return AsyncResponse(code, headers=headers, request=request)Update README.mdTom Christiebefore adoption53a3e55
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)Update README.mdTom Christiebefore adoption53a3e55
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)Update README.mdTom Christiebefore adoption53a3e55
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)Update README.mdTom Christiebefore adoption53a3e55
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 )Update README.mdTom Christiebefore adoption53a3e55
line 87why, one step back87Merge pull request #310 from encode/drop-content-length-on-get-redirectsTom Christiebefore adoption33c5fe0
lines 88–92why, one step back88 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)92Update README.mdTom Christiebefore adoption53a3e55
lines 93–94why, one step back93 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 back95 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 back97 return AsyncResponse(codes.OK, content=body, request=request)Update README.mdTom Christiebefore adoption53a3e55
line 98why, one step back98Remove "Host" header on redirects if URL origin has changed (#199)Yeray Diaz Diazbefore adoption95dad24
lines 99–109why, one step back99 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 )109Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7
line 110why, one step back110 return AsyncResponse(codes.OK, content=b"Hello, world!", request=request)Tighten up response.next() (#297)Florimond Mancabefore adoptiona464627
lines 111–119why, one step back111112113async 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 back120121Parametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058
lines 122–123why, one step back122async def test_redirect_301(backend):123 client = AsyncClient(dispatch=MockDispatch(), backend=backend)Update README.mdTom Christiebefore adoption53a3e55
lines 124–129why, one step back124 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) == 1128129Parametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058
lines 130–131why, one step back130async def test_redirect_302(backend):131 client = AsyncClient(dispatch=MockDispatch(), backend=backend)Update README.mdTom Christiebefore adoption53a3e55
lines 132–137why, one step back132 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) == 1136137Parametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058
lines 138–139why, one step back138async def test_redirect_303(backend):139 client = AsyncClient(dispatch=MockDispatch(), backend=backend)Update README.mdTom Christiebefore adoption53a3e55
lines 140–145why, one step back140 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) == 1144145Parametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058
lines 146–147why, one step back146async def test_disallow_redirects(backend):147 client = AsyncClient(dispatch=MockDispatch(), backend=backend)Update README.mdTom Christiebefore adoption53a3e55
lines 148–152why, one step back148 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 back153 assert response.is_redirect is TrueUpdate README.mdTom Christiebefore adoption53a3e55
lines 154–158why, one step back154 assert len(response.history) == 0155156 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 back159 assert response.is_redirect is FalseUpdate README.mdTom Christiebefore adoption53a3e55
lines 160–162why, one step back160 assert len(response.history) == 1161162Parametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058
lines 163–164why, one step back163async def test_relative_redirect(backend):164 client = AsyncClient(dispatch=MockDispatch(), backend=backend)Update README.mdTom Christiebefore adoption53a3e55
lines 165–170why, one step back165 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) == 1169170Parametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058
lines 171–172why, one step back171async def test_no_scheme_redirect(backend):172 client = AsyncClient(dispatch=MockDispatch(), backend=backend)Update README.mdTom Christiebefore adoption53a3e55
lines 173–178why, one step back173 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) == 1177178Parametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058
lines 179–180why, one step back179async def test_fragment_redirect(backend):180 client = AsyncClient(dispatch=MockDispatch(), backend=backend)Update README.mdTom Christiebefore adoption53a3e55
lines 181–186why, one step back181 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) == 1185186Parametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058
lines 187–188why, one step back187async def test_multiple_redirects(backend):188 client = AsyncClient(dispatch=MockDispatch(), backend=backend)Update README.mdTom Christiebefore adoption53a3e55
lines 189–192why, one step back189 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) == 20Multipart support (#90)Tom Christiebefore adoption6a44b05
lines 193–198why, one step back193 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 back199 assert len(response.history[0].history) == 0200 assert len(response.history[1].history) == 1Update README.mdTom Christiebefore adoption53a3e55
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(backend):204 client = AsyncClient(dispatch=MockDispatch(), backend=backend)Update README.mdTom Christiebefore adoption53a3e55
lines 205–208why, one step back205 with pytest.raises(TooManyRedirects):206 await client.get("https://example.org/multiple_redirects?count=21")207208Parametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058
lines 209–210why, one step back209async 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 back211 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()216217Parametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058
lines 218–219why, one step back218async def test_redirect_loop(backend):219 client = AsyncClient(dispatch=MockDispatch(), backend=backend)Update README.mdTom Christiebefore adoption53a3e55
lines 220–223why, one step back220 with pytest.raises(RedirectLoop):221 await client.get("https://example.org/redirect_loop")222223Parametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058
lines 224–225why, one step back224async def test_redirect_loop_calling_next(backend):225 client = AsyncClient(dispatch=MockDispatch(), backend=backend)Update README.mdTom Christiebefore adoption53a3e55
lines 226–232why, one step back226 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()231232Parametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058
lines 233–234why, one step back233async def test_cross_domain_redirect(backend):234 client = AsyncClient(dispatch=MockDispatch(), backend=backend)Update README.mdTom Christiebefore adoption53a3e55
lines 235–241why, one step back235 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"]240241Parametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058
lines 242–243why, one step back242async def test_same_domain_redirect(backend):243 client = AsyncClient(dispatch=MockDispatch(), backend=backend)Update README.mdTom Christiebefore adoption53a3e55
lines 244–250why, one step back244 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"249250Parametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058
line 251why, one step back251async def test_body_redirect(backend):Merge pull request #310 from encode/drop-content-length-on-get-redirectsTom Christiebefore adoption33c5fe0
lines 252–254why, one step back252 """253 A 308 redirect should preserve the request body.254 """Parametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058
line 255why, one step back255 client = AsyncClient(dispatch=MockDispatch(), backend=backend)Update README.mdTom Christiebefore adoption53a3e55
lines 256–259why, one step back256 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 back260 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 back275276Parametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058
lines 277–278why, one step back277async def test_cannot_redirect_streaming_body(backend):278 client = AsyncClient(dispatch=MockDispatch(), backend=backend)Update README.mdTom Christiebefore adoption53a3e55
lines 279–285why, one step back279 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 back286287Refactor client functionality into middleware (#268)Florimond Mancabefore adoption40e6e8c
line 288why, one step back288async def test_cross_subdomain_redirect(backend):Parametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058
line 289why, one step back289 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 back290 url = "https://example.com/cross_subdomain"291 response = await client.get(url)292 assert response.url == URL("https://www.example.org/cross_subdomain")