test_redirects.py
This file
Path
tests/client/test_redirects.py
485 lines · 17.9 KiB · Python
Last changed by
Increase test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067
History of this fileTake it
The walk stopped after 200 commits: lines older than 1e23855 point at it.
Warn if cert / verify / trust_env are passed to client.request() (#597)Tom Christiebefore adoption1e23855
line 1why, one step back1import jsonHttpcore interface (#804)Tom Christiebefore adoption3046e92
line 2why, one step back2import typingWarn if cert / verify / trust_env are passed to client.request() (#597)Tom Christiebefore adoption1e23855
lines 3–4why, one step back3from urllib.parse import parse_qs4Httpcore interface (#804)Tom Christiebefore adoption3046e92
line 5why, one step back5import httpcoreWarn if cert / verify / trust_env are passed to client.request() (#597)Tom Christiebefore adoption1e23855
lines 6–9why, one step back6import pytest78from httpx import (9 URL,Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84
line 10why, one step back10 AsyncClient,Increase test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067
line 11why, one step back11 Client,Improve error when redirecting with custom schemes (#1002)Josep Cugatbefore adoption093cb42
line 12why, one step back12 InvalidURL,Warn if cert / verify / trust_env are passed to client.request() (#597)Tom Christiebefore adoption1e23855
line 13why, one step back13 NotRedirectResponse,Repurpose RedirectBodyUnavailable as RequestBodyUnavailable (#690)Florimond Mancabefore adoption910aa90
line 14why, one step back14 RequestBodyUnavailable,Warn if cert / verify / trust_env are passed to client.request() (#597)Tom Christiebefore adoption1e23855
lines 15–17why, one step back15 TooManyRedirects,16 codes,17)Httpcore interface (#804)Tom Christiebefore adoption3046e92
line 18why, one step back18from httpx._content_streams import AsyncIteratorStream, ByteStream, ContentStreamWarn if cert / verify / trust_env are passed to client.request() (#597)Tom Christiebefore adoption1e23855
lines 19–20why, one step back1920Httpcore interface (#804)Tom Christiebefore adoption3046e92
lines 21–28why, one step back21def get_header_value(headers, key, default=None):22 lookup = key.encode("ascii").lower()23 for header_key, header_value in headers:24 if header_key.lower() == lookup:25 return header_value.decode("ascii")26 return default2728Increase test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067
lines 29–30why, one step back29class MockTransport:30 def _request(Warn if cert / verify / trust_env are passed to client.request() (#597)Tom Christiebefore adoption1e23855
line 31why, one step back31 self,Httpcore interface (#804)Tom Christiebefore adoption3046e92
lines 32–43why, one step back32 method: bytes,33 url: typing.Tuple[bytes, bytes, int, bytes],34 headers: typing.List[typing.Tuple[bytes, bytes]],35 stream: ContentStream,36 timeout: typing.Dict[str, typing.Optional[float]] = None,37 ) -> typing.Tuple[38 bytes, int, bytes, typing.List[typing.Tuple[bytes, bytes]], ContentStream39 ]:40 scheme, host, port, path = url41 path, _, query = path.partition(b"?")42 if path == b"/no_redirect":43 return b"HTTP/1.1", codes.OK, b"OK", [], ByteStream(b"")Warn if cert / verify / trust_env are passed to client.request() (#597)Tom Christiebefore adoption1e23855
line 44why, one step back44Httpcore interface (#804)Tom Christiebefore adoption3046e92
line 45why, one step back45 elif path == b"/redirect_301":Fix for streaming a redirect response body with allow_redirects=False (#766)Tom Christiebefore adoption5a63540
lines 46–49why, one step back4647 async def body():48 yield b"<a href='https://example.org/'>here</a>"49Warn if cert / verify / trust_env are passed to client.request() (#597)Tom Christiebefore adoption1e23855
line 50why, one step back50 status_code = codes.MOVED_PERMANENTLYHttpcore interface (#804)Tom Christiebefore adoption3046e92
line 51why, one step back51 headers = [(b"location", b"https://example.org/")]Fix for streaming a redirect response body with allow_redirects=False (#766)Tom Christiebefore adoption5a63540
line 52why, one step back52 stream = AsyncIteratorStream(aiterator=body())Httpcore interface (#804)Tom Christiebefore adoption3046e92
line 53why, one step back53 return b"HTTP/1.1", status_code, b"Moved Permanently", headers, streamWarn if cert / verify / trust_env are passed to client.request() (#597)Tom Christiebefore adoption1e23855
line 54why, one step back54Httpcore interface (#804)Tom Christiebefore adoption3046e92
line 55why, one step back55 elif path == b"/redirect_302":Warn if cert / verify / trust_env are passed to client.request() (#597)Tom Christiebefore adoption1e23855
line 56why, one step back56 status_code = codes.FOUNDHttpcore interface (#804)Tom Christiebefore adoption3046e92
lines 57–58why, one step back57 headers = [(b"location", b"https://example.org/")]58 return b"HTTP/1.1", status_code, b"Found", headers, ByteStream(b"")Warn if cert / verify / trust_env are passed to client.request() (#597)Tom Christiebefore adoption1e23855
line 59why, one step back59Httpcore interface (#804)Tom Christiebefore adoption3046e92
line 60why, one step back60 elif path == b"/redirect_303":Warn if cert / verify / trust_env are passed to client.request() (#597)Tom Christiebefore adoption1e23855
line 61why, one step back61 status_code = codes.SEE_OTHERHttpcore interface (#804)Tom Christiebefore adoption3046e92
lines 62–63why, one step back62 headers = [(b"location", b"https://example.org/")]63 return b"HTTP/1.1", status_code, b"See Other", headers, ByteStream(b"")Warn if cert / verify / trust_env are passed to client.request() (#597)Tom Christiebefore adoption1e23855
line 64why, one step back64Httpcore interface (#804)Tom Christiebefore adoption3046e92
lines 65–68why, one step back65 elif path == b"/relative_redirect":66 status_code = codes.SEE_OTHER67 headers = [(b"location", b"/")]68 return b"HTTP/1.1", status_code, b"See Other", headers, ByteStream(b"")Warn if cert / verify / trust_env are passed to client.request() (#597)Tom Christiebefore adoption1e23855
line 69why, one step back69Httpcore interface (#804)Tom Christiebefore adoption3046e92
lines 70–73why, one step back70 elif path == b"/malformed_redirect":71 status_code = codes.SEE_OTHER72 headers = [(b"location", b"https://:443/")]73 return b"HTTP/1.1", status_code, b"See Other", headers, ByteStream(b"")Handle redirect with malformed Location headers missing host. (#774)Tom Christiebefore adoption1f8fb28
line 74why, one step back74Httpcore interface (#804)Tom Christiebefore adoption3046e92
lines 75–78why, one step back75 elif path == b"/no_scheme_redirect":76 status_code = codes.SEE_OTHER77 headers = [(b"location", b"//example.org/")]78 return b"HTTP/1.1", status_code, b"See Other", headers, ByteStream(b"")Warn if cert / verify / trust_env are passed to client.request() (#597)Tom Christiebefore adoption1e23855
line 79why, one step back79Httpcore interface (#804)Tom Christiebefore adoption3046e92
lines 80–81why, one step back80 elif path == b"/multiple_redirects":81 params = parse_qs(query.decode("ascii"))Warn if cert / verify / trust_env are passed to client.request() (#597)Tom Christiebefore adoption1e23855
lines 82–84why, one step back82 count = int(params.get("count", "0")[0])83 redirect_count = count - 184 code = codes.SEE_OTHER if count else codes.OKHttpcore interface (#804)Tom Christiebefore adoption3046e92
lines 85–86why, one step back85 phrase = b"See Other" if count else b"OK"86 location = b"/multiple_redirects"Warn if cert / verify / trust_env are passed to client.request() (#597)Tom Christiebefore adoption1e23855
line 87why, one step back87 if redirect_count:Httpcore interface (#804)Tom Christiebefore adoption3046e92
lines 88–90why, one step back88 location += b"?count=" + str(redirect_count).encode("ascii")89 headers = [(b"location", location)] if count else []90 return b"HTTP/1.1", code, phrase, headers, ByteStream(b"")Warn if cert / verify / trust_env are passed to client.request() (#597)Tom Christiebefore adoption1e23855
line 91why, one step back91Httpcore interface (#804)Tom Christiebefore adoption3046e92
lines 92–95why, one step back92 if path == b"/redirect_loop":93 code = codes.SEE_OTHER94 headers = [(b"location", b"/redirect_loop")]95 return b"HTTP/1.1", code, b"See Other", headers, ByteStream(b"")Warn if cert / verify / trust_env are passed to client.request() (#597)Tom Christiebefore adoption1e23855
line 96why, one step back96Httpcore interface (#804)Tom Christiebefore adoption3046e92
lines 97–100why, one step back97 elif path == b"/cross_domain":98 code = codes.SEE_OTHER99 headers = [(b"location", b"https://example.org/cross_domain_target")]100 return b"HTTP/1.1", code, b"See Other", headers, ByteStream(b"")Warn if cert / verify / trust_env are passed to client.request() (#597)Tom Christiebefore adoption1e23855
line 101why, one step back101Httpcore interface (#804)Tom Christiebefore adoption3046e92
lines 102–107why, one step back102 elif path == b"/cross_domain_target":103 headers_dict = dict(104 [(key.decode("ascii"), value.decode("ascii")) for key, value in headers]105 )106 content = ByteStream(json.dumps({"headers": headers_dict}).encode())107 return b"HTTP/1.1", 200, b"OK", [], contentWarn if cert / verify / trust_env are passed to client.request() (#597)Tom Christiebefore adoption1e23855
line 108why, one step back108Httpcore interface (#804)Tom Christiebefore adoption3046e92
lines 109–112why, one step back109 elif path == b"/redirect_body":110 code = codes.PERMANENT_REDIRECT111 headers = [(b"location", b"/redirect_body_target")]112 return b"HTTP/1.1", code, b"Permanent Redirect", headers, ByteStream(b"")Warn if cert / verify / trust_env are passed to client.request() (#597)Tom Christiebefore adoption1e23855
line 113why, one step back113Httpcore interface (#804)Tom Christiebefore adoption3046e92
lines 114–117why, one step back114 elif path == b"/redirect_no_body":115 code = codes.SEE_OTHER116 headers = [(b"location", b"/redirect_body_target")]117 return b"HTTP/1.1", code, b"See Other", headers, ByteStream(b"")Warn if cert / verify / trust_env are passed to client.request() (#597)Tom Christiebefore adoption1e23855
line 118why, one step back118Httpcore interface (#804)Tom Christiebefore adoption3046e92
line 119why, one step back119 elif path == b"/redirect_body_target":Increase test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067
line 120why, one step back120 content = b"".join(stream)Httpcore interface (#804)Tom Christiebefore adoption3046e92
lines 121–127why, one step back121 headers_dict = dict(122 [(key.decode("ascii"), value.decode("ascii")) for key, value in headers]123 )124 body = ByteStream(125 json.dumps({"body": content.decode(), "headers": headers_dict}).encode()126 )127 return b"HTTP/1.1", 200, b"OK", [], bodyWarn if cert / verify / trust_env are passed to client.request() (#597)Tom Christiebefore adoption1e23855
line 128why, one step back128Httpcore interface (#804)Tom Christiebefore adoption3046e92
lines 129–138why, one step back129 elif path == b"/cross_subdomain":130 host = get_header_value(headers, "host")131 if host != "www.example.org":132 headers = [(b"location", b"https://www.example.org/cross_subdomain")]133 return (134 b"HTTP/1.1",135 codes.PERMANENT_REDIRECT,136 b"Permanent Redirect",137 headers,138 ByteStream(b""),Warn if cert / verify / trust_env are passed to client.request() (#597)Tom Christiebefore adoption1e23855
lines 139–140why, one step back139 )140 else:Httpcore interface (#804)Tom Christiebefore adoption3046e92
line 141why, one step back141 return b"HTTP/1.1", 200, b"OK", [], ByteStream(b"Hello, world!")Warn if cert / verify / trust_env are passed to client.request() (#597)Tom Christiebefore adoption1e23855
line 142why, one step back142Improve error when redirecting with custom schemes (#1002)Josep Cugatbefore adoption093cb42
lines 143–153why, one step back143 elif path == b"/redirect_custom_scheme":144 status_code = codes.MOVED_PERMANENTLY145 headers = [(b"location", b"market://details?id=42")]146 return (147 b"HTTP/1.1",148 status_code,149 b"Moved Permanently",150 headers,151 ByteStream(b""),152 )153Httpcore interface (#804)Tom Christiebefore adoption3046e92
line 154why, one step back154 return b"HTTP/1.1", 200, b"OK", [], ByteStream(b"Hello, world!")Warn if cert / verify / trust_env are passed to client.request() (#597)Tom Christiebefore adoption1e23855
lines 155–156why, one step back155156Increase test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067
lines 157–174why, one step back157class AsyncMockTransport(MockTransport, httpcore.AsyncHTTPTransport):158 async def request(159 self, *args, **kwargs160 ) -> typing.Tuple[161 bytes, int, bytes, typing.List[typing.Tuple[bytes, bytes]], ContentStream162 ]:163 return self._request(*args, **kwargs)164165166class SyncMockTransport(MockTransport, httpcore.SyncHTTPTransport):167 def request(168 self, *args, **kwargs169 ) -> typing.Tuple[170 bytes, int, bytes, typing.List[typing.Tuple[bytes, bytes]], ContentStream171 ]:172 return self._request(*args, **kwargs)173174Clean up 'backend' fixture (#664)Florimond Mancabefore adoptiond0427be
lines 175–176why, one step back175@pytest.mark.usefixtures("async_environment")176async def test_no_redirect():Increase test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067
line 177why, one step back177 client = AsyncClient(transport=AsyncMockTransport())Warn if cert / verify / trust_env are passed to client.request() (#597)Tom Christiebefore adoption1e23855
lines 178–181why, one step back178 url = "https://example.com/no_redirect"179 response = await client.get(url)180 assert response.status_code == 200181 with pytest.raises(NotRedirectResponse):Rename 'next' to 'anext' on Response (#676)Florimond Mancabefore adoption3462999
line 182why, one step back182 await response.anext()Warn if cert / verify / trust_env are passed to client.request() (#597)Tom Christiebefore adoption1e23855
lines 183–184why, one step back183184Clean up 'backend' fixture (#664)Florimond Mancabefore adoptiond0427be
lines 185–186why, one step back185@pytest.mark.usefixtures("async_environment")186async def test_redirect_301():Increase test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067
line 187why, one step back187 client = AsyncClient(transport=AsyncMockTransport())Warn if cert / verify / trust_env are passed to client.request() (#597)Tom Christiebefore adoption1e23855
lines 188–193why, one step back188 response = await client.post("https://example.org/redirect_301")189 assert response.status_code == codes.OK190 assert response.url == URL("https://example.org/")191 assert len(response.history) == 1192193Clean up 'backend' fixture (#664)Florimond Mancabefore adoptiond0427be
lines 194–195why, one step back194@pytest.mark.usefixtures("async_environment")195async def test_redirect_302():Increase test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067
line 196why, one step back196 client = AsyncClient(transport=AsyncMockTransport())Warn if cert / verify / trust_env are passed to client.request() (#597)Tom Christiebefore adoption1e23855
lines 197–202why, one step back197 response = await client.post("https://example.org/redirect_302")198 assert response.status_code == codes.OK199 assert response.url == URL("https://example.org/")200 assert len(response.history) == 1201202Clean up 'backend' fixture (#664)Florimond Mancabefore adoptiond0427be
lines 203–204why, one step back203@pytest.mark.usefixtures("async_environment")204async def test_redirect_303():Increase test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067
line 205why, one step back205 client = AsyncClient(transport=AsyncMockTransport())Warn if cert / verify / trust_env are passed to client.request() (#597)Tom Christiebefore adoption1e23855
lines 206–211why, one step back206 response = await client.get("https://example.org/redirect_303")207 assert response.status_code == codes.OK208 assert response.url == URL("https://example.org/")209 assert len(response.history) == 1210211Clean up 'backend' fixture (#664)Florimond Mancabefore adoptiond0427be
lines 212–213why, one step back212@pytest.mark.usefixtures("async_environment")213async def test_disallow_redirects():Increase test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067
line 214why, one step back214 client = AsyncClient(transport=AsyncMockTransport())Warn if cert / verify / trust_env are passed to client.request() (#597)Tom Christiebefore adoption1e23855
lines 215–222why, one step back215 response = await client.post(216 "https://example.org/redirect_303", allow_redirects=False217 )218 assert response.status_code == codes.SEE_OTHER219 assert response.url == URL("https://example.org/redirect_303")220 assert response.is_redirect is True221 assert len(response.history) == 0222Rename 'next' to 'anext' on Response (#676)Florimond Mancabefore adoption3462999
line 223why, one step back223 response = await response.anext()Warn if cert / verify / trust_env are passed to client.request() (#597)Tom Christiebefore adoption1e23855
lines 224–229why, one step back224 assert response.status_code == codes.OK225 assert response.url == URL("https://example.org/")226 assert response.is_redirect is False227 assert len(response.history) == 1228229Clean up 'backend' fixture (#664)Florimond Mancabefore adoptiond0427be
lines 230–231why, one step back230@pytest.mark.usefixtures("async_environment")231async def test_relative_redirect():Increase test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067
line 232why, one step back232 client = AsyncClient(transport=AsyncMockTransport())Warn if cert / verify / trust_env are passed to client.request() (#597)Tom Christiebefore adoption1e23855
line 233why, one step back233 response = await client.get("https://example.org/relative_redirect")Handle redirect with malformed Location headers missing host. (#774)Tom Christiebefore adoption1f8fb28
lines 234–241why, one step back234 assert response.status_code == codes.OK235 assert response.url == URL("https://example.org/")236 assert len(response.history) == 1237238239@pytest.mark.usefixtures("async_environment")240async def test_malformed_redirect():241 # https://github.com/encode/httpx/issues/771Increase test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067
line 242why, one step back242 client = AsyncClient(transport=AsyncMockTransport())Handle redirect with malformed Location headers missing host. (#774)Tom Christiebefore adoption1f8fb28
line 243why, one step back243 response = await client.get("http://example.org/malformed_redirect")Warn if cert / verify / trust_env are passed to client.request() (#597)Tom Christiebefore adoption1e23855
lines 244–248why, one step back244 assert response.status_code == codes.OK245 assert response.url == URL("https://example.org/")246 assert len(response.history) == 1247248Clean up 'backend' fixture (#664)Florimond Mancabefore adoptiond0427be
lines 249–250why, one step back249@pytest.mark.usefixtures("async_environment")250async def test_no_scheme_redirect():Increase test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067
line 251why, one step back251 client = AsyncClient(transport=AsyncMockTransport())Warn if cert / verify / trust_env are passed to client.request() (#597)Tom Christiebefore adoption1e23855
lines 252–257why, one step back252 response = await client.get("https://example.org/no_scheme_redirect")253 assert response.status_code == codes.OK254 assert response.url == URL("https://example.org/")255 assert len(response.history) == 1256257Clean up 'backend' fixture (#664)Florimond Mancabefore adoptiond0427be
lines 258–259why, one step back258@pytest.mark.usefixtures("async_environment")259async def test_fragment_redirect():Increase test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067
line 260why, one step back260 client = AsyncClient(transport=AsyncMockTransport())Warn if cert / verify / trust_env are passed to client.request() (#597)Tom Christiebefore adoption1e23855
lines 261–266why, one step back261 response = await client.get("https://example.org/relative_redirect#fragment")262 assert response.status_code == codes.OK263 assert response.url == URL("https://example.org/#fragment")264 assert len(response.history) == 1265266Clean up 'backend' fixture (#664)Florimond Mancabefore adoptiond0427be
lines 267–268why, one step back267@pytest.mark.usefixtures("async_environment")268async def test_multiple_redirects():Increase test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067
line 269why, one step back269 client = AsyncClient(transport=AsyncMockTransport())Warn if cert / verify / trust_env are passed to client.request() (#597)Tom Christiebefore adoption1e23855
lines 270–283why, one step back270 response = await client.get("https://example.org/multiple_redirects?count=20")271 assert response.status_code == codes.OK272 assert response.url == URL("https://example.org/multiple_redirects")273 assert len(response.history) == 20274 assert response.history[0].url == URL(275 "https://example.org/multiple_redirects?count=20"276 )277 assert response.history[1].url == URL(278 "https://example.org/multiple_redirects?count=19"279 )280 assert len(response.history[0].history) == 0281 assert len(response.history[1].history) == 1282283Clean up 'backend' fixture (#664)Florimond Mancabefore adoptiond0427be
line 284why, one step back284@pytest.mark.usefixtures("async_environment")Increase test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067
lines 285–286why, one step back285async def test_async_too_many_redirects():286 client = AsyncClient(transport=AsyncMockTransport())Warn if cert / verify / trust_env are passed to client.request() (#597)Tom Christiebefore adoption1e23855
lines 287–290why, one step back287 with pytest.raises(TooManyRedirects):288 await client.get("https://example.org/multiple_redirects?count=21")289290Clean up 'backend' fixture (#664)Florimond Mancabefore adoptiond0427be
line 291why, one step back291@pytest.mark.usefixtures("async_environment")Increase test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067
lines 292–293why, one step back292async def test_async_too_many_redirects_calling_next():293 client = AsyncClient(transport=AsyncMockTransport())Warn if cert / verify / trust_env are passed to client.request() (#597)Tom Christiebefore adoption1e23855
lines 294–297why, one step back294 url = "https://example.org/multiple_redirects?count=21"295 response = await client.get(url, allow_redirects=False)296 with pytest.raises(TooManyRedirects):297 while response.is_redirect:Rename 'next' to 'anext' on Response (#676)Florimond Mancabefore adoption3462999
line 298why, one step back298 response = await response.anext()Warn if cert / verify / trust_env are passed to client.request() (#597)Tom Christiebefore adoption1e23855
lines 299–300why, one step back299300Increase test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067
lines 301–315why, one step back301def test_sync_too_many_redirects():302 client = Client(transport=SyncMockTransport())303 with pytest.raises(TooManyRedirects):304 client.get("https://example.org/multiple_redirects?count=21")305306307def test_sync_too_many_redirects_calling_next():308 client = Client(transport=SyncMockTransport())309 url = "https://example.org/multiple_redirects?count=21"310 response = client.get(url, allow_redirects=False)311 with pytest.raises(TooManyRedirects):312 while response.is_redirect:313 response = response.call_next()314315Clean up 'backend' fixture (#664)Florimond Mancabefore adoptiond0427be
lines 316–317why, one step back316@pytest.mark.usefixtures("async_environment")317async def test_redirect_loop():Increase test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067
line 318why, one step back318 client = AsyncClient(transport=AsyncMockTransport())Drop RedirectLoop exception (#819)Piotr Staroszczykbefore adoptionefe9f61
line 319why, one step back319 with pytest.raises(TooManyRedirects):Warn if cert / verify / trust_env are passed to client.request() (#597)Tom Christiebefore adoption1e23855
lines 320–322why, one step back320 await client.get("https://example.org/redirect_loop")321322Clean up 'backend' fixture (#664)Florimond Mancabefore adoptiond0427be
lines 323–324why, one step back323@pytest.mark.usefixtures("async_environment")324async def test_cross_domain_redirect():Increase test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067
line 325why, one step back325 client = AsyncClient(transport=AsyncMockTransport())Warn if cert / verify / trust_env are passed to client.request() (#597)Tom Christiebefore adoption1e23855
lines 326–332why, one step back326 url = "https://example.com/cross_domain"327 headers = {"Authorization": "abc"}328 response = await client.get(url, headers=headers)329 assert response.url == URL("https://example.org/cross_domain_target")330 assert "authorization" not in response.json()["headers"]331332Clean up 'backend' fixture (#664)Florimond Mancabefore adoptiond0427be
lines 333–334why, one step back333@pytest.mark.usefixtures("async_environment")334async def test_same_domain_redirect():Increase test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067
line 335why, one step back335 client = AsyncClient(transport=AsyncMockTransport())Warn if cert / verify / trust_env are passed to client.request() (#597)Tom Christiebefore adoption1e23855
lines 336–342why, one step back336 url = "https://example.org/cross_domain"337 headers = {"Authorization": "abc"}338 response = await client.get(url, headers=headers)339 assert response.url == URL("https://example.org/cross_domain_target")340 assert response.json()["headers"]["authorization"] == "abc"341342Clean up 'backend' fixture (#664)Florimond Mancabefore adoptiond0427be
lines 343–344why, one step back343@pytest.mark.usefixtures("async_environment")344async def test_body_redirect():Warn if cert / verify / trust_env are passed to client.request() (#597)Tom Christiebefore adoption1e23855
lines 345–347why, one step back345 """346 A 308 redirect should preserve the request body.347 """Increase test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067
line 348why, one step back348 client = AsyncClient(transport=AsyncMockTransport())Warn if cert / verify / trust_env are passed to client.request() (#597)Tom Christiebefore adoption1e23855
lines 349–353why, one step back349 url = "https://example.org/redirect_body"350 data = b"Example request body"351 response = await client.post(url, data=data)352 assert response.url == URL("https://example.org/redirect_body_target")353 assert response.json()["body"] == "Example request body"Revert "Add content-length header for empty bytestream (#866)" (#898)Tom Christiebefore adoption631ba97
line 354why, one step back354 assert "content-length" in response.json()["headers"]Warn if cert / verify / trust_env are passed to client.request() (#597)Tom Christiebefore adoption1e23855
lines 355–356why, one step back355356Clean up 'backend' fixture (#664)Florimond Mancabefore adoptiond0427be
lines 357–358why, one step back357@pytest.mark.usefixtures("async_environment")358async def test_no_body_redirect():Warn if cert / verify / trust_env are passed to client.request() (#597)Tom Christiebefore adoption1e23855
lines 359–361why, one step back359 """360 A 303 redirect should remove the request body.361 """Increase test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067
line 362why, one step back362 client = AsyncClient(transport=AsyncMockTransport())Warn if cert / verify / trust_env are passed to client.request() (#597)Tom Christiebefore adoption1e23855
lines 363–367why, one step back363 url = "https://example.org/redirect_no_body"364 data = b"Example request body"365 response = await client.post(url, data=data)366 assert response.url == URL("https://example.org/redirect_body_target")367 assert response.json()["body"] == ""Revert "Add content-length header for empty bytestream (#866)" (#898)Tom Christiebefore adoption631ba97
line 368why, one step back368 assert "content-length" not in response.json()["headers"]Fix for streaming a redirect response body with allow_redirects=False (#766)Tom Christiebefore adoption5a63540
lines 369–372why, one step back369370371@pytest.mark.usefixtures("async_environment")372async def test_can_stream_if_no_redirect():Increase test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067
line 373why, one step back373 client = AsyncClient(transport=AsyncMockTransport())Fix for streaming a redirect response body with allow_redirects=False (#766)Tom Christiebefore adoption5a63540
lines 374–378why, one step back374 url = "https://example.org/redirect_301"375 async with client.stream("GET", url, allow_redirects=False) as response:376 assert not response.is_closed377 assert response.status_code == codes.MOVED_PERMANENTLY378 assert response.headers["location"] == "https://example.org/"Warn if cert / verify / trust_env are passed to client.request() (#597)Tom Christiebefore adoption1e23855
lines 379–380why, one step back379380Clean up 'backend' fixture (#664)Florimond Mancabefore adoptiond0427be
lines 381–382why, one step back381@pytest.mark.usefixtures("async_environment")382async def test_cannot_redirect_streaming_body():Increase test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067
line 383why, one step back383 client = AsyncClient(transport=AsyncMockTransport())Warn if cert / verify / trust_env are passed to client.request() (#597)Tom Christiebefore adoption1e23855
lines 384–386why, one step back384 url = "https://example.org/redirect_body"385386 async def streaming_body():Increase test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067
line 387why, one step back387 yield b"Example request body" # pragma: nocoverWarn if cert / verify / trust_env are passed to client.request() (#597)Tom Christiebefore adoption1e23855
line 388why, one step back388Repurpose RedirectBodyUnavailable as RequestBodyUnavailable (#690)Florimond Mancabefore adoption910aa90
line 389why, one step back389 with pytest.raises(RequestBodyUnavailable):Warn if cert / verify / trust_env are passed to client.request() (#597)Tom Christiebefore adoption1e23855
lines 390–392why, one step back390 await client.post(url, data=streaming_body())391392Clean up 'backend' fixture (#664)Florimond Mancabefore adoptiond0427be
lines 393–394why, one step back393@pytest.mark.usefixtures("async_environment")394async def test_cross_subdomain_redirect():Increase test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067
line 395why, one step back395 client = AsyncClient(transport=AsyncMockTransport())Warn if cert / verify / trust_env are passed to client.request() (#597)Tom Christiebefore adoption1e23855
lines 396–400why, one step back396 url = "https://example.com/cross_subdomain"397 response = await client.get(url)398 assert response.url == URL("https://www.example.org/cross_subdomain")399400Replace remaining occurrences of dispatch with transport (#1010)Josep Cugatbefore adoption89a8100
line 401why, one step back401class MockCookieTransport(httpcore.AsyncHTTPTransport):Httpcore interface (#804)Tom Christiebefore adoption3046e92
line 402why, one step back402 async def request(Warn if cert / verify / trust_env are passed to client.request() (#597)Tom Christiebefore adoption1e23855
line 403why, one step back403 self,Httpcore interface (#804)Tom Christiebefore adoption3046e92
lines 404–415why, one step back404 method: bytes,405 url: typing.Tuple[bytes, bytes, int, bytes],406 headers: typing.List[typing.Tuple[bytes, bytes]],407 stream: ContentStream,408 timeout: typing.Dict[str, typing.Optional[float]] = None,409 ) -> typing.Tuple[410 bytes, int, bytes, typing.List[typing.Tuple[bytes, bytes]], ContentStream411 ]:412 scheme, host, port, path = url413 if path == b"/":414 cookie = get_header_value(headers, "Cookie")415 if cookie is not None:Warn if cert / verify / trust_env are passed to client.request() (#597)Tom Christiebefore adoption1e23855
lines 416–418why, one step back416 content = b"Logged in"417 else:418 content = b"Not logged in"Httpcore interface (#804)Tom Christiebefore adoption3046e92
line 419why, one step back419 return b"HTTP/1.1", 200, b"OK", [], ByteStream(content)Warn if cert / verify / trust_env are passed to client.request() (#597)Tom Christiebefore adoption1e23855
line 420why, one step back420Httpcore interface (#804)Tom Christiebefore adoption3046e92
line 421why, one step back421 elif path == b"/login":Warn if cert / verify / trust_env are passed to client.request() (#597)Tom Christiebefore adoption1e23855
line 422why, one step back422 status_code = codes.SEE_OTHERHttpcore interface (#804)Tom Christiebefore adoption3046e92
lines 423–430why, one step back423 headers = [424 (b"location", b"/"),425 (426 b"set-cookie",427 (428 b"session=eyJ1c2VybmFtZSI6ICJ0b21; path=/; Max-Age=1209600; "429 b"httponly; samesite=lax"430 ),Warn if cert / verify / trust_env are passed to client.request() (#597)Tom Christiebefore adoption1e23855
line 431why, one step back431 ),Httpcore interface (#804)Tom Christiebefore adoption3046e92
lines 432–433why, one step back432 ]433 return b"HTTP/1.1", status_code, b"See Other", headers, ByteStream(b"")Warn if cert / verify / trust_env are passed to client.request() (#597)Tom Christiebefore adoption1e23855
line 434why, one step back434Httpcore interface (#804)Tom Christiebefore adoption3046e92
line 435why, one step back435 elif path == b"/logout":Warn if cert / verify / trust_env are passed to client.request() (#597)Tom Christiebefore adoption1e23855
line 436why, one step back436 status_code = codes.SEE_OTHERHttpcore interface (#804)Tom Christiebefore adoption3046e92
lines 437–444why, one step back437 headers = [438 (b"location", b"/"),439 (440 b"set-cookie",441 (442 b"session=null; path=/; expires=Thu, 01 Jan 1970 00:00:00 GMT; "443 b"httponly; samesite=lax"444 ),Warn if cert / verify / trust_env are passed to client.request() (#597)Tom Christiebefore adoption1e23855
line 445why, one step back445 ),Httpcore interface (#804)Tom Christiebefore adoption3046e92
lines 446–447why, one step back446 ]447 return b"HTTP/1.1", status_code, b"See Other", headers, ByteStream(b"")Warn if cert / verify / trust_env are passed to client.request() (#597)Tom Christiebefore adoption1e23855
lines 448–449why, one step back448449Clean up 'backend' fixture (#664)Florimond Mancabefore adoptiond0427be
lines 450–451why, one step back450@pytest.mark.usefixtures("async_environment")451async def test_redirect_cookie_behavior():Replace remaining occurrences of dispatch with transport (#1010)Josep Cugatbefore adoption89a8100
line 452why, one step back452 client = AsyncClient(transport=MockCookieTransport())Warn if cert / verify / trust_env are passed to client.request() (#597)Tom Christiebefore adoption1e23855
lines 453–477why, one step back453454 # The client is not logged in.455 response = await client.get("https://example.com/")456 assert response.url == "https://example.com/"457 assert response.text == "Not logged in"458459 # Login redirects to the homepage, setting a session cookie.460 response = await client.post("https://example.com/login")461 assert response.url == "https://example.com/"462 assert response.text == "Logged in"463464 # The client is logged in.465 response = await client.get("https://example.com/")466 assert response.url == "https://example.com/"467 assert response.text == "Logged in"468469 # Logout redirects to the homepage, expiring the session cookie.470 response = await client.post("https://example.com/logout")471 assert response.url == "https://example.com/"472 assert response.text == "Not logged in"473474 # The client is not logged in.475 response = await client.get("https://example.com/")476 assert response.url == "https://example.com/"477 assert response.text == "Not logged in"Improve error when redirecting with custom schemes (#1002)Josep Cugatbefore adoption093cb42
lines 478–481why, one step back478479480@pytest.mark.usefixtures("async_environment")481async def test_redirect_custom_scheme():Increase test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067
line 482why, one step back482 client = AsyncClient(transport=AsyncMockTransport())Improve error when redirecting with custom schemes (#1002)Josep Cugatbefore adoption093cb42
lines 483–485why, one step back483 with pytest.raises(InvalidURL) as e:484 await client.post("https://example.org/redirect_custom_scheme")485 assert str(e.value) == 'Scheme "market" not supported.'