Skip to content
Myr

test_auth.py

This file

Path

tests/client/test_auth.py

465 lines · 14.8 KiB · Python

Last changed by

Dispatcher -> AsyncDispatcher (#725)Tom Christiebefore adoptione19bd9b

History of this file

Take 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 back
1import hashlib
2import json3import os

Type-check test_auth.py (#665)Florimond Mancabefore adoption49e4d15

line 4why, one step back
4import typing

Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa

lines 5–7why, one step back
5
6import pytest7

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

lines 8–16why, one step back
8from httpx import (
9 URL,10 AsyncClient,11 DigestAuth,12 ProtocolError,13 Request,14 RequestBodyUnavailable,15 Response,16)

Record history of requests made during authentication (#718)Florimond Mancabefore adoptionbc6163c

line 17why, one step back
17from httpx.auth import Auth, AuthFlow

Tighten up top-level API to only expose public API (#608)Tom Christiebefore adoptiond15dc0b

line 18why, one step back
18from httpx.config import CertTypes, TimeoutTypes, VerifyTypes

Dispatcher -> AsyncDispatcher (#725)Tom Christiebefore adoptione19bd9b

line 19why, one step back
19from httpx.dispatch.base import AsyncDispatcher

Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa

lines 20–21why, one step back
20
21

Dispatcher -> AsyncDispatcher (#725)Tom Christiebefore adoptione19bd9b

line 22why, one step back
22class MockDispatch(AsyncDispatcher):

Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa

lines 23–28why, one step back
23 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 back
29 request: Request,

Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa

lines 30–32why, one step back
30 verify: VerifyTypes = None,
31 cert: CertTypes = None,32 timeout: TimeoutTypes = None,

Drop sync (#544)Tom Christiebefore adoption206c537

line 33why, one step back
33 ) -> Response:

Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa

lines 34–35why, one step back
34 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 back
36 return Response(

Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa

lines 37–40why, one step back
37 self.status_code, headers=headers, content=body, request=request
38 )3940

Dispatcher -> AsyncDispatcher (#725)Tom Christiebefore adoptione19bd9b

line 41why, one step back
41class MockDigestAuthDispatch(AsyncDispatcher):

Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa

lines 42–56why, one step back
42 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 back
57 request: Request,

Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa

lines 58–60why, one step back
58 verify: VerifyTypes = None,
59 cert: CertTypes = None,60 timeout: TimeoutTypes = None,

Drop sync (#544)Tom Christiebefore adoption206c537

line 61why, one step back
61 ) -> Response:

Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa

lines 62–65why, one step back
62 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 back
66 return Response(200, content=body, request=request)

Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa

line 67why, one step back
67

Drop sync (#544)Tom Christiebefore adoption206c537

line 68why, one step back
68 def challenge_send(self, request: Request) -> Response:

Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa

lines 69–92why, one step back
69 self._response_count += 1
70 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 back
93 return Response(401, headers=headers, content=b"", request=request)

Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa

lines 94–95why, one step back
94
95

Drop sync (#544)Tom Christiebefore adoption206c537

line 96why, one step back
96@pytest.mark.asyncio

Type-check test_auth.py (#665)Florimond Mancabefore adoption49e4d15

line 97why, one step back
97async def test_basic_auth() -> None:

Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa

lines 98–100why, one step back
98 url = "https://example.org/"
99 auth = ("tomchristie", "password123")100

Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84

line 101why, one step back
101 client = AsyncClient(dispatch=MockDispatch())

Drop sync (#544)Tom Christiebefore adoption206c537

line 102why, one step back
102 response = await client.get(url, auth=auth)

Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa

lines 103–107why, one step back
103
104 assert response.status_code == 200105 assert response.json() == {"auth": "Basic dG9tY2hyaXN0aWU6cGFzc3dvcmQxMjM="}106107

Drop sync (#544)Tom Christiebefore adoption206c537

line 108why, one step back
108@pytest.mark.asyncio

Type-check test_auth.py (#665)Florimond Mancabefore adoption49e4d15

line 109why, one step back
109async def test_basic_auth_in_url() -> None:

Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa

lines 110–111why, one step back
110 url = "https://tomchristie:password123@example.org/"
111

Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84

line 112why, one step back
112 client = AsyncClient(dispatch=MockDispatch())

Drop sync (#544)Tom Christiebefore adoption206c537

line 113why, one step back
113 response = await client.get(url)

Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa

lines 114–118why, one step back
114
115 assert response.status_code == 200116 assert response.json() == {"auth": "Basic dG9tY2hyaXN0aWU6cGFzc3dvcmQxMjM="}117118

Drop sync (#544)Tom Christiebefore adoption206c537

line 119why, one step back
119@pytest.mark.asyncio

Type-check test_auth.py (#665)Florimond Mancabefore adoption49e4d15

line 120why, one step back
120async def test_basic_auth_on_session() -> None:

Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa

lines 121–123why, one step back
121 url = "https://example.org/"
122 auth = ("tomchristie", "password123")123

Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84

line 124why, one step back
124 client = AsyncClient(dispatch=MockDispatch(), auth=auth)

Drop sync (#544)Tom Christiebefore adoption206c537

line 125why, one step back
125 response = await client.get(url)

Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa

lines 126–130why, one step back
126
127 assert response.status_code == 200128 assert response.json() == {"auth": "Basic dG9tY2hyaXN0aWU6cGFzc3dvcmQxMjM="}129130

Drop sync (#544)Tom Christiebefore adoption206c537

line 131why, one step back
131@pytest.mark.asyncio

Type-check test_auth.py (#665)Florimond Mancabefore adoption49e4d15

line 132why, one step back
132async def test_custom_auth() -> None:

Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa

lines 133–134why, one step back
133 url = "https://example.org/"
134

Type-check test_auth.py (#665)Florimond Mancabefore adoption49e4d15

line 135why, one step back
135 def auth(request: Request) -> Request:

Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa

lines 136–138why, one step back
136 request.headers["Authorization"] = "Token 123"
137 return request138

Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84

line 139why, one step back
139 client = AsyncClient(dispatch=MockDispatch())

Drop sync (#544)Tom Christiebefore adoption206c537

line 140why, one step back
140 response = await client.get(url, auth=auth)

Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa

lines 141–145why, one step back
141
142 assert response.status_code == 200143 assert response.json() == {"auth": "Token 123"}144145

Drop sync (#544)Tom Christiebefore adoption206c537

line 146why, one step back
146@pytest.mark.asyncio

Type-check test_auth.py (#665)Florimond Mancabefore adoption49e4d15

line 147why, one step back
147async def test_netrc_auth() -> None:

Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa

lines 148–150why, one step back
148 os.environ["NETRC"] = "tests/.netrc"
149 url = "http://netrcexample.org"150

Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84

line 151why, one step back
151 client = AsyncClient(dispatch=MockDispatch())

Drop sync (#544)Tom Christiebefore adoption206c537

line 152why, one step back
152 response = await client.get(url)

Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa

lines 153–159why, one step back
153
154 assert response.status_code == 200155 assert response.json() == {156 "auth": "Basic ZXhhbXBsZS11c2VybmFtZTpleGFtcGxlLXBhc3N3b3Jk"157 }158159

Drop sync (#544)Tom Christiebefore adoption206c537

line 160why, one step back
160@pytest.mark.asyncio

Type-check test_auth.py (#665)Florimond Mancabefore adoption49e4d15

line 161why, one step back
161async def test_auth_header_has_priority_over_netrc() -> None:

Ensure Authorization header has priority over .netrcTom Christiebefore adoption43331cf

lines 162–164why, one step back
162 os.environ["NETRC"] = "tests/.netrc"
163 url = "http://netrcexample.org"164

Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84

line 165why, one step back
165 client = AsyncClient(dispatch=MockDispatch())

Ensure Authorization header has priority over .netrcTom Christiebefore adoption43331cf

lines 166–172why, one step back
166 response = await client.get(url, headers={"Authorization": "Override"})
167168 assert response.status_code == 200169 assert response.json() == {"auth": "Override"}170171172@pytest.mark.asyncio

Type-check test_auth.py (#665)Florimond Mancabefore adoption49e4d15

line 173why, one step back
173async def test_trust_env_auth() -> None:

Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa

lines 174–176why, one step back
174 os.environ["NETRC"] = "tests/.netrc"
175 url = "http://netrcexample.org"176

Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84

line 177why, one step back
177 client = AsyncClient(dispatch=MockDispatch(), trust_env=False)

No I/O auth (#644)Tom Christiebefore adoption6c69e09

line 178why, one step back
178 response = await client.get(url)

Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa

lines 179–182why, one step back
179
180 assert response.status_code == 200181 assert response.json() == {"auth": None}182

Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84

line 183why, one step back
183 client = AsyncClient(dispatch=MockDispatch(), trust_env=True)

No I/O auth (#644)Tom Christiebefore adoption6c69e09

line 184why, one step back
184 response = await client.get(url)

Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa

lines 185–191why, one step back
185
186 assert response.status_code == 200187 assert response.json() == {188 "auth": "Basic ZXhhbXBsZS11c2VybmFtZTpleGFtcGxlLXBhc3N3b3Jk"189 }190191

Type-check test_auth.py (#665)Florimond Mancabefore adoption49e4d15

line 192why, one step back
192def test_auth_hidden_url() -> None:

Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa

lines 193–198why, one step back
193 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))197198

Drop sync (#544)Tom Christiebefore adoption206c537

line 199why, one step back
199@pytest.mark.asyncio

Type-check test_auth.py (#665)Florimond Mancabefore adoption49e4d15

line 200why, one step back
200async def test_auth_hidden_header() -> None:

Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa

lines 201–203why, one step back
201 url = "https://example.org/"
202 auth = ("example-username", "example-password")203

Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84

line 204why, one step back
204 client = AsyncClient(dispatch=MockDispatch())

Drop sync (#544)Tom Christiebefore adoption206c537

line 205why, one step back
205 response = await client.get(url, auth=auth)

Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa

lines 206–209why, one step back
206
207 assert "'authorization': '[secure]'" in str(response.request.headers)208209

Drop sync (#544)Tom Christiebefore adoption206c537

line 210why, one step back
210@pytest.mark.asyncio

Type-check test_auth.py (#665)Florimond Mancabefore adoption49e4d15

line 211why, one step back
211async def test_auth_invalid_type() -> None:

Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa

line 212why, one step back
212 url = "https://example.org/"

Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84

line 213why, one step back
213 client = AsyncClient(

Type-check test_auth.py (#665)Florimond Mancabefore adoption49e4d15

lines 214–215why, one step back
214 dispatch=MockDispatch(), auth="not a tuple, not a callable", # type: ignore
215 )

Drop sync (#544)Tom Christiebefore adoption206c537

lines 216–217why, one step back
216 with pytest.raises(TypeError):
217 await client.get(url)

Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa

lines 218–219why, one step back
218
219

Drop sync (#544)Tom Christiebefore adoption206c537

line 220why, one step back
220@pytest.mark.asyncio

Type-check test_auth.py (#665)Florimond Mancabefore adoption49e4d15

line 221why, one step back
221async def test_digest_auth_returns_no_auth_if_no_digest_header_in_response() -> None:

Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa

lines 222–224why, one step back
222 url = "https://example.org/"
223 auth = DigestAuth(username="tomchristie", password="password123")224

Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84

line 225why, one step back
225 client = AsyncClient(dispatch=MockDispatch())

Drop sync (#544)Tom Christiebefore adoption206c537

line 226why, one step back
226 response = await client.get(url, auth=auth)

Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa

lines 227–229why, one step back
227
228 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 back
230 assert len(response.history) == 0

Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa

lines 231–232why, one step back
231
232

Drop sync (#544)Tom Christiebefore adoption206c537

line 233why, one step back
233@pytest.mark.asyncio

Type-check test_auth.py (#665)Florimond Mancabefore adoption49e4d15

line 234why, one step back
234async def test_digest_auth_200_response_including_digest_auth_header() -> None:

Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa

lines 235–238why, one step back
235 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"'238

Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84

lines 239–241why, one step back
239 client = AsyncClient(
240 dispatch=MockDispatch(auth_header=auth_header, status_code=200)241 )

Drop sync (#544)Tom Christiebefore adoption206c537

line 242why, one step back
242 response = await client.get(url, auth=auth)

Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa

lines 243–245why, one step back
243
244 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 back
246 assert len(response.history) == 0

Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa

lines 247–248why, one step back
247
248

Drop sync (#544)Tom Christiebefore adoption206c537

line 249why, one step back
249@pytest.mark.asyncio

Type-check test_auth.py (#665)Florimond Mancabefore adoption49e4d15

line 250why, one step back
250async def test_digest_auth_401_response_without_digest_auth_header() -> None:

Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa

lines 251–253why, one step back
251 url = "https://example.org/"
252 auth = DigestAuth(username="tomchristie", password="password123")253

Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84

line 254why, one step back
254 client = AsyncClient(dispatch=MockDispatch(auth_header="", status_code=401))

Drop sync (#544)Tom Christiebefore adoption206c537

line 255why, one step back
255 response = await client.get(url, auth=auth)

Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa

lines 256–258why, one step back
256
257 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 back
259 assert len(response.history) == 0

Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa

lines 260–274why, one step back
260
261262@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 back
275@pytest.mark.asyncio

Type-check test_auth.py (#665)Florimond Mancabefore adoption49e4d15

lines 276–278why, one step back
276async def test_digest_auth(
277 algorithm: str, expected_hash_length: int, expected_response_length: int278) -> None:

Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa

lines 279–281why, one step back
279 url = "https://example.org/"
280 auth = DigestAuth(username="tomchristie", password="password123")281

Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84

line 282why, one step back
282 client = AsyncClient(dispatch=MockDigestAuthDispatch(algorithm=algorithm))

Drop sync (#544)Tom Christiebefore adoption206c537

line 283why, one step back
283 response = await client.get(url, auth=auth)

Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa

lines 284–285why, one step back
284
285 assert response.status_code == 200

Record history of requests made during authentication (#718)Florimond Mancabefore adoptionbc6163c

lines 286–287why, one step back
286 assert len(response.history) == 1
287

Type-check test_auth.py (#665)Florimond Mancabefore adoption49e4d15

lines 288–290why, one step back
288 authorization = typing.cast(dict, response.json())["auth"]
289 scheme, _, fields = authorization.partition(" ")290 assert scheme == "Digest"

Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa

line 291why, one step back
291

Type-check test_auth.py (#665)Florimond Mancabefore adoption49e4d15

line 292why, one step back
292 response_fields = [field.strip() for field in fields.split(",")]

Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa

lines 293–306why, one step back
293 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 + 2305306

Drop sync (#544)Tom Christiebefore adoption206c537

line 307why, one step back
307@pytest.mark.asyncio

Type-check test_auth.py (#665)Florimond Mancabefore adoption49e4d15

line 308why, one step back
308async def test_digest_auth_no_specified_qop() -> None:

Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa

lines 309–311why, one step back
309 url = "https://example.org/"
310 auth = DigestAuth(username="tomchristie", password="password123")311

Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84

line 312why, one step back
312 client = AsyncClient(dispatch=MockDigestAuthDispatch(qop=""))

Drop sync (#544)Tom Christiebefore adoption206c537

line 313why, one step back
313 response = await client.get(url, auth=auth)

Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa

lines 314–315why, one step back
314
315 assert response.status_code == 200

Record history of requests made during authentication (#718)Florimond Mancabefore adoptionbc6163c

lines 316–317why, one step back
316 assert len(response.history) == 1
317

Type-check test_auth.py (#665)Florimond Mancabefore adoption49e4d15

lines 318–320why, one step back
318 authorization = typing.cast(dict, response.json())["auth"]
319 scheme, _, fields = authorization.partition(" ")320 assert scheme == "Digest"

Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa

line 321why, one step back
321

Type-check test_auth.py (#665)Florimond Mancabefore adoption49e4d15

line 322why, one step back
322 response_fields = [field.strip() for field in fields.split(",")]

Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa

lines 323–337why, one step back
323 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 back
338@pytest.mark.asyncio

Type-check test_auth.py (#665)Florimond Mancabefore adoption49e4d15

line 339why, one step back
339async def test_digest_auth_qop_including_spaces_and_auth_returns_auth(qop: str) -> None:

Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa

lines 340–342why, one step back
340 url = "https://example.org/"
341 auth = DigestAuth(username="tomchristie", password="password123")342

Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84

line 343why, one step back
343 client = AsyncClient(dispatch=MockDigestAuthDispatch(qop=qop))

Record history of requests made during authentication (#718)Florimond Mancabefore adoptionbc6163c

line 344why, one step back
344 response = await client.get(url, auth=auth)

Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa

line 345why, one step back
345

Record history of requests made during authentication (#718)Florimond Mancabefore adoptionbc6163c

lines 346–347why, one step back
346 assert response.status_code == 200
347 assert len(response.history) == 1

Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa

line 348why, one step back
348

Record history of requests made during authentication (#718)Florimond Mancabefore adoptionbc6163c

line 349why, one step back
349

Drop sync (#544)Tom Christiebefore adoption206c537

line 350why, one step back
350@pytest.mark.asyncio

Type-check test_auth.py (#665)Florimond Mancabefore adoption49e4d15

line 351why, one step back
351async def test_digest_auth_qop_auth_int_not_implemented() -> None:

Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa

lines 352–353why, one step back
352 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 back
354 client = AsyncClient(dispatch=MockDigestAuthDispatch(qop="auth-int"))

Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa

lines 355–356why, one step back
355
356 with pytest.raises(NotImplementedError):

Drop sync (#544)Tom Christiebefore adoption206c537

line 357why, one step back
357 await client.get(url, auth=auth)

Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa

lines 358–359why, one step back
358
359

Drop sync (#544)Tom Christiebefore adoption206c537

line 360why, one step back
360@pytest.mark.asyncio

Type-check test_auth.py (#665)Florimond Mancabefore adoption49e4d15

line 361why, one step back
361async def test_digest_auth_qop_must_be_auth_or_auth_int() -> None:

Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa

lines 362–363why, one step back
362 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 back
364 client = AsyncClient(dispatch=MockDigestAuthDispatch(qop="not-auth"))

Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa

lines 365–366why, one step back
365
366 with pytest.raises(ProtocolError):

Drop sync (#544)Tom Christiebefore adoption206c537

line 367why, one step back
367 await client.get(url, auth=auth)

Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa

lines 368–369why, one step back
368
369

Drop sync (#544)Tom Christiebefore adoption206c537

line 370why, one step back
370@pytest.mark.asyncio

Type-check test_auth.py (#665)Florimond Mancabefore adoption49e4d15

line 371why, one step back
371async def test_digest_auth_incorrect_credentials() -> None:

Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa

lines 372–374why, one step back
372 url = "https://example.org/"
373 auth = DigestAuth(username="tomchristie", password="password123")374

Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84

line 375why, one step back
375 client = AsyncClient(dispatch=MockDigestAuthDispatch(send_response_after_attempt=2))

Drop sync (#544)Tom Christiebefore adoption206c537

line 376why, one step back
376 response = await client.get(url, auth=auth)

Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa

lines 377–378why, one step back
377
378 assert response.status_code == 401

Record history of requests made during authentication (#718)Florimond Mancabefore adoptionbc6163c

line 379why, one step back
379 assert len(response.history) == 1

Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa

lines 380–391why, one step back
380
381382@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 back
392@pytest.mark.asyncio

Type-check test_auth.py (#665)Florimond Mancabefore adoption49e4d15

lines 393–395why, one step back
393async def test_digest_auth_raises_protocol_error_on_malformed_header(
394 auth_header: str,395) -> None:

Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa

lines 396–397why, one step back
396 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 back
398 client = AsyncClient(
399 dispatch=MockDispatch(auth_header=auth_header, status_code=401)400 )

Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa

lines 401–402why, one step back
401
402 with pytest.raises(ProtocolError):

Drop sync (#544)Tom Christiebefore adoption206c537

line 403why, one step back
403 await client.get(url, auth=auth)

Record history of requests made during authentication (#718)Florimond Mancabefore adoptionbc6163c

lines 404–452why, one step back
404
405406@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 """420421 def __init__(self, repeat: int):422 self.repeat = repeat423424 def __call__(self, request: Request) -> AuthFlow:425 nonces = []426427 for index in range(self.repeat):428 request.headers["Authorization"] = f"Repeat {index}"429 response = yield request430 nonces.append(response.headers["www-authenticate"])431432 key = ".".join(nonces)433 request.headers["Authorization"] = f"Repeat {key}"434 yield request435436 url = "https://example.org/"437 auth = RepeatAuth(repeat=2)438 client = AsyncClient(dispatch=MockDispatch(auth_header="abc"))439440 response = await client.get(url, auth=auth)441 assert response.status_code == 200442 assert response.json() == {"auth": "Repeat abc.abc"}443444 assert len(response.history) == 2445 resp1, resp2 = response.history446 assert resp1.json() == {"auth": "Repeat 0"}447 assert resp2.json() == {"auth": "Repeat 1"}448449 assert len(resp2.history) == 1450 assert resp2.history == [resp1]451452 assert len(resp1.history) == 0

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

lines 453–465why, one step back
453
454455@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)