Skip to content
Myr

response.next()

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Tom Christie · committed by Tom Christie · parent 2b134a6 · Browse files · 5 files+53 added−17 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

5 files+53 added−17 removed

  • tests:+23 added−1 removed
  • other:+30 added−16 removed

5 files+53 added−17 removed

modified:httpcore/adapters/redirects.py+26 added-12 removed
1import functools1import typing2from urllib.parse import urljoin, urlparse34from ..config import DEFAULT_MAX_REDIRECTS
12 unmodified lines
17 def prepare_request(self, request: Request) -> None:18 self.dispatch.prepare_request(request)1920 async def send(self, request: Request, **options: typing.Any) -> Response:22 allow_redirects = options.pop("allow_redirects", True)21 allow_redirects = options.pop("allow_redirects", True) # type: bool2223 # The following will not typically be specified by the end-user developer,24 # but are included in `response.next()` calls.25 history = options.pop("history", []) # type: typing.List[Response]26 seen_urls = options.pop("seen_urls", set()) # type: typing.Set[URL]25 seen_urls.add(request.url)2728 while True:29 # We perform these checks here, so that calls to `response.next()`30 # will raise redirect errors if appropriate.31 if len(history) > self.max_redirects:32 raise TooManyRedirects()33 if request.url in seen_urls:34 raise RedirectLoop()3536 response = await self.dispatch.send(request, **options)37 response.history = list(history)38 if not response.is_redirect:39 break32 history.append(response)33 request = self.build_redirect_request(request, response)34 if not allow_redirects:4041 history.insert(0, response)42 seen_urls.add(request.url)4344 if allow_redirects:45 request = self.build_redirect_request(request, response)46 else:47 next_options = dict(options)48 next_options["seen_urls"] = seen_urls49 next_options["history"] = history38 response.next = functools.partial(self.send, request=request, **next_options)5051 async def send_next() -> Response:52 nonlocal request, response, next_options53 request = self.build_redirect_request(request, response)54 response = await self.send(request, **next_options)55 return response5657 response.next = send_next # type: ignore58 break40 if len(history) > self.max_redirects:41 raise TooManyRedirects()42 if request.url in seen_urls:43 raise RedirectLoop()44 seen_urls.add(request.url)5960 return response6162 async def close(self) -> None:
77 unmodified lines
modified:httpcore/decoders.py+2 added-3 removed
4 unmodified lines
5"""6import typing7import zlib89import httpcore.exceptions1011try:12 import brotli13except ImportError: # pragma: nocover14 brotli = None151615import httpcore.exceptions161717class Decoder:18 def decode(self, data: bytes) -> bytes:19 raise NotImplementedError() # pragma: nocover20
126 unmodified lines
modified:httpcore/exceptions.py+1 added-1 removed
76 unmodified lines
77 """78 Decoding of the response failed.79 """8081 8182class InvalidURL(Exception):83 """84 URL was missing a hostname, or was not one of HTTP/HTTPS.85 """
modified:httpcore/models.py+1 added
340 unmodified lines
341 self.body_aiter = body342343 self.request = request344 self.history = [] if history is None else list(history)345 self.next = None # typing.Optional[typing.Callable]346347 @property348 def url(self) -> typing.Optional[URL]:349 return None if self.request is None else self.request.url
52 unmodified lines
modified:tests/adapters/test_redirects.py+23 added-1 removed
109 unmodified lines
110111@pytest.mark.asyncio112async def test_disallow_redirects():113 client = RedirectAdapter(MockDispatch())114 response = await client.request("POST", "https://example.org/redirect_303", allow_redirects=False)114 response = await client.request(115 "POST", "https://example.org/redirect_303", allow_redirects=False116 )117 assert response.status_code == codes.see_other118 assert response.url == URL("https://example.org/redirect_303")119 assert len(response.history) == 0120
48 unmodified lines
169 await client.request("GET", "https://example.org/multiple_redirects?count=21")170171172@pytest.mark.asyncio173async def test_too_many_redirects_calling_next():174 client = RedirectAdapter(MockDispatch())175 url = "https://example.org/multiple_redirects?count=21"176 response = await client.request("GET", url, allow_redirects=False)177 with pytest.raises(TooManyRedirects):178 while response.is_redirect:179 response = await response.next()180181182@pytest.mark.asyncio183async def test_redirect_loop():184 client = RedirectAdapter(MockDispatch())185 with pytest.raises(RedirectLoop):186 await client.request("GET", "https://example.org/redirect_loop")187188189@pytest.mark.asyncio190async def test_redirect_loop_calling_next():191 client = RedirectAdapter(MockDispatch())192 url = "https://example.org/redirect_loop"193 response = await client.request("GET", url, allow_redirects=False)194 with pytest.raises(RedirectLoop):195 while response.is_redirect:196 response = await response.next()197198199@pytest.mark.asyncio200async def test_cross_domain_redirect():201 client = RedirectAdapter(MockDispatch())202 url = "https://example.com/cross_domain"203 headers = {"Authorization": "abc"}
38 unmodified lines