test_auth.py
This file
Path
tests/client/test_auth.py
444 lines · 14.3 KiB · Python
Last changed by
Record history of requests made during authentication (#718)Florimond Mancabefore adoptionbc6163c
History of this fileTake it
The walk stopped after 200 commits: lines older than 7a08aaa point at it.
Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa
lines 1–3why, one step back1import hashlib2import json3import osType-check test_auth.py (#665)Florimond Mancabefore adoption49e4d15
line 4why, one step back4import typingRelease 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa
lines 5–7why, one step back56import pytest7Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84
line 8why, one step back8from httpx import URL, AsyncClient, DigestAuth, ProtocolError, Request, ResponseRecord history of requests made during authentication (#718)Florimond Mancabefore adoptionbc6163c
line 9why, one step back9from httpx.auth import Auth, AuthFlowTighten up top-level API to only expose public API (#608)Tom Christiebefore adoptiond15dc0b
lines 10–11why, one step back10from httpx.config import CertTypes, TimeoutTypes, VerifyTypes11from httpx.dispatch.base import DispatcherRelease 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa
lines 12–13why, one step back1213Drop sync (#544)Tom Christiebefore adoption206c537
line 14why, one step back14class MockDispatch(Dispatcher):Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa
lines 15–20why, one step back15 def __init__(self, auth_header: str = "", status_code: int = 200) -> None:16 self.auth_header = auth_header17 self.status_code = status_code1819 async def send(20 self,Drop sync (#544)Tom Christiebefore adoption206c537
line 21why, one step back21 request: Request,Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa
lines 22–24why, one step back22 verify: VerifyTypes = None,23 cert: CertTypes = None,24 timeout: TimeoutTypes = None,Drop sync (#544)Tom Christiebefore adoption206c537
line 25why, one step back25 ) -> Response:Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa
lines 26–27why, one step back26 headers = [("www-authenticate", self.auth_header)] if self.auth_header else []27 body = json.dumps({"auth": request.headers.get("Authorization")}).encode()Drop sync (#544)Tom Christiebefore adoption206c537
line 28why, one step back28 return Response(Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa
lines 29–32why, one step back29 self.status_code, headers=headers, content=body, request=request30 )3132Drop sync (#544)Tom Christiebefore adoption206c537
line 33why, one step back33class MockDigestAuthDispatch(Dispatcher):Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa
lines 34–48why, one step back34 def __init__(35 self,36 algorithm: str = "SHA-256",37 send_response_after_attempt: int = 1,38 qop: str = "auth",39 regenerate_nonce: bool = True,40 ) -> None:41 self.algorithm = algorithm42 self.send_response_after_attempt = send_response_after_attempt43 self.qop = qop44 self._regenerate_nonce = regenerate_nonce45 self._response_count = 04647 async def send(48 self,Drop sync (#544)Tom Christiebefore adoption206c537
line 49why, one step back49 request: Request,Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa
lines 50–52why, one step back50 verify: VerifyTypes = None,51 cert: CertTypes = None,52 timeout: TimeoutTypes = None,Drop sync (#544)Tom Christiebefore adoption206c537
line 53why, one step back53 ) -> Response:Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa
lines 54–57why, one step back54 if self._response_count < self.send_response_after_attempt:55 return self.challenge_send(request)5657 body = json.dumps({"auth": request.headers.get("Authorization")}).encode()Drop sync (#544)Tom Christiebefore adoption206c537
line 58why, one step back58 return Response(200, content=body, request=request)Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa
line 59why, one step back59Drop sync (#544)Tom Christiebefore adoption206c537
line 60why, one step back60 def challenge_send(self, request: Request) -> Response:Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa
lines 61–84why, one step back61 self._response_count += 162 nonce = (63 hashlib.sha256(os.urandom(8)).hexdigest()64 if self._regenerate_nonce65 else "ee96edced2a0b43e4869e96ebe27563f369c1205a049d06419bb51d8aeddf3d3"66 )67 challenge_data = {68 "nonce": nonce,69 "qop": self.qop,70 "opaque": (71 "ee6378f3ee14ebfd2fff54b70a91a7c9390518047f242ab2271380db0e14bda1"72 ),73 "algorithm": self.algorithm,74 "stale": "FALSE",75 }76 challenge_str = ", ".join(77 '{}="{}"'.format(key, value)78 for key, value in challenge_data.items()79 if value80 )8182 headers = [83 ("www-authenticate", 'Digest realm="httpx@example.org", ' + challenge_str)84 ]Drop sync (#544)Tom Christiebefore adoption206c537
line 85why, one step back85 return Response(401, headers=headers, content=b"", request=request)Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa
lines 86–87why, one step back8687Drop sync (#544)Tom Christiebefore adoption206c537
line 88why, one step back88@pytest.mark.asyncioType-check test_auth.py (#665)Florimond Mancabefore adoption49e4d15
line 89why, one step back89async def test_basic_auth() -> None:Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa
lines 90–92why, one step back90 url = "https://example.org/"91 auth = ("tomchristie", "password123")92Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84
line 93why, one step back93 client = AsyncClient(dispatch=MockDispatch())Drop sync (#544)Tom Christiebefore adoption206c537
line 94why, one step back94 response = await client.get(url, auth=auth)Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa
lines 95–99why, one step back9596 assert response.status_code == 20097 assert response.json() == {"auth": "Basic dG9tY2hyaXN0aWU6cGFzc3dvcmQxMjM="}9899Drop sync (#544)Tom Christiebefore adoption206c537
line 100why, one step back100@pytest.mark.asyncioType-check test_auth.py (#665)Florimond Mancabefore adoption49e4d15
line 101why, one step back101async def test_basic_auth_in_url() -> None:Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa
lines 102–103why, one step back102 url = "https://tomchristie:password123@example.org/"103Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84
line 104why, one step back104 client = AsyncClient(dispatch=MockDispatch())Drop sync (#544)Tom Christiebefore adoption206c537
line 105why, one step back105 response = await client.get(url)Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa
lines 106–110why, one step back106107 assert response.status_code == 200108 assert response.json() == {"auth": "Basic dG9tY2hyaXN0aWU6cGFzc3dvcmQxMjM="}109110Drop sync (#544)Tom Christiebefore adoption206c537
line 111why, one step back111@pytest.mark.asyncioType-check test_auth.py (#665)Florimond Mancabefore adoption49e4d15
line 112why, one step back112async def test_basic_auth_on_session() -> None:Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa
lines 113–115why, one step back113 url = "https://example.org/"114 auth = ("tomchristie", "password123")115Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84
line 116why, one step back116 client = AsyncClient(dispatch=MockDispatch(), auth=auth)Drop sync (#544)Tom Christiebefore adoption206c537
line 117why, one step back117 response = await client.get(url)Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa
lines 118–122why, one step back118119 assert response.status_code == 200120 assert response.json() == {"auth": "Basic dG9tY2hyaXN0aWU6cGFzc3dvcmQxMjM="}121122Drop sync (#544)Tom Christiebefore adoption206c537
line 123why, one step back123@pytest.mark.asyncioType-check test_auth.py (#665)Florimond Mancabefore adoption49e4d15
line 124why, one step back124async def test_custom_auth() -> None:Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa
lines 125–126why, one step back125 url = "https://example.org/"126Type-check test_auth.py (#665)Florimond Mancabefore adoption49e4d15
line 127why, one step back127 def auth(request: Request) -> Request:Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa
lines 128–130why, one step back128 request.headers["Authorization"] = "Token 123"129 return request130Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84
line 131why, one step back131 client = AsyncClient(dispatch=MockDispatch())Drop sync (#544)Tom Christiebefore adoption206c537
line 132why, one step back132 response = await client.get(url, auth=auth)Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa
lines 133–137why, one step back133134 assert response.status_code == 200135 assert response.json() == {"auth": "Token 123"}136137Drop sync (#544)Tom Christiebefore adoption206c537
line 138why, one step back138@pytest.mark.asyncioType-check test_auth.py (#665)Florimond Mancabefore adoption49e4d15
line 139why, one step back139async def test_netrc_auth() -> None:Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa
lines 140–142why, one step back140 os.environ["NETRC"] = "tests/.netrc"141 url = "http://netrcexample.org"142Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84
line 143why, one step back143 client = AsyncClient(dispatch=MockDispatch())Drop sync (#544)Tom Christiebefore adoption206c537
line 144why, one step back144 response = await client.get(url)Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa
lines 145–151why, one step back145146 assert response.status_code == 200147 assert response.json() == {148 "auth": "Basic ZXhhbXBsZS11c2VybmFtZTpleGFtcGxlLXBhc3N3b3Jk"149 }150151Drop sync (#544)Tom Christiebefore adoption206c537
line 152why, one step back152@pytest.mark.asyncioType-check test_auth.py (#665)Florimond Mancabefore adoption49e4d15
line 153why, one step back153async def test_auth_header_has_priority_over_netrc() -> None:Ensure Authorization header has priority over .netrcTom Christiebefore adoption43331cf
lines 154–156why, one step back154 os.environ["NETRC"] = "tests/.netrc"155 url = "http://netrcexample.org"156Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84
line 157why, one step back157 client = AsyncClient(dispatch=MockDispatch())Ensure Authorization header has priority over .netrcTom Christiebefore adoption43331cf
lines 158–164why, one step back158 response = await client.get(url, headers={"Authorization": "Override"})159160 assert response.status_code == 200161 assert response.json() == {"auth": "Override"}162163164@pytest.mark.asyncioType-check test_auth.py (#665)Florimond Mancabefore adoption49e4d15
line 165why, one step back165async def test_trust_env_auth() -> None:Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa
lines 166–168why, one step back166 os.environ["NETRC"] = "tests/.netrc"167 url = "http://netrcexample.org"168Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84
line 169why, one step back169 client = AsyncClient(dispatch=MockDispatch(), trust_env=False)No I/O auth (#644)Tom Christiebefore adoption6c69e09
line 170why, one step back170 response = await client.get(url)Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa
lines 171–174why, one step back171172 assert response.status_code == 200173 assert response.json() == {"auth": None}174Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84
line 175why, one step back175 client = AsyncClient(dispatch=MockDispatch(), trust_env=True)No I/O auth (#644)Tom Christiebefore adoption6c69e09
line 176why, one step back176 response = await client.get(url)Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa
lines 177–183why, one step back177178 assert response.status_code == 200179 assert response.json() == {180 "auth": "Basic ZXhhbXBsZS11c2VybmFtZTpleGFtcGxlLXBhc3N3b3Jk"181 }182183Type-check test_auth.py (#665)Florimond Mancabefore adoption49e4d15
line 184why, one step back184def test_auth_hidden_url() -> None:Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa
lines 185–190why, one step back185 url = "http://example-username:example-password@example.org/"186 expected = "URL('http://example-username:[secure]@example.org/')"187 assert url == URL(url)188 assert expected == repr(URL(url))189190Drop sync (#544)Tom Christiebefore adoption206c537
line 191why, one step back191@pytest.mark.asyncioType-check test_auth.py (#665)Florimond Mancabefore adoption49e4d15
line 192why, one step back192async def test_auth_hidden_header() -> None:Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa
lines 193–195why, one step back193 url = "https://example.org/"194 auth = ("example-username", "example-password")195Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84
line 196why, one step back196 client = AsyncClient(dispatch=MockDispatch())Drop sync (#544)Tom Christiebefore adoption206c537
line 197why, one step back197 response = await client.get(url, auth=auth)Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa
lines 198–201why, one step back198199 assert "'authorization': '[secure]'" in str(response.request.headers)200201Drop sync (#544)Tom Christiebefore adoption206c537
line 202why, one step back202@pytest.mark.asyncioType-check test_auth.py (#665)Florimond Mancabefore adoption49e4d15
line 203why, one step back203async def test_auth_invalid_type() -> None:Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa
line 204why, one step back204 url = "https://example.org/"Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84
line 205why, one step back205 client = AsyncClient(Type-check test_auth.py (#665)Florimond Mancabefore adoption49e4d15
lines 206–207why, one step back206 dispatch=MockDispatch(), auth="not a tuple, not a callable", # type: ignore207 )Drop sync (#544)Tom Christiebefore adoption206c537
lines 208–209why, one step back208 with pytest.raises(TypeError):209 await client.get(url)Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa
lines 210–211why, one step back210211Drop sync (#544)Tom Christiebefore adoption206c537
line 212why, one step back212@pytest.mark.asyncioType-check test_auth.py (#665)Florimond Mancabefore adoption49e4d15
line 213why, one step back213async def test_digest_auth_returns_no_auth_if_no_digest_header_in_response() -> None:Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa
lines 214–216why, one step back214 url = "https://example.org/"215 auth = DigestAuth(username="tomchristie", password="password123")216Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84
line 217why, one step back217 client = AsyncClient(dispatch=MockDispatch())Drop sync (#544)Tom Christiebefore adoption206c537
line 218why, one step back218 response = await client.get(url, auth=auth)Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa
lines 219–221why, one step back219220 assert response.status_code == 200221 assert response.json() == {"auth": None}Record history of requests made during authentication (#718)Florimond Mancabefore adoptionbc6163c
line 222why, one step back222 assert len(response.history) == 0Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa
lines 223–224why, one step back223224Drop sync (#544)Tom Christiebefore adoption206c537
line 225why, one step back225@pytest.mark.asyncioType-check test_auth.py (#665)Florimond Mancabefore adoption49e4d15
line 226why, one step back226async def test_digest_auth_200_response_including_digest_auth_header() -> None:Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa
lines 227–230why, one step back227 url = "https://example.org/"228 auth = DigestAuth(username="tomchristie", password="password123")229 auth_header = 'Digest realm="realm@host.com",qop="auth",nonce="abc",opaque="xyz"'230Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84
lines 231–233why, one step back231 client = AsyncClient(232 dispatch=MockDispatch(auth_header=auth_header, status_code=200)233 )Drop sync (#544)Tom Christiebefore adoption206c537
line 234why, one step back234 response = await client.get(url, auth=auth)Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa
lines 235–237why, one step back235236 assert response.status_code == 200237 assert response.json() == {"auth": None}Record history of requests made during authentication (#718)Florimond Mancabefore adoptionbc6163c
line 238why, one step back238 assert len(response.history) == 0Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa
lines 239–240why, one step back239240Drop sync (#544)Tom Christiebefore adoption206c537
line 241why, one step back241@pytest.mark.asyncioType-check test_auth.py (#665)Florimond Mancabefore adoption49e4d15
line 242why, one step back242async def test_digest_auth_401_response_without_digest_auth_header() -> None:Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa
lines 243–245why, one step back243 url = "https://example.org/"244 auth = DigestAuth(username="tomchristie", password="password123")245Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84
line 246why, one step back246 client = AsyncClient(dispatch=MockDispatch(auth_header="", status_code=401))Drop sync (#544)Tom Christiebefore adoption206c537
line 247why, one step back247 response = await client.get(url, auth=auth)Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa
lines 248–250why, one step back248249 assert response.status_code == 401250 assert response.json() == {"auth": None}Record history of requests made during authentication (#718)Florimond Mancabefore adoptionbc6163c
line 251why, one step back251 assert len(response.history) == 0Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa
lines 252–266why, one step back252253254@pytest.mark.parametrize(255 "algorithm,expected_hash_length,expected_response_length",256 [257 ("MD5", 64, 32),258 ("MD5-SESS", 64, 32),259 ("SHA", 64, 40),260 ("SHA-SESS", 64, 40),261 ("SHA-256", 64, 64),262 ("SHA-256-SESS", 64, 64),263 ("SHA-512", 64, 128),264 ("SHA-512-SESS", 64, 128),265 ],266)Drop sync (#544)Tom Christiebefore adoption206c537
line 267why, one step back267@pytest.mark.asyncioType-check test_auth.py (#665)Florimond Mancabefore adoption49e4d15
lines 268–270why, one step back268async def test_digest_auth(269 algorithm: str, expected_hash_length: int, expected_response_length: int270) -> None:Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa
lines 271–273why, one step back271 url = "https://example.org/"272 auth = DigestAuth(username="tomchristie", password="password123")273Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84
line 274why, one step back274 client = AsyncClient(dispatch=MockDigestAuthDispatch(algorithm=algorithm))Drop sync (#544)Tom Christiebefore adoption206c537
line 275why, one step back275 response = await client.get(url, auth=auth)Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa
lines 276–277why, one step back276277 assert response.status_code == 200Record history of requests made during authentication (#718)Florimond Mancabefore adoptionbc6163c
lines 278–279why, one step back278 assert len(response.history) == 1279Type-check test_auth.py (#665)Florimond Mancabefore adoption49e4d15
lines 280–282why, one step back280 authorization = typing.cast(dict, response.json())["auth"]281 scheme, _, fields = authorization.partition(" ")282 assert scheme == "Digest"Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa
line 283why, one step back283Type-check test_auth.py (#665)Florimond Mancabefore adoption49e4d15
line 284why, one step back284 response_fields = [field.strip() for field in fields.split(",")]Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa
lines 285–298why, one step back285 digest_data = dict(field.split("=") for field in response_fields)286287 assert digest_data["username"] == '"tomchristie"'288 assert digest_data["realm"] == '"httpx@example.org"'289 assert "nonce" in digest_data290 assert digest_data["uri"] == '"/"'291 assert len(digest_data["response"]) == expected_response_length + 2 # extra quotes292 assert len(digest_data["opaque"]) == expected_hash_length + 2293 assert digest_data["algorithm"] == algorithm294 assert digest_data["qop"] == "auth"295 assert digest_data["nc"] == "00000001"296 assert len(digest_data["cnonce"]) == 16 + 2297298Drop sync (#544)Tom Christiebefore adoption206c537
line 299why, one step back299@pytest.mark.asyncioType-check test_auth.py (#665)Florimond Mancabefore adoption49e4d15
line 300why, one step back300async def test_digest_auth_no_specified_qop() -> None:Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa
lines 301–303why, one step back301 url = "https://example.org/"302 auth = DigestAuth(username="tomchristie", password="password123")303Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84
line 304why, one step back304 client = AsyncClient(dispatch=MockDigestAuthDispatch(qop=""))Drop sync (#544)Tom Christiebefore adoption206c537
line 305why, one step back305 response = await client.get(url, auth=auth)Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa
lines 306–307why, one step back306307 assert response.status_code == 200Record history of requests made during authentication (#718)Florimond Mancabefore adoptionbc6163c
lines 308–309why, one step back308 assert len(response.history) == 1309Type-check test_auth.py (#665)Florimond Mancabefore adoption49e4d15
lines 310–312why, one step back310 authorization = typing.cast(dict, response.json())["auth"]311 scheme, _, fields = authorization.partition(" ")312 assert scheme == "Digest"Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa
line 313why, one step back313Type-check test_auth.py (#665)Florimond Mancabefore adoption49e4d15
line 314why, one step back314 response_fields = [field.strip() for field in fields.split(",")]Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa
lines 315–329why, one step back315 digest_data = dict(field.split("=") for field in response_fields)316317 assert "qop" not in digest_data318 assert "nc" not in digest_data319 assert "cnonce" not in digest_data320 assert digest_data["username"] == '"tomchristie"'321 assert digest_data["realm"] == '"httpx@example.org"'322 assert len(digest_data["nonce"]) == 64 + 2 # extra quotes323 assert digest_data["uri"] == '"/"'324 assert len(digest_data["response"]) == 64 + 2325 assert len(digest_data["opaque"]) == 64 + 2326 assert digest_data["algorithm"] == "SHA-256"327328329@pytest.mark.parametrize("qop", ("auth, auth-int", "auth,auth-int", "unknown,auth"))Drop sync (#544)Tom Christiebefore adoption206c537
line 330why, one step back330@pytest.mark.asyncioType-check test_auth.py (#665)Florimond Mancabefore adoption49e4d15
line 331why, one step back331async def test_digest_auth_qop_including_spaces_and_auth_returns_auth(qop: str) -> None:Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa
lines 332–334why, one step back332 url = "https://example.org/"333 auth = DigestAuth(username="tomchristie", password="password123")334Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84
line 335why, one step back335 client = AsyncClient(dispatch=MockDigestAuthDispatch(qop=qop))Record history of requests made during authentication (#718)Florimond Mancabefore adoptionbc6163c
line 336why, one step back336 response = await client.get(url, auth=auth)Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa
line 337why, one step back337Record history of requests made during authentication (#718)Florimond Mancabefore adoptionbc6163c
lines 338–339why, one step back338 assert response.status_code == 200339 assert len(response.history) == 1Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa
line 340why, one step back340Record history of requests made during authentication (#718)Florimond Mancabefore adoptionbc6163c
line 341why, one step back341Drop sync (#544)Tom Christiebefore adoption206c537
line 342why, one step back342@pytest.mark.asyncioType-check test_auth.py (#665)Florimond Mancabefore adoption49e4d15
line 343why, one step back343async def test_digest_auth_qop_auth_int_not_implemented() -> None:Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa
lines 344–345why, one step back344 url = "https://example.org/"345 auth = DigestAuth(username="tomchristie", password="password123")Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84
line 346why, one step back346 client = AsyncClient(dispatch=MockDigestAuthDispatch(qop="auth-int"))Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa
lines 347–348why, one step back347348 with pytest.raises(NotImplementedError):Drop sync (#544)Tom Christiebefore adoption206c537
line 349why, one step back349 await client.get(url, auth=auth)Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa
lines 350–351why, one step back350351Drop sync (#544)Tom Christiebefore adoption206c537
line 352why, one step back352@pytest.mark.asyncioType-check test_auth.py (#665)Florimond Mancabefore adoption49e4d15
line 353why, one step back353async def test_digest_auth_qop_must_be_auth_or_auth_int() -> None:Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa
lines 354–355why, one step back354 url = "https://example.org/"355 auth = DigestAuth(username="tomchristie", password="password123")Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84
line 356why, one step back356 client = AsyncClient(dispatch=MockDigestAuthDispatch(qop="not-auth"))Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa
lines 357–358why, one step back357358 with pytest.raises(ProtocolError):Drop sync (#544)Tom Christiebefore adoption206c537
line 359why, one step back359 await client.get(url, auth=auth)Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa
lines 360–361why, one step back360361Drop sync (#544)Tom Christiebefore adoption206c537
line 362why, one step back362@pytest.mark.asyncioType-check test_auth.py (#665)Florimond Mancabefore adoption49e4d15
line 363why, one step back363async def test_digest_auth_incorrect_credentials() -> None:Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa
lines 364–366why, one step back364 url = "https://example.org/"365 auth = DigestAuth(username="tomchristie", password="password123")366Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84
line 367why, one step back367 client = AsyncClient(dispatch=MockDigestAuthDispatch(send_response_after_attempt=2))Drop sync (#544)Tom Christiebefore adoption206c537
line 368why, one step back368 response = await client.get(url, auth=auth)Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa
lines 369–370why, one step back369370 assert response.status_code == 401Record history of requests made during authentication (#718)Florimond Mancabefore adoptionbc6163c
line 371why, one step back371 assert len(response.history) == 1Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa
lines 372–383why, one step back372373374@pytest.mark.parametrize(375 "auth_header",376 [377 'Digest realm="httpx@example.org", qop="auth"', # missing fields378 'realm="httpx@example.org", qop="auth"', # not starting with Digest379 'DigestZ realm="httpx@example.org", qop="auth"'380 'qop="auth,auth-int",nonce="abc",opaque="xyz"',381 'Digest realm="httpx@example.org", qop="auth,au', # malformed fields list382 ],383)Drop sync (#544)Tom Christiebefore adoption206c537
line 384why, one step back384@pytest.mark.asyncioType-check test_auth.py (#665)Florimond Mancabefore adoption49e4d15
lines 385–387why, one step back385async def test_digest_auth_raises_protocol_error_on_malformed_header(386 auth_header: str,387) -> None:Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa
lines 388–389why, one step back388 url = "https://example.org/"389 auth = DigestAuth(username="tomchristie", password="password123")Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84
lines 390–392why, one step back390 client = AsyncClient(391 dispatch=MockDispatch(auth_header=auth_header, status_code=401)392 )Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa
lines 393–394why, one step back393394 with pytest.raises(ProtocolError):Drop sync (#544)Tom Christiebefore adoption206c537
line 395why, one step back395 await client.get(url, auth=auth)Record history of requests made during authentication (#718)Florimond Mancabefore adoptionbc6163c
lines 396–444why, one step back396397398@pytest.mark.asyncio399async def test_auth_history() -> None:400 """401 Test that intermediate requests sent as part of an authentication flow402 are recorded in the response history.403 """404405 class RepeatAuth(Auth):406 """407 A mock authentication scheme that requires clients to send408 the request a fixed number of times, and then send a last request containing409 an aggregation of nonces that the server sent in 'WWW-Authenticate' headers410 of intermediate responses.411 """412413 def __init__(self, repeat: int):414 self.repeat = repeat415416 def __call__(self, request: Request) -> AuthFlow:417 nonces = []418419 for index in range(self.repeat):420 request.headers["Authorization"] = f"Repeat {index}"421 response = yield request422 nonces.append(response.headers["www-authenticate"])423424 key = ".".join(nonces)425 request.headers["Authorization"] = f"Repeat {key}"426 yield request427428 url = "https://example.org/"429 auth = RepeatAuth(repeat=2)430 client = AsyncClient(dispatch=MockDispatch(auth_header="abc"))431432 response = await client.get(url, auth=auth)433 assert response.status_code == 200434 assert response.json() == {"auth": "Repeat abc.abc"}435436 assert len(response.history) == 2437 resp1, resp2 = response.history438 assert resp1.json() == {"auth": "Repeat 0"}439 assert resp2.json() == {"auth": "Repeat 1"}440441 assert len(resp2.history) == 1442 assert resp2.history == [resp1]443444 assert len(resp1.history) == 0