Skip to content
Myr

Raise an exception in DigestAuth if non-replayable request is passed (#685)

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

* DigestAuth will raise exception if non-replayble request is passed #670 * Added new exception RequestBodyUnavailble exception to raise non replayable request error #670 * Changed RedirectBodyUnavailable exception to RequestBodyUnavailble to raise non-replayable exception #670 * fixed class declaration * Added RequestBodyUnavailable exception. Imported it in module level * Added RequestBodyUnavailable in the module list * Code reformat * Added Test to check if exception is raising if non-replayble request body is passed to DigestAuth #670

authored by Kousik Mitra · committed by Florimond Manca · parent bc6163c · Browse files · 2 files+25 added−2 removed
This commit

State

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

Diff stats

2 files+25 added−2 removed

  • tests:+22 added−1 removed
  • other:+3 added−1 removed

2 files+25 added−2 removed

modified:httpx/auth.py+3 added-1 removed
4 unmodified lines
5import typing6from base64 import b64encode7from urllib.request import parse_http_list89from .exceptions import ProtocolError9from .exceptions import ProtocolError, RequestBodyUnavailable10from .models import Request, Response11from .utils import to_bytes, to_str, unquote1213AuthFlow = typing.Generator[Request, Response, None]
89 unmodified lines
103 self.username = to_bytes(username)104 self.password = to_bytes(password)105106 def __call__(self, request: Request) -> AuthFlow:107 if not request.stream.can_replay():108 raise RequestBodyUnavailable("Request body is no longer available.")109 response = yield request110111 if response.status_code != 401 or "www-authenticate" not in response.headers:112 # If the response is not a 401 WWW-Authenticate, then we don't
143 unmodified lines
modified:tests/client/test_auth.py+22 added-1 removed
3 unmodified lines
4import typing56import pytest78from httpx import URL, AsyncClient, DigestAuth, ProtocolError, Request, Response8from httpx import (9 URL,10 AsyncClient,11 DigestAuth,12 ProtocolError,13 Request,14 RequestBodyUnavailable,15 Response,16)17from httpx.auth import Auth, AuthFlow18from httpx.config import CertTypes, TimeoutTypes, VerifyTypes19from httpx.dispatch.base import Dispatcher20
428 unmodified lines
449 assert len(resp2.history) == 1450 assert resp2.history == [resp1]451452 assert len(resp1.history) == 0453454455@pytest.mark.asyncio456async def test_digest_auth_unavailable_streaming_body():457 url = "https://example.org/"458 auth = DigestAuth(username="tomchristie", password="password123")459 client = AsyncClient(dispatch=MockDispatch())460461 async def streaming_body():462 yield b"Example request body"463464 with pytest.raises(RequestBodyUnavailable):465 await client.post(url, data=streaming_body(), auth=auth)