test_auth.py
This file
Path
tests/client/test_auth.py
681 lines · 21.5 KiB · Python
Last changed by
Support Response(text=...), Response(html=...), Response(json=...) (#1297)Tom Christiebefore adoptionf3c2941
History of this fileTake it
The walk stopped after 200 commits: lines older than 19bf211 point at it.
Add support for sync-specific or async-specific auth flows (#1217)Florimond Mancabefore adoption016e4ee
lines 1–6why, one step back1"""2Integration tests for authentication.34Unit tests for auth classes also exist in tests/test_auth.py5"""6import asyncioTest Suite on GitHub Actions (#907)Tom Christiebefore adoption19bf211
lines 7–8why, one step back7import hashlib8import osAdd support for sync-specific or async-specific auth flows (#1217)Florimond Mancabefore adoption016e4ee
line 9why, one step back9import threadingTest Suite on GitHub Actions (#907)Tom Christiebefore adoption19bf211
lines 10–13why, one step back10import typing1112import pytest13Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
line 14why, one step back14import httpxDrop ContentStream (#1295)Tom Christiebefore adoptionfbb21fb
line 15why, one step back15from httpx import URL, Auth, BasicAuth, DigestAuth, ProtocolError, Request, ResponseRefactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 16why, one step back16from tests.utils import AsyncMockTransport, MockTransportTest Suite on GitHub Actions (#907)Tom Christiebefore adoption19bf211
line 17why, one step back17Use relative tests directory references (#1052)Florimond Mancabefore adoption09672a9
line 18why, one step back18from ..common import FIXTURES_DIRTest Suite on GitHub Actions (#907)Tom Christiebefore adoption19bf211
line 19why, one step back19Use relative tests directory references (#1052)Florimond Mancabefore adoption09672a9
line 20why, one step back20Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
lines 21–22why, one step back21class App:22 def __init__(self, auth_header: str = "", status_code: int = 200) -> None:Test Suite on GitHub Actions (#907)Tom Christiebefore adoption19bf211
lines 23–25why, one step back23 self.auth_header = auth_header24 self.status_code = status_code25Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
lines 26–28why, one step back26 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 back29 return httpx.Response(self.status_code, headers=headers, json=data)Test Suite on GitHub Actions (#907)Tom Christiebefore adoption19bf211
lines 30–31why, one step back3031Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 32why, one step back32class DigestApp:Test Suite on GitHub Actions (#907)Tom Christiebefore adoption19bf211
lines 33–45why, one step back33 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 = 045Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 46why, one step back46 def __call__(self, request: httpx.Request) -> httpx.Response:Test Suite on GitHub Actions (#907)Tom Christiebefore adoption19bf211
line 47why, one step back47 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 back48 return self.challenge_send(request)Test Suite on GitHub Actions (#907)Tom Christiebefore adoption19bf211
line 49why, one step back49Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 50why, one step back50 data = {"auth": request.headers.get("Authorization")}Support Response(text=...), Response(html=...), Response(json=...) (#1297)Tom Christiebefore adoptionf3c2941
line 51why, one step back51 return httpx.Response(200, json=data)Test Suite on GitHub Actions (#907)Tom Christiebefore adoption19bf211
line 52why, one step back52Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 53why, one step back53 def challenge_send(self, request: httpx.Request) -> httpx.Response:Test Suite on GitHub Actions (#907)Tom Christiebefore adoption19bf211
lines 54–74why, one step back54 self._response_count += 155 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 )74Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
lines 75–78why, one step back75 headers = {76 "www-authenticate": f'Digest realm="httpx@example.org", {challenge_str}',77 }78 return Response(401, headers=headers)Test Suite on GitHub Actions (#907)Tom Christiebefore adoption19bf211
lines 79–80why, one step back7980Increase test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067
lines 81–126why, one step back81class 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 request125126Add support for sync-specific or async-specific auth flows (#1217)Florimond Mancabefore adoption016e4ee
lines 127–151why, one step back127class 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 request150151Test Suite on GitHub Actions (#907)Tom Christiebefore adoption19bf211
lines 152–155why, one step back152@pytest.mark.asyncio153async 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 back156 app = App()Test Suite on GitHub Actions (#907)Tom Christiebefore adoption19bf211
line 157why, one step back157Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 158why, one step back158 async with httpx.AsyncClient(transport=AsyncMockTransport(app)) as client:Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
line 159why, one step back159 response = await client.get(url, auth=auth)Test Suite on GitHub Actions (#907)Tom Christiebefore adoption19bf211
lines 160–167why, one step back160161 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 back168 app = App()Test Suite on GitHub Actions (#907)Tom Christiebefore adoption19bf211
line 169why, one step back169Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 170why, one step back170 async with httpx.AsyncClient(transport=AsyncMockTransport(app)) as client:Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
line 171why, one step back171 response = await client.get(url)Test Suite on GitHub Actions (#907)Tom Christiebefore adoption19bf211
lines 172–180why, one step back172173 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 back181 app = App()Test Suite on GitHub Actions (#907)Tom Christiebefore adoption19bf211
line 182why, one step back182Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
lines 183–185why, one step back183 async with httpx.AsyncClient(184 transport=AsyncMockTransport(app), auth=auth185 ) as client:Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
line 186why, one step back186 response = await client.get(url)Test Suite on GitHub Actions (#907)Tom Christiebefore adoption19bf211
lines 187–194why, one step back187188 assert response.status_code == 200189 assert response.json() == {"auth": "Basic dG9tY2hyaXN0aWU6cGFzc3dvcmQxMjM="}190191192@pytest.mark.asyncio193async def test_custom_auth() -> None:194 url = "https://example.org/"Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 195why, one step back195 app = App()Test Suite on GitHub Actions (#907)Tom Christiebefore adoption19bf211
lines 196–200why, one step back196197 def auth(request: Request) -> Request:198 request.headers["Authorization"] = "Token 123"199 return request200Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 201why, one step back201 async with httpx.AsyncClient(transport=AsyncMockTransport(app)) as client:Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
line 202why, one step back202 response = await client.get(url, auth=auth)Test Suite on GitHub Actions (#907)Tom Christiebefore adoption19bf211
lines 203–209why, one step back203204 assert response.status_code == 200205 assert response.json() == {"auth": "Token 123"}206207208@pytest.mark.asyncio209async def test_netrc_auth() -> None:Use relative tests directory references (#1052)Florimond Mancabefore adoption09672a9
line 210why, one step back210 os.environ["NETRC"] = str(FIXTURES_DIR / ".netrc")Test Suite on GitHub Actions (#907)Tom Christiebefore adoption19bf211
line 211why, one step back211 url = "http://netrcexample.org"Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 212why, one step back212 app = App()Test Suite on GitHub Actions (#907)Tom Christiebefore adoption19bf211
line 213why, one step back213Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 214why, one step back214 async with httpx.AsyncClient(transport=AsyncMockTransport(app)) as client:Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
line 215why, one step back215 response = await client.get(url)Test Suite on GitHub Actions (#907)Tom Christiebefore adoption19bf211
lines 216–224why, one step back216217 assert response.status_code == 200218 assert response.json() == {219 "auth": "Basic ZXhhbXBsZS11c2VybmFtZTpleGFtcGxlLXBhc3N3b3Jk"220 }221222223@pytest.mark.asyncio224async def test_auth_header_has_priority_over_netrc() -> None:Use relative tests directory references (#1052)Florimond Mancabefore adoption09672a9
line 225why, one step back225 os.environ["NETRC"] = str(FIXTURES_DIR / ".netrc")Test Suite on GitHub Actions (#907)Tom Christiebefore adoption19bf211
line 226why, one step back226 url = "http://netrcexample.org"Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 227why, one step back227 app = App()Test Suite on GitHub Actions (#907)Tom Christiebefore adoption19bf211
line 228why, one step back228Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 229why, one step back229 async with httpx.AsyncClient(transport=AsyncMockTransport(app)) as client:Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
line 230why, one step back230 response = await client.get(url, headers={"Authorization": "Override"})Test Suite on GitHub Actions (#907)Tom Christiebefore adoption19bf211
lines 231–237why, one step back231232 assert response.status_code == 200233 assert response.json() == {"auth": "Override"}234235236@pytest.mark.asyncio237async def test_trust_env_auth() -> None:Use relative tests directory references (#1052)Florimond Mancabefore adoption09672a9
line 238why, one step back238 os.environ["NETRC"] = str(FIXTURES_DIR / ".netrc")Test Suite on GitHub Actions (#907)Tom Christiebefore adoption19bf211
line 239why, one step back239 url = "http://netrcexample.org"Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 240why, one step back240 app = App()Test Suite on GitHub Actions (#907)Tom Christiebefore adoption19bf211
line 241why, one step back241Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
line 242why, one step back242 async with httpx.AsyncClient(Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 243why, one step back243 transport=AsyncMockTransport(app), trust_env=FalseClosing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
lines 244–245why, one step back244 ) as client:245 response = await client.get(url)Test Suite on GitHub Actions (#907)Tom Christiebefore adoption19bf211
lines 246–249why, one step back246247 assert response.status_code == 200248 assert response.json() == {"auth": None}249Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
line 250why, one step back250 async with httpx.AsyncClient(Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 251why, one step back251 transport=AsyncMockTransport(app), trust_env=TrueClosing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
lines 252–253why, one step back252 ) as client:253 response = await client.get(url)Test Suite on GitHub Actions (#907)Tom Christiebefore adoption19bf211
lines 254–260why, one step back254255 assert response.status_code == 200256 assert response.json() == {257 "auth": "Basic ZXhhbXBsZS11c2VybmFtZTpleGFtcGxlLXBhc3N3b3Jk"258 }259260Allow disabling auth per-request using `auth=None` (#1115)Florimond Mancabefore adoptiona4463d0
lines 261–264why, one step back261@pytest.mark.asyncio262async def test_auth_disable_per_request() -> None:263 url = "https://example.org/"264 auth = ("tomchristie", "password123")Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 265why, one step back265 app = App()Allow disabling auth per-request using `auth=None` (#1115)Florimond Mancabefore adoptiona4463d0
line 266why, one step back266Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
lines 267–269why, one step back267 async with httpx.AsyncClient(268 transport=AsyncMockTransport(app), auth=auth269 ) as client:Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
line 270why, one step back270 response = await client.get(url, auth=None)Allow disabling auth per-request using `auth=None` (#1115)Florimond Mancabefore adoptiona4463d0
lines 271–275why, one step back271272 assert response.status_code == 200273 assert response.json() == {"auth": None}274275Test Suite on GitHub Actions (#907)Tom Christiebefore adoption19bf211
lines 276–286why, one step back276def test_auth_hidden_url() -> None:277 url = "http://example-username:example-password@example.org/"278 expected = "URL('http://example-username:[secure]@example.org/')"279 assert url == URL(url)280 assert expected == repr(URL(url))281282283@pytest.mark.asyncio284async def test_auth_hidden_header() -> None:285 url = "https://example.org/"286 auth = ("example-username", "example-password")Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 287why, one step back287 app = App()Test Suite on GitHub Actions (#907)Tom Christiebefore adoption19bf211
line 288why, one step back288Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 289why, one step back289 async with httpx.AsyncClient(transport=AsyncMockTransport(app)) as client:Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
line 290why, one step back290 response = await client.get(url, auth=auth)Test Suite on GitHub Actions (#907)Tom Christiebefore adoption19bf211
lines 291–295why, one step back291292 assert "'authorization': '[secure]'" in str(response.request.headers)293294295@pytest.mark.asyncioAdd `Client.auth` setter (#1185)Florimond Mancabefore adoptioncb620e6
line 296why, one step back296async def test_auth_property() -> None:Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
lines 297–299why, one step back297 app = App()298299 async with httpx.AsyncClient(transport=AsyncMockTransport(app)) as client:Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
line 300why, one step back300 assert client.auth is NoneAdd `Client.auth` setter (#1185)Florimond Mancabefore adoptioncb620e6
line 301why, one step back301Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
lines 302–303why, one step back302 client.auth = ("tomchristie", "password123") # type: ignore303 assert isinstance(client.auth, BasicAuth)Add `Client.auth` setter (#1185)Florimond Mancabefore adoptioncb620e6
line 304why, one step back304Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
lines 305–308why, one step back305 url = "https://example.org/"306 response = await client.get(url)307 assert response.status_code == 200308 assert response.json() == {"auth": "Basic dG9tY2hyaXN0aWU6cGFzc3dvcmQxMjM="}Add `Client.auth` setter (#1185)Florimond Mancabefore adoptioncb620e6
lines 309–312why, one step back309310311@pytest.mark.asyncio312async def test_auth_invalid_type() -> None:Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
lines 313–314why, one step back313 app = App()314Test Suite on GitHub Actions (#907)Tom Christiebefore adoption19bf211
line 315why, one step back315 with pytest.raises(TypeError):Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
line 316why, one step back316 client = httpx.AsyncClient(Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 317why, one step back317 transport=AsyncMockTransport(app),Add `Client.auth` setter (#1185)Florimond Mancabefore adoptioncb620e6
lines 318–320why, one step back318 auth="not a tuple, not a callable", # type: ignore319 )320Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 321why, one step back321 async with httpx.AsyncClient(transport=AsyncMockTransport(app)) as client:Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
lines 322–323why, one step back322 with pytest.raises(TypeError):323 await client.get(auth="not a tuple, not a callable") # type: ignoreAdd `Client.auth` setter (#1185)Florimond Mancabefore adoptioncb620e6
line 324why, one step back324Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
lines 325–326why, one step back325 with pytest.raises(TypeError):326 client.auth = "not a tuple, not a callable" # type: ignoreAdd `Client.auth` setter (#1185)Florimond Mancabefore adoptioncb620e6
line 327why, one step back327Test Suite on GitHub Actions (#907)Tom Christiebefore adoption19bf211
lines 328–332why, one step back328329@pytest.mark.asyncio330async def test_digest_auth_returns_no_auth_if_no_digest_header_in_response() -> None:331 url = "https://example.org/"332 auth = DigestAuth(username="tomchristie", password="password123")Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 333why, one step back333 app = App()Test Suite on GitHub Actions (#907)Tom Christiebefore adoption19bf211
line 334why, one step back334Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 335why, one step back335 async with httpx.AsyncClient(transport=AsyncMockTransport(app)) as client:Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
line 336why, one step back336 response = await client.get(url, auth=auth)Test Suite on GitHub Actions (#907)Tom Christiebefore adoption19bf211
lines 337–342why, one step back337338 assert response.status_code == 200339 assert response.json() == {"auth": None}340 assert len(response.history) == 0341342Handle multiple auth headers correctly (#1240)Tom Christiebefore adoption33d339a
lines 343–345why, one step back343def test_digest_auth_returns_no_auth_if_alternate_auth_scheme() -> None:344 url = "https://example.org/"345 auth = DigestAuth(username="tomchristie", password="password123")Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
lines 346–347why, one step back346 auth_header = "Token ..."347 app = App(auth_header=auth_header, status_code=401)Handle multiple auth headers correctly (#1240)Tom Christiebefore adoption33d339a
line 348why, one step back348Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 349why, one step back349 client = httpx.Client(transport=MockTransport(app))Handle multiple auth headers correctly (#1240)Tom Christiebefore adoption33d339a
lines 350–356why, one step back350 response = client.get(url, auth=auth)351352 assert response.status_code == 401353 assert response.json() == {"auth": None}354 assert len(response.history) == 0355356Test Suite on GitHub Actions (#907)Tom Christiebefore adoption19bf211
lines 357–360why, one step back357@pytest.mark.asyncio358async def test_digest_auth_200_response_including_digest_auth_header() -> None:359 url = "https://example.org/"360 auth = DigestAuth(username="tomchristie", password="password123")Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
lines 361–362why, one step back361 auth_header = 'Digest realm="realm@host.com",qop="auth",nonce="abc",opaque="xyz"'362 app = App(auth_header=auth_header, status_code=200)Test Suite on GitHub Actions (#907)Tom Christiebefore adoption19bf211
line 363why, one step back363Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 364why, one step back364 async with httpx.AsyncClient(transport=AsyncMockTransport(app)) as client:Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
line 365why, one step back365 response = await client.get(url, auth=auth)Test Suite on GitHub Actions (#907)Tom Christiebefore adoption19bf211
lines 366–375why, one step back366367 assert response.status_code == 200368 assert response.json() == {"auth": None}369 assert len(response.history) == 0370371372@pytest.mark.asyncio373async def test_digest_auth_401_response_without_digest_auth_header() -> None:374 url = "https://example.org/"375 auth = DigestAuth(username="tomchristie", password="password123")Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 376why, one step back376 app = App(auth_header="", status_code=401)Test Suite on GitHub Actions (#907)Tom Christiebefore adoption19bf211
line 377why, one step back377Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 378why, one step back378 async with httpx.AsyncClient(transport=AsyncMockTransport(app)) as client:Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
line 379why, one step back379 response = await client.get(url, auth=auth)Test Suite on GitHub Actions (#907)Tom Christiebefore adoption19bf211
lines 380–404why, one step back380381 assert response.status_code == 401382 assert response.json() == {"auth": None}383 assert len(response.history) == 0384385386@pytest.mark.parametrize(387 "algorithm,expected_hash_length,expected_response_length",388 [389 ("MD5", 64, 32),390 ("MD5-SESS", 64, 32),391 ("SHA", 64, 40),392 ("SHA-SESS", 64, 40),393 ("SHA-256", 64, 64),394 ("SHA-256-SESS", 64, 64),395 ("SHA-512", 64, 128),396 ("SHA-512-SESS", 64, 128),397 ],398)399@pytest.mark.asyncio400async def test_digest_auth(401 algorithm: str, expected_hash_length: int, expected_response_length: int402) -> None:403 url = "https://example.org/"404 auth = DigestAuth(username="tomchristie", password="password123")Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 405why, one step back405 app = DigestApp(algorithm=algorithm)Test Suite on GitHub Actions (#907)Tom Christiebefore adoption19bf211
line 406why, one step back406Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 407why, one step back407 async with httpx.AsyncClient(transport=AsyncMockTransport(app)) as client:Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
line 408why, one step back408 response = await client.get(url, auth=auth)Test Suite on GitHub Actions (#907)Tom Christiebefore adoption19bf211
lines 409–435why, one step back409410 assert response.status_code == 200411 assert len(response.history) == 1412413 authorization = typing.cast(dict, response.json())["auth"]414 scheme, _, fields = authorization.partition(" ")415 assert scheme == "Digest"416417 response_fields = [field.strip() for field in fields.split(",")]418 digest_data = dict(field.split("=") for field in response_fields)419420 assert digest_data["username"] == '"tomchristie"'421 assert digest_data["realm"] == '"httpx@example.org"'422 assert "nonce" in digest_data423 assert digest_data["uri"] == '"/"'424 assert len(digest_data["response"]) == expected_response_length + 2 # extra quotes425 assert len(digest_data["opaque"]) == expected_hash_length + 2426 assert digest_data["algorithm"] == algorithm427 assert digest_data["qop"] == "auth"428 assert digest_data["nc"] == "00000001"429 assert len(digest_data["cnonce"]) == 16 + 2430431432@pytest.mark.asyncio433async def test_digest_auth_no_specified_qop() -> None:434 url = "https://example.org/"435 auth = DigestAuth(username="tomchristie", password="password123")Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 436why, one step back436 app = DigestApp(qop="")Test Suite on GitHub Actions (#907)Tom Christiebefore adoption19bf211
line 437why, one step back437Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 438why, one step back438 async with httpx.AsyncClient(transport=AsyncMockTransport(app)) as client:Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
line 439why, one step back439 response = await client.get(url, auth=auth)Test Suite on GitHub Actions (#907)Tom Christiebefore adoption19bf211
lines 440–467why, one step back440441 assert response.status_code == 200442 assert len(response.history) == 1443444 authorization = typing.cast(dict, response.json())["auth"]445 scheme, _, fields = authorization.partition(" ")446 assert scheme == "Digest"447448 response_fields = [field.strip() for field in fields.split(",")]449 digest_data = dict(field.split("=") for field in response_fields)450451 assert "qop" not in digest_data452 assert "nc" not in digest_data453 assert "cnonce" not in digest_data454 assert digest_data["username"] == '"tomchristie"'455 assert digest_data["realm"] == '"httpx@example.org"'456 assert len(digest_data["nonce"]) == 64 + 2 # extra quotes457 assert digest_data["uri"] == '"/"'458 assert len(digest_data["response"]) == 64 + 2459 assert len(digest_data["opaque"]) == 64 + 2460 assert digest_data["algorithm"] == "SHA-256"461462463@pytest.mark.parametrize("qop", ("auth, auth-int", "auth,auth-int", "unknown,auth"))464@pytest.mark.asyncio465async def test_digest_auth_qop_including_spaces_and_auth_returns_auth(qop: str) -> None:466 url = "https://example.org/"467 auth = DigestAuth(username="tomchristie", password="password123")Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 468why, one step back468 app = DigestApp(qop=qop)Test Suite on GitHub Actions (#907)Tom Christiebefore adoption19bf211
line 469why, one step back469Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 470why, one step back470 async with httpx.AsyncClient(transport=AsyncMockTransport(app)) as client:Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
line 471why, one step back471 response = await client.get(url, auth=auth)Test Suite on GitHub Actions (#907)Tom Christiebefore adoption19bf211
lines 472–480why, one step back472473 assert response.status_code == 200474 assert len(response.history) == 1475476477@pytest.mark.asyncio478async def test_digest_auth_qop_auth_int_not_implemented() -> None:479 url = "https://example.org/"480 auth = DigestAuth(username="tomchristie", password="password123")Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 481why, one step back481 app = DigestApp(qop="auth-int")Test Suite on GitHub Actions (#907)Tom Christiebefore adoption19bf211
line 482why, one step back482Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 483why, one step back483 async with httpx.AsyncClient(transport=AsyncMockTransport(app)) as client:Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
lines 484–485why, one step back484 with pytest.raises(NotImplementedError):485 await client.get(url, auth=auth)Test Suite on GitHub Actions (#907)Tom Christiebefore adoption19bf211
lines 486–491why, one step back486487488@pytest.mark.asyncio489async def test_digest_auth_qop_must_be_auth_or_auth_int() -> None:490 url = "https://example.org/"491 auth = DigestAuth(username="tomchristie", password="password123")Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 492why, one step back492 app = DigestApp(qop="not-auth")Test Suite on GitHub Actions (#907)Tom Christiebefore adoption19bf211
line 493why, one step back493Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 494why, one step back494 async with httpx.AsyncClient(transport=AsyncMockTransport(app)) as client:Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
lines 495–496why, one step back495 with pytest.raises(ProtocolError):496 await client.get(url, auth=auth)Test Suite on GitHub Actions (#907)Tom Christiebefore adoption19bf211
lines 497–502why, one step back497498499@pytest.mark.asyncio500async def test_digest_auth_incorrect_credentials() -> None:501 url = "https://example.org/"502 auth = DigestAuth(username="tomchristie", password="password123")Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 503why, one step back503 app = DigestApp(send_response_after_attempt=2)Test Suite on GitHub Actions (#907)Tom Christiebefore adoption19bf211
line 504why, one step back504Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 505why, one step back505 async with httpx.AsyncClient(transport=AsyncMockTransport(app)) as client:Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
line 506why, one step back506 response = await client.get(url, auth=auth)Test Suite on GitHub Actions (#907)Tom Christiebefore adoption19bf211
lines 507–514why, one step back507508 assert response.status_code == 401509 assert len(response.history) == 1510511512@pytest.mark.parametrize(513 "auth_header",514 [Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
lines 515–516why, one step back515 'Digest realm="httpx@example.org", qop="auth"', # missing fields516 'Digest realm="httpx@example.org", qop="auth,au', # malformed fields listTest Suite on GitHub Actions (#907)Tom Christiebefore adoption19bf211
lines 517–519why, one step back517 ],518)519@pytest.mark.asyncioIncrease test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067
line 520why, one step back520async def test_async_digest_auth_raises_protocol_error_on_malformed_header(Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 521why, one step back521 auth_header: str,Test Suite on GitHub Actions (#907)Tom Christiebefore adoption19bf211
lines 522–524why, one step back522) -> None:523 url = "https://example.org/"524 auth = DigestAuth(username="tomchristie", password="password123")Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
lines 525–527why, one step back525 app = App(auth_header=auth_header, status_code=401)526527 async with httpx.AsyncClient(transport=AsyncMockTransport(app)) as client:Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
lines 528–529why, one step back528 with pytest.raises(ProtocolError):529 await client.get(url, auth=auth)Test Suite on GitHub Actions (#907)Tom Christiebefore adoption19bf211
lines 530–531why, one step back530531Increase test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067
lines 532–534why, one step back532@pytest.mark.parametrize(533 "auth_header",534 [Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
lines 535–536why, one step back535 'Digest realm="httpx@example.org", qop="auth"', # missing fields536 'Digest realm="httpx@example.org", qop="auth,au', # malformed fields listIncrease test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067
lines 537–539why, one step back537 ],538)539def test_sync_digest_auth_raises_protocol_error_on_malformed_header(Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 540why, one step back540 auth_header: str,Increase test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067
lines 541–543why, one step back541) -> None:542 url = "https://example.org/"543 auth = DigestAuth(username="tomchristie", password="password123")Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 544why, one step back544 app = App(auth_header=auth_header, status_code=401)Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
line 545why, one step back545Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 546why, one step back546 with httpx.Client(transport=MockTransport(app)) as client:Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
lines 547–548why, one step back547 with pytest.raises(ProtocolError):548 client.get(url, auth=auth)Increase test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067
lines 549–550why, one step back549550Test Suite on GitHub Actions (#907)Tom Christiebefore adoption19bf211
line 551why, one step back551@pytest.mark.asyncioIncrease test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067
line 552why, one step back552async def test_async_auth_history() -> None:Test Suite on GitHub Actions (#907)Tom Christiebefore adoption19bf211
lines 553–556why, one step back553 """554 Test that intermediate requests sent as part of an authentication flow555 are recorded in the response history.556 """Increase test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067
lines 557–558why, one step back557 url = "https://example.org/"558 auth = RepeatAuth(repeat=2)Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 559why, one step back559 app = App(auth_header="abc")Test Suite on GitHub Actions (#907)Tom Christiebefore adoption19bf211
line 560why, one step back560Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 561why, one step back561 async with httpx.AsyncClient(transport=AsyncMockTransport(app)) as client:Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
lines 562–563why, one step back562 response = await client.get(url, auth=auth)563Increase test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067
lines 564–565why, one step back564 assert response.status_code == 200565 assert response.json() == {"auth": "Repeat abc.abc"}Test Suite on GitHub Actions (#907)Tom Christiebefore adoption19bf211
line 566why, one step back566Increase test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067
lines 567–570why, one step back567 assert len(response.history) == 2568 resp1, resp2 = response.history569 assert resp1.json() == {"auth": "Repeat 0"}570 assert resp2.json() == {"auth": "Repeat 1"}Test Suite on GitHub Actions (#907)Tom Christiebefore adoption19bf211
line 571why, one step back571Increase test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067
lines 572–573why, one step back572 assert len(resp2.history) == 1573 assert resp2.history == [resp1]Test Suite on GitHub Actions (#907)Tom Christiebefore adoption19bf211
line 574why, one step back574Increase test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067
line 575why, one step back575 assert len(resp1.history) == 0Test Suite on GitHub Actions (#907)Tom Christiebefore adoption19bf211
lines 576–577why, one step back576577Increase test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067
lines 578–582why, one step back578def test_sync_auth_history() -> None:579 """580 Test that intermediate requests sent as part of an authentication flow581 are recorded in the response history.582 """Test Suite on GitHub Actions (#907)Tom Christiebefore adoption19bf211
lines 583–584why, one step back583 url = "https://example.org/"584 auth = RepeatAuth(repeat=2)Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 585why, one step back585 app = App(auth_header="abc")Test Suite on GitHub Actions (#907)Tom Christiebefore adoption19bf211
line 586why, one step back586Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 587why, one step back587 with httpx.Client(transport=MockTransport(app)) as client:Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
lines 588–589why, one step back588 response = client.get(url, auth=auth)589Test Suite on GitHub Actions (#907)Tom Christiebefore adoption19bf211
lines 590–607why, one step back590 assert response.status_code == 200591 assert response.json() == {"auth": "Repeat abc.abc"}592593 assert len(response.history) == 2594 resp1, resp2 = response.history595 assert resp1.json() == {"auth": "Repeat 0"}596 assert resp2.json() == {"auth": "Repeat 1"}597598 assert len(resp2.history) == 1599 assert resp2.history == [resp1]600601 assert len(resp1.history) == 0602603604@pytest.mark.asyncio605async def test_digest_auth_unavailable_streaming_body():606 url = "https://example.org/"607 auth = DigestAuth(username="tomchristie", password="password123")Drop ContentStream (#1295)Tom Christiebefore adoptionfbb21fb
line 608why, one step back608 app = DigestApp()Test Suite on GitHub Actions (#907)Tom Christiebefore adoption19bf211
lines 609–610why, one step back609610 async def streaming_body():Transport API (#963)Yeray Diaz Diazbefore adoptiond2816c9
line 611why, one step back611 yield b"Example request body" # pragma: nocoverTest Suite on GitHub Actions (#907)Tom Christiebefore adoption19bf211
line 612why, one step back612Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 613why, one step back613 async with httpx.AsyncClient(transport=AsyncMockTransport(app)) as client:Drop ContentStream (#1295)Tom Christiebefore adoptionfbb21fb
line 614why, one step back614 with pytest.raises(httpx.StreamConsumed):Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
line 615why, one step back615 await client.post(url, data=streaming_body(), auth=auth)Test Suite on GitHub Actions (#907)Tom Christiebefore adoption19bf211
lines 616–618why, one step back616617618@pytest.mark.asyncioIncrease test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067
line 619why, one step back619async def test_async_auth_reads_response_body() -> None:Test Suite on GitHub Actions (#907)Tom Christiebefore adoption19bf211
lines 620–623why, one step back620 """621 Test that we can read the response body in an auth flow if `requires_response_body`622 is set.623 """Increase test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067
lines 624–625why, one step back624 url = "https://example.org/"625 auth = ResponseBodyAuth("xyz")Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
lines 626–628why, one step back626 app = App()627628 async with httpx.AsyncClient(transport=AsyncMockTransport(app)) as client:Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
line 629why, one step back629 response = await client.get(url, auth=auth)Test Suite on GitHub Actions (#907)Tom Christiebefore adoption19bf211
line 630why, one step back630Increase test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067
lines 631–632why, one step back631 assert response.status_code == 200632 assert response.json() == {"auth": '{"auth": "xyz"}'}Test Suite on GitHub Actions (#907)Tom Christiebefore adoption19bf211
lines 633–634why, one step back633634Increase test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067
lines 635–639why, one step back635def test_sync_auth_reads_response_body() -> None:636 """637 Test that we can read the response body in an auth flow if `requires_response_body`638 is set.639 """Test Suite on GitHub Actions (#907)Tom Christiebefore adoption19bf211
lines 640–641why, one step back640 url = "https://example.org/"641 auth = ResponseBodyAuth("xyz")Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 642why, one step back642 app = App()Test Suite on GitHub Actions (#907)Tom Christiebefore adoption19bf211
line 643why, one step back643Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 644why, one step back644 with httpx.Client(transport=MockTransport(app)) as client:Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
lines 645–646why, one step back645 response = client.get(url, auth=auth)646Test Suite on GitHub Actions (#907)Tom Christiebefore adoption19bf211
lines 647–648why, one step back647 assert response.status_code == 200648 assert response.json() == {"auth": '{"auth": "xyz"}'}Add support for sync-specific or async-specific auth flows (#1217)Florimond Mancabefore adoption016e4ee
lines 649–659why, one step back649650651@pytest.mark.asyncio652async def test_async_auth() -> None:653 """654 Test that we can use an auth implementation specific to the async case, to655 support cases that require performing I/O or using concurrency primitives (such656 as checking a disk-based cache or fetching a token from a remote auth server).657 """658 url = "https://example.org/"659 auth = SyncOrAsyncAuth()Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 660why, one step back660 app = App()Add support for sync-specific or async-specific auth flows (#1217)Florimond Mancabefore adoption016e4ee
line 661why, one step back661Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 662why, one step back662 async with httpx.AsyncClient(transport=AsyncMockTransport(app)) as client:Add support for sync-specific or async-specific auth flows (#1217)Florimond Mancabefore adoption016e4ee
lines 663–674why, one step back663 response = await client.get(url, auth=auth)664665 assert response.status_code == 200666 assert response.json() == {"auth": "async-auth"}667668669def test_sync_auth() -> None:670 """671 Test that we can use an auth implementation specific to the sync case.672 """673 url = "https://example.org/"674 auth = SyncOrAsyncAuth()Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 675why, one step back675 app = App()Add support for sync-specific or async-specific auth flows (#1217)Florimond Mancabefore adoption016e4ee
line 676why, one step back676Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 677why, one step back677 with httpx.Client(transport=MockTransport(app)) as client:Add support for sync-specific or async-specific auth flows (#1217)Florimond Mancabefore adoption016e4ee
lines 678–681why, one step back678 response = client.get(url, auth=auth)679680 assert response.status_code == 200681 assert response.json() == {"auth": "sync-auth"}