test_auth.py
This file
Path
tests/client/test_auth.py
694 lines · 21.9 KiB · Python
Last changed by
Fix stream unsetting auth (#1312)Tom Christiebefore adoptiond25f2bf
History of this fileTake 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 back1"""2Integration tests for authentication.34Unit tests for auth classes also exist in tests/test_auth.py5"""6import asyncioVersion 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a
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 threadingVersion 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a
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, ResponseVersion 0.15.0 (#1301)Tom Christiebefore adoptionf932af9
line 16why, one step back16from tests.utils import MockTransportVersion 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a
line 17why, one step back17Use relative tests directory references (#1052)Florimond Mancabefore adoption09672a9
line 18why, one step back18from ..common import FIXTURES_DIRVersion 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a
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:Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a
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)Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a
lines 30–31why, one step back3031Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 32why, one step back32class DigestApp:Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a
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:Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a
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)Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a
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)Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a
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:Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a
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)Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a
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 request150151Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a
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()Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a
line 157why, one step back157Version 0.15.0 (#1301)Tom Christiebefore adoptionf932af9
line 158why, one step back158 async with httpx.AsyncClient(transport=MockTransport(app)) as client:Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
line 159why, one step back159 response = await client.get(url, auth=auth)Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a
lines 160–165why, one step back160161 assert response.status_code == 200162 assert response.json() == {"auth": "Basic dG9tY2hyaXN0aWU6cGFzc3dvcmQxMjM="}163164165@pytest.mark.asyncioFix stream unsetting auth (#1312)Tom Christiebefore adoptiond25f2bf
lines 166–182why, one step back166async def test_basic_auth_with_stream() -> None:167 """168 See: https://github.com/encode/httpx/pull/1312169 """170 url = "https://example.org/"171 auth = ("tomchristie", "password123")172 app = App()173174 async with httpx.AsyncClient(transport=MockTransport(app), auth=auth) as client:175 async with client.stream("GET", url) as response:176 response.read()177178 assert response.status_code == 200179 assert response.json() == {"auth": "Basic dG9tY2hyaXN0aWU6cGFzc3dvcmQxMjM="}180181182@pytest.mark.asyncioVersion 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a
lines 183–184why, one step back183async def test_basic_auth_in_url() -> None:184 url = "https://tomchristie:password123@example.org/"Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 185why, one step back185 app = App()Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a
line 186why, one step back186Version 0.15.0 (#1301)Tom Christiebefore adoptionf932af9
line 187why, one step back187 async with httpx.AsyncClient(transport=MockTransport(app)) as client:Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
line 188why, one step back188 response = await client.get(url)Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a
lines 189–197why, one step back189190 assert response.status_code == 200191 assert response.json() == {"auth": "Basic dG9tY2hyaXN0aWU6cGFzc3dvcmQxMjM="}192193194@pytest.mark.asyncio195async def test_basic_auth_on_session() -> None:196 url = "https://example.org/"197 auth = ("tomchristie", "password123")Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 198why, one step back198 app = App()Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a
line 199why, one step back199Version 0.15.0 (#1301)Tom Christiebefore adoptionf932af9
line 200why, one step back200 async with httpx.AsyncClient(transport=MockTransport(app), auth=auth) as client:Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
line 201why, one step back201 response = await client.get(url)Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a
lines 202–209why, one step back202203 assert response.status_code == 200204 assert response.json() == {"auth": "Basic dG9tY2hyaXN0aWU6cGFzc3dvcmQxMjM="}205206207@pytest.mark.asyncio208async def test_custom_auth() -> None:209 url = "https://example.org/"Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 210why, one step back210 app = App()Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a
lines 211–215why, one step back211212 def auth(request: Request) -> Request:213 request.headers["Authorization"] = "Token 123"214 return request215Version 0.15.0 (#1301)Tom Christiebefore adoptionf932af9
line 216why, one step back216 async with httpx.AsyncClient(transport=MockTransport(app)) as client:Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
line 217why, one step back217 response = await client.get(url, auth=auth)Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a
lines 218–224why, one step back218219 assert response.status_code == 200220 assert response.json() == {"auth": "Token 123"}221222223@pytest.mark.asyncio224async def test_netrc_auth() -> None:Use relative tests directory references (#1052)Florimond Mancabefore adoption09672a9
line 225why, one step back225 os.environ["NETRC"] = str(FIXTURES_DIR / ".netrc")Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a
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()Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a
line 228why, one step back228Version 0.15.0 (#1301)Tom Christiebefore adoptionf932af9
line 229why, one step back229 async with httpx.AsyncClient(transport=MockTransport(app)) as client:Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
line 230why, one step back230 response = await client.get(url)Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a
lines 231–239why, one step back231232 assert response.status_code == 200233 assert response.json() == {234 "auth": "Basic ZXhhbXBsZS11c2VybmFtZTpleGFtcGxlLXBhc3N3b3Jk"235 }236237238@pytest.mark.asyncio239async def test_auth_header_has_priority_over_netrc() -> None:Use relative tests directory references (#1052)Florimond Mancabefore adoption09672a9
line 240why, one step back240 os.environ["NETRC"] = str(FIXTURES_DIR / ".netrc")Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a
line 241why, one step back241 url = "http://netrcexample.org"Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 242why, one step back242 app = App()Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a
line 243why, one step back243Version 0.15.0 (#1301)Tom Christiebefore adoptionf932af9
line 244why, one step back244 async with httpx.AsyncClient(transport=MockTransport(app)) as client:Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
line 245why, one step back245 response = await client.get(url, headers={"Authorization": "Override"})Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a
lines 246–252why, one step back246247 assert response.status_code == 200248 assert response.json() == {"auth": "Override"}249250251@pytest.mark.asyncio252async def test_trust_env_auth() -> None:Use relative tests directory references (#1052)Florimond Mancabefore adoption09672a9
line 253why, one step back253 os.environ["NETRC"] = str(FIXTURES_DIR / ".netrc")Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a
line 254why, one step back254 url = "http://netrcexample.org"Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 255why, one step back255 app = App()Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a
line 256why, one step back256Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
line 257why, one step back257 async with httpx.AsyncClient(Version 0.15.0 (#1301)Tom Christiebefore adoptionf932af9
line 258why, one step back258 transport=MockTransport(app), trust_env=FalseClosing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
lines 259–260why, one step back259 ) as client:260 response = await client.get(url)Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a
lines 261–264why, one step back261262 assert response.status_code == 200263 assert response.json() == {"auth": None}264Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
line 265why, one step back265 async with httpx.AsyncClient(Version 0.15.0 (#1301)Tom Christiebefore adoptionf932af9
line 266why, one step back266 transport=MockTransport(app), trust_env=TrueClosing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
lines 267–268why, one step back267 ) as client:268 response = await client.get(url)Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a
lines 269–275why, one step back269270 assert response.status_code == 200271 assert response.json() == {272 "auth": "Basic ZXhhbXBsZS11c2VybmFtZTpleGFtcGxlLXBhc3N3b3Jk"273 }274275Allow disabling auth per-request using `auth=None` (#1115)Florimond Mancabefore adoptiona4463d0
lines 276–279why, one step back276@pytest.mark.asyncio277async def test_auth_disable_per_request() -> None:278 url = "https://example.org/"279 auth = ("tomchristie", "password123")Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 280why, one step back280 app = App()Allow disabling auth per-request using `auth=None` (#1115)Florimond Mancabefore adoptiona4463d0
line 281why, one step back281Version 0.15.0 (#1301)Tom Christiebefore adoptionf932af9
line 282why, one step back282 async with httpx.AsyncClient(transport=MockTransport(app), auth=auth) as client:Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
line 283why, one step back283 response = await client.get(url, auth=None)Allow disabling auth per-request using `auth=None` (#1115)Florimond Mancabefore adoptiona4463d0
lines 284–288why, one step back284285 assert response.status_code == 200286 assert response.json() == {"auth": None}287288Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a
lines 289–299why, one step back289def test_auth_hidden_url() -> None:290 url = "http://example-username:example-password@example.org/"291 expected = "URL('http://example-username:[secure]@example.org/')"292 assert url == URL(url)293 assert expected == repr(URL(url))294295296@pytest.mark.asyncio297async def test_auth_hidden_header() -> None:298 url = "https://example.org/"299 auth = ("example-username", "example-password")Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 300why, one step back300 app = App()Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a
line 301why, one step back301Version 0.15.0 (#1301)Tom Christiebefore adoptionf932af9
line 302why, one step back302 async with httpx.AsyncClient(transport=MockTransport(app)) as client:Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
line 303why, one step back303 response = await client.get(url, auth=auth)Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a
lines 304–308why, one step back304305 assert "'authorization': '[secure]'" in str(response.request.headers)306307308@pytest.mark.asyncioAdd `Client.auth` setter (#1185)Florimond Mancabefore adoptioncb620e6
line 309why, one step back309async def test_auth_property() -> None:Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
lines 310–311why, one step back310 app = App()311Version 0.15.0 (#1301)Tom Christiebefore adoptionf932af9
line 312why, one step back312 async with httpx.AsyncClient(transport=MockTransport(app)) as client:Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
line 313why, one step back313 assert client.auth is NoneAdd `Client.auth` setter (#1185)Florimond Mancabefore adoptioncb620e6
line 314why, one step back314Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
lines 315–316why, one step back315 client.auth = ("tomchristie", "password123") # type: ignore316 assert isinstance(client.auth, BasicAuth)Add `Client.auth` setter (#1185)Florimond Mancabefore adoptioncb620e6
line 317why, one step back317Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
lines 318–321why, one step back318 url = "https://example.org/"319 response = await client.get(url)320 assert response.status_code == 200321 assert response.json() == {"auth": "Basic dG9tY2hyaXN0aWU6cGFzc3dvcmQxMjM="}Add `Client.auth` setter (#1185)Florimond Mancabefore adoptioncb620e6
lines 322–325why, one step back322323324@pytest.mark.asyncio325async def test_auth_invalid_type() -> None:Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
lines 326–327why, one step back326 app = App()327Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a
line 328why, one step back328 with pytest.raises(TypeError):Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
line 329why, one step back329 client = httpx.AsyncClient(Version 0.15.0 (#1301)Tom Christiebefore adoptionf932af9
line 330why, one step back330 transport=MockTransport(app),Add `Client.auth` setter (#1185)Florimond Mancabefore adoptioncb620e6
lines 331–333why, one step back331 auth="not a tuple, not a callable", # type: ignore332 )333Version 0.15.0 (#1301)Tom Christiebefore adoptionf932af9
line 334why, one step back334 async with httpx.AsyncClient(transport=MockTransport(app)) as client:Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
lines 335–336why, one step back335 with pytest.raises(TypeError):336 await client.get(auth="not a tuple, not a callable") # type: ignoreAdd `Client.auth` setter (#1185)Florimond Mancabefore adoptioncb620e6
line 337why, one step back337Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
lines 338–339why, one step back338 with pytest.raises(TypeError):339 client.auth = "not a tuple, not a callable" # type: ignoreAdd `Client.auth` setter (#1185)Florimond Mancabefore adoptioncb620e6
line 340why, one step back340Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a
lines 341–345why, one step back341342@pytest.mark.asyncio343async def test_digest_auth_returns_no_auth_if_no_digest_header_in_response() -> 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
line 346why, one step back346 app = App()Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a
line 347why, one step back347Version 0.15.0 (#1301)Tom Christiebefore adoptionf932af9
line 348why, one step back348 async with httpx.AsyncClient(transport=MockTransport(app)) as client:Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
line 349why, one step back349 response = await client.get(url, auth=auth)Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a
lines 350–355why, one step back350351 assert response.status_code == 200352 assert response.json() == {"auth": None}353 assert len(response.history) == 0354355Handle multiple auth headers correctly (#1240)Tom Christiebefore adoption33d339a
lines 356–358why, one step back356def test_digest_auth_returns_no_auth_if_alternate_auth_scheme() -> None:357 url = "https://example.org/"358 auth = DigestAuth(username="tomchristie", password="password123")Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
lines 359–360why, one step back359 auth_header = "Token ..."360 app = App(auth_header=auth_header, status_code=401)Handle multiple auth headers correctly (#1240)Tom Christiebefore adoption33d339a
line 361why, one step back361Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 362why, one step back362 client = httpx.Client(transport=MockTransport(app))Handle multiple auth headers correctly (#1240)Tom Christiebefore adoption33d339a
lines 363–369why, one step back363 response = client.get(url, auth=auth)364365 assert response.status_code == 401366 assert response.json() == {"auth": None}367 assert len(response.history) == 0368369Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a
lines 370–373why, one step back370@pytest.mark.asyncio371async def test_digest_auth_200_response_including_digest_auth_header() -> None:372 url = "https://example.org/"373 auth = DigestAuth(username="tomchristie", password="password123")Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
lines 374–375why, one step back374 auth_header = 'Digest realm="realm@host.com",qop="auth",nonce="abc",opaque="xyz"'375 app = App(auth_header=auth_header, status_code=200)Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a
line 376why, one step back376Version 0.15.0 (#1301)Tom Christiebefore adoptionf932af9
line 377why, one step back377 async with httpx.AsyncClient(transport=MockTransport(app)) as client:Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
line 378why, one step back378 response = await client.get(url, auth=auth)Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a
lines 379–388why, one step back379380 assert response.status_code == 200381 assert response.json() == {"auth": None}382 assert len(response.history) == 0383384385@pytest.mark.asyncio386async def test_digest_auth_401_response_without_digest_auth_header() -> None:387 url = "https://example.org/"388 auth = DigestAuth(username="tomchristie", password="password123")Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 389why, one step back389 app = App(auth_header="", status_code=401)Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a
line 390why, one step back390Version 0.15.0 (#1301)Tom Christiebefore adoptionf932af9
line 391why, one step back391 async with httpx.AsyncClient(transport=MockTransport(app)) as client:Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
line 392why, one step back392 response = await client.get(url, auth=auth)Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a
lines 393–417why, one step back393394 assert response.status_code == 401395 assert response.json() == {"auth": None}396 assert len(response.history) == 0397398399@pytest.mark.parametrize(400 "algorithm,expected_hash_length,expected_response_length",401 [402 ("MD5", 64, 32),403 ("MD5-SESS", 64, 32),404 ("SHA", 64, 40),405 ("SHA-SESS", 64, 40),406 ("SHA-256", 64, 64),407 ("SHA-256-SESS", 64, 64),408 ("SHA-512", 64, 128),409 ("SHA-512-SESS", 64, 128),410 ],411)412@pytest.mark.asyncio413async def test_digest_auth(414 algorithm: str, expected_hash_length: int, expected_response_length: int415) -> None:416 url = "https://example.org/"417 auth = DigestAuth(username="tomchristie", password="password123")Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 418why, one step back418 app = DigestApp(algorithm=algorithm)Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a
line 419why, one step back419Version 0.15.0 (#1301)Tom Christiebefore adoptionf932af9
line 420why, one step back420 async with httpx.AsyncClient(transport=MockTransport(app)) as client:Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
line 421why, one step back421 response = await client.get(url, auth=auth)Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a
lines 422–448why, one step back422423 assert response.status_code == 200424 assert len(response.history) == 1425426 authorization = typing.cast(dict, response.json())["auth"]427 scheme, _, fields = authorization.partition(" ")428 assert scheme == "Digest"429430 response_fields = [field.strip() for field in fields.split(",")]431 digest_data = dict(field.split("=") for field in response_fields)432433 assert digest_data["username"] == '"tomchristie"'434 assert digest_data["realm"] == '"httpx@example.org"'435 assert "nonce" in digest_data436 assert digest_data["uri"] == '"/"'437 assert len(digest_data["response"]) == expected_response_length + 2 # extra quotes438 assert len(digest_data["opaque"]) == expected_hash_length + 2439 assert digest_data["algorithm"] == algorithm440 assert digest_data["qop"] == "auth"441 assert digest_data["nc"] == "00000001"442 assert len(digest_data["cnonce"]) == 16 + 2443444445@pytest.mark.asyncio446async def test_digest_auth_no_specified_qop() -> None:447 url = "https://example.org/"448 auth = DigestAuth(username="tomchristie", password="password123")Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 449why, one step back449 app = DigestApp(qop="")Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a
line 450why, one step back450Version 0.15.0 (#1301)Tom Christiebefore adoptionf932af9
line 451why, one step back451 async with httpx.AsyncClient(transport=MockTransport(app)) as client:Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
line 452why, one step back452 response = await client.get(url, auth=auth)Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a
lines 453–480why, one step back453454 assert response.status_code == 200455 assert len(response.history) == 1456457 authorization = typing.cast(dict, response.json())["auth"]458 scheme, _, fields = authorization.partition(" ")459 assert scheme == "Digest"460461 response_fields = [field.strip() for field in fields.split(",")]462 digest_data = dict(field.split("=") for field in response_fields)463464 assert "qop" not in digest_data465 assert "nc" not in digest_data466 assert "cnonce" not in digest_data467 assert digest_data["username"] == '"tomchristie"'468 assert digest_data["realm"] == '"httpx@example.org"'469 assert len(digest_data["nonce"]) == 64 + 2 # extra quotes470 assert digest_data["uri"] == '"/"'471 assert len(digest_data["response"]) == 64 + 2472 assert len(digest_data["opaque"]) == 64 + 2473 assert digest_data["algorithm"] == "SHA-256"474475476@pytest.mark.parametrize("qop", ("auth, auth-int", "auth,auth-int", "unknown,auth"))477@pytest.mark.asyncio478async def test_digest_auth_qop_including_spaces_and_auth_returns_auth(qop: str) -> 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=qop)Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a
line 482why, one step back482Version 0.15.0 (#1301)Tom Christiebefore adoptionf932af9
line 483why, one step back483 async with httpx.AsyncClient(transport=MockTransport(app)) as client:Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
line 484why, one step back484 response = await client.get(url, auth=auth)Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a
lines 485–493why, one step back485486 assert response.status_code == 200487 assert len(response.history) == 1488489490@pytest.mark.asyncio491async def test_digest_auth_qop_auth_int_not_implemented() -> None:492 url = "https://example.org/"493 auth = DigestAuth(username="tomchristie", password="password123")Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 494why, one step back494 app = DigestApp(qop="auth-int")Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a
line 495why, one step back495Version 0.15.0 (#1301)Tom Christiebefore adoptionf932af9
line 496why, one step back496 async with httpx.AsyncClient(transport=MockTransport(app)) as client:Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
lines 497–498why, one step back497 with pytest.raises(NotImplementedError):498 await client.get(url, auth=auth)Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a
lines 499–504why, one step back499500501@pytest.mark.asyncio502async def test_digest_auth_qop_must_be_auth_or_auth_int() -> None:503 url = "https://example.org/"504 auth = DigestAuth(username="tomchristie", password="password123")Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 505why, one step back505 app = DigestApp(qop="not-auth")Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a
line 506why, one step back506Version 0.15.0 (#1301)Tom Christiebefore adoptionf932af9
line 507why, one step back507 async with httpx.AsyncClient(transport=MockTransport(app)) as client:Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
lines 508–509why, one step back508 with pytest.raises(ProtocolError):509 await client.get(url, auth=auth)Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a
lines 510–515why, one step back510511512@pytest.mark.asyncio513async def test_digest_auth_incorrect_credentials() -> None:514 url = "https://example.org/"515 auth = DigestAuth(username="tomchristie", password="password123")Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 516why, one step back516 app = DigestApp(send_response_after_attempt=2)Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a
line 517why, one step back517Version 0.15.0 (#1301)Tom Christiebefore adoptionf932af9
line 518why, one step back518 async with httpx.AsyncClient(transport=MockTransport(app)) as client:Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
line 519why, one step back519 response = await client.get(url, auth=auth)Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a
lines 520–527why, one step back520521 assert response.status_code == 401522 assert len(response.history) == 1523524525@pytest.mark.parametrize(526 "auth_header",527 [Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
lines 528–529why, one step back528 'Digest realm="httpx@example.org", qop="auth"', # missing fields529 'Digest realm="httpx@example.org", qop="auth,au', # malformed fields listVersion 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a
lines 530–532why, one step back530 ],531)532@pytest.mark.asyncioIncrease test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067
line 533why, one step back533async def test_async_digest_auth_raises_protocol_error_on_malformed_header(Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 534why, one step back534 auth_header: str,Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a
lines 535–537why, one step back535) -> None:536 url = "https://example.org/"537 auth = DigestAuth(username="tomchristie", password="password123")Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
lines 538–539why, one step back538 app = App(auth_header=auth_header, status_code=401)539Version 0.15.0 (#1301)Tom Christiebefore adoptionf932af9
line 540why, one step back540 async with httpx.AsyncClient(transport=MockTransport(app)) as client:Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
lines 541–542why, one step back541 with pytest.raises(ProtocolError):542 await client.get(url, auth=auth)Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a
lines 543–544why, one step back543544Increase test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067
lines 545–547why, one step back545@pytest.mark.parametrize(546 "auth_header",547 [Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
lines 548–549why, one step back548 'Digest realm="httpx@example.org", qop="auth"', # missing fields549 'Digest realm="httpx@example.org", qop="auth,au', # malformed fields listIncrease test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067
lines 550–552why, one step back550 ],551)552def test_sync_digest_auth_raises_protocol_error_on_malformed_header(Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 553why, one step back553 auth_header: str,Increase test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067
lines 554–556why, one step back554) -> None:555 url = "https://example.org/"556 auth = DigestAuth(username="tomchristie", password="password123")Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 557why, one step back557 app = App(auth_header=auth_header, status_code=401)Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
line 558why, one step back558Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 559why, one step back559 with httpx.Client(transport=MockTransport(app)) as client:Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
lines 560–561why, one step back560 with pytest.raises(ProtocolError):561 client.get(url, auth=auth)Increase test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067
lines 562–563why, one step back562563Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a
line 564why, one step back564@pytest.mark.asyncioIncrease test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067
line 565why, one step back565async def test_async_auth_history() -> None:Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a
lines 566–569why, one step back566 """567 Test that intermediate requests sent as part of an authentication flow568 are recorded in the response history.569 """Increase test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067
lines 570–571why, one step back570 url = "https://example.org/"571 auth = RepeatAuth(repeat=2)Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 572why, one step back572 app = App(auth_header="abc")Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a
line 573why, one step back573Version 0.15.0 (#1301)Tom Christiebefore adoptionf932af9
line 574why, one step back574 async with httpx.AsyncClient(transport=MockTransport(app)) as client:Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
lines 575–576why, one step back575 response = await client.get(url, auth=auth)576Increase test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067
lines 577–578why, one step back577 assert response.status_code == 200578 assert response.json() == {"auth": "Repeat abc.abc"}Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a
line 579why, one step back579Increase test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067
lines 580–583why, one step back580 assert len(response.history) == 2581 resp1, resp2 = response.history582 assert resp1.json() == {"auth": "Repeat 0"}583 assert resp2.json() == {"auth": "Repeat 1"}Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a
line 584why, one step back584Increase test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067
lines 585–586why, one step back585 assert len(resp2.history) == 1586 assert resp2.history == [resp1]Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a
line 587why, one step back587Increase test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067
line 588why, one step back588 assert len(resp1.history) == 0Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a
lines 589–590why, one step back589590Increase test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067
lines 591–595why, one step back591def test_sync_auth_history() -> None:592 """593 Test that intermediate requests sent as part of an authentication flow594 are recorded in the response history.595 """Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a
lines 596–597why, one step back596 url = "https://example.org/"597 auth = RepeatAuth(repeat=2)Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 598why, one step back598 app = App(auth_header="abc")Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a
line 599why, one step back599Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 600why, one step back600 with httpx.Client(transport=MockTransport(app)) as client:Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
lines 601–602why, one step back601 response = client.get(url, auth=auth)602Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a
lines 603–620why, one step back603 assert response.status_code == 200604 assert response.json() == {"auth": "Repeat abc.abc"}605606 assert len(response.history) == 2607 resp1, resp2 = response.history608 assert resp1.json() == {"auth": "Repeat 0"}609 assert resp2.json() == {"auth": "Repeat 1"}610611 assert len(resp2.history) == 1612 assert resp2.history == [resp1]613614 assert len(resp1.history) == 0615616617@pytest.mark.asyncio618async def test_digest_auth_unavailable_streaming_body():619 url = "https://example.org/"620 auth = DigestAuth(username="tomchristie", password="password123")Drop ContentStream (#1295)Tom Christiebefore adoptionfbb21fb
line 621why, one step back621 app = DigestApp()Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a
lines 622–623why, one step back622623 async def streaming_body():Transport API (#963)Yeray Diaz Diazbefore adoptiond2816c9
line 624why, one step back624 yield b"Example request body" # pragma: nocoverVersion 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a
line 625why, one step back625Version 0.15.0 (#1301)Tom Christiebefore adoptionf932af9
line 626why, one step back626 async with httpx.AsyncClient(transport=MockTransport(app)) as client:Drop ContentStream (#1295)Tom Christiebefore adoptionfbb21fb
line 627why, one step back627 with pytest.raises(httpx.StreamConsumed):Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
line 628why, one step back628 await client.post(url, data=streaming_body(), auth=auth)Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a
lines 629–631why, one step back629630631@pytest.mark.asyncioIncrease test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067
line 632why, one step back632async def test_async_auth_reads_response_body() -> None:Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a
lines 633–636why, one step back633 """634 Test that we can read the response body in an auth flow if `requires_response_body`635 is set.636 """Increase test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067
lines 637–638why, one step back637 url = "https://example.org/"638 auth = ResponseBodyAuth("xyz")Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
lines 639–640why, one step back639 app = App()640Version 0.15.0 (#1301)Tom Christiebefore adoptionf932af9
line 641why, one step back641 async with httpx.AsyncClient(transport=MockTransport(app)) as client:Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
line 642why, one step back642 response = await client.get(url, auth=auth)Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a
line 643why, one step back643Increase test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067
lines 644–645why, one step back644 assert response.status_code == 200645 assert response.json() == {"auth": '{"auth": "xyz"}'}Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a
lines 646–647why, one step back646647Increase test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067
lines 648–652why, one step back648def test_sync_auth_reads_response_body() -> None:649 """650 Test that we can read the response body in an auth flow if `requires_response_body`651 is set.652 """Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a
lines 653–654why, one step back653 url = "https://example.org/"654 auth = ResponseBodyAuth("xyz")Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 655why, one step back655 app = App()Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a
line 656why, one step back656Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 657why, one step back657 with httpx.Client(transport=MockTransport(app)) as client:Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
lines 658–659why, one step back658 response = client.get(url, auth=auth)659Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a
lines 660–661why, one step back660 assert response.status_code == 200661 assert response.json() == {"auth": '{"auth": "xyz"}'}Add support for sync-specific or async-specific auth flows (#1217)Florimond Mancabefore adoption016e4ee
lines 662–672why, one step back662663664@pytest.mark.asyncio665async def test_async_auth() -> None:666 """667 Test that we can use an auth implementation specific to the async case, to668 support cases that require performing I/O or using concurrency primitives (such669 as checking a disk-based cache or fetching a token from a remote auth server).670 """671 url = "https://example.org/"672 auth = SyncOrAsyncAuth()Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 673why, one step back673 app = App()Add support for sync-specific or async-specific auth flows (#1217)Florimond Mancabefore adoption016e4ee
line 674why, one step back674Version 0.15.0 (#1301)Tom Christiebefore adoptionf932af9
line 675why, one step back675 async with httpx.AsyncClient(transport=MockTransport(app)) as client:Add support for sync-specific or async-specific auth flows (#1217)Florimond Mancabefore adoption016e4ee
lines 676–687why, one step back676 response = await client.get(url, auth=auth)677678 assert response.status_code == 200679 assert response.json() == {"auth": "async-auth"}680681682def test_sync_auth() -> None:683 """684 Test that we can use an auth implementation specific to the sync case.685 """686 url = "https://example.org/"687 auth = SyncOrAsyncAuth()Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 688why, one step back688 app = App()Add support for sync-specific or async-specific auth flows (#1217)Florimond Mancabefore adoption016e4ee
line 689why, one step back689Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 690why, one step back690 with httpx.Client(transport=MockTransport(app)) as client:Add support for sync-specific or async-specific auth flows (#1217)Florimond Mancabefore adoption016e4ee
lines 691–694why, one step back691 response = client.get(url, auth=auth)692693 assert response.status_code == 200694 assert response.json() == {"auth": "sync-auth"}