Skip to content
Myr

test_auth.py

This file

Path

tests/client/test_auth.py

383 lines · 12.4 KiB · Python

Last changed by

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

History of this file

Take it

The walk stopped after 200 commits: lines older than 3065dd6 point at it.

Add DigestAuth middleware (#332)Yeray Diaz Diazbefore adoptionfd3b69d

line 1why, one step back
1import hashlib

Set Host header when redirecting cross-origin (#321)Gaurav Dhameejabefore adoption3065dd6

lines 2–3why, one step back
2import json
3import os

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

line 4why, one step back
4import typing

Set Host header when redirecting cross-origin (#321)Gaurav Dhameejabefore adoption3065dd6

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

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

line 8why, one step back
8from httpx import URL, AsyncClient, DigestAuth, ProtocolError, Request, Response

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

lines 9–10why, one step back
9from httpx.config import CertTypes, TimeoutTypes, VerifyTypes
10from httpx.dispatch.base import Dispatcher

Set Host header when redirecting cross-origin (#321)Gaurav Dhameejabefore adoption3065dd6

lines 11–12why, one step back
11
12

Drop sync (#544)Tom Christiebefore adoption206c537

line 13why, one step back
13class MockDispatch(Dispatcher):

Additional DigestAuth tests (#334)Yeray Diaz Diazbefore adoption1cd9156

lines 14–17why, one step back
14 def __init__(self, auth_header: str = "", status_code: int = 200) -> None:
15 self.auth_header = auth_header16 self.status_code = status_code17

Set Host header when redirecting cross-origin (#321)Gaurav Dhameejabefore adoption3065dd6

lines 18–19why, one step back
18 async def send(
19 self,

Drop sync (#544)Tom Christiebefore adoption206c537

line 20why, one step back
20 request: Request,

Set Host header when redirecting cross-origin (#321)Gaurav Dhameejabefore adoption3065dd6

lines 21–23why, one step back
21 verify: VerifyTypes = None,
22 cert: CertTypes = None,23 timeout: TimeoutTypes = None,

Drop sync (#544)Tom Christiebefore adoption206c537

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

Additional DigestAuth tests (#334)Yeray Diaz Diazbefore adoption1cd9156

line 25why, one step back
25 headers = [("www-authenticate", self.auth_header)] if self.auth_header else []

Set Host header when redirecting cross-origin (#321)Gaurav Dhameejabefore adoption3065dd6

line 26why, one step back
26 body = json.dumps({"auth": request.headers.get("Authorization")}).encode()

Drop sync (#544)Tom Christiebefore adoption206c537

line 27why, one step back
27 return Response(

Additional DigestAuth tests (#334)Yeray Diaz Diazbefore adoption1cd9156

lines 28–29why, one step back
28 self.status_code, headers=headers, content=body, request=request
29 )

Set Host header when redirecting cross-origin (#321)Gaurav Dhameejabefore adoption3065dd6

lines 30–31why, one step back
30
31

Drop sync (#544)Tom Christiebefore adoption206c537

line 32why, one step back
32class MockDigestAuthDispatch(Dispatcher):

Add DigestAuth middleware (#332)Yeray Diaz Diazbefore adoptionfd3b69d

lines 33–47why, one step back
33 def __init__(
34 self,35 algorithm: str = "SHA-256",36 send_response_after_attempt: int = 1,37 qop: str = "auth",38 regenerate_nonce: bool = True,39 ) -> None:40 self.algorithm = algorithm41 self.send_response_after_attempt = send_response_after_attempt42 self.qop = qop43 self._regenerate_nonce = regenerate_nonce44 self._response_count = 04546 async def send(47 self,

Drop sync (#544)Tom Christiebefore adoption206c537

line 48why, one step back
48 request: Request,

Add DigestAuth middleware (#332)Yeray Diaz Diazbefore adoptionfd3b69d

lines 49–51why, one step back
49 verify: VerifyTypes = None,
50 cert: CertTypes = None,51 timeout: TimeoutTypes = None,

Drop sync (#544)Tom Christiebefore adoption206c537

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

Add DigestAuth middleware (#332)Yeray Diaz Diazbefore adoptionfd3b69d

lines 53–56why, one step back
53 if self._response_count < self.send_response_after_attempt:
54 return self.challenge_send(request)5556 body = json.dumps({"auth": request.headers.get("Authorization")}).encode()

Drop sync (#544)Tom Christiebefore adoption206c537

line 57why, one step back
57 return Response(200, content=body, request=request)

Add DigestAuth middleware (#332)Yeray Diaz Diazbefore adoptionfd3b69d

line 58why, one step back
58

Drop sync (#544)Tom Christiebefore adoption206c537

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

Add DigestAuth middleware (#332)Yeray Diaz Diazbefore adoptionfd3b69d

lines 60–83why, one step back
60 self._response_count += 1
61 nonce = (62 hashlib.sha256(os.urandom(8)).hexdigest()63 if self._regenerate_nonce64 else "ee96edced2a0b43e4869e96ebe27563f369c1205a049d06419bb51d8aeddf3d3"65 )66 challenge_data = {67 "nonce": nonce,68 "qop": self.qop,69 "opaque": (70 "ee6378f3ee14ebfd2fff54b70a91a7c9390518047f242ab2271380db0e14bda1"71 ),72 "algorithm": self.algorithm,73 "stale": "FALSE",74 }75 challenge_str = ", ".join(76 '{}="{}"'.format(key, value)77 for key, value in challenge_data.items()78 if value79 )8081 headers = [82 ("www-authenticate", 'Digest realm="httpx@example.org", ' + challenge_str)83 ]

Drop sync (#544)Tom Christiebefore adoption206c537

line 84why, one step back
84 return Response(401, headers=headers, content=b"", request=request)

Add DigestAuth middleware (#332)Yeray Diaz Diazbefore adoptionfd3b69d

lines 85–86why, one step back
85
86

Drop sync (#544)Tom Christiebefore adoption206c537

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

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

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

Set Host header when redirecting cross-origin (#321)Gaurav Dhameejabefore adoption3065dd6

lines 89–91why, one step back
89 url = "https://example.org/"
90 auth = ("tomchristie", "password123")91

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

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

Drop sync (#544)Tom Christiebefore adoption206c537

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

Set Host header when redirecting cross-origin (#321)Gaurav Dhameejabefore adoption3065dd6

lines 94–98why, one step back
94
95 assert response.status_code == 20096 assert response.json() == {"auth": "Basic dG9tY2hyaXN0aWU6cGFzc3dvcmQxMjM="}9798

Drop sync (#544)Tom Christiebefore adoption206c537

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

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

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

Set Host header when redirecting cross-origin (#321)Gaurav Dhameejabefore adoption3065dd6

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

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

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

Drop sync (#544)Tom Christiebefore adoption206c537

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

Set Host header when redirecting cross-origin (#321)Gaurav Dhameejabefore adoption3065dd6

lines 105–109why, one step back
105
106 assert response.status_code == 200107 assert response.json() == {"auth": "Basic dG9tY2hyaXN0aWU6cGFzc3dvcmQxMjM="}108109

Drop sync (#544)Tom Christiebefore adoption206c537

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

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

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

Set Host header when redirecting cross-origin (#321)Gaurav Dhameejabefore adoption3065dd6

lines 112–114why, one step back
112 url = "https://example.org/"
113 auth = ("tomchristie", "password123")114

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

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

Drop sync (#544)Tom Christiebefore adoption206c537

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

Set Host header when redirecting cross-origin (#321)Gaurav Dhameejabefore adoption3065dd6

lines 117–121why, one step back
117
118 assert response.status_code == 200119 assert response.json() == {"auth": "Basic dG9tY2hyaXN0aWU6cGFzc3dvcmQxMjM="}120121

Drop sync (#544)Tom Christiebefore adoption206c537

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

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

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

Set Host header when redirecting cross-origin (#321)Gaurav Dhameejabefore adoption3065dd6

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

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

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

Set Host header when redirecting cross-origin (#321)Gaurav Dhameejabefore adoption3065dd6

lines 127–129why, one step back
127 request.headers["Authorization"] = "Token 123"
128 return request129

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

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

Drop sync (#544)Tom Christiebefore adoption206c537

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

Set Host header when redirecting cross-origin (#321)Gaurav Dhameejabefore adoption3065dd6

lines 132–136why, one step back
132
133 assert response.status_code == 200134 assert response.json() == {"auth": "Token 123"}135136

Drop sync (#544)Tom Christiebefore adoption206c537

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

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

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

Set Host header when redirecting cross-origin (#321)Gaurav Dhameejabefore adoption3065dd6

lines 139–141why, one step back
139 os.environ["NETRC"] = "tests/.netrc"
140 url = "http://netrcexample.org"141

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

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

Drop sync (#544)Tom Christiebefore adoption206c537

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

Set Host header when redirecting cross-origin (#321)Gaurav Dhameejabefore adoption3065dd6

lines 144–150why, one step back
144
145 assert response.status_code == 200146 assert response.json() == {147 "auth": "Basic ZXhhbXBsZS11c2VybmFtZTpleGFtcGxlLXBhc3N3b3Jk"148 }149150

Drop sync (#544)Tom Christiebefore adoption206c537

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

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

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

Ensure Authorization header has priority over .netrcTom Christiebefore adoption43331cf

lines 153–155why, one step back
153 os.environ["NETRC"] = "tests/.netrc"
154 url = "http://netrcexample.org"155

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

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

Ensure Authorization header has priority over .netrcTom Christiebefore adoption43331cf

lines 157–163why, one step back
157 response = await client.get(url, headers={"Authorization": "Override"})
158159 assert response.status_code == 200160 assert response.json() == {"auth": "Override"}161162163@pytest.mark.asyncio

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

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

Set Host header when redirecting cross-origin (#321)Gaurav Dhameejabefore adoption3065dd6

lines 165–167why, one step back
165 os.environ["NETRC"] = "tests/.netrc"
166 url = "http://netrcexample.org"167

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

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

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

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

Set Host header when redirecting cross-origin (#321)Gaurav Dhameejabefore adoption3065dd6

lines 170–173why, one step back
170
171 assert response.status_code == 200172 assert response.json() == {"auth": None}173

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

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

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

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

Set Host header when redirecting cross-origin (#321)Gaurav Dhameejabefore adoption3065dd6

lines 176–182why, one step back
176
177 assert response.status_code == 200178 assert response.json() == {179 "auth": "Basic ZXhhbXBsZS11c2VybmFtZTpleGFtcGxlLXBhc3N3b3Jk"180 }181182

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

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

Set Host header when redirecting cross-origin (#321)Gaurav Dhameejabefore adoption3065dd6

lines 184–189why, one step back
184 url = "http://example-username:example-password@example.org/"
185 expected = "URL('http://example-username:[secure]@example.org/')"186 assert url == URL(url)187 assert expected == repr(URL(url))188189

Drop sync (#544)Tom Christiebefore adoption206c537

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

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

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

Set Host header when redirecting cross-origin (#321)Gaurav Dhameejabefore adoption3065dd6

lines 192–194why, one step back
192 url = "https://example.org/"
193 auth = ("example-username", "example-password")194

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

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

Drop sync (#544)Tom Christiebefore adoption206c537

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

Set Host header when redirecting cross-origin (#321)Gaurav Dhameejabefore adoption3065dd6

lines 197–200why, one step back
197
198 assert "'authorization': '[secure]'" in str(response.request.headers)199200

Drop sync (#544)Tom Christiebefore adoption206c537

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

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

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

Set Host header when redirecting cross-origin (#321)Gaurav Dhameejabefore adoption3065dd6

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

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

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

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

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

Drop sync (#544)Tom Christiebefore adoption206c537

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

Add DigestAuth middleware (#332)Yeray Diaz Diazbefore adoptionfd3b69d

lines 209–210why, one step back
209
210

Drop sync (#544)Tom Christiebefore adoption206c537

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

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

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

Add DigestAuth middleware (#332)Yeray Diaz Diazbefore adoptionfd3b69d

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

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

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

Drop sync (#544)Tom Christiebefore adoption206c537

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

Add DigestAuth middleware (#332)Yeray Diaz Diazbefore adoptionfd3b69d

lines 218–222why, one step back
218
219 assert response.status_code == 200220 assert response.json() == {"auth": None}221222

Drop sync (#544)Tom Christiebefore adoption206c537

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

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

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

Additional DigestAuth tests (#334)Yeray Diaz Diazbefore adoption1cd9156

lines 225–228why, one step back
225 url = "https://example.org/"
226 auth = DigestAuth(username="tomchristie", password="password123")227 auth_header = 'Digest realm="realm@host.com",qop="auth",nonce="abc",opaque="xyz"'228

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

lines 229–231why, one step back
229 client = AsyncClient(
230 dispatch=MockDispatch(auth_header=auth_header, status_code=200)231 )

Drop sync (#544)Tom Christiebefore adoption206c537

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

Additional DigestAuth tests (#334)Yeray Diaz Diazbefore adoption1cd9156

lines 233–237why, one step back
233
234 assert response.status_code == 200235 assert response.json() == {"auth": None}236237

Drop sync (#544)Tom Christiebefore adoption206c537

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

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

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

Additional DigestAuth tests (#334)Yeray Diaz Diazbefore adoption1cd9156

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

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

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

Drop sync (#544)Tom Christiebefore adoption206c537

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

Additional DigestAuth tests (#334)Yeray Diaz Diazbefore adoption1cd9156

lines 245–249why, one step back
245
246 assert response.status_code == 401247 assert response.json() == {"auth": None}248249

Add DigestAuth middleware (#332)Yeray Diaz Diazbefore adoptionfd3b69d

lines 250–262why, one step back
250@pytest.mark.parametrize(
251 "algorithm,expected_hash_length,expected_response_length",252 [253 ("MD5", 64, 32),254 ("MD5-SESS", 64, 32),255 ("SHA", 64, 40),256 ("SHA-SESS", 64, 40),257 ("SHA-256", 64, 64),258 ("SHA-256-SESS", 64, 64),259 ("SHA-512", 64, 128),260 ("SHA-512-SESS", 64, 128),261 ],262)

Drop sync (#544)Tom Christiebefore adoption206c537

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

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

lines 264–266why, one step back
264async def test_digest_auth(
265 algorithm: str, expected_hash_length: int, expected_response_length: int266) -> None:

Add DigestAuth middleware (#332)Yeray Diaz Diazbefore adoptionfd3b69d

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

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

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

Drop sync (#544)Tom Christiebefore adoption206c537

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

Add DigestAuth middleware (#332)Yeray Diaz Diazbefore adoptionfd3b69d

lines 272–273why, one step back
272
273 assert response.status_code == 200

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

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

Add DigestAuth middleware (#332)Yeray Diaz Diazbefore adoptionfd3b69d

line 277why, one step back
277

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

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

Add DigestAuth middleware (#332)Yeray Diaz Diazbefore adoptionfd3b69d

lines 279–292why, one step back
279 digest_data = dict(field.split("=") for field in response_fields)
280281 assert digest_data["username"] == '"tomchristie"'282 assert digest_data["realm"] == '"httpx@example.org"'283 assert "nonce" in digest_data284 assert digest_data["uri"] == '"/"'285 assert len(digest_data["response"]) == expected_response_length + 2 # extra quotes286 assert len(digest_data["opaque"]) == expected_hash_length + 2287 assert digest_data["algorithm"] == algorithm288 assert digest_data["qop"] == "auth"289 assert digest_data["nc"] == "00000001"290 assert len(digest_data["cnonce"]) == 16 + 2291292

Drop sync (#544)Tom Christiebefore adoption206c537

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

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

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

Add DigestAuth middleware (#332)Yeray Diaz Diazbefore adoptionfd3b69d

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

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

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

Drop sync (#544)Tom Christiebefore adoption206c537

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

Add DigestAuth middleware (#332)Yeray Diaz Diazbefore adoptionfd3b69d

lines 300–301why, one step back
300
301 assert response.status_code == 200

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

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

Add DigestAuth middleware (#332)Yeray Diaz Diazbefore adoptionfd3b69d

line 305why, one step back
305

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

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

Add DigestAuth middleware (#332)Yeray Diaz Diazbefore adoptionfd3b69d

lines 307–320why, one step back
307 digest_data = dict(field.split("=") for field in response_fields)
308309 assert "qop" not in digest_data310 assert "nc" not in digest_data311 assert "cnonce" not in digest_data312 assert digest_data["username"] == '"tomchristie"'313 assert digest_data["realm"] == '"httpx@example.org"'314 assert len(digest_data["nonce"]) == 64 + 2 # extra quotes315 assert digest_data["uri"] == '"/"'316 assert len(digest_data["response"]) == 64 + 2317 assert len(digest_data["opaque"]) == 64 + 2318 assert digest_data["algorithm"] == "SHA-256"319320

Additional DigestAuth tests (#334)Yeray Diaz Diazbefore adoption1cd9156

line 321why, one step back
321@pytest.mark.parametrize("qop", ("auth, auth-int", "auth,auth-int", "unknown,auth"))

Drop sync (#544)Tom Christiebefore adoption206c537

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

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

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

Add DigestAuth middleware (#332)Yeray Diaz Diazbefore adoptionfd3b69d

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

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

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

Drop sync (#544)Tom Christiebefore adoption206c537

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

Add DigestAuth middleware (#332)Yeray Diaz Diazbefore adoptionfd3b69d

lines 329–330why, one step back
329
330

Drop sync (#544)Tom Christiebefore adoption206c537

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

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

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

Add DigestAuth middleware (#332)Yeray Diaz Diazbefore adoptionfd3b69d

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

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

line 335why, one step back
335 client = AsyncClient(dispatch=MockDigestAuthDispatch(qop="auth-int"))

Add DigestAuth middleware (#332)Yeray Diaz Diazbefore adoptionfd3b69d

lines 336–337why, one step back
336
337 with pytest.raises(NotImplementedError):

Drop sync (#544)Tom Christiebefore adoption206c537

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

Add DigestAuth middleware (#332)Yeray Diaz Diazbefore adoptionfd3b69d

lines 339–340why, one step back
339
340

Drop sync (#544)Tom Christiebefore adoption206c537

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

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

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

Add DigestAuth middleware (#332)Yeray Diaz Diazbefore adoptionfd3b69d

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

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

line 345why, one step back
345 client = AsyncClient(dispatch=MockDigestAuthDispatch(qop="not-auth"))

Add DigestAuth middleware (#332)Yeray Diaz Diazbefore adoptionfd3b69d

lines 346–347why, one step back
346
347 with pytest.raises(ProtocolError):

Drop sync (#544)Tom Christiebefore adoption206c537

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

Add DigestAuth middleware (#332)Yeray Diaz Diazbefore adoptionfd3b69d

lines 349–350why, one step back
349
350

Drop sync (#544)Tom Christiebefore adoption206c537

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

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

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

Add DigestAuth middleware (#332)Yeray Diaz Diazbefore adoptionfd3b69d

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

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

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

Drop sync (#544)Tom Christiebefore adoption206c537

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

Add DigestAuth middleware (#332)Yeray Diaz Diazbefore adoptionfd3b69d

lines 358–371why, one step back
358
359 assert response.status_code == 401360361362@pytest.mark.parametrize(363 "auth_header",364 [365 'Digest realm="httpx@example.org", qop="auth"', # missing fields366 'realm="httpx@example.org", qop="auth"', # not starting with Digest367 'DigestZ realm="httpx@example.org", qop="auth"'368 'qop="auth,auth-int",nonce="abc",opaque="xyz"',369 'Digest realm="httpx@example.org", qop="auth,au', # malformed fields list370 ],371)

Drop sync (#544)Tom Christiebefore adoption206c537

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

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

lines 373–375why, one step back
373async def test_digest_auth_raises_protocol_error_on_malformed_header(
374 auth_header: str,375) -> None:

Add DigestAuth middleware (#332)Yeray Diaz Diazbefore adoptionfd3b69d

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

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

lines 378–380why, one step back
378 client = AsyncClient(
379 dispatch=MockDispatch(auth_header=auth_header, status_code=401)380 )

Add DigestAuth middleware (#332)Yeray Diaz Diazbefore adoptionfd3b69d

lines 381–382why, one step back
381
382 with pytest.raises(ProtocolError):

Drop sync (#544)Tom Christiebefore adoption206c537

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