test_auth.py
This file
Path
tests/client/test_auth.py
677 lines · 21.3 KiB · Python
Last changed by
Version 0.15.0 (#1301)Tom Christiebefore adoptionf932af9
History of this 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–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()Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a
line 169why, one step back169Version 0.15.0 (#1301)Tom Christiebefore adoptionf932af9
line 170why, one step back170 async with httpx.AsyncClient(transport=MockTransport(app)) as client:Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
line 171why, one step back171 response = await client.get(url)Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a
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()Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a
line 182why, one step back182Version 0.15.0 (#1301)Tom Christiebefore adoptionf932af9
line 183why, one step back183 async with httpx.AsyncClient(transport=MockTransport(app), auth=auth) as client:Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
line 184why, one step back184 response = await client.get(url)Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a
lines 185–192why, one step back185186 assert response.status_code == 200187 assert response.json() == {"auth": "Basic dG9tY2hyaXN0aWU6cGFzc3dvcmQxMjM="}188189190@pytest.mark.asyncio191async def test_custom_auth() -> None:192 url = "https://example.org/"Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 193why, one step back193 app = App()Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a
lines 194–198why, one step back194195 def auth(request: Request) -> Request:196 request.headers["Authorization"] = "Token 123"197 return request198Version 0.15.0 (#1301)Tom Christiebefore adoptionf932af9
line 199why, one step back199 async with httpx.AsyncClient(transport=MockTransport(app)) as client:Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
line 200why, one step back200 response = await client.get(url, auth=auth)Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a
lines 201–207why, one step back201202 assert response.status_code == 200203 assert response.json() == {"auth": "Token 123"}204205206@pytest.mark.asyncio207async def test_netrc_auth() -> None:Use relative tests directory references (#1052)Florimond Mancabefore adoption09672a9
line 208why, one step back208 os.environ["NETRC"] = str(FIXTURES_DIR / ".netrc")Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a
line 209why, one step back209 url = "http://netrcexample.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
line 211why, one step back211Version 0.15.0 (#1301)Tom Christiebefore adoptionf932af9
line 212why, one step back212 async with httpx.AsyncClient(transport=MockTransport(app)) as client:Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
line 213why, one step back213 response = await client.get(url)Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a
lines 214–222why, one step back214215 assert response.status_code == 200216 assert response.json() == {217 "auth": "Basic ZXhhbXBsZS11c2VybmFtZTpleGFtcGxlLXBhc3N3b3Jk"218 }219220221@pytest.mark.asyncio222async def test_auth_header_has_priority_over_netrc() -> None:Use relative tests directory references (#1052)Florimond Mancabefore adoption09672a9
line 223why, one step back223 os.environ["NETRC"] = str(FIXTURES_DIR / ".netrc")Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a
line 224why, one step back224 url = "http://netrcexample.org"Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 225why, one step back225 app = App()Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a
line 226why, one step back226Version 0.15.0 (#1301)Tom Christiebefore adoptionf932af9
line 227why, one step back227 async with httpx.AsyncClient(transport=MockTransport(app)) as client:Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
line 228why, one step back228 response = await client.get(url, headers={"Authorization": "Override"})Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a
lines 229–235why, one step back229230 assert response.status_code == 200231 assert response.json() == {"auth": "Override"}232233234@pytest.mark.asyncio235async def test_trust_env_auth() -> None:Use relative tests directory references (#1052)Florimond Mancabefore adoption09672a9
line 236why, one step back236 os.environ["NETRC"] = str(FIXTURES_DIR / ".netrc")Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a
line 237why, one step back237 url = "http://netrcexample.org"Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 238why, one step back238 app = App()Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a
line 239why, one step back239Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
line 240why, one step back240 async with httpx.AsyncClient(Version 0.15.0 (#1301)Tom Christiebefore adoptionf932af9
line 241why, one step back241 transport=MockTransport(app), trust_env=FalseClosing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
lines 242–243why, one step back242 ) as client:243 response = await client.get(url)Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a
lines 244–247why, one step back244245 assert response.status_code == 200246 assert response.json() == {"auth": None}247Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
line 248why, one step back248 async with httpx.AsyncClient(Version 0.15.0 (#1301)Tom Christiebefore adoptionf932af9
line 249why, one step back249 transport=MockTransport(app), trust_env=TrueClosing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
lines 250–251why, one step back250 ) as client:251 response = await client.get(url)Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a
lines 252–258why, one step back252253 assert response.status_code == 200254 assert response.json() == {255 "auth": "Basic ZXhhbXBsZS11c2VybmFtZTpleGFtcGxlLXBhc3N3b3Jk"256 }257258Allow disabling auth per-request using `auth=None` (#1115)Florimond Mancabefore adoptiona4463d0
lines 259–262why, one step back259@pytest.mark.asyncio260async def test_auth_disable_per_request() -> None:261 url = "https://example.org/"262 auth = ("tomchristie", "password123")Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 263why, one step back263 app = App()Allow disabling auth per-request using `auth=None` (#1115)Florimond Mancabefore adoptiona4463d0
line 264why, one step back264Version 0.15.0 (#1301)Tom Christiebefore adoptionf932af9
line 265why, one step back265 async with httpx.AsyncClient(transport=MockTransport(app), auth=auth) as client:Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
line 266why, one step back266 response = await client.get(url, auth=None)Allow disabling auth per-request using `auth=None` (#1115)Florimond Mancabefore adoptiona4463d0
lines 267–271why, one step back267268 assert response.status_code == 200269 assert response.json() == {"auth": None}270271Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a
lines 272–282why, one step back272def test_auth_hidden_url() -> None:273 url = "http://example-username:example-password@example.org/"274 expected = "URL('http://example-username:[secure]@example.org/')"275 assert url == URL(url)276 assert expected == repr(URL(url))277278279@pytest.mark.asyncio280async def test_auth_hidden_header() -> None:281 url = "https://example.org/"282 auth = ("example-username", "example-password")Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 283why, one step back283 app = App()Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a
line 284why, one step back284Version 0.15.0 (#1301)Tom Christiebefore adoptionf932af9
line 285why, one step back285 async with httpx.AsyncClient(transport=MockTransport(app)) as client:Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
line 286why, one step back286 response = await client.get(url, auth=auth)Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a
lines 287–291why, one step back287288 assert "'authorization': '[secure]'" in str(response.request.headers)289290291@pytest.mark.asyncioAdd `Client.auth` setter (#1185)Florimond Mancabefore adoptioncb620e6
line 292why, one step back292async def test_auth_property() -> None:Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
lines 293–294why, one step back293 app = App()294Version 0.15.0 (#1301)Tom Christiebefore adoptionf932af9
line 295why, one step back295 async with httpx.AsyncClient(transport=MockTransport(app)) as client:Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
line 296why, one step back296 assert client.auth is NoneAdd `Client.auth` setter (#1185)Florimond Mancabefore adoptioncb620e6
line 297why, one step back297Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
lines 298–299why, one step back298 client.auth = ("tomchristie", "password123") # type: ignore299 assert isinstance(client.auth, BasicAuth)Add `Client.auth` setter (#1185)Florimond Mancabefore adoptioncb620e6
line 300why, one step back300Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
lines 301–304why, one step back301 url = "https://example.org/"302 response = await client.get(url)303 assert response.status_code == 200304 assert response.json() == {"auth": "Basic dG9tY2hyaXN0aWU6cGFzc3dvcmQxMjM="}Add `Client.auth` setter (#1185)Florimond Mancabefore adoptioncb620e6
lines 305–308why, one step back305306307@pytest.mark.asyncio308async def test_auth_invalid_type() -> None:Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
lines 309–310why, one step back309 app = App()310Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a
line 311why, one step back311 with pytest.raises(TypeError):Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
line 312why, one step back312 client = httpx.AsyncClient(Version 0.15.0 (#1301)Tom Christiebefore adoptionf932af9
line 313why, one step back313 transport=MockTransport(app),Add `Client.auth` setter (#1185)Florimond Mancabefore adoptioncb620e6
lines 314–316why, one step back314 auth="not a tuple, not a callable", # type: ignore315 )316Version 0.15.0 (#1301)Tom Christiebefore adoptionf932af9
line 317why, one step back317 async with httpx.AsyncClient(transport=MockTransport(app)) as client:Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
lines 318–319why, one step back318 with pytest.raises(TypeError):319 await client.get(auth="not a tuple, not a callable") # type: ignoreAdd `Client.auth` setter (#1185)Florimond Mancabefore adoptioncb620e6
line 320why, one step back320Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
lines 321–322why, one step back321 with pytest.raises(TypeError):322 client.auth = "not a tuple, not a callable" # type: ignoreAdd `Client.auth` setter (#1185)Florimond Mancabefore adoptioncb620e6
line 323why, one step back323Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a
lines 324–328why, one step back324325@pytest.mark.asyncio326async def test_digest_auth_returns_no_auth_if_no_digest_header_in_response() -> None:327 url = "https://example.org/"328 auth = DigestAuth(username="tomchristie", password="password123")Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 329why, one step back329 app = App()Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a
line 330why, one step back330Version 0.15.0 (#1301)Tom Christiebefore adoptionf932af9
line 331why, one step back331 async with httpx.AsyncClient(transport=MockTransport(app)) as client:Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
line 332why, one step back332 response = await client.get(url, auth=auth)Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a
lines 333–338why, one step back333334 assert response.status_code == 200335 assert response.json() == {"auth": None}336 assert len(response.history) == 0337338Handle multiple auth headers correctly (#1240)Tom Christiebefore adoption33d339a
lines 339–341why, one step back339def test_digest_auth_returns_no_auth_if_alternate_auth_scheme() -> None:340 url = "https://example.org/"341 auth = DigestAuth(username="tomchristie", password="password123")Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
lines 342–343why, one step back342 auth_header = "Token ..."343 app = App(auth_header=auth_header, status_code=401)Handle multiple auth headers correctly (#1240)Tom Christiebefore adoption33d339a
line 344why, one step back344Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 345why, one step back345 client = httpx.Client(transport=MockTransport(app))Handle multiple auth headers correctly (#1240)Tom Christiebefore adoption33d339a
lines 346–352why, one step back346 response = client.get(url, auth=auth)347348 assert response.status_code == 401349 assert response.json() == {"auth": None}350 assert len(response.history) == 0351352Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a
lines 353–356why, one step back353@pytest.mark.asyncio354async def test_digest_auth_200_response_including_digest_auth_header() -> None:355 url = "https://example.org/"356 auth = DigestAuth(username="tomchristie", password="password123")Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
lines 357–358why, one step back357 auth_header = 'Digest realm="realm@host.com",qop="auth",nonce="abc",opaque="xyz"'358 app = App(auth_header=auth_header, status_code=200)Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a
line 359why, one step back359Version 0.15.0 (#1301)Tom Christiebefore adoptionf932af9
line 360why, one step back360 async with httpx.AsyncClient(transport=MockTransport(app)) as client:Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
line 361why, one step back361 response = await client.get(url, auth=auth)Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a
lines 362–371why, one step back362363 assert response.status_code == 200364 assert response.json() == {"auth": None}365 assert len(response.history) == 0366367368@pytest.mark.asyncio369async def test_digest_auth_401_response_without_digest_auth_header() -> None:370 url = "https://example.org/"371 auth = DigestAuth(username="tomchristie", password="password123")Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 372why, one step back372 app = App(auth_header="", status_code=401)Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a
line 373why, one step back373Version 0.15.0 (#1301)Tom Christiebefore adoptionf932af9
line 374why, one step back374 async with httpx.AsyncClient(transport=MockTransport(app)) as client:Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
line 375why, one step back375 response = await client.get(url, auth=auth)Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a
lines 376–400why, one step back376377 assert response.status_code == 401378 assert response.json() == {"auth": None}379 assert len(response.history) == 0380381382@pytest.mark.parametrize(383 "algorithm,expected_hash_length,expected_response_length",384 [385 ("MD5", 64, 32),386 ("MD5-SESS", 64, 32),387 ("SHA", 64, 40),388 ("SHA-SESS", 64, 40),389 ("SHA-256", 64, 64),390 ("SHA-256-SESS", 64, 64),391 ("SHA-512", 64, 128),392 ("SHA-512-SESS", 64, 128),393 ],394)395@pytest.mark.asyncio396async def test_digest_auth(397 algorithm: str, expected_hash_length: int, expected_response_length: int398) -> None:399 url = "https://example.org/"400 auth = DigestAuth(username="tomchristie", password="password123")Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 401why, one step back401 app = DigestApp(algorithm=algorithm)Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a
line 402why, one step back402Version 0.15.0 (#1301)Tom Christiebefore adoptionf932af9
line 403why, one step back403 async with httpx.AsyncClient(transport=MockTransport(app)) as client:Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
line 404why, one step back404 response = await client.get(url, auth=auth)Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a
lines 405–431why, one step back405406 assert response.status_code == 200407 assert len(response.history) == 1408409 authorization = typing.cast(dict, response.json())["auth"]410 scheme, _, fields = authorization.partition(" ")411 assert scheme == "Digest"412413 response_fields = [field.strip() for field in fields.split(",")]414 digest_data = dict(field.split("=") for field in response_fields)415416 assert digest_data["username"] == '"tomchristie"'417 assert digest_data["realm"] == '"httpx@example.org"'418 assert "nonce" in digest_data419 assert digest_data["uri"] == '"/"'420 assert len(digest_data["response"]) == expected_response_length + 2 # extra quotes421 assert len(digest_data["opaque"]) == expected_hash_length + 2422 assert digest_data["algorithm"] == algorithm423 assert digest_data["qop"] == "auth"424 assert digest_data["nc"] == "00000001"425 assert len(digest_data["cnonce"]) == 16 + 2426427428@pytest.mark.asyncio429async def test_digest_auth_no_specified_qop() -> None:430 url = "https://example.org/"431 auth = DigestAuth(username="tomchristie", password="password123")Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 432why, one step back432 app = DigestApp(qop="")Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a
line 433why, one step back433Version 0.15.0 (#1301)Tom Christiebefore adoptionf932af9
line 434why, one step back434 async with httpx.AsyncClient(transport=MockTransport(app)) as client:Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
line 435why, one step back435 response = await client.get(url, auth=auth)Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a
lines 436–463why, one step back436437 assert response.status_code == 200438 assert len(response.history) == 1439440 authorization = typing.cast(dict, response.json())["auth"]441 scheme, _, fields = authorization.partition(" ")442 assert scheme == "Digest"443444 response_fields = [field.strip() for field in fields.split(",")]445 digest_data = dict(field.split("=") for field in response_fields)446447 assert "qop" not in digest_data448 assert "nc" not in digest_data449 assert "cnonce" not in digest_data450 assert digest_data["username"] == '"tomchristie"'451 assert digest_data["realm"] == '"httpx@example.org"'452 assert len(digest_data["nonce"]) == 64 + 2 # extra quotes453 assert digest_data["uri"] == '"/"'454 assert len(digest_data["response"]) == 64 + 2455 assert len(digest_data["opaque"]) == 64 + 2456 assert digest_data["algorithm"] == "SHA-256"457458459@pytest.mark.parametrize("qop", ("auth, auth-int", "auth,auth-int", "unknown,auth"))460@pytest.mark.asyncio461async def test_digest_auth_qop_including_spaces_and_auth_returns_auth(qop: str) -> None:462 url = "https://example.org/"463 auth = DigestAuth(username="tomchristie", password="password123")Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 464why, one step back464 app = DigestApp(qop=qop)Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a
line 465why, one step back465Version 0.15.0 (#1301)Tom Christiebefore adoptionf932af9
line 466why, one step back466 async with httpx.AsyncClient(transport=MockTransport(app)) as client:Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
line 467why, one step back467 response = await client.get(url, auth=auth)Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a
lines 468–476why, one step back468469 assert response.status_code == 200470 assert len(response.history) == 1471472473@pytest.mark.asyncio474async def test_digest_auth_qop_auth_int_not_implemented() -> None:475 url = "https://example.org/"476 auth = DigestAuth(username="tomchristie", password="password123")Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 477why, one step back477 app = DigestApp(qop="auth-int")Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a
line 478why, one step back478Version 0.15.0 (#1301)Tom Christiebefore adoptionf932af9
line 479why, one step back479 async with httpx.AsyncClient(transport=MockTransport(app)) as client:Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
lines 480–481why, one step back480 with pytest.raises(NotImplementedError):481 await client.get(url, auth=auth)Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a
lines 482–487why, one step back482483484@pytest.mark.asyncio485async def test_digest_auth_qop_must_be_auth_or_auth_int() -> None:486 url = "https://example.org/"487 auth = DigestAuth(username="tomchristie", password="password123")Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 488why, one step back488 app = DigestApp(qop="not-auth")Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a
line 489why, one step back489Version 0.15.0 (#1301)Tom Christiebefore adoptionf932af9
line 490why, one step back490 async with httpx.AsyncClient(transport=MockTransport(app)) as client:Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
lines 491–492why, one step back491 with pytest.raises(ProtocolError):492 await client.get(url, auth=auth)Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a
lines 493–498why, one step back493494495@pytest.mark.asyncio496async def test_digest_auth_incorrect_credentials() -> None:497 url = "https://example.org/"498 auth = DigestAuth(username="tomchristie", password="password123")Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 499why, one step back499 app = DigestApp(send_response_after_attempt=2)Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a
line 500why, one step back500Version 0.15.0 (#1301)Tom Christiebefore adoptionf932af9
line 501why, one step back501 async with httpx.AsyncClient(transport=MockTransport(app)) as client:Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
line 502why, one step back502 response = await client.get(url, auth=auth)Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a
lines 503–510why, one step back503504 assert response.status_code == 401505 assert len(response.history) == 1506507508@pytest.mark.parametrize(509 "auth_header",510 [Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
lines 511–512why, one step back511 'Digest realm="httpx@example.org", qop="auth"', # missing fields512 'Digest realm="httpx@example.org", qop="auth,au', # malformed fields listVersion 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a
lines 513–515why, one step back513 ],514)515@pytest.mark.asyncioIncrease test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067
line 516why, one step back516async def test_async_digest_auth_raises_protocol_error_on_malformed_header(Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 517why, one step back517 auth_header: str,Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a
lines 518–520why, one step back518) -> None:519 url = "https://example.org/"520 auth = DigestAuth(username="tomchristie", password="password123")Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
lines 521–522why, one step back521 app = App(auth_header=auth_header, status_code=401)522Version 0.15.0 (#1301)Tom Christiebefore adoptionf932af9
line 523why, one step back523 async with httpx.AsyncClient(transport=MockTransport(app)) as client:Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
lines 524–525why, one step back524 with pytest.raises(ProtocolError):525 await client.get(url, auth=auth)Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a
lines 526–527why, one step back526527Increase test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067
lines 528–530why, one step back528@pytest.mark.parametrize(529 "auth_header",530 [Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
lines 531–532why, one step back531 'Digest realm="httpx@example.org", qop="auth"', # missing fields532 'Digest realm="httpx@example.org", qop="auth,au', # malformed fields listIncrease test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067
lines 533–535why, one step back533 ],534)535def test_sync_digest_auth_raises_protocol_error_on_malformed_header(Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 536why, one step back536 auth_header: str,Increase test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067
lines 537–539why, one step back537) -> None:538 url = "https://example.org/"539 auth = DigestAuth(username="tomchristie", password="password123")Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 540why, one step back540 app = App(auth_header=auth_header, status_code=401)Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
line 541why, one step back541Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 542why, one step back542 with httpx.Client(transport=MockTransport(app)) as client:Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
lines 543–544why, one step back543 with pytest.raises(ProtocolError):544 client.get(url, auth=auth)Increase test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067
lines 545–546why, one step back545546Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a
line 547why, one step back547@pytest.mark.asyncioIncrease test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067
line 548why, one step back548async def test_async_auth_history() -> None:Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a
lines 549–552why, one step back549 """550 Test that intermediate requests sent as part of an authentication flow551 are recorded in the response history.552 """Increase test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067
lines 553–554why, one step back553 url = "https://example.org/"554 auth = RepeatAuth(repeat=2)Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 555why, one step back555 app = App(auth_header="abc")Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a
line 556why, one step back556Version 0.15.0 (#1301)Tom Christiebefore adoptionf932af9
line 557why, one step back557 async with httpx.AsyncClient(transport=MockTransport(app)) as client:Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
lines 558–559why, one step back558 response = await client.get(url, auth=auth)559Increase test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067
lines 560–561why, one step back560 assert response.status_code == 200561 assert response.json() == {"auth": "Repeat abc.abc"}Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a
line 562why, one step back562Increase test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067
lines 563–566why, one step back563 assert len(response.history) == 2564 resp1, resp2 = response.history565 assert resp1.json() == {"auth": "Repeat 0"}566 assert resp2.json() == {"auth": "Repeat 1"}Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a
line 567why, one step back567Increase test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067
lines 568–569why, one step back568 assert len(resp2.history) == 1569 assert resp2.history == [resp1]Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a
line 570why, one step back570Increase test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067
line 571why, one step back571 assert len(resp1.history) == 0Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a
lines 572–573why, one step back572573Increase test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067
lines 574–578why, one step back574def test_sync_auth_history() -> None:575 """576 Test that intermediate requests sent as part of an authentication flow577 are recorded in the response history.578 """Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a
lines 579–580why, one step back579 url = "https://example.org/"580 auth = RepeatAuth(repeat=2)Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 581why, one step back581 app = App(auth_header="abc")Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a
line 582why, one step back582Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 583why, one step back583 with httpx.Client(transport=MockTransport(app)) as client:Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
lines 584–585why, one step back584 response = client.get(url, auth=auth)585Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a
lines 586–603why, one step back586 assert response.status_code == 200587 assert response.json() == {"auth": "Repeat abc.abc"}588589 assert len(response.history) == 2590 resp1, resp2 = response.history591 assert resp1.json() == {"auth": "Repeat 0"}592 assert resp2.json() == {"auth": "Repeat 1"}593594 assert len(resp2.history) == 1595 assert resp2.history == [resp1]596597 assert len(resp1.history) == 0598599600@pytest.mark.asyncio601async def test_digest_auth_unavailable_streaming_body():602 url = "https://example.org/"603 auth = DigestAuth(username="tomchristie", password="password123")Drop ContentStream (#1295)Tom Christiebefore adoptionfbb21fb
line 604why, one step back604 app = DigestApp()Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a
lines 605–606why, one step back605606 async def streaming_body():Transport API (#963)Yeray Diaz Diazbefore adoptiond2816c9
line 607why, one step back607 yield b"Example request body" # pragma: nocoverVersion 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a
line 608why, one step back608Version 0.15.0 (#1301)Tom Christiebefore adoptionf932af9
line 609why, one step back609 async with httpx.AsyncClient(transport=MockTransport(app)) as client:Drop ContentStream (#1295)Tom Christiebefore adoptionfbb21fb
line 610why, one step back610 with pytest.raises(httpx.StreamConsumed):Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
line 611why, one step back611 await client.post(url, data=streaming_body(), auth=auth)Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a
lines 612–614why, one step back612613614@pytest.mark.asyncioIncrease test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067
line 615why, one step back615async def test_async_auth_reads_response_body() -> None:Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a
lines 616–619why, one step back616 """617 Test that we can read the response body in an auth flow if `requires_response_body`618 is set.619 """Increase test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067
lines 620–621why, one step back620 url = "https://example.org/"621 auth = ResponseBodyAuth("xyz")Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
lines 622–623why, one step back622 app = App()623Version 0.15.0 (#1301)Tom Christiebefore adoptionf932af9
line 624why, one step back624 async with httpx.AsyncClient(transport=MockTransport(app)) as client:Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
line 625why, one step back625 response = await client.get(url, auth=auth)Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a
line 626why, one step back626Increase test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067
lines 627–628why, one step back627 assert response.status_code == 200628 assert response.json() == {"auth": '{"auth": "xyz"}'}Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a
lines 629–630why, one step back629630Increase test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067
lines 631–635why, one step back631def test_sync_auth_reads_response_body() -> None:632 """633 Test that we can read the response body in an auth flow if `requires_response_body`634 is set.635 """Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a
lines 636–637why, one step back636 url = "https://example.org/"637 auth = ResponseBodyAuth("xyz")Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 638why, one step back638 app = App()Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a
line 639why, one step back639Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 640why, one step back640 with httpx.Client(transport=MockTransport(app)) as client:Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
lines 641–642why, one step back641 response = client.get(url, auth=auth)642Version 0.13.0.dev2 (#941)Tom Christiebefore adoption97d488a
lines 643–644why, one step back643 assert response.status_code == 200644 assert response.json() == {"auth": '{"auth": "xyz"}'}Add support for sync-specific or async-specific auth flows (#1217)Florimond Mancabefore adoption016e4ee
lines 645–655why, one step back645646647@pytest.mark.asyncio648async def test_async_auth() -> None:649 """650 Test that we can use an auth implementation specific to the async case, to651 support cases that require performing I/O or using concurrency primitives (such652 as checking a disk-based cache or fetching a token from a remote auth server).653 """654 url = "https://example.org/"655 auth = SyncOrAsyncAuth()Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 656why, one step back656 app = App()Add support for sync-specific or async-specific auth flows (#1217)Florimond Mancabefore adoption016e4ee
line 657why, one step back657Version 0.15.0 (#1301)Tom Christiebefore adoptionf932af9
line 658why, one step back658 async with httpx.AsyncClient(transport=MockTransport(app)) as client:Add support for sync-specific or async-specific auth flows (#1217)Florimond Mancabefore adoption016e4ee
lines 659–670why, one step back659 response = await client.get(url, auth=auth)660661 assert response.status_code == 200662 assert response.json() == {"auth": "async-auth"}663664665def test_sync_auth() -> None:666 """667 Test that we can use an auth implementation specific to the sync case.668 """669 url = "https://example.org/"670 auth = SyncOrAsyncAuth()Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 671why, one step back671 app = App()Add support for sync-specific or async-specific auth flows (#1217)Florimond Mancabefore adoption016e4ee
line 672why, one step back672Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 673why, one step back673 with httpx.Client(transport=MockTransport(app)) as client:Add support for sync-specific or async-specific auth flows (#1217)Florimond Mancabefore adoption016e4ee
lines 674–677why, one step back674 response = client.get(url, auth=auth)675676 assert response.status_code == 200677 assert response.json() == {"auth": "sync-auth"}