Skip to content
Myr

test_auth.py

This file

Path

tests/client/test_auth.py

677 lines · 21.3 KiB · Python

Last changed by

Version 0.15.0 (#1301)Tom Christiebefore adoptionf932af9

History of this file

Take it

The walk stopped after 200 commits: lines older than 97d488a point at it.

Add support for sync-specific or async-specific auth flows (#1217)Florimond Mancabefore adoption016e4ee

lines 1–6why, one step back
1"""
2Integration tests for authentication.34Unit tests for auth classes also exist in tests/test_auth.py5"""6import asyncio

Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a

lines 7–8why, one step back
7import hashlib
8import os

Add support for sync-specific or async-specific auth flows (#1217)Florimond Mancabefore adoption016e4ee

line 9why, one step back
9import threading

Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a

lines 10–13why, one step back
10import typing
1112import pytest13

Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b

line 14why, one step back
14import httpx

Drop ContentStream (#1295)Tom Christiebefore adoptionfbb21fb

line 15why, one step back
15from httpx import URL, Auth, BasicAuth, DigestAuth, ProtocolError, Request, Response

Version 0.15.0 (#1301)Tom Christiebefore adoptionf932af9

line 16why, one step back
16from tests.utils import MockTransport

Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a

line 17why, one step back
17

Use relative tests directory references (#1052)Florimond Mancabefore adoption09672a9

line 18why, one step back
18from ..common import FIXTURES_DIR

Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a

line 19why, one step back
19

Use relative tests directory references (#1052)Florimond Mancabefore adoption09672a9

line 20why, one step back
20

Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d

lines 21–22why, one step back
21class App:
22 def __init__(self, auth_header: str = "", status_code: int = 200) -> None:

Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a

lines 23–25why, one step back
23 self.auth_header = auth_header
24 self.status_code = status_code25

Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d

lines 26–28why, one step back
26 def __call__(self, request: httpx.Request) -> httpx.Response:
27 headers = {"www-authenticate": self.auth_header} if self.auth_header else {}28 data = {"auth": request.headers.get("Authorization")}

Support Response(text=...), Response(html=...), Response(json=...) (#1297)Tom Christiebefore adoptionf3c2941

line 29why, one step back
29 return httpx.Response(self.status_code, headers=headers, json=data)

Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a

lines 30–31why, one step back
30
31

Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d

line 32why, one step back
32class DigestApp:

Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a

lines 33–45why, 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 = 045

Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d

line 46why, one step back
46 def __call__(self, request: httpx.Request) -> httpx.Response:

Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a

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

Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d

line 48why, one step back
48 return self.challenge_send(request)

Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a

line 49why, one step back
49

Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d

line 50why, one step back
50 data = {"auth": request.headers.get("Authorization")}

Support Response(text=...), Response(html=...), Response(json=...) (#1297)Tom Christiebefore adoptionf3c2941

line 51why, one step back
51 return httpx.Response(200, json=data)

Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a

line 52why, one step back
52

Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d

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

Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a

lines 54–74why, one step back
54 self._response_count += 1
55 nonce = (56 hashlib.sha256(os.urandom(8)).hexdigest()57 if self._regenerate_nonce58 else "ee96edced2a0b43e4869e96ebe27563f369c1205a049d06419bb51d8aeddf3d3"59 )60 challenge_data = {61 "nonce": nonce,62 "qop": self.qop,63 "opaque": (64 "ee6378f3ee14ebfd2fff54b70a91a7c9390518047f242ab2271380db0e14bda1"65 ),66 "algorithm": self.algorithm,67 "stale": "FALSE",68 }69 challenge_str = ", ".join(70 '{}="{}"'.format(key, value)71 for key, value in challenge_data.items()72 if value73 )74

Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d

lines 75–78why, one step back
75 headers = {
76 "www-authenticate": f'Digest realm="httpx@example.org", {challenge_str}',77 }78 return Response(401, headers=headers)

Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a

lines 79–80why, one step back
79
80

Increase test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067

lines 81–126why, one step back
81class RepeatAuth(Auth):
82 """83 A mock authentication scheme that requires clients to send84 the request a fixed number of times, and then send a last request containing85 an aggregation of nonces that the server sent in 'WWW-Authenticate' headers86 of intermediate responses.87 """8889 requires_request_body = True9091 def __init__(self, repeat: int):92 self.repeat = repeat9394 def auth_flow(self, request: Request) -> typing.Generator[Request, Response, None]:95 nonces = []9697 for index in range(self.repeat):98 request.headers["Authorization"] = f"Repeat {index}"99 response = yield request100 nonces.append(response.headers["www-authenticate"])101102 key = ".".join(nonces)103 request.headers["Authorization"] = f"Repeat {key}"104 yield request105106107class ResponseBodyAuth(Auth):108 """109 A mock authentication scheme that requires clients to send an 'Authorization'110 header, then send back the contents of the response in the 'Authorization'111 header.112 """113114 requires_response_body = True115116 def __init__(self, token):117 self.token = token118119 def auth_flow(self, request: Request) -> typing.Generator[Request, Response, None]:120 request.headers["Authorization"] = self.token121 response = yield request122 data = response.text123 request.headers["Authorization"] = data124 yield request125126

Add support for sync-specific or async-specific auth flows (#1217)Florimond Mancabefore adoption016e4ee

lines 127–151why, one step back
127class SyncOrAsyncAuth(Auth):
128 """129 A mock authentication scheme that uses a different implementation for the130 sync and async cases.131 """132133 def __init__(self) -> None:134 self._lock = threading.Lock()135 self._async_lock = asyncio.Lock()136137 def sync_auth_flow(138 self, request: Request139 ) -> typing.Generator[Request, Response, None]:140 with self._lock:141 request.headers["Authorization"] = "sync-auth"142 yield request143144 async def async_auth_flow(145 self, request: Request146 ) -> typing.AsyncGenerator[Request, Response]:147 async with self._async_lock:148 request.headers["Authorization"] = "async-auth"149 yield request150151

Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a

lines 152–155why, one step back
152@pytest.mark.asyncio
153async def test_basic_auth() -> None:154 url = "https://example.org/"155 auth = ("tomchristie", "password123")

Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d

line 156why, one step back
156 app = App()

Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a

line 157why, one step back
157

Version 0.15.0 (#1301)Tom Christiebefore adoptionf932af9

line 158why, one step back
158 async with httpx.AsyncClient(transport=MockTransport(app)) as client:

Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b

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

Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a

lines 160–167why, one step back
160
161 assert response.status_code == 200162 assert response.json() == {"auth": "Basic dG9tY2hyaXN0aWU6cGFzc3dvcmQxMjM="}163164165@pytest.mark.asyncio166async def test_basic_auth_in_url() -> None:167 url = "https://tomchristie:password123@example.org/"

Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d

line 168why, one step back
168 app = App()

Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a

line 169why, one step back
169

Version 0.15.0 (#1301)Tom Christiebefore adoptionf932af9

line 170why, one step back
170 async with httpx.AsyncClient(transport=MockTransport(app)) as client:

Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b

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

Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a

lines 172–180why, one step back
172
173 assert response.status_code == 200174 assert response.json() == {"auth": "Basic dG9tY2hyaXN0aWU6cGFzc3dvcmQxMjM="}175176177@pytest.mark.asyncio178async def test_basic_auth_on_session() -> None:179 url = "https://example.org/"180 auth = ("tomchristie", "password123")

Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d

line 181why, one step back
181 app = App()

Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a

line 182why, one step back
182

Version 0.15.0 (#1301)Tom Christiebefore adoptionf932af9

line 183why, one step back
183 async with httpx.AsyncClient(transport=MockTransport(app), auth=auth) as client:

Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b

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

Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a

lines 185–192why, one step back
185
186 assert response.status_code == 200187 assert response.json() == {"auth": "Basic dG9tY2hyaXN0aWU6cGFzc3dvcmQxMjM="}188189190@pytest.mark.asyncio191async def test_custom_auth() -> None:192 url = "https://example.org/"

Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d

line 193why, one step back
193 app = App()

Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a

lines 194–198why, one step back
194
195 def auth(request: Request) -> Request:196 request.headers["Authorization"] = "Token 123"197 return request198

Version 0.15.0 (#1301)Tom Christiebefore adoptionf932af9

line 199why, one step back
199 async with httpx.AsyncClient(transport=MockTransport(app)) as client:

Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b

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

Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a

lines 201–207why, one step back
201
202 assert response.status_code == 200203 assert response.json() == {"auth": "Token 123"}204205206@pytest.mark.asyncio207async def test_netrc_auth() -> None:

Use relative tests directory references (#1052)Florimond Mancabefore adoption09672a9

line 208why, one step back
208 os.environ["NETRC"] = str(FIXTURES_DIR / ".netrc")

Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a

line 209why, one step back
209 url = "http://netrcexample.org"

Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d

line 210why, one step back
210 app = App()

Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a

line 211why, one step back
211

Version 0.15.0 (#1301)Tom Christiebefore adoptionf932af9

line 212why, one step back
212 async with httpx.AsyncClient(transport=MockTransport(app)) as client:

Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b

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

Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a

lines 214–222why, one step back
214
215 assert response.status_code == 200216 assert response.json() == {217 "auth": "Basic ZXhhbXBsZS11c2VybmFtZTpleGFtcGxlLXBhc3N3b3Jk"218 }219220221@pytest.mark.asyncio222async def test_auth_header_has_priority_over_netrc() -> None:

Use relative tests directory references (#1052)Florimond Mancabefore adoption09672a9

line 223why, one step back
223 os.environ["NETRC"] = str(FIXTURES_DIR / ".netrc")

Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a

line 224why, one step back
224 url = "http://netrcexample.org"

Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d

line 225why, one step back
225 app = App()

Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a

line 226why, one step back
226

Version 0.15.0 (#1301)Tom Christiebefore adoptionf932af9

line 227why, one step back
227 async with httpx.AsyncClient(transport=MockTransport(app)) as client:

Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b

line 228why, one step back
228 response = await client.get(url, headers={"Authorization": "Override"})

Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a

lines 229–235why, one step back
229
230 assert response.status_code == 200231 assert response.json() == {"auth": "Override"}232233234@pytest.mark.asyncio235async def test_trust_env_auth() -> None:

Use relative tests directory references (#1052)Florimond Mancabefore adoption09672a9

line 236why, one step back
236 os.environ["NETRC"] = str(FIXTURES_DIR / ".netrc")

Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a

line 237why, one step back
237 url = "http://netrcexample.org"

Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d

line 238why, one step back
238 app = App()

Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a

line 239why, one step back
239

Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b

line 240why, one step back
240 async with httpx.AsyncClient(

Version 0.15.0 (#1301)Tom Christiebefore adoptionf932af9

line 241why, one step back
241 transport=MockTransport(app), trust_env=False

Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b

lines 242–243why, one step back
242 ) as client:
243 response = await client.get(url)

Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a

lines 244–247why, one step back
244
245 assert response.status_code == 200246 assert response.json() == {"auth": None}247

Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b

line 248why, one step back
248 async with httpx.AsyncClient(

Version 0.15.0 (#1301)Tom Christiebefore adoptionf932af9

line 249why, one step back
249 transport=MockTransport(app), trust_env=True

Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b

lines 250–251why, one step back
250 ) as client:
251 response = await client.get(url)

Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a

lines 252–258why, one step back
252
253 assert response.status_code == 200254 assert response.json() == {255 "auth": "Basic ZXhhbXBsZS11c2VybmFtZTpleGFtcGxlLXBhc3N3b3Jk"256 }257258

Allow disabling auth per-request using `auth=None` (#1115)Florimond Mancabefore adoptiona4463d0

lines 259–262why, one step back
259@pytest.mark.asyncio
260async def test_auth_disable_per_request() -> None:261 url = "https://example.org/"262 auth = ("tomchristie", "password123")

Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d

line 263why, one step back
263 app = App()

Allow disabling auth per-request using `auth=None` (#1115)Florimond Mancabefore adoptiona4463d0

line 264why, one step back
264

Version 0.15.0 (#1301)Tom Christiebefore adoptionf932af9

line 265why, one step back
265 async with httpx.AsyncClient(transport=MockTransport(app), auth=auth) as client:

Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b

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

Allow disabling auth per-request using `auth=None` (#1115)Florimond Mancabefore adoptiona4463d0

lines 267–271why, one step back
267
268 assert response.status_code == 200269 assert response.json() == {"auth": None}270271

Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a

lines 272–282why, one step back
272def test_auth_hidden_url() -> None:
273 url = "http://example-username:example-password@example.org/"274 expected = "URL('http://example-username:[secure]@example.org/')"275 assert url == URL(url)276 assert expected == repr(URL(url))277278279@pytest.mark.asyncio280async def test_auth_hidden_header() -> None:281 url = "https://example.org/"282 auth = ("example-username", "example-password")

Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d

line 283why, one step back
283 app = App()

Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a

line 284why, one step back
284

Version 0.15.0 (#1301)Tom Christiebefore adoptionf932af9

line 285why, one step back
285 async with httpx.AsyncClient(transport=MockTransport(app)) as client:

Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b

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

Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a

lines 287–291why, one step back
287
288 assert "'authorization': '[secure]'" in str(response.request.headers)289290291@pytest.mark.asyncio

Add `Client.auth` setter (#1185)Florimond Mancabefore adoptioncb620e6

line 292why, one step back
292async def test_auth_property() -> None:

Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d

lines 293–294why, one step back
293 app = App()
294

Version 0.15.0 (#1301)Tom Christiebefore adoptionf932af9

line 295why, one step back
295 async with httpx.AsyncClient(transport=MockTransport(app)) as client:

Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b

line 296why, one step back
296 assert client.auth is None

Add `Client.auth` setter (#1185)Florimond Mancabefore adoptioncb620e6

line 297why, one step back
297

Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b

lines 298–299why, one step back
298 client.auth = ("tomchristie", "password123") # type: ignore
299 assert isinstance(client.auth, BasicAuth)

Add `Client.auth` setter (#1185)Florimond Mancabefore adoptioncb620e6

line 300why, one step back
300

Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b

lines 301–304why, one step back
301 url = "https://example.org/"
302 response = await client.get(url)303 assert response.status_code == 200304 assert response.json() == {"auth": "Basic dG9tY2hyaXN0aWU6cGFzc3dvcmQxMjM="}

Add `Client.auth` setter (#1185)Florimond Mancabefore adoptioncb620e6

lines 305–308why, one step back
305
306307@pytest.mark.asyncio308async def test_auth_invalid_type() -> None:

Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d

lines 309–310why, one step back
309 app = App()
310

Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a

line 311why, one step back
311 with pytest.raises(TypeError):

Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b

line 312why, one step back
312 client = httpx.AsyncClient(

Version 0.15.0 (#1301)Tom Christiebefore adoptionf932af9

line 313why, one step back
313 transport=MockTransport(app),

Add `Client.auth` setter (#1185)Florimond Mancabefore adoptioncb620e6

lines 314–316why, one step back
314 auth="not a tuple, not a callable", # type: ignore
315 )316

Version 0.15.0 (#1301)Tom Christiebefore adoptionf932af9

line 317why, one step back
317 async with httpx.AsyncClient(transport=MockTransport(app)) as client:

Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b

lines 318–319why, one step back
318 with pytest.raises(TypeError):
319 await client.get(auth="not a tuple, not a callable") # type: ignore

Add `Client.auth` setter (#1185)Florimond Mancabefore adoptioncb620e6

line 320why, one step back
320

Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b

lines 321–322why, one step back
321 with pytest.raises(TypeError):
322 client.auth = "not a tuple, not a callable" # type: ignore

Add `Client.auth` setter (#1185)Florimond Mancabefore adoptioncb620e6

line 323why, one step back
323

Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a

lines 324–328why, one step back
324
325@pytest.mark.asyncio326async def test_digest_auth_returns_no_auth_if_no_digest_header_in_response() -> None:327 url = "https://example.org/"328 auth = DigestAuth(username="tomchristie", password="password123")

Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d

line 329why, one step back
329 app = App()

Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a

line 330why, one step back
330

Version 0.15.0 (#1301)Tom Christiebefore adoptionf932af9

line 331why, one step back
331 async with httpx.AsyncClient(transport=MockTransport(app)) as client:

Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b

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

Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a

lines 333–338why, one step back
333
334 assert response.status_code == 200335 assert response.json() == {"auth": None}336 assert len(response.history) == 0337338

Handle multiple auth headers correctly (#1240)Tom Christiebefore adoption33d339a

lines 339–341why, one step back
339def test_digest_auth_returns_no_auth_if_alternate_auth_scheme() -> None:
340 url = "https://example.org/"341 auth = DigestAuth(username="tomchristie", password="password123")

Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d

lines 342–343why, one step back
342 auth_header = "Token ..."
343 app = App(auth_header=auth_header, status_code=401)

Handle multiple auth headers correctly (#1240)Tom Christiebefore adoption33d339a

line 344why, one step back
344

Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d

line 345why, one step back
345 client = httpx.Client(transport=MockTransport(app))

Handle multiple auth headers correctly (#1240)Tom Christiebefore adoption33d339a

lines 346–352why, one step back
346 response = client.get(url, auth=auth)
347348 assert response.status_code == 401349 assert response.json() == {"auth": None}350 assert len(response.history) == 0351352

Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a

lines 353–356why, one step back
353@pytest.mark.asyncio
354async def test_digest_auth_200_response_including_digest_auth_header() -> None:355 url = "https://example.org/"356 auth = DigestAuth(username="tomchristie", password="password123")

Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d

lines 357–358why, one step back
357 auth_header = 'Digest realm="realm@host.com",qop="auth",nonce="abc",opaque="xyz"'
358 app = App(auth_header=auth_header, status_code=200)

Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a

line 359why, one step back
359

Version 0.15.0 (#1301)Tom Christiebefore adoptionf932af9

line 360why, one step back
360 async with httpx.AsyncClient(transport=MockTransport(app)) as client:

Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b

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

Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a

lines 362–371why, one step back
362
363 assert response.status_code == 200364 assert response.json() == {"auth": None}365 assert len(response.history) == 0366367368@pytest.mark.asyncio369async def test_digest_auth_401_response_without_digest_auth_header() -> None:370 url = "https://example.org/"371 auth = DigestAuth(username="tomchristie", password="password123")

Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d

line 372why, one step back
372 app = App(auth_header="", status_code=401)

Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a

line 373why, one step back
373

Version 0.15.0 (#1301)Tom Christiebefore adoptionf932af9

line 374why, one step back
374 async with httpx.AsyncClient(transport=MockTransport(app)) as client:

Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b

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

Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a

lines 376–400why, one step back
376
377 assert response.status_code == 401378 assert response.json() == {"auth": None}379 assert len(response.history) == 0380381382@pytest.mark.parametrize(383 "algorithm,expected_hash_length,expected_response_length",384 [385 ("MD5", 64, 32),386 ("MD5-SESS", 64, 32),387 ("SHA", 64, 40),388 ("SHA-SESS", 64, 40),389 ("SHA-256", 64, 64),390 ("SHA-256-SESS", 64, 64),391 ("SHA-512", 64, 128),392 ("SHA-512-SESS", 64, 128),393 ],394)395@pytest.mark.asyncio396async def test_digest_auth(397 algorithm: str, expected_hash_length: int, expected_response_length: int398) -> None:399 url = "https://example.org/"400 auth = DigestAuth(username="tomchristie", password="password123")

Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d

line 401why, one step back
401 app = DigestApp(algorithm=algorithm)

Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a

line 402why, one step back
402

Version 0.15.0 (#1301)Tom Christiebefore adoptionf932af9

line 403why, one step back
403 async with httpx.AsyncClient(transport=MockTransport(app)) as client:

Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b

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

Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a

lines 405–431why, one step back
405
406 assert response.status_code == 200407 assert len(response.history) == 1408409 authorization = typing.cast(dict, response.json())["auth"]410 scheme, _, fields = authorization.partition(" ")411 assert scheme == "Digest"412413 response_fields = [field.strip() for field in fields.split(",")]414 digest_data = dict(field.split("=") for field in response_fields)415416 assert digest_data["username"] == '"tomchristie"'417 assert digest_data["realm"] == '"httpx@example.org"'418 assert "nonce" in digest_data419 assert digest_data["uri"] == '"/"'420 assert len(digest_data["response"]) == expected_response_length + 2 # extra quotes421 assert len(digest_data["opaque"]) == expected_hash_length + 2422 assert digest_data["algorithm"] == algorithm423 assert digest_data["qop"] == "auth"424 assert digest_data["nc"] == "00000001"425 assert len(digest_data["cnonce"]) == 16 + 2426427428@pytest.mark.asyncio429async def test_digest_auth_no_specified_qop() -> None:430 url = "https://example.org/"431 auth = DigestAuth(username="tomchristie", password="password123")

Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d

line 432why, one step back
432 app = DigestApp(qop="")

Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a

line 433why, one step back
433

Version 0.15.0 (#1301)Tom Christiebefore adoptionf932af9

line 434why, one step back
434 async with httpx.AsyncClient(transport=MockTransport(app)) as client:

Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b

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

Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a

lines 436–463why, one step back
436
437 assert response.status_code == 200438 assert len(response.history) == 1439440 authorization = typing.cast(dict, response.json())["auth"]441 scheme, _, fields = authorization.partition(" ")442 assert scheme == "Digest"443444 response_fields = [field.strip() for field in fields.split(",")]445 digest_data = dict(field.split("=") for field in response_fields)446447 assert "qop" not in digest_data448 assert "nc" not in digest_data449 assert "cnonce" not in digest_data450 assert digest_data["username"] == '"tomchristie"'451 assert digest_data["realm"] == '"httpx@example.org"'452 assert len(digest_data["nonce"]) == 64 + 2 # extra quotes453 assert digest_data["uri"] == '"/"'454 assert len(digest_data["response"]) == 64 + 2455 assert len(digest_data["opaque"]) == 64 + 2456 assert digest_data["algorithm"] == "SHA-256"457458459@pytest.mark.parametrize("qop", ("auth, auth-int", "auth,auth-int", "unknown,auth"))460@pytest.mark.asyncio461async def test_digest_auth_qop_including_spaces_and_auth_returns_auth(qop: str) -> None:462 url = "https://example.org/"463 auth = DigestAuth(username="tomchristie", password="password123")

Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d

line 464why, one step back
464 app = DigestApp(qop=qop)

Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a

line 465why, one step back
465

Version 0.15.0 (#1301)Tom Christiebefore adoptionf932af9

line 466why, one step back
466 async with httpx.AsyncClient(transport=MockTransport(app)) as client:

Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b

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

Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a

lines 468–476why, one step back
468
469 assert response.status_code == 200470 assert len(response.history) == 1471472473@pytest.mark.asyncio474async def test_digest_auth_qop_auth_int_not_implemented() -> None:475 url = "https://example.org/"476 auth = DigestAuth(username="tomchristie", password="password123")

Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d

line 477why, one step back
477 app = DigestApp(qop="auth-int")

Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a

line 478why, one step back
478

Version 0.15.0 (#1301)Tom Christiebefore adoptionf932af9

line 479why, one step back
479 async with httpx.AsyncClient(transport=MockTransport(app)) as client:

Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b

lines 480–481why, one step back
480 with pytest.raises(NotImplementedError):
481 await client.get(url, auth=auth)

Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a

lines 482–487why, one step back
482
483484@pytest.mark.asyncio485async def test_digest_auth_qop_must_be_auth_or_auth_int() -> None:486 url = "https://example.org/"487 auth = DigestAuth(username="tomchristie", password="password123")

Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d

line 488why, one step back
488 app = DigestApp(qop="not-auth")

Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a

line 489why, one step back
489

Version 0.15.0 (#1301)Tom Christiebefore adoptionf932af9

line 490why, one step back
490 async with httpx.AsyncClient(transport=MockTransport(app)) as client:

Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b

lines 491–492why, one step back
491 with pytest.raises(ProtocolError):
492 await client.get(url, auth=auth)

Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a

lines 493–498why, one step back
493
494495@pytest.mark.asyncio496async def test_digest_auth_incorrect_credentials() -> None:497 url = "https://example.org/"498 auth = DigestAuth(username="tomchristie", password="password123")

Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d

line 499why, one step back
499 app = DigestApp(send_response_after_attempt=2)

Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a

line 500why, one step back
500

Version 0.15.0 (#1301)Tom Christiebefore adoptionf932af9

line 501why, one step back
501 async with httpx.AsyncClient(transport=MockTransport(app)) as client:

Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b

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

Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a

lines 503–510why, one step back
503
504 assert response.status_code == 401505 assert len(response.history) == 1506507508@pytest.mark.parametrize(509 "auth_header",510 [

Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d

lines 511–512why, one step back
511 'Digest realm="httpx@example.org", qop="auth"', # missing fields
512 'Digest realm="httpx@example.org", qop="auth,au', # malformed fields list

Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a

lines 513–515why, one step back
513 ],
514)515@pytest.mark.asyncio

Increase test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067

line 516why, one step back
516async def test_async_digest_auth_raises_protocol_error_on_malformed_header(

Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d

line 517why, one step back
517 auth_header: str,

Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a

lines 518–520why, one step back
518) -> None:
519 url = "https://example.org/"520 auth = DigestAuth(username="tomchristie", password="password123")

Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d

lines 521–522why, one step back
521 app = App(auth_header=auth_header, status_code=401)
522

Version 0.15.0 (#1301)Tom Christiebefore adoptionf932af9

line 523why, one step back
523 async with httpx.AsyncClient(transport=MockTransport(app)) as client:

Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b

lines 524–525why, one step back
524 with pytest.raises(ProtocolError):
525 await client.get(url, auth=auth)

Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a

lines 526–527why, one step back
526
527

Increase test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067

lines 528–530why, one step back
528@pytest.mark.parametrize(
529 "auth_header",530 [

Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d

lines 531–532why, one step back
531 'Digest realm="httpx@example.org", qop="auth"', # missing fields
532 'Digest realm="httpx@example.org", qop="auth,au', # malformed fields list

Increase test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067

lines 533–535why, one step back
533 ],
534)535def test_sync_digest_auth_raises_protocol_error_on_malformed_header(

Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d

line 536why, one step back
536 auth_header: str,

Increase test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067

lines 537–539why, one step back
537) -> None:
538 url = "https://example.org/"539 auth = DigestAuth(username="tomchristie", password="password123")

Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d

line 540why, one step back
540 app = App(auth_header=auth_header, status_code=401)

Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b

line 541why, one step back
541

Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d

line 542why, one step back
542 with httpx.Client(transport=MockTransport(app)) as client:

Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b

lines 543–544why, one step back
543 with pytest.raises(ProtocolError):
544 client.get(url, auth=auth)

Increase test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067

lines 545–546why, one step back
545
546

Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a

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

Increase test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067

line 548why, one step back
548async def test_async_auth_history() -> None:

Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a

lines 549–552why, one step back
549 """
550 Test that intermediate requests sent as part of an authentication flow551 are recorded in the response history.552 """

Increase test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067

lines 553–554why, one step back
553 url = "https://example.org/"
554 auth = RepeatAuth(repeat=2)

Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d

line 555why, one step back
555 app = App(auth_header="abc")

Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a

line 556why, one step back
556

Version 0.15.0 (#1301)Tom Christiebefore adoptionf932af9

line 557why, one step back
557 async with httpx.AsyncClient(transport=MockTransport(app)) as client:

Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b

lines 558–559why, one step back
558 response = await client.get(url, auth=auth)
559

Increase test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067

lines 560–561why, one step back
560 assert response.status_code == 200
561 assert response.json() == {"auth": "Repeat abc.abc"}

Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a

line 562why, one step back
562

Increase test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067

lines 563–566why, one step back
563 assert len(response.history) == 2
564 resp1, resp2 = response.history565 assert resp1.json() == {"auth": "Repeat 0"}566 assert resp2.json() == {"auth": "Repeat 1"}

Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a

line 567why, one step back
567

Increase test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067

lines 568–569why, one step back
568 assert len(resp2.history) == 1
569 assert resp2.history == [resp1]

Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a

line 570why, one step back
570

Increase test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067

line 571why, one step back
571 assert len(resp1.history) == 0

Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a

lines 572–573why, one step back
572
573

Increase test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067

lines 574–578why, one step back
574def test_sync_auth_history() -> None:
575 """576 Test that intermediate requests sent as part of an authentication flow577 are recorded in the response history.578 """

Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a

lines 579–580why, one step back
579 url = "https://example.org/"
580 auth = RepeatAuth(repeat=2)

Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d

line 581why, one step back
581 app = App(auth_header="abc")

Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a

line 582why, one step back
582

Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d

line 583why, one step back
583 with httpx.Client(transport=MockTransport(app)) as client:

Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b

lines 584–585why, one step back
584 response = client.get(url, auth=auth)
585

Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a

lines 586–603why, one step back
586 assert response.status_code == 200
587 assert response.json() == {"auth": "Repeat abc.abc"}588589 assert len(response.history) == 2590 resp1, resp2 = response.history591 assert resp1.json() == {"auth": "Repeat 0"}592 assert resp2.json() == {"auth": "Repeat 1"}593594 assert len(resp2.history) == 1595 assert resp2.history == [resp1]596597 assert len(resp1.history) == 0598599600@pytest.mark.asyncio601async def test_digest_auth_unavailable_streaming_body():602 url = "https://example.org/"603 auth = DigestAuth(username="tomchristie", password="password123")

Drop ContentStream (#1295)Tom Christiebefore adoptionfbb21fb

line 604why, one step back
604 app = DigestApp()

Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a

lines 605–606why, one step back
605
606 async def streaming_body():

Transport API (#963)Yeray Diaz Diazbefore adoptiond2816c9

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

Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a

line 608why, one step back
608

Version 0.15.0 (#1301)Tom Christiebefore adoptionf932af9

line 609why, one step back
609 async with httpx.AsyncClient(transport=MockTransport(app)) as client:

Drop ContentStream (#1295)Tom Christiebefore adoptionfbb21fb

line 610why, one step back
610 with pytest.raises(httpx.StreamConsumed):

Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b

line 611why, one step back
611 await client.post(url, data=streaming_body(), auth=auth)

Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a

lines 612–614why, one step back
612
613614@pytest.mark.asyncio

Increase test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067

line 615why, one step back
615async def test_async_auth_reads_response_body() -> None:

Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a

lines 616–619why, one step back
616 """
617 Test that we can read the response body in an auth flow if `requires_response_body`618 is set.619 """

Increase test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067

lines 620–621why, one step back
620 url = "https://example.org/"
621 auth = ResponseBodyAuth("xyz")

Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d

lines 622–623why, one step back
622 app = App()
623

Version 0.15.0 (#1301)Tom Christiebefore adoptionf932af9

line 624why, one step back
624 async with httpx.AsyncClient(transport=MockTransport(app)) as client:

Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b

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

Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a

line 626why, one step back
626

Increase test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067

lines 627–628why, one step back
627 assert response.status_code == 200
628 assert response.json() == {"auth": '{"auth": "xyz"}'}

Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a

lines 629–630why, one step back
629
630

Increase test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067

lines 631–635why, one step back
631def test_sync_auth_reads_response_body() -> None:
632 """633 Test that we can read the response body in an auth flow if `requires_response_body`634 is set.635 """

Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a

lines 636–637why, one step back
636 url = "https://example.org/"
637 auth = ResponseBodyAuth("xyz")

Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d

line 638why, one step back
638 app = App()

Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a

line 639why, one step back
639

Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d

line 640why, one step back
640 with httpx.Client(transport=MockTransport(app)) as client:

Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b

lines 641–642why, one step back
641 response = client.get(url, auth=auth)
642

Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a

lines 643–644why, one step back
643 assert response.status_code == 200
644 assert response.json() == {"auth": '{"auth": "xyz"}'}

Add support for sync-specific or async-specific auth flows (#1217)Florimond Mancabefore adoption016e4ee

lines 645–655why, one step back
645
646647@pytest.mark.asyncio648async def test_async_auth() -> None:649 """650 Test that we can use an auth implementation specific to the async case, to651 support cases that require performing I/O or using concurrency primitives (such652 as checking a disk-based cache or fetching a token from a remote auth server).653 """654 url = "https://example.org/"655 auth = SyncOrAsyncAuth()

Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d

line 656why, one step back
656 app = App()

Add support for sync-specific or async-specific auth flows (#1217)Florimond Mancabefore adoption016e4ee

line 657why, one step back
657

Version 0.15.0 (#1301)Tom Christiebefore adoptionf932af9

line 658why, one step back
658 async with httpx.AsyncClient(transport=MockTransport(app)) as client:

Add support for sync-specific or async-specific auth flows (#1217)Florimond Mancabefore adoption016e4ee

lines 659–670why, one step back
659 response = await client.get(url, auth=auth)
660661 assert response.status_code == 200662 assert response.json() == {"auth": "async-auth"}663664665def test_sync_auth() -> None:666 """667 Test that we can use an auth implementation specific to the sync case.668 """669 url = "https://example.org/"670 auth = SyncOrAsyncAuth()

Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d

line 671why, one step back
671 app = App()

Add support for sync-specific or async-specific auth flows (#1217)Florimond Mancabefore adoption016e4ee

line 672why, one step back
672

Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d

line 673why, one step back
673 with httpx.Client(transport=MockTransport(app)) as client:

Add support for sync-specific or async-specific auth flows (#1217)Florimond Mancabefore adoption016e4ee

lines 674–677why, one step back
674 response = client.get(url, auth=auth)
675676 assert response.status_code == 200677 assert response.json() == {"auth": "sync-auth"}