Skip to content
Myr

test_auth.py

This file

Path

tests/client/test_auth.py

527 lines · 16.8 KiB · Python

Last changed by

Transport API (#963)Yeray Diaz Diazbefore adoptiond2816c9

History of this file

Take it

The walk stopped after 200 commits: lines older than 7a96a2c point at it.

Add support for unix domain sockets (#511)Jonas Lundbergbefore adoption7a96a2c

lines 1–2why, one step back
1import hashlib
2import os

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

line 3why, one step back
3import typing

Add support for unix domain sockets (#511)Jonas Lundbergbefore adoption7a96a2c

line 4why, one step back
4

Httpcore interface (#804)Tom Christiebefore adoption3046e92

line 5why, one step back
5import httpcore

Add support for unix domain sockets (#511)Jonas Lundbergbefore adoption7a96a2c

lines 6–7why, one step back
6import pytest
7

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

lines 8–10why, one step back
8from httpx import (
9 URL,10 AsyncClient,

Public Auth API (#732)Tom Christiebefore adoption12dd157

line 11why, one step back
11 Auth,

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

line 12why, one step back
12 DigestAuth,

Httpcore interface (#804)Tom Christiebefore adoption3046e92

line 13why, one step back
13 Headers,

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

lines 14–18why, one step back
14 ProtocolError,
15 Request,16 RequestBodyUnavailable,17 Response,18)

Httpcore interface (#804)Tom Christiebefore adoption3046e92

line 19why, one step back
19from httpx._content_streams import ContentStream, JSONStream

Add support for unix domain sockets (#511)Jonas Lundbergbefore adoption7a96a2c

lines 20–21why, one step back
20
21

Httpcore interface (#804)Tom Christiebefore adoption3046e92

lines 22–31why, one step back
22def get_header_value(headers, key, default=None):
23 lookup = key.encode("ascii").lower()24 for header_key, header_value in headers:25 if header_key.lower() == lookup:26 return header_value.decode("ascii")27 return default282930class MockDispatch(httpcore.AsyncHTTPTransport):31 def __init__(self, auth_header: bytes = b"", status_code: int = 200) -> None:

Add support for unix domain sockets (#511)Jonas Lundbergbefore adoption7a96a2c

lines 32–34why, one step back
32 self.auth_header = auth_header
33 self.status_code = status_code34

Httpcore interface (#804)Tom Christiebefore adoption3046e92

line 35why, one step back
35 async def request(

Add support for unix domain sockets (#511)Jonas Lundbergbefore adoption7a96a2c

line 36why, one step back
36 self,

Httpcore interface (#804)Tom Christiebefore adoption3046e92

lines 37–47why, one step back
37 method: bytes,
38 url: typing.Tuple[bytes, bytes, int, bytes],39 headers: typing.List[typing.Tuple[bytes, bytes]],40 stream: ContentStream,41 timeout: typing.Dict[str, typing.Optional[float]] = None,42 ) -> typing.Tuple[43 bytes, int, bytes, typing.List[typing.Tuple[bytes, bytes]], ContentStream44 ]:45 authorization = get_header_value(headers, "Authorization")46 response_headers = (47 [(b"www-authenticate", self.auth_header)] if self.auth_header else []

Add support for unix domain sockets (#511)Jonas Lundbergbefore adoption7a96a2c

line 48why, one step back
48 )

Httpcore interface (#804)Tom Christiebefore adoption3046e92

lines 49–50why, one step back
49 response_stream = JSONStream({"auth": authorization})
50 return b"HTTP/1.1", self.status_code, b"", response_headers, response_stream

Add support for unix domain sockets (#511)Jonas Lundbergbefore adoption7a96a2c

lines 51–52why, one step back
51
52

Httpcore interface (#804)Tom Christiebefore adoption3046e92

line 53why, one step back
53class MockDigestAuthDispatch(httpcore.AsyncHTTPTransport):

Add support for unix domain sockets (#511)Jonas Lundbergbefore adoption7a96a2c

lines 54–66why, one step back
54 def __init__(
55 self,56 algorithm: str = "SHA-256",57 send_response_after_attempt: int = 1,58 qop: str = "auth",59 regenerate_nonce: bool = True,60 ) -> None:61 self.algorithm = algorithm62 self.send_response_after_attempt = send_response_after_attempt63 self.qop = qop64 self._regenerate_nonce = regenerate_nonce65 self._response_count = 066

Httpcore interface (#804)Tom Christiebefore adoption3046e92

line 67why, one step back
67 async def request(

Add support for unix domain sockets (#511)Jonas Lundbergbefore adoption7a96a2c

line 68why, one step back
68 self,

Httpcore interface (#804)Tom Christiebefore adoption3046e92

lines 69–76why, one step back
69 method: bytes,
70 url: typing.Tuple[bytes, bytes, int, bytes],71 headers: typing.List[typing.Tuple[bytes, bytes]],72 stream: ContentStream,73 timeout: typing.Dict[str, typing.Optional[float]] = None,74 ) -> typing.Tuple[75 bytes, int, bytes, typing.List[typing.Tuple[bytes, bytes]], ContentStream76 ]:

Add support for unix domain sockets (#511)Jonas Lundbergbefore adoption7a96a2c

line 77why, one step back
77 if self._response_count < self.send_response_after_attempt:

Httpcore interface (#804)Tom Christiebefore adoption3046e92

line 78why, one step back
78 return self.challenge_send(method, url, headers, stream)

Add support for unix domain sockets (#511)Jonas Lundbergbefore adoption7a96a2c

line 79why, one step back
79

Httpcore interface (#804)Tom Christiebefore adoption3046e92

lines 80–82why, one step back
80 authorization = get_header_value(headers, "Authorization")
81 body = JSONStream({"auth": authorization})82 return b"HTTP/1.1", 200, b"", [], body

Add support for unix domain sockets (#511)Jonas Lundbergbefore adoption7a96a2c

line 83why, one step back
83

Httpcore interface (#804)Tom Christiebefore adoption3046e92

lines 84–86why, one step back
84 def challenge_send(
85 self, method: bytes, url: URL, headers: Headers, stream: ContentStream,86 ) -> typing.Tuple[int, bytes, Headers, ContentStream]:

Add support for unix domain sockets (#511)Jonas Lundbergbefore adoption7a96a2c

lines 87–108why, one step back
87 self._response_count += 1
88 nonce = (89 hashlib.sha256(os.urandom(8)).hexdigest()90 if self._regenerate_nonce91 else "ee96edced2a0b43e4869e96ebe27563f369c1205a049d06419bb51d8aeddf3d3"92 )93 challenge_data = {94 "nonce": nonce,95 "qop": self.qop,96 "opaque": (97 "ee6378f3ee14ebfd2fff54b70a91a7c9390518047f242ab2271380db0e14bda1"98 ),99 "algorithm": self.algorithm,100 "stale": "FALSE",101 }102 challenge_str = ", ".join(103 '{}="{}"'.format(key, value)104 for key, value in challenge_data.items()105 if value106 )107108 headers = [

Httpcore interface (#804)Tom Christiebefore adoption3046e92

lines 109–112why, one step back
109 (
110 b"www-authenticate",111 b'Digest realm="httpx@example.org", ' + challenge_str.encode("ascii"),112 )

Add support for unix domain sockets (#511)Jonas Lundbergbefore adoption7a96a2c

line 113why, one step back
113 ]

Httpcore interface (#804)Tom Christiebefore adoption3046e92

line 114why, one step back
114 return b"HTTP/1.1", 401, b"", headers, ContentStream()

Add support for unix domain sockets (#511)Jonas Lundbergbefore adoption7a96a2c

lines 115–116why, one step back
115
116

Drop sync (#544)Tom Christiebefore adoption206c537

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

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

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

Add support for unix domain sockets (#511)Jonas Lundbergbefore adoption7a96a2c

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

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

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

Drop sync (#544)Tom Christiebefore adoption206c537

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

Add support for unix domain sockets (#511)Jonas Lundbergbefore adoption7a96a2c

lines 124–128why, one step back
124
125 assert response.status_code == 200126 assert response.json() == {"auth": "Basic dG9tY2hyaXN0aWU6cGFzc3dvcmQxMjM="}127128

Drop sync (#544)Tom Christiebefore adoption206c537

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

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

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

Add support for unix domain sockets (#511)Jonas Lundbergbefore adoption7a96a2c

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

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

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

Drop sync (#544)Tom Christiebefore adoption206c537

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

Add support for unix domain sockets (#511)Jonas Lundbergbefore adoption7a96a2c

lines 135–139why, one step back
135
136 assert response.status_code == 200137 assert response.json() == {"auth": "Basic dG9tY2hyaXN0aWU6cGFzc3dvcmQxMjM="}138139

Drop sync (#544)Tom Christiebefore adoption206c537

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

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

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

Add support for unix domain sockets (#511)Jonas Lundbergbefore adoption7a96a2c

lines 142–144why, one step back
142 url = "https://example.org/"
143 auth = ("tomchristie", "password123")144

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

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

Drop sync (#544)Tom Christiebefore adoption206c537

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

Add support for unix domain sockets (#511)Jonas Lundbergbefore adoption7a96a2c

lines 147–151why, one step back
147
148 assert response.status_code == 200149 assert response.json() == {"auth": "Basic dG9tY2hyaXN0aWU6cGFzc3dvcmQxMjM="}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_custom_auth() -> None:

Add support for unix domain sockets (#511)Jonas Lundbergbefore adoption7a96a2c

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

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

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

Add support for unix domain sockets (#511)Jonas Lundbergbefore adoption7a96a2c

lines 157–159why, one step back
157 request.headers["Authorization"] = "Token 123"
158 return request159

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

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

Drop sync (#544)Tom Christiebefore adoption206c537

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

Add support for unix domain sockets (#511)Jonas Lundbergbefore adoption7a96a2c

lines 162–166why, one step back
162
163 assert response.status_code == 200164 assert response.json() == {"auth": "Token 123"}165166

Drop sync (#544)Tom Christiebefore adoption206c537

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

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

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

Add support for unix domain sockets (#511)Jonas Lundbergbefore adoption7a96a2c

lines 169–171why, one step back
169 os.environ["NETRC"] = "tests/.netrc"
170 url = "http://netrcexample.org"171

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

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

Drop sync (#544)Tom Christiebefore adoption206c537

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

Add support for unix domain sockets (#511)Jonas Lundbergbefore adoption7a96a2c

lines 174–180why, one step back
174
175 assert response.status_code == 200176 assert response.json() == {177 "auth": "Basic ZXhhbXBsZS11c2VybmFtZTpleGFtcGxlLXBhc3N3b3Jk"178 }179180

Drop sync (#544)Tom Christiebefore adoption206c537

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

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

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

Ensure Authorization header has priority over .netrcTom Christiebefore adoption43331cf

lines 183–185why, one step back
183 os.environ["NETRC"] = "tests/.netrc"
184 url = "http://netrcexample.org"185

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

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

Ensure Authorization header has priority over .netrcTom Christiebefore adoption43331cf

lines 187–193why, one step back
187 response = await client.get(url, headers={"Authorization": "Override"})
188189 assert response.status_code == 200190 assert response.json() == {"auth": "Override"}191192193@pytest.mark.asyncio

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

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

Add support for unix domain sockets (#511)Jonas Lundbergbefore adoption7a96a2c

lines 195–197why, one step back
195 os.environ["NETRC"] = "tests/.netrc"
196 url = "http://netrcexample.org"197

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

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

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

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

Add support for unix domain sockets (#511)Jonas Lundbergbefore adoption7a96a2c

lines 200–203why, one step back
200
201 assert response.status_code == 200202 assert response.json() == {"auth": None}203

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

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

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

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

Add support for unix domain sockets (#511)Jonas Lundbergbefore adoption7a96a2c

lines 206–212why, one step back
206
207 assert response.status_code == 200208 assert response.json() == {209 "auth": "Basic ZXhhbXBsZS11c2VybmFtZTpleGFtcGxlLXBhc3N3b3Jk"210 }211212

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

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

Add support for unix domain sockets (#511)Jonas Lundbergbefore adoption7a96a2c

lines 214–219why, one step back
214 url = "http://example-username:example-password@example.org/"
215 expected = "URL('http://example-username:[secure]@example.org/')"216 assert url == URL(url)217 assert expected == repr(URL(url))218219

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_auth_hidden_header() -> None:

Add support for unix domain sockets (#511)Jonas Lundbergbefore adoption7a96a2c

lines 222–224why, one step back
222 url = "https://example.org/"
223 auth = ("example-username", "example-password")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)

Add support for unix domain sockets (#511)Jonas Lundbergbefore adoption7a96a2c

lines 227–230why, one step back
227
228 assert "'authorization': '[secure]'" in str(response.request.headers)229230

Drop sync (#544)Tom Christiebefore adoption206c537

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

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

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

Add support for unix domain sockets (#511)Jonas Lundbergbefore adoption7a96a2c

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

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

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

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

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

Drop sync (#544)Tom Christiebefore adoption206c537

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

Add support for unix domain sockets (#511)Jonas Lundbergbefore adoption7a96a2c

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_returns_no_auth_if_no_digest_header_in_response() -> None:

Add support for unix domain sockets (#511)Jonas Lundbergbefore adoption7a96a2c

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

Drop sync (#544)Tom Christiebefore adoption206c537

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

Add support for unix domain sockets (#511)Jonas Lundbergbefore adoption7a96a2c

lines 248–250why, one step back
248
249 assert response.status_code == 200250 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

Add support for unix domain sockets (#511)Jonas Lundbergbefore adoption7a96a2c

lines 252–253why, one step back
252
253

Drop sync (#544)Tom Christiebefore adoption206c537

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

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

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

Add support for unix domain sockets (#511)Jonas Lundbergbefore adoption7a96a2c

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

Httpcore interface (#804)Tom Christiebefore adoption3046e92

line 258why, one step back
258 auth_header = b'Digest realm="realm@host.com",qop="auth",nonce="abc",opaque="xyz"'

Add support for unix domain sockets (#511)Jonas Lundbergbefore adoption7a96a2c

line 259why, one step back
259

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

lines 260–262why, one step back
260 client = AsyncClient(
261 dispatch=MockDispatch(auth_header=auth_header, status_code=200)262 )

Drop sync (#544)Tom Christiebefore adoption206c537

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

Add support for unix domain sockets (#511)Jonas Lundbergbefore adoption7a96a2c

lines 264–266why, one step back
264
265 assert response.status_code == 200266 assert response.json() == {"auth": None}

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

line 267why, one step back
267 assert len(response.history) == 0

Add support for unix domain sockets (#511)Jonas Lundbergbefore adoption7a96a2c

lines 268–269why, one step back
268
269

Drop sync (#544)Tom Christiebefore adoption206c537

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

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

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

Add support for unix domain sockets (#511)Jonas Lundbergbefore adoption7a96a2c

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

Httpcore interface (#804)Tom Christiebefore adoption3046e92

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

Drop sync (#544)Tom Christiebefore adoption206c537

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

Add support for unix domain sockets (#511)Jonas Lundbergbefore adoption7a96a2c

lines 277–279why, one step back
277
278 assert response.status_code == 401279 assert response.json() == {"auth": None}

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

line 280why, one step back
280 assert len(response.history) == 0

Add support for unix domain sockets (#511)Jonas Lundbergbefore adoption7a96a2c

lines 281–295why, one step back
281
282283@pytest.mark.parametrize(284 "algorithm,expected_hash_length,expected_response_length",285 [286 ("MD5", 64, 32),287 ("MD5-SESS", 64, 32),288 ("SHA", 64, 40),289 ("SHA-SESS", 64, 40),290 ("SHA-256", 64, 64),291 ("SHA-256-SESS", 64, 64),292 ("SHA-512", 64, 128),293 ("SHA-512-SESS", 64, 128),294 ],295)

Drop sync (#544)Tom Christiebefore adoption206c537

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

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

lines 297–299why, one step back
297async def test_digest_auth(
298 algorithm: str, expected_hash_length: int, expected_response_length: int299) -> None:

Add support for unix domain sockets (#511)Jonas Lundbergbefore adoption7a96a2c

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

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

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

Drop sync (#544)Tom Christiebefore adoption206c537

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

Add support for unix domain sockets (#511)Jonas Lundbergbefore adoption7a96a2c

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

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

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

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

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

Add support for unix domain sockets (#511)Jonas Lundbergbefore adoption7a96a2c

line 312why, one step back
312

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

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

Add support for unix domain sockets (#511)Jonas Lundbergbefore adoption7a96a2c

lines 314–327why, one step back
314 digest_data = dict(field.split("=") for field in response_fields)
315316 assert digest_data["username"] == '"tomchristie"'317 assert digest_data["realm"] == '"httpx@example.org"'318 assert "nonce" in digest_data319 assert digest_data["uri"] == '"/"'320 assert len(digest_data["response"]) == expected_response_length + 2 # extra quotes321 assert len(digest_data["opaque"]) == expected_hash_length + 2322 assert digest_data["algorithm"] == algorithm323 assert digest_data["qop"] == "auth"324 assert digest_data["nc"] == "00000001"325 assert len(digest_data["cnonce"]) == 16 + 2326327

Drop sync (#544)Tom Christiebefore adoption206c537

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

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

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

Add support for unix domain sockets (#511)Jonas Lundbergbefore adoption7a96a2c

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

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

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

Drop sync (#544)Tom Christiebefore adoption206c537

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

Add support for unix domain sockets (#511)Jonas Lundbergbefore adoption7a96a2c

lines 335–336why, one step back
335
336 assert response.status_code == 200

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

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

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

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

Add support for unix domain sockets (#511)Jonas Lundbergbefore adoption7a96a2c

line 342why, one step back
342

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

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

Add support for unix domain sockets (#511)Jonas Lundbergbefore adoption7a96a2c

lines 344–358why, one step back
344 digest_data = dict(field.split("=") for field in response_fields)
345346 assert "qop" not in digest_data347 assert "nc" not in digest_data348 assert "cnonce" not in digest_data349 assert digest_data["username"] == '"tomchristie"'350 assert digest_data["realm"] == '"httpx@example.org"'351 assert len(digest_data["nonce"]) == 64 + 2 # extra quotes352 assert digest_data["uri"] == '"/"'353 assert len(digest_data["response"]) == 64 + 2354 assert len(digest_data["opaque"]) == 64 + 2355 assert digest_data["algorithm"] == "SHA-256"356357358@pytest.mark.parametrize("qop", ("auth, auth-int", "auth,auth-int", "unknown,auth"))

Drop sync (#544)Tom Christiebefore adoption206c537

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

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

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

Add support for unix domain sockets (#511)Jonas Lundbergbefore adoption7a96a2c

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

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

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

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

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

Add support for unix domain sockets (#511)Jonas Lundbergbefore adoption7a96a2c

line 366why, one step back
366

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

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

Add support for unix domain sockets (#511)Jonas Lundbergbefore adoption7a96a2c

line 369why, one step back
369

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

line 370why, one step back
370

Drop sync (#544)Tom Christiebefore adoption206c537

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

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

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

Add support for unix domain sockets (#511)Jonas Lundbergbefore adoption7a96a2c

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

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

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

Add support for unix domain sockets (#511)Jonas Lundbergbefore adoption7a96a2c

lines 376–377why, one step back
376
377 with pytest.raises(NotImplementedError):

Drop sync (#544)Tom Christiebefore adoption206c537

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

Add support for unix domain sockets (#511)Jonas Lundbergbefore adoption7a96a2c

lines 379–380why, one step back
379
380

Drop sync (#544)Tom Christiebefore adoption206c537

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

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

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

Add support for unix domain sockets (#511)Jonas Lundbergbefore adoption7a96a2c

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

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

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

Add support for unix domain sockets (#511)Jonas Lundbergbefore adoption7a96a2c

lines 386–387why, one step back
386
387 with pytest.raises(ProtocolError):

Drop sync (#544)Tom Christiebefore adoption206c537

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

Add support for unix domain sockets (#511)Jonas Lundbergbefore adoption7a96a2c

lines 389–390why, one step back
389
390

Drop sync (#544)Tom Christiebefore adoption206c537

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

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

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

Add support for unix domain sockets (#511)Jonas Lundbergbefore adoption7a96a2c

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

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

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

Drop sync (#544)Tom Christiebefore adoption206c537

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

Add support for unix domain sockets (#511)Jonas Lundbergbefore adoption7a96a2c

lines 398–399why, one step back
398
399 assert response.status_code == 401

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

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

Add support for unix domain sockets (#511)Jonas Lundbergbefore adoption7a96a2c

lines 401–405why, one step back
401
402403@pytest.mark.parametrize(404 "auth_header",405 [

Httpcore interface (#804)Tom Christiebefore adoption3046e92

lines 406–410why, one step back
406 b'Digest realm="httpx@example.org", qop="auth"', # missing fields
407 b'realm="httpx@example.org", qop="auth"', # not starting with Digest408 b'DigestZ realm="httpx@example.org", qop="auth"'409 b'qop="auth,auth-int",nonce="abc",opaque="xyz"',410 b'Digest realm="httpx@example.org", qop="auth,au', # malformed fields list

Add support for unix domain sockets (#511)Jonas Lundbergbefore adoption7a96a2c

lines 411–412why, one step back
411 ],
412)

Drop sync (#544)Tom Christiebefore adoption206c537

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

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

line 414why, one step back
414async def test_digest_auth_raises_protocol_error_on_malformed_header(

Httpcore interface (#804)Tom Christiebefore adoption3046e92

line 415why, one step back
415 auth_header: bytes,

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

line 416why, one step back
416) -> None:

Add support for unix domain sockets (#511)Jonas Lundbergbefore adoption7a96a2c

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

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

lines 419–421why, one step back
419 client = AsyncClient(
420 dispatch=MockDispatch(auth_header=auth_header, status_code=401)421 )

Add support for unix domain sockets (#511)Jonas Lundbergbefore adoption7a96a2c

lines 422–423why, one step back
422
423 with pytest.raises(ProtocolError):

Drop sync (#544)Tom Christiebefore adoption206c537

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

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

lines 425–441why, one step back
425
426427@pytest.mark.asyncio428async def test_auth_history() -> None:429 """430 Test that intermediate requests sent as part of an authentication flow431 are recorded in the response history.432 """433434 class RepeatAuth(Auth):435 """436 A mock authentication scheme that requires clients to send437 the request a fixed number of times, and then send a last request containing438 an aggregation of nonces that the server sent in 'WWW-Authenticate' headers439 of intermediate responses.440 """441

Public Auth API (#732)Tom Christiebefore adoption12dd157

lines 442–443why, one step back
442 requires_request_body = True
443

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

lines 444–446why, one step back
444 def __init__(self, repeat: int):
445 self.repeat = repeat446

Public Auth API (#732)Tom Christiebefore adoption12dd157

lines 447–449why, one step back
447 def auth_flow(
448 self, request: Request449 ) -> typing.Generator[Request, Response, None]:

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

lines 450–462why, one step back
450 nonces = []
451452 for index in range(self.repeat):453 request.headers["Authorization"] = f"Repeat {index}"454 response = yield request455 nonces.append(response.headers["www-authenticate"])456457 key = ".".join(nonces)458 request.headers["Authorization"] = f"Repeat {key}"459 yield request460461 url = "https://example.org/"462 auth = RepeatAuth(repeat=2)

Httpcore interface (#804)Tom Christiebefore adoption3046e92

line 463why, one step back
463 client = AsyncClient(dispatch=MockDispatch(auth_header=b"abc"))

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

lines 464–477why, one step back
464
465 response = await client.get(url, auth=auth)466 assert response.status_code == 200467 assert response.json() == {"auth": "Repeat abc.abc"}468469 assert len(response.history) == 2470 resp1, resp2 = response.history471 assert resp1.json() == {"auth": "Repeat 0"}472 assert resp2.json() == {"auth": "Repeat 1"}473474 assert len(resp2.history) == 1475 assert resp2.history == [resp1]476477 assert len(resp1.history) == 0

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

lines 478–486why, one step back
478
479480@pytest.mark.asyncio481async def test_digest_auth_unavailable_streaming_body():482 url = "https://example.org/"483 auth = DigestAuth(username="tomchristie", password="password123")484 client = AsyncClient(dispatch=MockDispatch())485486 async def streaming_body():

Transport API (#963)Yeray Diaz Diazbefore adoptiond2816c9

line 487why, one step back
487 yield b"Example request body" # pragma: nocover

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

lines 488–490why, one step back
488
489 with pytest.raises(RequestBodyUnavailable):490 await client.post(url, data=streaming_body(), auth=auth)

Add Auth.requires_response_body attribute (#803)George Kettleboroughbefore adoptionb3db9ff

lines 491–527why, one step back
491
492493@pytest.mark.asyncio494async def test_auth_reads_response_body() -> None:495 """496 Test that we can read the response body in an auth flow if `requires_response_body`497 is set.498 """499500 class ResponseBodyAuth(Auth):501 """502 A mock authentication scheme that requires clients to send an 'Authorization'503 header, then send back the contents of the response in the 'Authorization'504 header.505 """506507 requires_response_body = True508509 def __init__(self, token):510 self.token = token511512 def auth_flow(513 self, request: Request514 ) -> typing.Generator[Request, Response, None]:515 request.headers["Authorization"] = self.token516 response = yield request517 data = response.text518 request.headers["Authorization"] = data519 yield request520521 url = "https://example.org/"522 auth = ResponseBodyAuth("xyz")523 client = AsyncClient(dispatch=MockDispatch())524525 response = await client.get(url, auth=auth)526 assert response.status_code == 200527 assert response.json() == {"auth": '{"auth": "xyz"}'}