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
Provenance
- Commit
a464627- Tree
ad37caa- Author
Florimond Manca <florimond.manca@gmail.com>- Committer
Seth Michael Larson <sethmichaellarson@gmail.com>- Parent
994403b
5 files changed+35 added−4 removed
modified:httpx/__init__.py+2 added
25 unmodified lines26 ConnectTimeout,27 CookieConflict,28 DecodingError,29 InvalidURL,30+ NotRedirectResponse,31 PoolTimeout,32 ProtocolError,33 ReadTimeout,34 RedirectBodyUnavailable,57 unmodified lines92 "ConnectTimeout",93 "CookieConflict",94 "DecodingError",95 "InvalidURL",96+ "NotRedirectResponse",97 "PoolTimeout",98 "ProtocolError",99 "ReadTimeout",100 "RedirectBodyUnavailable",36 unmodified linesmodified:httpx/client.py+2 added-2 removed
250 unmodified lines251 await response.close()252 253 if response.is_redirect:254 255- 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 lines265 history=history,266 )267 return response268 269- response.next = send_next # type: ignore269+ response.call_next = call_next # type: ignore270 271 return response272 273 def build_redirect_request(735 unmodified linesmodified:httpx/exceptions.py+6 added
94 unmodified lines95 Infinite redirect loop.96 """97 98 99+class NotRedirectResponse(RedirectError):100+ """101+ Response was not a redirect response.102+ """103+104+105 # Stream exceptions...106 107 108 class StreamError(HTTPError):42 unmodified linesmodified:httpx/models.py+11 added-1 removed
21 unmodified lines22 from .exceptions import (23 CookieConflict,24 HTTPError,25 InvalidURL,26+ NotRedirectResponse,27 ResponseClosed,28 ResponseNotRead,29 StreamConsumed,30 )674 unmodified lines705 self.headers = Headers(headers)706 707 self.request = request708 self.on_close = on_close708- self.next: typing.Optional[typing.Callable] = None709+ self.call_next: typing.Optional[typing.Callable] = None710 711 @property712 def reason_phrase(self) -> str:713 return StatusCode.get_reason_phrase(self.status_code)236 unmodified lines950 async for part in self._raw_stream:951 yield part952 await self.close()953 954+ 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()962+963 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 linesmodified:tests/client/test_redirects.py+14 added-1 removed
8 unmodified lines9 AsyncDispatcher,10 AsyncRequest,11 AsyncResponse,12 CertTypes,13+ NotRedirectResponse,14 RedirectBodyUnavailable,15 RedirectLoop,16 TimeoutTypes,17 TooManyRedirects,9 unmodified lines27 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)33+34+ 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 lines103 104 return AsyncResponse(codes.OK, content=b"Hello, world!", request=request)105 106 107+async 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()114+115+116 async 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.OK150 unmodified lines