test_auth.py
This file
Path
tests/client/test_auth.py
684 lines · 21.6 KiB · Python
Last changed by
Drop ContentStream (#1295)Tom Christiebefore adoptionfbb21fb
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
line 7why, one step back7import hashlibRefactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 8why, one step back8import jsonTest Suite on GitHub Actions (#907)Tom Christiebefore adoption19bf211
line 9why, one step back9import osAdd support for sync-specific or async-specific auth flows (#1217)Florimond Mancabefore adoption016e4ee
line 10why, one step back10import threadingTest Suite on GitHub Actions (#907)Tom Christiebefore adoption19bf211
lines 11–14why, one step back11import typing1213import pytest14Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
line 15why, one step back15import httpxDrop ContentStream (#1295)Tom Christiebefore adoptionfbb21fb
line 16why, one step back16from httpx import URL, Auth, BasicAuth, DigestAuth, ProtocolError, Request, ResponseRefactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 17why, one step back17from tests.utils import AsyncMockTransport, MockTransportTest Suite on GitHub Actions (#907)Tom Christiebefore adoption19bf211
line 18why, one step back18Use relative tests directory references (#1052)Florimond Mancabefore adoption09672a9
line 19why, one step back19from ..common import FIXTURES_DIRTest Suite on GitHub Actions (#907)Tom Christiebefore adoption19bf211
line 20why, one step back20Use relative tests directory references (#1052)Florimond Mancabefore adoption09672a9
line 21why, one step back21Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
lines 22–23why, one step back22class App:23 def __init__(self, auth_header: str = "", status_code: int = 200) -> None:Test Suite on GitHub Actions (#907)Tom Christiebefore adoption19bf211
lines 24–26why, one step back24 self.auth_header = auth_header25 self.status_code = status_code26Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
lines 27–31why, one step back27 def __call__(self, request: httpx.Request) -> httpx.Response:28 headers = {"www-authenticate": self.auth_header} if self.auth_header else {}29 data = {"auth": request.headers.get("Authorization")}30 content = json.dumps(data).encode("utf-8")31 return httpx.Response(self.status_code, headers=headers, content=content)Test Suite on GitHub Actions (#907)Tom Christiebefore adoption19bf211
lines 32–33why, one step back3233Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 34why, one step back34class DigestApp:Test Suite on GitHub Actions (#907)Tom Christiebefore adoption19bf211
lines 35–47why, one step back35 def __init__(36 self,37 algorithm: str = "SHA-256",38 send_response_after_attempt: int = 1,39 qop: str = "auth",40 regenerate_nonce: bool = True,41 ) -> None:42 self.algorithm = algorithm43 self.send_response_after_attempt = send_response_after_attempt44 self.qop = qop45 self._regenerate_nonce = regenerate_nonce46 self._response_count = 047Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 48why, one step back48 def __call__(self, request: httpx.Request) -> httpx.Response:Test Suite on GitHub Actions (#907)Tom Christiebefore adoption19bf211
line 49why, one step back49 if self._response_count < self.send_response_after_attempt:Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 50why, one step back50 return self.challenge_send(request)Test Suite on GitHub Actions (#907)Tom Christiebefore adoption19bf211
line 51why, one step back51Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
lines 52–54why, one step back52 data = {"auth": request.headers.get("Authorization")}53 content = json.dumps(data).encode("utf-8")54 return httpx.Response(200, content=content)Test Suite on GitHub Actions (#907)Tom Christiebefore adoption19bf211
line 55why, one step back55Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 56why, one step back56 def challenge_send(self, request: httpx.Request) -> httpx.Response:Test Suite on GitHub Actions (#907)Tom Christiebefore adoption19bf211
lines 57–77why, one step back57 self._response_count += 158 nonce = (59 hashlib.sha256(os.urandom(8)).hexdigest()60 if self._regenerate_nonce61 else "ee96edced2a0b43e4869e96ebe27563f369c1205a049d06419bb51d8aeddf3d3"62 )63 challenge_data = {64 "nonce": nonce,65 "qop": self.qop,66 "opaque": (67 "ee6378f3ee14ebfd2fff54b70a91a7c9390518047f242ab2271380db0e14bda1"68 ),69 "algorithm": self.algorithm,70 "stale": "FALSE",71 }72 challenge_str = ", ".join(73 '{}="{}"'.format(key, value)74 for key, value in challenge_data.items()75 if value76 )77Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
lines 78–81why, one step back78 headers = {79 "www-authenticate": f'Digest realm="httpx@example.org", {challenge_str}',80 }81 return Response(401, headers=headers)Test Suite on GitHub Actions (#907)Tom Christiebefore adoption19bf211
lines 82–83why, one step back8283Increase test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067
lines 84–129why, one step back84class RepeatAuth(Auth):85 """86 A mock authentication scheme that requires clients to send87 the request a fixed number of times, and then send a last request containing88 an aggregation of nonces that the server sent in 'WWW-Authenticate' headers89 of intermediate responses.90 """9192 requires_request_body = True9394 def __init__(self, repeat: int):95 self.repeat = repeat9697 def auth_flow(self, request: Request) -> typing.Generator[Request, Response, None]:98 nonces = []99100 for index in range(self.repeat):101 request.headers["Authorization"] = f"Repeat {index}"102 response = yield request103 nonces.append(response.headers["www-authenticate"])104105 key = ".".join(nonces)106 request.headers["Authorization"] = f"Repeat {key}"107 yield request108109110class ResponseBodyAuth(Auth):111 """112 A mock authentication scheme that requires clients to send an 'Authorization'113 header, then send back the contents of the response in the 'Authorization'114 header.115 """116117 requires_response_body = True118119 def __init__(self, token):120 self.token = token121122 def auth_flow(self, request: Request) -> typing.Generator[Request, Response, None]:123 request.headers["Authorization"] = self.token124 response = yield request125 data = response.text126 request.headers["Authorization"] = data127 yield request128129Add support for sync-specific or async-specific auth flows (#1217)Florimond Mancabefore adoption016e4ee
lines 130–154why, one step back130class SyncOrAsyncAuth(Auth):131 """132 A mock authentication scheme that uses a different implementation for the133 sync and async cases.134 """135136 def __init__(self) -> None:137 self._lock = threading.Lock()138 self._async_lock = asyncio.Lock()139140 def sync_auth_flow(141 self, request: Request142 ) -> typing.Generator[Request, Response, None]:143 with self._lock:144 request.headers["Authorization"] = "sync-auth"145 yield request146147 async def async_auth_flow(148 self, request: Request149 ) -> typing.AsyncGenerator[Request, Response]:150 async with self._async_lock:151 request.headers["Authorization"] = "async-auth"152 yield request153154Test Suite on GitHub Actions (#907)Tom Christiebefore adoption19bf211
lines 155–158why, one step back155@pytest.mark.asyncio156async def test_basic_auth() -> None:157 url = "https://example.org/"158 auth = ("tomchristie", "password123")Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 159why, one step back159 app = App()Test Suite on GitHub Actions (#907)Tom Christiebefore adoption19bf211
line 160why, one step back160Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 161why, one step back161 async with httpx.AsyncClient(transport=AsyncMockTransport(app)) as client:Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
line 162why, one step back162 response = await client.get(url, auth=auth)Test Suite on GitHub Actions (#907)Tom Christiebefore adoption19bf211
lines 163–170why, one step back163164 assert response.status_code == 200165 assert response.json() == {"auth": "Basic dG9tY2hyaXN0aWU6cGFzc3dvcmQxMjM="}166167168@pytest.mark.asyncio169async def test_basic_auth_in_url() -> None:170 url = "https://tomchristie:password123@example.org/"Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 171why, one step back171 app = App()Test Suite on GitHub Actions (#907)Tom Christiebefore adoption19bf211
line 172why, one step back172Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 173why, one step back173 async with httpx.AsyncClient(transport=AsyncMockTransport(app)) as client:Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
line 174why, one step back174 response = await client.get(url)Test Suite on GitHub Actions (#907)Tom Christiebefore adoption19bf211
lines 175–183why, one step back175176 assert response.status_code == 200177 assert response.json() == {"auth": "Basic dG9tY2hyaXN0aWU6cGFzc3dvcmQxMjM="}178179180@pytest.mark.asyncio181async def test_basic_auth_on_session() -> None:182 url = "https://example.org/"183 auth = ("tomchristie", "password123")Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 184why, one step back184 app = App()Test Suite on GitHub Actions (#907)Tom Christiebefore adoption19bf211
line 185why, one step back185Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
lines 186–188why, one step back186 async with httpx.AsyncClient(187 transport=AsyncMockTransport(app), auth=auth188 ) as client:Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
line 189why, one step back189 response = await client.get(url)Test Suite on GitHub Actions (#907)Tom Christiebefore adoption19bf211
lines 190–197why, one step back190191 assert response.status_code == 200192 assert response.json() == {"auth": "Basic dG9tY2hyaXN0aWU6cGFzc3dvcmQxMjM="}193194195@pytest.mark.asyncio196async def test_custom_auth() -> None:197 url = "https://example.org/"Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 198why, one step back198 app = App()Test Suite on GitHub Actions (#907)Tom Christiebefore adoption19bf211
lines 199–203why, one step back199200 def auth(request: Request) -> Request:201 request.headers["Authorization"] = "Token 123"202 return request203Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 204why, one step back204 async with httpx.AsyncClient(transport=AsyncMockTransport(app)) as client:Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
line 205why, one step back205 response = await client.get(url, auth=auth)Test Suite on GitHub Actions (#907)Tom Christiebefore adoption19bf211
lines 206–212why, one step back206207 assert response.status_code == 200208 assert response.json() == {"auth": "Token 123"}209210211@pytest.mark.asyncio212async def test_netrc_auth() -> None:Use relative tests directory references (#1052)Florimond Mancabefore adoption09672a9
line 213why, one step back213 os.environ["NETRC"] = str(FIXTURES_DIR / ".netrc")Test Suite on GitHub Actions (#907)Tom Christiebefore adoption19bf211
line 214why, one step back214 url = "http://netrcexample.org"Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 215why, one step back215 app = App()Test Suite on GitHub Actions (#907)Tom Christiebefore adoption19bf211
line 216why, one step back216Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 217why, one step back217 async with httpx.AsyncClient(transport=AsyncMockTransport(app)) as client:Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
line 218why, one step back218 response = await client.get(url)Test Suite on GitHub Actions (#907)Tom Christiebefore adoption19bf211
lines 219–227why, one step back219220 assert response.status_code == 200221 assert response.json() == {222 "auth": "Basic ZXhhbXBsZS11c2VybmFtZTpleGFtcGxlLXBhc3N3b3Jk"223 }224225226@pytest.mark.asyncio227async def test_auth_header_has_priority_over_netrc() -> None:Use relative tests directory references (#1052)Florimond Mancabefore adoption09672a9
line 228why, one step back228 os.environ["NETRC"] = str(FIXTURES_DIR / ".netrc")Test Suite on GitHub Actions (#907)Tom Christiebefore adoption19bf211
line 229why, one step back229 url = "http://netrcexample.org"Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 230why, one step back230 app = App()Test Suite on GitHub Actions (#907)Tom Christiebefore adoption19bf211
line 231why, one step back231Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 232why, one step back232 async with httpx.AsyncClient(transport=AsyncMockTransport(app)) as client:Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
line 233why, one step back233 response = await client.get(url, headers={"Authorization": "Override"})Test Suite on GitHub Actions (#907)Tom Christiebefore adoption19bf211
lines 234–240why, one step back234235 assert response.status_code == 200236 assert response.json() == {"auth": "Override"}237238239@pytest.mark.asyncio240async def test_trust_env_auth() -> None:Use relative tests directory references (#1052)Florimond Mancabefore adoption09672a9
line 241why, one step back241 os.environ["NETRC"] = str(FIXTURES_DIR / ".netrc")Test Suite on GitHub Actions (#907)Tom Christiebefore adoption19bf211
line 242why, one step back242 url = "http://netrcexample.org"Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 243why, one step back243 app = App()Test Suite on GitHub Actions (#907)Tom Christiebefore adoption19bf211
line 244why, one step back244Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
line 245why, one step back245 async with httpx.AsyncClient(Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 246why, one step back246 transport=AsyncMockTransport(app), trust_env=FalseClosing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
lines 247–248why, one step back247 ) as client:248 response = await client.get(url)Test Suite on GitHub Actions (#907)Tom Christiebefore adoption19bf211
lines 249–252why, one step back249250 assert response.status_code == 200251 assert response.json() == {"auth": None}252Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
line 253why, one step back253 async with httpx.AsyncClient(Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 254why, one step back254 transport=AsyncMockTransport(app), trust_env=TrueClosing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
lines 255–256why, one step back255 ) as client:256 response = await client.get(url)Test Suite on GitHub Actions (#907)Tom Christiebefore adoption19bf211
lines 257–263why, one step back257258 assert response.status_code == 200259 assert response.json() == {260 "auth": "Basic ZXhhbXBsZS11c2VybmFtZTpleGFtcGxlLXBhc3N3b3Jk"261 }262263Allow disabling auth per-request using `auth=None` (#1115)Florimond Mancabefore adoptiona4463d0
lines 264–267why, one step back264@pytest.mark.asyncio265async def test_auth_disable_per_request() -> None:266 url = "https://example.org/"267 auth = ("tomchristie", "password123")Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 268why, one step back268 app = App()Allow disabling auth per-request using `auth=None` (#1115)Florimond Mancabefore adoptiona4463d0
line 269why, one step back269Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
lines 270–272why, one step back270 async with httpx.AsyncClient(271 transport=AsyncMockTransport(app), auth=auth272 ) as client:Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
line 273why, one step back273 response = await client.get(url, auth=None)Allow disabling auth per-request using `auth=None` (#1115)Florimond Mancabefore adoptiona4463d0
lines 274–278why, one step back274275 assert response.status_code == 200276 assert response.json() == {"auth": None}277278Test Suite on GitHub Actions (#907)Tom Christiebefore adoption19bf211
lines 279–289why, one step back279def test_auth_hidden_url() -> None:280 url = "http://example-username:example-password@example.org/"281 expected = "URL('http://example-username:[secure]@example.org/')"282 assert url == URL(url)283 assert expected == repr(URL(url))284285286@pytest.mark.asyncio287async def test_auth_hidden_header() -> None:288 url = "https://example.org/"289 auth = ("example-username", "example-password")Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 290why, one step back290 app = App()Test Suite on GitHub Actions (#907)Tom Christiebefore adoption19bf211
line 291why, one step back291Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 292why, one step back292 async with httpx.AsyncClient(transport=AsyncMockTransport(app)) as client:Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
line 293why, one step back293 response = await client.get(url, auth=auth)Test Suite on GitHub Actions (#907)Tom Christiebefore adoption19bf211
lines 294–298why, one step back294295 assert "'authorization': '[secure]'" in str(response.request.headers)296297298@pytest.mark.asyncioAdd `Client.auth` setter (#1185)Florimond Mancabefore adoptioncb620e6
line 299why, one step back299async def test_auth_property() -> None:Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
lines 300–302why, one step back300 app = App()301302 async with httpx.AsyncClient(transport=AsyncMockTransport(app)) as client:Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
line 303why, one step back303 assert client.auth is NoneAdd `Client.auth` setter (#1185)Florimond Mancabefore adoptioncb620e6
line 304why, one step back304Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
lines 305–306why, one step back305 client.auth = ("tomchristie", "password123") # type: ignore306 assert isinstance(client.auth, BasicAuth)Add `Client.auth` setter (#1185)Florimond Mancabefore adoptioncb620e6
line 307why, one step back307Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
lines 308–311why, one step back308 url = "https://example.org/"309 response = await client.get(url)310 assert response.status_code == 200311 assert response.json() == {"auth": "Basic dG9tY2hyaXN0aWU6cGFzc3dvcmQxMjM="}Add `Client.auth` setter (#1185)Florimond Mancabefore adoptioncb620e6
lines 312–315why, one step back312313314@pytest.mark.asyncio315async def test_auth_invalid_type() -> None:Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
lines 316–317why, one step back316 app = App()317Test Suite on GitHub Actions (#907)Tom Christiebefore adoption19bf211
line 318why, one step back318 with pytest.raises(TypeError):Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
line 319why, one step back319 client = httpx.AsyncClient(Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 320why, one step back320 transport=AsyncMockTransport(app),Add `Client.auth` setter (#1185)Florimond Mancabefore adoptioncb620e6
lines 321–323why, one step back321 auth="not a tuple, not a callable", # type: ignore322 )323Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 324why, one step back324 async with httpx.AsyncClient(transport=AsyncMockTransport(app)) as client:Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
lines 325–326why, one step back325 with pytest.raises(TypeError):326 await client.get(auth="not a tuple, not a callable") # type: ignoreAdd `Client.auth` setter (#1185)Florimond Mancabefore adoptioncb620e6
line 327why, one step back327Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
lines 328–329why, one step back328 with pytest.raises(TypeError):329 client.auth = "not a tuple, not a callable" # type: ignoreAdd `Client.auth` setter (#1185)Florimond Mancabefore adoptioncb620e6
line 330why, one step back330Test Suite on GitHub Actions (#907)Tom Christiebefore adoption19bf211
lines 331–335why, one step back331332@pytest.mark.asyncio333async def test_digest_auth_returns_no_auth_if_no_digest_header_in_response() -> None:334 url = "https://example.org/"335 auth = DigestAuth(username="tomchristie", password="password123")Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 336why, one step back336 app = App()Test Suite on GitHub Actions (#907)Tom Christiebefore adoption19bf211
line 337why, one step back337Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 338why, one step back338 async with httpx.AsyncClient(transport=AsyncMockTransport(app)) as client:Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
line 339why, one step back339 response = await client.get(url, auth=auth)Test Suite on GitHub Actions (#907)Tom Christiebefore adoption19bf211
lines 340–345why, one step back340341 assert response.status_code == 200342 assert response.json() == {"auth": None}343 assert len(response.history) == 0344345Handle multiple auth headers correctly (#1240)Tom Christiebefore adoption33d339a
lines 346–348why, one step back346def test_digest_auth_returns_no_auth_if_alternate_auth_scheme() -> None:347 url = "https://example.org/"348 auth = DigestAuth(username="tomchristie", password="password123")Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
lines 349–350why, one step back349 auth_header = "Token ..."350 app = App(auth_header=auth_header, status_code=401)Handle multiple auth headers correctly (#1240)Tom Christiebefore adoption33d339a
line 351why, one step back351Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 352why, one step back352 client = httpx.Client(transport=MockTransport(app))Handle multiple auth headers correctly (#1240)Tom Christiebefore adoption33d339a
lines 353–359why, one step back353 response = client.get(url, auth=auth)354355 assert response.status_code == 401356 assert response.json() == {"auth": None}357 assert len(response.history) == 0358359Test Suite on GitHub Actions (#907)Tom Christiebefore adoption19bf211
lines 360–363why, one step back360@pytest.mark.asyncio361async def test_digest_auth_200_response_including_digest_auth_header() -> None:362 url = "https://example.org/"363 auth = DigestAuth(username="tomchristie", password="password123")Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
lines 364–365why, one step back364 auth_header = 'Digest realm="realm@host.com",qop="auth",nonce="abc",opaque="xyz"'365 app = App(auth_header=auth_header, status_code=200)Test Suite on GitHub Actions (#907)Tom Christiebefore adoption19bf211
line 366why, one step back366Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 367why, one step back367 async with httpx.AsyncClient(transport=AsyncMockTransport(app)) as client:Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
line 368why, one step back368 response = await client.get(url, auth=auth)Test Suite on GitHub Actions (#907)Tom Christiebefore adoption19bf211
lines 369–378why, one step back369370 assert response.status_code == 200371 assert response.json() == {"auth": None}372 assert len(response.history) == 0373374375@pytest.mark.asyncio376async def test_digest_auth_401_response_without_digest_auth_header() -> None:377 url = "https://example.org/"378 auth = DigestAuth(username="tomchristie", password="password123")Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 379why, one step back379 app = App(auth_header="", status_code=401)Test Suite on GitHub Actions (#907)Tom Christiebefore adoption19bf211
line 380why, one step back380Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 381why, one step back381 async with httpx.AsyncClient(transport=AsyncMockTransport(app)) as client:Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
line 382why, one step back382 response = await client.get(url, auth=auth)Test Suite on GitHub Actions (#907)Tom Christiebefore adoption19bf211
lines 383–407why, one step back383384 assert response.status_code == 401385 assert response.json() == {"auth": None}386 assert len(response.history) == 0387388389@pytest.mark.parametrize(390 "algorithm,expected_hash_length,expected_response_length",391 [392 ("MD5", 64, 32),393 ("MD5-SESS", 64, 32),394 ("SHA", 64, 40),395 ("SHA-SESS", 64, 40),396 ("SHA-256", 64, 64),397 ("SHA-256-SESS", 64, 64),398 ("SHA-512", 64, 128),399 ("SHA-512-SESS", 64, 128),400 ],401)402@pytest.mark.asyncio403async def test_digest_auth(404 algorithm: str, expected_hash_length: int, expected_response_length: int405) -> None:406 url = "https://example.org/"407 auth = DigestAuth(username="tomchristie", password="password123")Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 408why, one step back408 app = DigestApp(algorithm=algorithm)Test Suite on GitHub Actions (#907)Tom Christiebefore adoption19bf211
line 409why, one step back409Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 410why, one step back410 async with httpx.AsyncClient(transport=AsyncMockTransport(app)) as client:Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
line 411why, one step back411 response = await client.get(url, auth=auth)Test Suite on GitHub Actions (#907)Tom Christiebefore adoption19bf211
lines 412–438why, one step back412413 assert response.status_code == 200414 assert len(response.history) == 1415416 authorization = typing.cast(dict, response.json())["auth"]417 scheme, _, fields = authorization.partition(" ")418 assert scheme == "Digest"419420 response_fields = [field.strip() for field in fields.split(",")]421 digest_data = dict(field.split("=") for field in response_fields)422423 assert digest_data["username"] == '"tomchristie"'424 assert digest_data["realm"] == '"httpx@example.org"'425 assert "nonce" in digest_data426 assert digest_data["uri"] == '"/"'427 assert len(digest_data["response"]) == expected_response_length + 2 # extra quotes428 assert len(digest_data["opaque"]) == expected_hash_length + 2429 assert digest_data["algorithm"] == algorithm430 assert digest_data["qop"] == "auth"431 assert digest_data["nc"] == "00000001"432 assert len(digest_data["cnonce"]) == 16 + 2433434435@pytest.mark.asyncio436async def test_digest_auth_no_specified_qop() -> None:437 url = "https://example.org/"438 auth = DigestAuth(username="tomchristie", password="password123")Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 439why, one step back439 app = DigestApp(qop="")Test Suite on GitHub Actions (#907)Tom Christiebefore adoption19bf211
line 440why, one step back440Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 441why, one step back441 async with httpx.AsyncClient(transport=AsyncMockTransport(app)) as client:Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
line 442why, one step back442 response = await client.get(url, auth=auth)Test Suite on GitHub Actions (#907)Tom Christiebefore adoption19bf211
lines 443–470why, one step back443444 assert response.status_code == 200445 assert len(response.history) == 1446447 authorization = typing.cast(dict, response.json())["auth"]448 scheme, _, fields = authorization.partition(" ")449 assert scheme == "Digest"450451 response_fields = [field.strip() for field in fields.split(",")]452 digest_data = dict(field.split("=") for field in response_fields)453454 assert "qop" not in digest_data455 assert "nc" not in digest_data456 assert "cnonce" not in digest_data457 assert digest_data["username"] == '"tomchristie"'458 assert digest_data["realm"] == '"httpx@example.org"'459 assert len(digest_data["nonce"]) == 64 + 2 # extra quotes460 assert digest_data["uri"] == '"/"'461 assert len(digest_data["response"]) == 64 + 2462 assert len(digest_data["opaque"]) == 64 + 2463 assert digest_data["algorithm"] == "SHA-256"464465466@pytest.mark.parametrize("qop", ("auth, auth-int", "auth,auth-int", "unknown,auth"))467@pytest.mark.asyncio468async def test_digest_auth_qop_including_spaces_and_auth_returns_auth(qop: str) -> None:469 url = "https://example.org/"470 auth = DigestAuth(username="tomchristie", password="password123")Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 471why, one step back471 app = DigestApp(qop=qop)Test Suite on GitHub Actions (#907)Tom Christiebefore adoption19bf211
line 472why, one step back472Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 473why, one step back473 async with httpx.AsyncClient(transport=AsyncMockTransport(app)) as client:Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
line 474why, one step back474 response = await client.get(url, auth=auth)Test Suite on GitHub Actions (#907)Tom Christiebefore adoption19bf211
lines 475–483why, one step back475476 assert response.status_code == 200477 assert len(response.history) == 1478479480@pytest.mark.asyncio481async def test_digest_auth_qop_auth_int_not_implemented() -> None:482 url = "https://example.org/"483 auth = DigestAuth(username="tomchristie", password="password123")Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 484why, one step back484 app = DigestApp(qop="auth-int")Test Suite on GitHub Actions (#907)Tom Christiebefore adoption19bf211
line 485why, one step back485Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 486why, one step back486 async with httpx.AsyncClient(transport=AsyncMockTransport(app)) as client:Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
lines 487–488why, one step back487 with pytest.raises(NotImplementedError):488 await client.get(url, auth=auth)Test Suite on GitHub Actions (#907)Tom Christiebefore adoption19bf211
lines 489–494why, one step back489490491@pytest.mark.asyncio492async def test_digest_auth_qop_must_be_auth_or_auth_int() -> None:493 url = "https://example.org/"494 auth = DigestAuth(username="tomchristie", password="password123")Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 495why, one step back495 app = DigestApp(qop="not-auth")Test Suite on GitHub Actions (#907)Tom Christiebefore adoption19bf211
line 496why, one step back496Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 497why, one step back497 async with httpx.AsyncClient(transport=AsyncMockTransport(app)) as client:Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
lines 498–499why, one step back498 with pytest.raises(ProtocolError):499 await client.get(url, auth=auth)Test Suite on GitHub Actions (#907)Tom Christiebefore adoption19bf211
lines 500–505why, one step back500501502@pytest.mark.asyncio503async def test_digest_auth_incorrect_credentials() -> None:504 url = "https://example.org/"505 auth = DigestAuth(username="tomchristie", password="password123")Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 506why, one step back506 app = DigestApp(send_response_after_attempt=2)Test Suite on GitHub Actions (#907)Tom Christiebefore adoption19bf211
line 507why, one step back507Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 508why, one step back508 async with httpx.AsyncClient(transport=AsyncMockTransport(app)) as client:Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
line 509why, one step back509 response = await client.get(url, auth=auth)Test Suite on GitHub Actions (#907)Tom Christiebefore adoption19bf211
lines 510–517why, one step back510511 assert response.status_code == 401512 assert len(response.history) == 1513514515@pytest.mark.parametrize(516 "auth_header",517 [Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
lines 518–519why, one step back518 'Digest realm="httpx@example.org", qop="auth"', # missing fields519 'Digest realm="httpx@example.org", qop="auth,au', # malformed fields listTest Suite on GitHub Actions (#907)Tom Christiebefore adoption19bf211
lines 520–522why, one step back520 ],521)522@pytest.mark.asyncioIncrease test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067
line 523why, one step back523async def test_async_digest_auth_raises_protocol_error_on_malformed_header(Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 524why, one step back524 auth_header: str,Test Suite on GitHub Actions (#907)Tom Christiebefore adoption19bf211
lines 525–527why, one step back525) -> None:526 url = "https://example.org/"527 auth = DigestAuth(username="tomchristie", password="password123")Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
lines 528–530why, one step back528 app = App(auth_header=auth_header, status_code=401)529530 async with httpx.AsyncClient(transport=AsyncMockTransport(app)) as client:Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
lines 531–532why, one step back531 with pytest.raises(ProtocolError):532 await client.get(url, auth=auth)Test Suite on GitHub Actions (#907)Tom Christiebefore adoption19bf211
lines 533–534why, one step back533534Increase test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067
lines 535–537why, one step back535@pytest.mark.parametrize(536 "auth_header",537 [Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
lines 538–539why, one step back538 'Digest realm="httpx@example.org", qop="auth"', # missing fields539 'Digest realm="httpx@example.org", qop="auth,au', # malformed fields listIncrease test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067
lines 540–542why, one step back540 ],541)542def test_sync_digest_auth_raises_protocol_error_on_malformed_header(Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 543why, one step back543 auth_header: str,Increase test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067
lines 544–546why, one step back544) -> None:545 url = "https://example.org/"546 auth = DigestAuth(username="tomchristie", password="password123")Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 547why, one step back547 app = App(auth_header=auth_header, status_code=401)Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
line 548why, one step back548Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 549why, one step back549 with httpx.Client(transport=MockTransport(app)) as client:Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
lines 550–551why, one step back550 with pytest.raises(ProtocolError):551 client.get(url, auth=auth)Increase test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067
lines 552–553why, one step back552553Test Suite on GitHub Actions (#907)Tom Christiebefore adoption19bf211
line 554why, one step back554@pytest.mark.asyncioIncrease test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067
line 555why, one step back555async def test_async_auth_history() -> None:Test Suite on GitHub Actions (#907)Tom Christiebefore adoption19bf211
lines 556–559why, one step back556 """557 Test that intermediate requests sent as part of an authentication flow558 are recorded in the response history.559 """Increase test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067
lines 560–561why, one step back560 url = "https://example.org/"561 auth = RepeatAuth(repeat=2)Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 562why, one step back562 app = App(auth_header="abc")Test Suite on GitHub Actions (#907)Tom Christiebefore adoption19bf211
line 563why, one step back563Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 564why, one step back564 async with httpx.AsyncClient(transport=AsyncMockTransport(app)) as client:Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
lines 565–566why, one step back565 response = await client.get(url, auth=auth)566Increase test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067
lines 567–568why, one step back567 assert response.status_code == 200568 assert response.json() == {"auth": "Repeat abc.abc"}Test Suite on GitHub Actions (#907)Tom Christiebefore adoption19bf211
line 569why, one step back569Increase test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067
lines 570–573why, one step back570 assert len(response.history) == 2571 resp1, resp2 = response.history572 assert resp1.json() == {"auth": "Repeat 0"}573 assert resp2.json() == {"auth": "Repeat 1"}Test Suite on GitHub Actions (#907)Tom Christiebefore adoption19bf211
line 574why, one step back574Increase test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067
lines 575–576why, one step back575 assert len(resp2.history) == 1576 assert resp2.history == [resp1]Test Suite on GitHub Actions (#907)Tom Christiebefore adoption19bf211
line 577why, one step back577Increase test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067
line 578why, one step back578 assert len(resp1.history) == 0Test Suite on GitHub Actions (#907)Tom Christiebefore adoption19bf211
lines 579–580why, one step back579580Increase test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067
lines 581–585why, one step back581def test_sync_auth_history() -> None:582 """583 Test that intermediate requests sent as part of an authentication flow584 are recorded in the response history.585 """Test Suite on GitHub Actions (#907)Tom Christiebefore adoption19bf211
lines 586–587why, one step back586 url = "https://example.org/"587 auth = RepeatAuth(repeat=2)Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 588why, one step back588 app = App(auth_header="abc")Test Suite on GitHub Actions (#907)Tom Christiebefore adoption19bf211
line 589why, one step back589Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 590why, one step back590 with httpx.Client(transport=MockTransport(app)) as client:Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
lines 591–592why, one step back591 response = client.get(url, auth=auth)592Test Suite on GitHub Actions (#907)Tom Christiebefore adoption19bf211
lines 593–610why, one step back593 assert response.status_code == 200594 assert response.json() == {"auth": "Repeat abc.abc"}595596 assert len(response.history) == 2597 resp1, resp2 = response.history598 assert resp1.json() == {"auth": "Repeat 0"}599 assert resp2.json() == {"auth": "Repeat 1"}600601 assert len(resp2.history) == 1602 assert resp2.history == [resp1]603604 assert len(resp1.history) == 0605606607@pytest.mark.asyncio608async def test_digest_auth_unavailable_streaming_body():609 url = "https://example.org/"610 auth = DigestAuth(username="tomchristie", password="password123")Drop ContentStream (#1295)Tom Christiebefore adoptionfbb21fb
line 611why, one step back611 app = DigestApp()Test Suite on GitHub Actions (#907)Tom Christiebefore adoption19bf211
lines 612–613why, one step back612613 async def streaming_body():Transport API (#963)Yeray Diaz Diazbefore adoptiond2816c9
line 614why, one step back614 yield b"Example request body" # pragma: nocoverTest Suite on GitHub Actions (#907)Tom Christiebefore adoption19bf211
line 615why, one step back615Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 616why, one step back616 async with httpx.AsyncClient(transport=AsyncMockTransport(app)) as client:Drop ContentStream (#1295)Tom Christiebefore adoptionfbb21fb
line 617why, one step back617 with pytest.raises(httpx.StreamConsumed):Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
line 618why, one step back618 await client.post(url, data=streaming_body(), auth=auth)Test Suite on GitHub Actions (#907)Tom Christiebefore adoption19bf211
lines 619–621why, one step back619620621@pytest.mark.asyncioIncrease test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067
line 622why, one step back622async def test_async_auth_reads_response_body() -> None:Test Suite on GitHub Actions (#907)Tom Christiebefore adoption19bf211
lines 623–626why, one step back623 """624 Test that we can read the response body in an auth flow if `requires_response_body`625 is set.626 """Increase test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067
lines 627–628why, one step back627 url = "https://example.org/"628 auth = ResponseBodyAuth("xyz")Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
lines 629–631why, one step back629 app = App()630631 async with httpx.AsyncClient(transport=AsyncMockTransport(app)) as client:Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
line 632why, one step back632 response = await client.get(url, auth=auth)Test Suite on GitHub Actions (#907)Tom Christiebefore adoption19bf211
line 633why, one step back633Increase test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067
lines 634–635why, one step back634 assert response.status_code == 200635 assert response.json() == {"auth": '{"auth": "xyz"}'}Test Suite on GitHub Actions (#907)Tom Christiebefore adoption19bf211
lines 636–637why, one step back636637Increase test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067
lines 638–642why, one step back638def test_sync_auth_reads_response_body() -> None:639 """640 Test that we can read the response body in an auth flow if `requires_response_body`641 is set.642 """Test Suite on GitHub Actions (#907)Tom Christiebefore adoption19bf211
lines 643–644why, one step back643 url = "https://example.org/"644 auth = ResponseBodyAuth("xyz")Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 645why, one step back645 app = App()Test Suite on GitHub Actions (#907)Tom Christiebefore adoption19bf211
line 646why, one step back646Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 647why, one step back647 with httpx.Client(transport=MockTransport(app)) as client:Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
lines 648–649why, one step back648 response = client.get(url, auth=auth)649Test Suite on GitHub Actions (#907)Tom Christiebefore adoption19bf211
lines 650–651why, one step back650 assert response.status_code == 200651 assert response.json() == {"auth": '{"auth": "xyz"}'}Add support for sync-specific or async-specific auth flows (#1217)Florimond Mancabefore adoption016e4ee
lines 652–662why, one step back652653654@pytest.mark.asyncio655async def test_async_auth() -> None:656 """657 Test that we can use an auth implementation specific to the async case, to658 support cases that require performing I/O or using concurrency primitives (such659 as checking a disk-based cache or fetching a token from a remote auth server).660 """661 url = "https://example.org/"662 auth = SyncOrAsyncAuth()Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 663why, one step back663 app = App()Add support for sync-specific or async-specific auth flows (#1217)Florimond Mancabefore adoption016e4ee
line 664why, one step back664Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 665why, one step back665 async with httpx.AsyncClient(transport=AsyncMockTransport(app)) as client:Add support for sync-specific or async-specific auth flows (#1217)Florimond Mancabefore adoption016e4ee
lines 666–677why, one step back666 response = await client.get(url, auth=auth)667668 assert response.status_code == 200669 assert response.json() == {"auth": "async-auth"}670671672def test_sync_auth() -> None:673 """674 Test that we can use an auth implementation specific to the sync case.675 """676 url = "https://example.org/"677 auth = SyncOrAsyncAuth()Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 678why, one step back678 app = App()Add support for sync-specific or async-specific auth flows (#1217)Florimond Mancabefore adoption016e4ee
line 679why, one step back679Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 680why, one step back680 with httpx.Client(transport=MockTransport(app)) as client:Add support for sync-specific or async-specific auth flows (#1217)Florimond Mancabefore adoption016e4ee
lines 681–684why, one step back681 response = client.get(url, auth=auth)682683 assert response.status_code == 200684 assert response.json() == {"auth": "sync-auth"}