Skip to content
Myr

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 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

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

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

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

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

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

Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa

lines 12–13why, one step back
12
13

Drop sync (#544)Tom Christiebefore adoption206c537

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

Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa

lines 15–20why, one step back
15 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 back
21 request: Request,

Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa

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

Drop sync (#544)Tom Christiebefore adoption206c537

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

Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa

lines 26–27why, one step back
26 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 back
28 return Response(

Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa

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

Drop sync (#544)Tom Christiebefore adoption206c537

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

Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa

lines 34–48why, one step back
34 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 back
49 request: Request,

Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa

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

Drop sync (#544)Tom Christiebefore adoption206c537

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

Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa

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

Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa

line 59why, one step back
59

Drop sync (#544)Tom Christiebefore adoption206c537

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

Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa

lines 61–84why, one step back
61 self._response_count += 1
62 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 back
85 return Response(401, headers=headers, content=b"", request=request)

Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa

lines 86–87why, one step back
86
87

Drop sync (#544)Tom Christiebefore adoption206c537

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

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

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

Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa

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

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

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

Drop sync (#544)Tom Christiebefore adoption206c537

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

Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa

lines 95–99why, one step back
95
96 assert response.status_code == 20097 assert response.json() == {"auth": "Basic dG9tY2hyaXN0aWU6cGFzc3dvcmQxMjM="}9899

Drop sync (#544)Tom Christiebefore adoption206c537

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

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

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

Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa

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

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

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

Drop sync (#544)Tom Christiebefore adoption206c537

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

Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa

lines 106–110why, one step back
106
107 assert response.status_code == 200108 assert response.json() == {"auth": "Basic dG9tY2hyaXN0aWU6cGFzc3dvcmQxMjM="}109110

Drop sync (#544)Tom Christiebefore adoption206c537

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

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

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

Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa

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

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

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

Drop sync (#544)Tom Christiebefore adoption206c537

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

Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa

lines 118–122why, one step back
118
119 assert response.status_code == 200120 assert response.json() == {"auth": "Basic dG9tY2hyaXN0aWU6cGFzc3dvcmQxMjM="}121122

Drop sync (#544)Tom Christiebefore adoption206c537

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

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

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

Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa

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

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

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

Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa

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

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

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

Drop sync (#544)Tom Christiebefore adoption206c537

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

Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa

lines 133–137why, one step back
133
134 assert response.status_code == 200135 assert response.json() == {"auth": "Token 123"}136137

Drop sync (#544)Tom Christiebefore adoption206c537

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

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

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

Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa

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

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

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

Drop sync (#544)Tom Christiebefore adoption206c537

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

Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa

lines 145–151why, one step back
145
146 assert response.status_code == 200147 assert response.json() == {148 "auth": "Basic ZXhhbXBsZS11c2VybmFtZTpleGFtcGxlLXBhc3N3b3Jk"149 }150151

Drop sync (#544)Tom Christiebefore adoption206c537

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

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

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

Ensure Authorization header has priority over .netrcTom Christiebefore adoption43331cf

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

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

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

Ensure Authorization header has priority over .netrcTom Christiebefore adoption43331cf

lines 158–164why, one step back
158 response = await client.get(url, headers={"Authorization": "Override"})
159160 assert response.status_code == 200161 assert response.json() == {"auth": "Override"}162163164@pytest.mark.asyncio

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

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

Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa

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

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

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

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

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

Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa

lines 171–174why, one step back
171
172 assert response.status_code == 200173 assert response.json() == {"auth": None}174

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

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

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

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

Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa

lines 177–183why, one step back
177
178 assert response.status_code == 200179 assert response.json() == {180 "auth": "Basic ZXhhbXBsZS11c2VybmFtZTpleGFtcGxlLXBhc3N3b3Jk"181 }182183

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

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

Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa

lines 185–190why, one step back
185 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))189190

Drop sync (#544)Tom Christiebefore adoption206c537

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

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

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

Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa

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

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

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

Drop sync (#544)Tom Christiebefore adoption206c537

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

Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa

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

Drop sync (#544)Tom Christiebefore adoption206c537

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

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

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

Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa

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

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

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

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

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

Drop sync (#544)Tom Christiebefore adoption206c537

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

Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa

lines 210–211why, one step back
210
211

Drop sync (#544)Tom Christiebefore adoption206c537

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

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

line 213why, one step back
213async 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 back
214 url = "https://example.org/"
215 auth = DigestAuth(username="tomchristie", password="password123")216

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

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

Drop sync (#544)Tom Christiebefore adoption206c537

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

Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa

lines 219–221why, one step back
219
220 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 back
222 assert len(response.history) == 0

Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa

lines 223–224why, one step back
223
224

Drop sync (#544)Tom Christiebefore adoption206c537

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

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

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

Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa

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

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

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

Drop sync (#544)Tom Christiebefore adoption206c537

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

Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa

lines 235–237why, one step back
235
236 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 back
238 assert len(response.history) == 0

Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa

lines 239–240why, one step back
239
240

Drop sync (#544)Tom Christiebefore adoption206c537

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

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

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

Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa

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

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

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

Drop sync (#544)Tom Christiebefore adoption206c537

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

Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa

lines 248–250why, one step back
248
249 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 back
251 assert len(response.history) == 0

Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa

lines 252–266why, one step back
252
253254@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 back
267@pytest.mark.asyncio

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

lines 268–270why, one step back
268async 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 back
271 url = "https://example.org/"
272 auth = DigestAuth(username="tomchristie", password="password123")273

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

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

Drop sync (#544)Tom Christiebefore adoption206c537

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

Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa

lines 276–277why, one step back
276
277 assert response.status_code == 200

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

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

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

lines 280–282why, one step back
280 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 back
283

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

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

Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa

lines 285–298why, one step back
285 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 + 2297298

Drop sync (#544)Tom Christiebefore adoption206c537

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

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

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

Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa

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

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

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

Drop sync (#544)Tom Christiebefore adoption206c537

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

Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa

lines 306–307why, one step back
306
307 assert response.status_code == 200

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

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

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

lines 310–312why, one step back
310 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 back
313

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

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

Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa

lines 315–329why, one step back
315 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 back
330@pytest.mark.asyncio

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

line 331why, one step back
331async 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 back
332 url = "https://example.org/"
333 auth = DigestAuth(username="tomchristie", password="password123")334

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

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

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

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

Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa

line 337why, one step back
337

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

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

Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa

line 340why, one step back
340

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

line 341why, one step back
341

Drop sync (#544)Tom Christiebefore adoption206c537

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

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

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

Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa

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

Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa

lines 347–348why, one step back
347
348 with pytest.raises(NotImplementedError):

Drop sync (#544)Tom Christiebefore adoption206c537

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

Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa

lines 350–351why, one step back
350
351

Drop sync (#544)Tom Christiebefore adoption206c537

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

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

line 353why, one step back
353async 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 back
354 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 back
356 client = AsyncClient(dispatch=MockDigestAuthDispatch(qop="not-auth"))

Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa

lines 357–358why, one step back
357
358 with pytest.raises(ProtocolError):

Drop sync (#544)Tom Christiebefore adoption206c537

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

Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa

lines 360–361why, one step back
360
361

Drop sync (#544)Tom Christiebefore adoption206c537

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

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

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

Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa

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

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

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

Drop sync (#544)Tom Christiebefore adoption206c537

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

Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa

lines 369–370why, one step back
369
370 assert response.status_code == 401

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

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

Release 0.7.3 (#362)Florimond Mancabefore adoption7a08aaa

lines 372–383why, one step back
372
373374@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 back
384@pytest.mark.asyncio

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

lines 385–387why, one step back
385async 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 back
388 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 back
390 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 back
393
394 with pytest.raises(ProtocolError):

Drop sync (#544)Tom Christiebefore adoption206c537

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

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

lines 396–444why, one step back
396
397398@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