test_auth.py
This file
Path
tests/client/test_auth.py
506 lines · 15.9 KiB · Python
Last changed by
Add Auth.requires_response_body attribute (#803)George Kettleboroughbefore adoptionb3db9ff
History of this fileTake it
The walk stopped after 200 commits: lines older than ad38db8 point at it.
Document client-level configuration (#488)Florimond Mancabefore adoptionad38db8
lines 1–3why, one step back1import hashlib2import json3import osType-check test_auth.py (#665)Florimond Mancabefore adoption49e4d15
line 4why, one step back4import typingDocument client-level configuration (#488)Florimond Mancabefore adoptionad38db8
lines 5–7why, one step back56import pytest7Raise an exception in DigestAuth if non-replayable request is passed (#685)Kousik Mitrabefore adoption0ed0314
lines 8–10why, one step back8from httpx import (9 URL,10 AsyncClient,Public Auth API (#732)Tom Christiebefore adoption12dd157
line 11why, one step back11 Auth,Raise an exception in DigestAuth if non-replayable request is passed (#685)Kousik Mitrabefore adoption0ed0314
lines 12–17why, one step back12 DigestAuth,13 ProtocolError,14 Request,15 RequestBodyUnavailable,16 Response,17)Switch to private module names (#785)Florimond Mancabefore adoption82dc6f3
lines 18–19why, one step back18from httpx._config import CertTypes, TimeoutTypes, VerifyTypes19from httpx._dispatch.base import AsyncDispatcherDocument client-level configuration (#488)Florimond Mancabefore adoptionad38db8
lines 20–21why, one step back2021Dispatcher -> AsyncDispatcher (#725)Tom Christiebefore adoptione19bd9b
line 22why, one step back22class MockDispatch(AsyncDispatcher):Document client-level configuration (#488)Florimond Mancabefore adoptionad38db8
lines 23–28why, one step back23 def __init__(self, auth_header: str = "", status_code: int = 200) -> None:24 self.auth_header = auth_header25 self.status_code = status_code2627 async def send(28 self,Drop sync (#544)Tom Christiebefore adoption206c537
line 29why, one step back29 request: Request,Document client-level configuration (#488)Florimond Mancabefore adoptionad38db8
lines 30–32why, one step back30 verify: VerifyTypes = None,31 cert: CertTypes = None,32 timeout: TimeoutTypes = None,Drop sync (#544)Tom Christiebefore adoption206c537
line 33why, one step back33 ) -> Response:Document client-level configuration (#488)Florimond Mancabefore adoptionad38db8
lines 34–35why, one step back34 headers = [("www-authenticate", self.auth_header)] if self.auth_header else []35 body = json.dumps({"auth": request.headers.get("Authorization")}).encode()Drop sync (#544)Tom Christiebefore adoption206c537
line 36why, one step back36 return Response(Document client-level configuration (#488)Florimond Mancabefore adoptionad38db8
lines 37–40why, one step back37 self.status_code, headers=headers, content=body, request=request38 )3940Dispatcher -> AsyncDispatcher (#725)Tom Christiebefore adoptione19bd9b
line 41why, one step back41class MockDigestAuthDispatch(AsyncDispatcher):Document client-level configuration (#488)Florimond Mancabefore adoptionad38db8
lines 42–56why, one step back42 def __init__(43 self,44 algorithm: str = "SHA-256",45 send_response_after_attempt: int = 1,46 qop: str = "auth",47 regenerate_nonce: bool = True,48 ) -> None:49 self.algorithm = algorithm50 self.send_response_after_attempt = send_response_after_attempt51 self.qop = qop52 self._regenerate_nonce = regenerate_nonce53 self._response_count = 05455 async def send(56 self,Drop sync (#544)Tom Christiebefore adoption206c537
line 57why, one step back57 request: Request,Document client-level configuration (#488)Florimond Mancabefore adoptionad38db8
lines 58–60why, one step back58 verify: VerifyTypes = None,59 cert: CertTypes = None,60 timeout: TimeoutTypes = None,Drop sync (#544)Tom Christiebefore adoption206c537
line 61why, one step back61 ) -> Response:Document client-level configuration (#488)Florimond Mancabefore adoptionad38db8
lines 62–65why, one step back62 if self._response_count < self.send_response_after_attempt:63 return self.challenge_send(request)6465 body = json.dumps({"auth": request.headers.get("Authorization")}).encode()Drop sync (#544)Tom Christiebefore adoption206c537
line 66why, one step back66 return Response(200, content=body, request=request)Document client-level configuration (#488)Florimond Mancabefore adoptionad38db8
line 67why, one step back67Drop sync (#544)Tom Christiebefore adoption206c537
line 68why, one step back68 def challenge_send(self, request: Request) -> Response:Document client-level configuration (#488)Florimond Mancabefore adoptionad38db8
lines 69–92why, one step back69 self._response_count += 170 nonce = (71 hashlib.sha256(os.urandom(8)).hexdigest()72 if self._regenerate_nonce73 else "ee96edced2a0b43e4869e96ebe27563f369c1205a049d06419bb51d8aeddf3d3"74 )75 challenge_data = {76 "nonce": nonce,77 "qop": self.qop,78 "opaque": (79 "ee6378f3ee14ebfd2fff54b70a91a7c9390518047f242ab2271380db0e14bda1"80 ),81 "algorithm": self.algorithm,82 "stale": "FALSE",83 }84 challenge_str = ", ".join(85 '{}="{}"'.format(key, value)86 for key, value in challenge_data.items()87 if value88 )8990 headers = [91 ("www-authenticate", 'Digest realm="httpx@example.org", ' + challenge_str)92 ]Drop sync (#544)Tom Christiebefore adoption206c537
line 93why, one step back93 return Response(401, headers=headers, content=b"", request=request)Document client-level configuration (#488)Florimond Mancabefore adoptionad38db8
lines 94–95why, one step back9495Drop sync (#544)Tom Christiebefore adoption206c537
line 96why, one step back96@pytest.mark.asyncioType-check test_auth.py (#665)Florimond Mancabefore adoption49e4d15
line 97why, one step back97async def test_basic_auth() -> None:Document client-level configuration (#488)Florimond Mancabefore adoptionad38db8
lines 98–100why, one step back98 url = "https://example.org/"99 auth = ("tomchristie", "password123")100Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84
line 101why, one step back101 client = AsyncClient(dispatch=MockDispatch())Drop sync (#544)Tom Christiebefore adoption206c537
line 102why, one step back102 response = await client.get(url, auth=auth)Document client-level configuration (#488)Florimond Mancabefore adoptionad38db8
lines 103–107why, one step back103104 assert response.status_code == 200105 assert response.json() == {"auth": "Basic dG9tY2hyaXN0aWU6cGFzc3dvcmQxMjM="}106107Drop sync (#544)Tom Christiebefore adoption206c537
line 108why, one step back108@pytest.mark.asyncioType-check test_auth.py (#665)Florimond Mancabefore adoption49e4d15
line 109why, one step back109async def test_basic_auth_in_url() -> None:Document client-level configuration (#488)Florimond Mancabefore adoptionad38db8
lines 110–111why, one step back110 url = "https://tomchristie:password123@example.org/"111Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84
line 112why, one step back112 client = AsyncClient(dispatch=MockDispatch())Drop sync (#544)Tom Christiebefore adoption206c537
line 113why, one step back113 response = await client.get(url)Document client-level configuration (#488)Florimond Mancabefore adoptionad38db8
lines 114–118why, one step back114115 assert response.status_code == 200116 assert response.json() == {"auth": "Basic dG9tY2hyaXN0aWU6cGFzc3dvcmQxMjM="}117118Drop sync (#544)Tom Christiebefore adoption206c537
line 119why, one step back119@pytest.mark.asyncioType-check test_auth.py (#665)Florimond Mancabefore adoption49e4d15
line 120why, one step back120async def test_basic_auth_on_session() -> None:Document client-level configuration (#488)Florimond Mancabefore adoptionad38db8
lines 121–123why, one step back121 url = "https://example.org/"122 auth = ("tomchristie", "password123")123Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84
line 124why, one step back124 client = AsyncClient(dispatch=MockDispatch(), auth=auth)Drop sync (#544)Tom Christiebefore adoption206c537
line 125why, one step back125 response = await client.get(url)Document client-level configuration (#488)Florimond Mancabefore adoptionad38db8
lines 126–130why, one step back126127 assert response.status_code == 200128 assert response.json() == {"auth": "Basic dG9tY2hyaXN0aWU6cGFzc3dvcmQxMjM="}129130Drop sync (#544)Tom Christiebefore adoption206c537
line 131why, one step back131@pytest.mark.asyncioType-check test_auth.py (#665)Florimond Mancabefore adoption49e4d15
line 132why, one step back132async def test_custom_auth() -> None:Document client-level configuration (#488)Florimond Mancabefore adoptionad38db8
lines 133–134why, one step back133 url = "https://example.org/"134Type-check test_auth.py (#665)Florimond Mancabefore adoption49e4d15
line 135why, one step back135 def auth(request: Request) -> Request:Document client-level configuration (#488)Florimond Mancabefore adoptionad38db8
lines 136–138why, one step back136 request.headers["Authorization"] = "Token 123"137 return request138Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84
line 139why, one step back139 client = AsyncClient(dispatch=MockDispatch())Drop sync (#544)Tom Christiebefore adoption206c537
line 140why, one step back140 response = await client.get(url, auth=auth)Document client-level configuration (#488)Florimond Mancabefore adoptionad38db8
lines 141–145why, one step back141142 assert response.status_code == 200143 assert response.json() == {"auth": "Token 123"}144145Drop sync (#544)Tom Christiebefore adoption206c537
line 146why, one step back146@pytest.mark.asyncioType-check test_auth.py (#665)Florimond Mancabefore adoption49e4d15
line 147why, one step back147async def test_netrc_auth() -> None:Document client-level configuration (#488)Florimond Mancabefore adoptionad38db8
lines 148–150why, one step back148 os.environ["NETRC"] = "tests/.netrc"149 url = "http://netrcexample.org"150Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84
line 151why, one step back151 client = AsyncClient(dispatch=MockDispatch())Drop sync (#544)Tom Christiebefore adoption206c537
line 152why, one step back152 response = await client.get(url)Document client-level configuration (#488)Florimond Mancabefore adoptionad38db8
lines 153–159why, one step back153154 assert response.status_code == 200155 assert response.json() == {156 "auth": "Basic ZXhhbXBsZS11c2VybmFtZTpleGFtcGxlLXBhc3N3b3Jk"157 }158159Drop sync (#544)Tom Christiebefore adoption206c537
line 160why, one step back160@pytest.mark.asyncioType-check test_auth.py (#665)Florimond Mancabefore adoption49e4d15
line 161why, one step back161async def test_auth_header_has_priority_over_netrc() -> None:Ensure Authorization header has priority over .netrcTom Christiebefore adoption43331cf
lines 162–164why, one step back162 os.environ["NETRC"] = "tests/.netrc"163 url = "http://netrcexample.org"164Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84
line 165why, one step back165 client = AsyncClient(dispatch=MockDispatch())Ensure Authorization header has priority over .netrcTom Christiebefore adoption43331cf
lines 166–172why, one step back166 response = await client.get(url, headers={"Authorization": "Override"})167168 assert response.status_code == 200169 assert response.json() == {"auth": "Override"}170171172@pytest.mark.asyncioType-check test_auth.py (#665)Florimond Mancabefore adoption49e4d15
line 173why, one step back173async def test_trust_env_auth() -> None:Document client-level configuration (#488)Florimond Mancabefore adoptionad38db8
lines 174–176why, one step back174 os.environ["NETRC"] = "tests/.netrc"175 url = "http://netrcexample.org"176Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84
line 177why, one step back177 client = AsyncClient(dispatch=MockDispatch(), trust_env=False)No I/O auth (#644)Tom Christiebefore adoption6c69e09
line 178why, one step back178 response = await client.get(url)Document client-level configuration (#488)Florimond Mancabefore adoptionad38db8
lines 179–182why, one step back179180 assert response.status_code == 200181 assert response.json() == {"auth": None}182Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84
line 183why, one step back183 client = AsyncClient(dispatch=MockDispatch(), trust_env=True)No I/O auth (#644)Tom Christiebefore adoption6c69e09
line 184why, one step back184 response = await client.get(url)Document client-level configuration (#488)Florimond Mancabefore adoptionad38db8
lines 185–191why, one step back185186 assert response.status_code == 200187 assert response.json() == {188 "auth": "Basic ZXhhbXBsZS11c2VybmFtZTpleGFtcGxlLXBhc3N3b3Jk"189 }190191Type-check test_auth.py (#665)Florimond Mancabefore adoption49e4d15
line 192why, one step back192def test_auth_hidden_url() -> None:Document client-level configuration (#488)Florimond Mancabefore adoptionad38db8
lines 193–198why, one step back193 url = "http://example-username:example-password@example.org/"194 expected = "URL('http://example-username:[secure]@example.org/')"195 assert url == URL(url)196 assert expected == repr(URL(url))197198Drop sync (#544)Tom Christiebefore adoption206c537
line 199why, one step back199@pytest.mark.asyncioType-check test_auth.py (#665)Florimond Mancabefore adoption49e4d15
line 200why, one step back200async def test_auth_hidden_header() -> None:Document client-level configuration (#488)Florimond Mancabefore adoptionad38db8
lines 201–203why, one step back201 url = "https://example.org/"202 auth = ("example-username", "example-password")203Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84
line 204why, one step back204 client = AsyncClient(dispatch=MockDispatch())Drop sync (#544)Tom Christiebefore adoption206c537
line 205why, one step back205 response = await client.get(url, auth=auth)Document client-level configuration (#488)Florimond Mancabefore adoptionad38db8
lines 206–209why, one step back206207 assert "'authorization': '[secure]'" in str(response.request.headers)208209Drop sync (#544)Tom Christiebefore adoption206c537
line 210why, one step back210@pytest.mark.asyncioType-check test_auth.py (#665)Florimond Mancabefore adoption49e4d15
line 211why, one step back211async def test_auth_invalid_type() -> None:Document client-level configuration (#488)Florimond Mancabefore adoptionad38db8
line 212why, one step back212 url = "https://example.org/"Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84
line 213why, one step back213 client = AsyncClient(Type-check test_auth.py (#665)Florimond Mancabefore adoption49e4d15
lines 214–215why, one step back214 dispatch=MockDispatch(), auth="not a tuple, not a callable", # type: ignore215 )Drop sync (#544)Tom Christiebefore adoption206c537
lines 216–217why, one step back216 with pytest.raises(TypeError):217 await client.get(url)Document client-level configuration (#488)Florimond Mancabefore adoptionad38db8
lines 218–219why, one step back218219Drop sync (#544)Tom Christiebefore adoption206c537
line 220why, one step back220@pytest.mark.asyncioType-check test_auth.py (#665)Florimond Mancabefore adoption49e4d15
line 221why, one step back221async def test_digest_auth_returns_no_auth_if_no_digest_header_in_response() -> None:Document client-level configuration (#488)Florimond Mancabefore adoptionad38db8
lines 222–224why, one step back222 url = "https://example.org/"223 auth = DigestAuth(username="tomchristie", password="password123")224Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84
line 225why, one step back225 client = AsyncClient(dispatch=MockDispatch())Drop sync (#544)Tom Christiebefore adoption206c537
line 226why, one step back226 response = await client.get(url, auth=auth)Document client-level configuration (#488)Florimond Mancabefore adoptionad38db8
lines 227–229why, one step back227228 assert response.status_code == 200229 assert response.json() == {"auth": None}Record history of requests made during authentication (#718)Florimond Mancabefore adoptionbc6163c
line 230why, one step back230 assert len(response.history) == 0Document client-level configuration (#488)Florimond Mancabefore adoptionad38db8
lines 231–232why, one step back231232Drop sync (#544)Tom Christiebefore adoption206c537
line 233why, one step back233@pytest.mark.asyncioType-check test_auth.py (#665)Florimond Mancabefore adoption49e4d15
line 234why, one step back234async def test_digest_auth_200_response_including_digest_auth_header() -> None:Document client-level configuration (#488)Florimond Mancabefore adoptionad38db8
lines 235–238why, one step back235 url = "https://example.org/"236 auth = DigestAuth(username="tomchristie", password="password123")237 auth_header = 'Digest realm="realm@host.com",qop="auth",nonce="abc",opaque="xyz"'238Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84
lines 239–241why, one step back239 client = AsyncClient(240 dispatch=MockDispatch(auth_header=auth_header, status_code=200)241 )Drop sync (#544)Tom Christiebefore adoption206c537
line 242why, one step back242 response = await client.get(url, auth=auth)Document client-level configuration (#488)Florimond Mancabefore adoptionad38db8
lines 243–245why, one step back243244 assert response.status_code == 200245 assert response.json() == {"auth": None}Record history of requests made during authentication (#718)Florimond Mancabefore adoptionbc6163c
line 246why, one step back246 assert len(response.history) == 0Document client-level configuration (#488)Florimond Mancabefore adoptionad38db8
lines 247–248why, one step back247248Drop sync (#544)Tom Christiebefore adoption206c537
line 249why, one step back249@pytest.mark.asyncioType-check test_auth.py (#665)Florimond Mancabefore adoption49e4d15
line 250why, one step back250async def test_digest_auth_401_response_without_digest_auth_header() -> None:Document client-level configuration (#488)Florimond Mancabefore adoptionad38db8
lines 251–253why, one step back251 url = "https://example.org/"252 auth = DigestAuth(username="tomchristie", password="password123")253Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84
line 254why, one step back254 client = AsyncClient(dispatch=MockDispatch(auth_header="", status_code=401))Drop sync (#544)Tom Christiebefore adoption206c537
line 255why, one step back255 response = await client.get(url, auth=auth)Document client-level configuration (#488)Florimond Mancabefore adoptionad38db8
lines 256–258why, one step back256257 assert response.status_code == 401258 assert response.json() == {"auth": None}Record history of requests made during authentication (#718)Florimond Mancabefore adoptionbc6163c
line 259why, one step back259 assert len(response.history) == 0Document client-level configuration (#488)Florimond Mancabefore adoptionad38db8
lines 260–274why, one step back260261262@pytest.mark.parametrize(263 "algorithm,expected_hash_length,expected_response_length",264 [265 ("MD5", 64, 32),266 ("MD5-SESS", 64, 32),267 ("SHA", 64, 40),268 ("SHA-SESS", 64, 40),269 ("SHA-256", 64, 64),270 ("SHA-256-SESS", 64, 64),271 ("SHA-512", 64, 128),272 ("SHA-512-SESS", 64, 128),273 ],274)Drop sync (#544)Tom Christiebefore adoption206c537
line 275why, one step back275@pytest.mark.asyncioType-check test_auth.py (#665)Florimond Mancabefore adoption49e4d15
lines 276–278why, one step back276async def test_digest_auth(277 algorithm: str, expected_hash_length: int, expected_response_length: int278) -> None:Document client-level configuration (#488)Florimond Mancabefore adoptionad38db8
lines 279–281why, one step back279 url = "https://example.org/"280 auth = DigestAuth(username="tomchristie", password="password123")281Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84
line 282why, one step back282 client = AsyncClient(dispatch=MockDigestAuthDispatch(algorithm=algorithm))Drop sync (#544)Tom Christiebefore adoption206c537
line 283why, one step back283 response = await client.get(url, auth=auth)Document client-level configuration (#488)Florimond Mancabefore adoptionad38db8
lines 284–285why, one step back284285 assert response.status_code == 200Record history of requests made during authentication (#718)Florimond Mancabefore adoptionbc6163c
lines 286–287why, one step back286 assert len(response.history) == 1287Type-check test_auth.py (#665)Florimond Mancabefore adoption49e4d15
lines 288–290why, one step back288 authorization = typing.cast(dict, response.json())["auth"]289 scheme, _, fields = authorization.partition(" ")290 assert scheme == "Digest"Document client-level configuration (#488)Florimond Mancabefore adoptionad38db8
line 291why, one step back291Type-check test_auth.py (#665)Florimond Mancabefore adoption49e4d15
line 292why, one step back292 response_fields = [field.strip() for field in fields.split(",")]Document client-level configuration (#488)Florimond Mancabefore adoptionad38db8
lines 293–306why, one step back293 digest_data = dict(field.split("=") for field in response_fields)294295 assert digest_data["username"] == '"tomchristie"'296 assert digest_data["realm"] == '"httpx@example.org"'297 assert "nonce" in digest_data298 assert digest_data["uri"] == '"/"'299 assert len(digest_data["response"]) == expected_response_length + 2 # extra quotes300 assert len(digest_data["opaque"]) == expected_hash_length + 2301 assert digest_data["algorithm"] == algorithm302 assert digest_data["qop"] == "auth"303 assert digest_data["nc"] == "00000001"304 assert len(digest_data["cnonce"]) == 16 + 2305306Drop sync (#544)Tom Christiebefore adoption206c537
line 307why, one step back307@pytest.mark.asyncioType-check test_auth.py (#665)Florimond Mancabefore adoption49e4d15
line 308why, one step back308async def test_digest_auth_no_specified_qop() -> None:Document client-level configuration (#488)Florimond Mancabefore adoptionad38db8
lines 309–311why, one step back309 url = "https://example.org/"310 auth = DigestAuth(username="tomchristie", password="password123")311Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84
line 312why, one step back312 client = AsyncClient(dispatch=MockDigestAuthDispatch(qop=""))Drop sync (#544)Tom Christiebefore adoption206c537
line 313why, one step back313 response = await client.get(url, auth=auth)Document client-level configuration (#488)Florimond Mancabefore adoptionad38db8
lines 314–315why, one step back314315 assert response.status_code == 200Record history of requests made during authentication (#718)Florimond Mancabefore adoptionbc6163c
lines 316–317why, one step back316 assert len(response.history) == 1317Type-check test_auth.py (#665)Florimond Mancabefore adoption49e4d15
lines 318–320why, one step back318 authorization = typing.cast(dict, response.json())["auth"]319 scheme, _, fields = authorization.partition(" ")320 assert scheme == "Digest"Document client-level configuration (#488)Florimond Mancabefore adoptionad38db8
line 321why, one step back321Type-check test_auth.py (#665)Florimond Mancabefore adoption49e4d15
line 322why, one step back322 response_fields = [field.strip() for field in fields.split(",")]Document client-level configuration (#488)Florimond Mancabefore adoptionad38db8
lines 323–337why, one step back323 digest_data = dict(field.split("=") for field in response_fields)324325 assert "qop" not in digest_data326 assert "nc" not in digest_data327 assert "cnonce" not in digest_data328 assert digest_data["username"] == '"tomchristie"'329 assert digest_data["realm"] == '"httpx@example.org"'330 assert len(digest_data["nonce"]) == 64 + 2 # extra quotes331 assert digest_data["uri"] == '"/"'332 assert len(digest_data["response"]) == 64 + 2333 assert len(digest_data["opaque"]) == 64 + 2334 assert digest_data["algorithm"] == "SHA-256"335336337@pytest.mark.parametrize("qop", ("auth, auth-int", "auth,auth-int", "unknown,auth"))Drop sync (#544)Tom Christiebefore adoption206c537
line 338why, one step back338@pytest.mark.asyncioType-check test_auth.py (#665)Florimond Mancabefore adoption49e4d15
line 339why, one step back339async def test_digest_auth_qop_including_spaces_and_auth_returns_auth(qop: str) -> None:Document client-level configuration (#488)Florimond Mancabefore adoptionad38db8
lines 340–342why, one step back340 url = "https://example.org/"341 auth = DigestAuth(username="tomchristie", password="password123")342Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84
line 343why, one step back343 client = AsyncClient(dispatch=MockDigestAuthDispatch(qop=qop))Record history of requests made during authentication (#718)Florimond Mancabefore adoptionbc6163c
line 344why, one step back344 response = await client.get(url, auth=auth)Document client-level configuration (#488)Florimond Mancabefore adoptionad38db8
line 345why, one step back345Record history of requests made during authentication (#718)Florimond Mancabefore adoptionbc6163c
lines 346–347why, one step back346 assert response.status_code == 200347 assert len(response.history) == 1Document client-level configuration (#488)Florimond Mancabefore adoptionad38db8
line 348why, one step back348Record history of requests made during authentication (#718)Florimond Mancabefore adoptionbc6163c
line 349why, one step back349Drop sync (#544)Tom Christiebefore adoption206c537
line 350why, one step back350@pytest.mark.asyncioType-check test_auth.py (#665)Florimond Mancabefore adoption49e4d15
line 351why, one step back351async def test_digest_auth_qop_auth_int_not_implemented() -> None:Document client-level configuration (#488)Florimond Mancabefore adoptionad38db8
lines 352–353why, one step back352 url = "https://example.org/"353 auth = DigestAuth(username="tomchristie", password="password123")Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84
line 354why, one step back354 client = AsyncClient(dispatch=MockDigestAuthDispatch(qop="auth-int"))Document client-level configuration (#488)Florimond Mancabefore adoptionad38db8
lines 355–356why, one step back355356 with pytest.raises(NotImplementedError):Drop sync (#544)Tom Christiebefore adoption206c537
line 357why, one step back357 await client.get(url, auth=auth)Document client-level configuration (#488)Florimond Mancabefore adoptionad38db8
lines 358–359why, one step back358359Drop sync (#544)Tom Christiebefore adoption206c537
line 360why, one step back360@pytest.mark.asyncioType-check test_auth.py (#665)Florimond Mancabefore adoption49e4d15
line 361why, one step back361async def test_digest_auth_qop_must_be_auth_or_auth_int() -> None:Document client-level configuration (#488)Florimond Mancabefore adoptionad38db8
lines 362–363why, one step back362 url = "https://example.org/"363 auth = DigestAuth(username="tomchristie", password="password123")Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84
line 364why, one step back364 client = AsyncClient(dispatch=MockDigestAuthDispatch(qop="not-auth"))Document client-level configuration (#488)Florimond Mancabefore adoptionad38db8
lines 365–366why, one step back365366 with pytest.raises(ProtocolError):Drop sync (#544)Tom Christiebefore adoption206c537
line 367why, one step back367 await client.get(url, auth=auth)Document client-level configuration (#488)Florimond Mancabefore adoptionad38db8
lines 368–369why, one step back368369Drop sync (#544)Tom Christiebefore adoption206c537
line 370why, one step back370@pytest.mark.asyncioType-check test_auth.py (#665)Florimond Mancabefore adoption49e4d15
line 371why, one step back371async def test_digest_auth_incorrect_credentials() -> None:Document client-level configuration (#488)Florimond Mancabefore adoptionad38db8
lines 372–374why, one step back372 url = "https://example.org/"373 auth = DigestAuth(username="tomchristie", password="password123")374Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84
line 375why, one step back375 client = AsyncClient(dispatch=MockDigestAuthDispatch(send_response_after_attempt=2))Drop sync (#544)Tom Christiebefore adoption206c537
line 376why, one step back376 response = await client.get(url, auth=auth)Document client-level configuration (#488)Florimond Mancabefore adoptionad38db8
lines 377–378why, one step back377378 assert response.status_code == 401Record history of requests made during authentication (#718)Florimond Mancabefore adoptionbc6163c
line 379why, one step back379 assert len(response.history) == 1Document client-level configuration (#488)Florimond Mancabefore adoptionad38db8
lines 380–391why, one step back380381382@pytest.mark.parametrize(383 "auth_header",384 [385 'Digest realm="httpx@example.org", qop="auth"', # missing fields386 'realm="httpx@example.org", qop="auth"', # not starting with Digest387 'DigestZ realm="httpx@example.org", qop="auth"'388 'qop="auth,auth-int",nonce="abc",opaque="xyz"',389 'Digest realm="httpx@example.org", qop="auth,au', # malformed fields list390 ],391)Drop sync (#544)Tom Christiebefore adoption206c537
line 392why, one step back392@pytest.mark.asyncioType-check test_auth.py (#665)Florimond Mancabefore adoption49e4d15
lines 393–395why, one step back393async def test_digest_auth_raises_protocol_error_on_malformed_header(394 auth_header: str,395) -> None:Document client-level configuration (#488)Florimond Mancabefore adoptionad38db8
lines 396–397why, one step back396 url = "https://example.org/"397 auth = DigestAuth(username="tomchristie", password="password123")Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84
lines 398–400why, one step back398 client = AsyncClient(399 dispatch=MockDispatch(auth_header=auth_header, status_code=401)400 )Document client-level configuration (#488)Florimond Mancabefore adoptionad38db8
lines 401–402why, one step back401402 with pytest.raises(ProtocolError):Drop sync (#544)Tom Christiebefore adoption206c537
line 403why, one step back403 await client.get(url, auth=auth)Record history of requests made during authentication (#718)Florimond Mancabefore adoptionbc6163c
lines 404–420why, one step back404405406@pytest.mark.asyncio407async def test_auth_history() -> None:408 """409 Test that intermediate requests sent as part of an authentication flow410 are recorded in the response history.411 """412413 class RepeatAuth(Auth):414 """415 A mock authentication scheme that requires clients to send416 the request a fixed number of times, and then send a last request containing417 an aggregation of nonces that the server sent in 'WWW-Authenticate' headers418 of intermediate responses.419 """420Public Auth API (#732)Tom Christiebefore adoption12dd157
lines 421–422why, one step back421 requires_request_body = True422Record history of requests made during authentication (#718)Florimond Mancabefore adoptionbc6163c
lines 423–425why, one step back423 def __init__(self, repeat: int):424 self.repeat = repeat425Public Auth API (#732)Tom Christiebefore adoption12dd157
lines 426–428why, one step back426 def auth_flow(427 self, request: Request428 ) -> typing.Generator[Request, Response, None]:Record history of requests made during authentication (#718)Florimond Mancabefore adoptionbc6163c
lines 429–456why, one step back429 nonces = []430431 for index in range(self.repeat):432 request.headers["Authorization"] = f"Repeat {index}"433 response = yield request434 nonces.append(response.headers["www-authenticate"])435436 key = ".".join(nonces)437 request.headers["Authorization"] = f"Repeat {key}"438 yield request439440 url = "https://example.org/"441 auth = RepeatAuth(repeat=2)442 client = AsyncClient(dispatch=MockDispatch(auth_header="abc"))443444 response = await client.get(url, auth=auth)445 assert response.status_code == 200446 assert response.json() == {"auth": "Repeat abc.abc"}447448 assert len(response.history) == 2449 resp1, resp2 = response.history450 assert resp1.json() == {"auth": "Repeat 0"}451 assert resp2.json() == {"auth": "Repeat 1"}452453 assert len(resp2.history) == 1454 assert resp2.history == [resp1]455456 assert len(resp1.history) == 0Raise an exception in DigestAuth if non-replayable request is passed (#685)Kousik Mitrabefore adoption0ed0314
lines 457–469why, one step back457458459@pytest.mark.asyncio460async def test_digest_auth_unavailable_streaming_body():461 url = "https://example.org/"462 auth = DigestAuth(username="tomchristie", password="password123")463 client = AsyncClient(dispatch=MockDispatch())464465 async def streaming_body():466 yield b"Example request body"467468 with pytest.raises(RequestBodyUnavailable):469 await client.post(url, data=streaming_body(), auth=auth)Add Auth.requires_response_body attribute (#803)George Kettleboroughbefore adoptionb3db9ff
lines 470–506why, one step back470471472@pytest.mark.asyncio473async def test_auth_reads_response_body() -> None:474 """475 Test that we can read the response body in an auth flow if `requires_response_body`476 is set.477 """478479 class ResponseBodyAuth(Auth):480 """481 A mock authentication scheme that requires clients to send an 'Authorization'482 header, then send back the contents of the response in the 'Authorization'483 header.484 """485486 requires_response_body = True487488 def __init__(self, token):489 self.token = token490491 def auth_flow(492 self, request: Request493 ) -> typing.Generator[Request, Response, None]:494 request.headers["Authorization"] = self.token495 response = yield request496 data = response.text497 request.headers["Authorization"] = data498 yield request499500 url = "https://example.org/"501 auth = ResponseBodyAuth("xyz")502 client = AsyncClient(dispatch=MockDispatch())503504 response = await client.get(url, auth=auth)505 assert response.status_code == 200506 assert response.json() == {"auth": '{"auth": "xyz"}'}