Skip to content
Myr

Tighten up response.next() (#297)

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

authored by Florimond Manca · committed by Seth Michael Larson · parent 994403b · Browse files · 5 files+35 added−4 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+35 added−4 removed

  • tests:+14 added−1 removed
  • other:+21 added−3 removed

5 files+35 added−4 removed

modified:httpx/__init__.py+2 added
25 unmodified lines
26 ConnectTimeout,27 CookieConflict,28 DecodingError,29 InvalidURL,30 NotRedirectResponse,31 PoolTimeout,32 ProtocolError,33 ReadTimeout,34 RedirectBodyUnavailable,
57 unmodified lines
92 "ConnectTimeout",93 "CookieConflict",94 "DecodingError",95 "InvalidURL",96 "NotRedirectResponse",97 "PoolTimeout",98 "ProtocolError",99 "ReadTimeout",100 "RedirectBodyUnavailable",
36 unmodified lines
modified:httpx/client.py+2 added-2 removed
250 unmodified lines
251 await response.close()252253 if response.is_redirect:254255 async def send_next() -> AsyncResponse:255 async def call_next() -> AsyncResponse:256 nonlocal request, response, verify, cert257 nonlocal allow_redirects, timeout, history258 request = self.build_redirect_request(request, response)259 response = await self.send_handling_redirects(
5 unmodified lines
265 history=history,266 )267 return response268269 response.next = send_next # type: ignore269 response.call_next = call_next # type: ignore270271 return response272273 def build_redirect_request(
735 unmodified lines
modified:httpx/exceptions.py+6 added
94 unmodified lines
95 Infinite redirect loop.96 """979899class NotRedirectResponse(RedirectError):100 """101 Response was not a redirect response.102 """103104105# Stream exceptions...106107108class StreamError(HTTPError):
42 unmodified lines
modified:httpx/models.py+11 added-1 removed
21 unmodified lines
22from .exceptions import (23 CookieConflict,24 HTTPError,25 InvalidURL,26 NotRedirectResponse,27 ResponseClosed,28 ResponseNotRead,29 StreamConsumed,30)
674 unmodified lines
705 self.headers = Headers(headers)706707 self.request = request708 self.on_close = on_close708 self.next: typing.Optional[typing.Callable] = None709 self.call_next: typing.Optional[typing.Callable] = None710711 @property712 def reason_phrase(self) -> str:713 return StatusCode.get_reason_phrase(self.status_code)
236 unmodified lines
950 async for part in self._raw_stream:951 yield part952 await self.close()953954 async def next(self) -> "AsyncResponse":955 """956 Get the next response from a redirect response.957 """958 if not self.is_redirect:959 raise NotRedirectResponse()960 assert self.call_next is not None961 return await self.call_next()962963 async def close(self) -> None:964 """965 Close the response and release the connection.966 Automatically called if the response body is read to completion.
270 unmodified lines
modified:tests/client/test_redirects.py+14 added-1 removed
8 unmodified lines
9 AsyncDispatcher,10 AsyncRequest,11 AsyncResponse,12 CertTypes,13 NotRedirectResponse,14 RedirectBodyUnavailable,15 RedirectLoop,16 TimeoutTypes,17 TooManyRedirects,
9 unmodified lines
27 verify: VerifyTypes = None,28 cert: CertTypes = None,29 timeout: TimeoutTypes = None,30 ) -> AsyncResponse:30 if request.url.path == "/redirect_301":31 if request.url.path == "/no_redirect":32 return AsyncResponse(codes.OK, request=request)3334 elif request.url.path == "/redirect_301":35 status_code = codes.MOVED_PERMANENTLY36 headers = {"location": "https://example.org/"}37 return AsyncResponse(status_code, headers=headers, request=request)38
64 unmodified lines
103104 return AsyncResponse(codes.OK, content=b"Hello, world!", request=request)105106107async 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()114115116async def test_redirect_301(backend):117 client = AsyncClient(dispatch=MockDispatch(), backend=backend)118 response = await client.post("https://example.org/redirect_301")119 assert response.status_code == codes.OK
150 unmodified lines