test_auth.py
This file
Path
tests/client/test_auth.py
693 lines · 21.7 KiB · Python
Last changed by
Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
History of this fileTake it
The walk stopped after 200 commits: lines older than 5829ecb 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 asyncioImplement streaming multipart uploads (#857)Florimond Mancabefore adoption5829ecb
line 7why, one step back7import hashlibRefactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 8why, one step back8import jsonImplement streaming multipart uploads (#857)Florimond Mancabefore adoption5829ecb
line 9why, one step back9import osAdd support for sync-specific or async-specific auth flows (#1217)Florimond Mancabefore adoption016e4ee
line 10why, one step back10import threadingImplement streaming multipart uploads (#857)Florimond Mancabefore adoption5829ecb
lines 11–14why, one step back11import typing1213import pytest14Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
line 15why, one step back15import httpxImplement streaming multipart uploads (#857)Florimond Mancabefore adoption5829ecb
lines 16–18why, one step back16from httpx import (17 URL,18 Auth,Add `Client.auth` setter (#1185)Florimond Mancabefore adoptioncb620e6
line 19why, one step back19 BasicAuth,Implement streaming multipart uploads (#857)Florimond Mancabefore adoption5829ecb
lines 20–25why, one step back20 DigestAuth,21 ProtocolError,22 Request,23 RequestBodyUnavailable,24 Response,25)Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 26why, one step back26from tests.utils import AsyncMockTransport, MockTransportImplement streaming multipart uploads (#857)Florimond Mancabefore adoption5829ecb
line 27why, one step back27Use relative tests directory references (#1052)Florimond Mancabefore adoption09672a9
line 28why, one step back28from ..common import FIXTURES_DIRImplement streaming multipart uploads (#857)Florimond Mancabefore adoption5829ecb
line 29why, one step back29Use relative tests directory references (#1052)Florimond Mancabefore adoption09672a9
line 30why, one step back30Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
lines 31–32why, one step back31class App:32 def __init__(self, auth_header: str = "", status_code: int = 200) -> None:Implement streaming multipart uploads (#857)Florimond Mancabefore adoption5829ecb
lines 33–35why, one step back33 self.auth_header = auth_header34 self.status_code = status_code35Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
lines 36–40why, one step back36 def __call__(self, request: httpx.Request) -> httpx.Response:37 headers = {"www-authenticate": self.auth_header} if self.auth_header else {}38 data = {"auth": request.headers.get("Authorization")}39 content = json.dumps(data).encode("utf-8")40 return httpx.Response(self.status_code, headers=headers, content=content)Implement streaming multipart uploads (#857)Florimond Mancabefore adoption5829ecb
lines 41–42why, one step back4142Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 43why, one step back43class DigestApp:Implement streaming multipart uploads (#857)Florimond Mancabefore adoption5829ecb
lines 44–56why, one step back44 def __init__(45 self,46 algorithm: str = "SHA-256",47 send_response_after_attempt: int = 1,48 qop: str = "auth",49 regenerate_nonce: bool = True,50 ) -> None:51 self.algorithm = algorithm52 self.send_response_after_attempt = send_response_after_attempt53 self.qop = qop54 self._regenerate_nonce = regenerate_nonce55 self._response_count = 056Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 57why, one step back57 def __call__(self, request: httpx.Request) -> httpx.Response:Implement streaming multipart uploads (#857)Florimond Mancabefore adoption5829ecb
line 58why, one step back58 if self._response_count < self.send_response_after_attempt:Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 59why, one step back59 return self.challenge_send(request)Implement streaming multipart uploads (#857)Florimond Mancabefore adoption5829ecb
line 60why, one step back60Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
lines 61–63why, one step back61 data = {"auth": request.headers.get("Authorization")}62 content = json.dumps(data).encode("utf-8")63 return httpx.Response(200, content=content)Implement streaming multipart uploads (#857)Florimond Mancabefore adoption5829ecb
line 64why, one step back64Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 65why, one step back65 def challenge_send(self, request: httpx.Request) -> httpx.Response:Implement streaming multipart uploads (#857)Florimond Mancabefore adoption5829ecb
lines 66–86why, one step back66 self._response_count += 167 nonce = (68 hashlib.sha256(os.urandom(8)).hexdigest()69 if self._regenerate_nonce70 else "ee96edced2a0b43e4869e96ebe27563f369c1205a049d06419bb51d8aeddf3d3"71 )72 challenge_data = {73 "nonce": nonce,74 "qop": self.qop,75 "opaque": (76 "ee6378f3ee14ebfd2fff54b70a91a7c9390518047f242ab2271380db0e14bda1"77 ),78 "algorithm": self.algorithm,79 "stale": "FALSE",80 }81 challenge_str = ", ".join(82 '{}="{}"'.format(key, value)83 for key, value in challenge_data.items()84 if value85 )86Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
lines 87–90why, one step back87 headers = {88 "www-authenticate": f'Digest realm="httpx@example.org", {challenge_str}',89 }90 return Response(401, headers=headers)Implement streaming multipart uploads (#857)Florimond Mancabefore adoption5829ecb
lines 91–92why, one step back9192Increase test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067
lines 93–138why, one step back93class RepeatAuth(Auth):94 """95 A mock authentication scheme that requires clients to send96 the request a fixed number of times, and then send a last request containing97 an aggregation of nonces that the server sent in 'WWW-Authenticate' headers98 of intermediate responses.99 """100101 requires_request_body = True102103 def __init__(self, repeat: int):104 self.repeat = repeat105106 def auth_flow(self, request: Request) -> typing.Generator[Request, Response, None]:107 nonces = []108109 for index in range(self.repeat):110 request.headers["Authorization"] = f"Repeat {index}"111 response = yield request112 nonces.append(response.headers["www-authenticate"])113114 key = ".".join(nonces)115 request.headers["Authorization"] = f"Repeat {key}"116 yield request117118119class ResponseBodyAuth(Auth):120 """121 A mock authentication scheme that requires clients to send an 'Authorization'122 header, then send back the contents of the response in the 'Authorization'123 header.124 """125126 requires_response_body = True127128 def __init__(self, token):129 self.token = token130131 def auth_flow(self, request: Request) -> typing.Generator[Request, Response, None]:132 request.headers["Authorization"] = self.token133 response = yield request134 data = response.text135 request.headers["Authorization"] = data136 yield request137138Add support for sync-specific or async-specific auth flows (#1217)Florimond Mancabefore adoption016e4ee
lines 139–163why, one step back139class SyncOrAsyncAuth(Auth):140 """141 A mock authentication scheme that uses a different implementation for the142 sync and async cases.143 """144145 def __init__(self) -> None:146 self._lock = threading.Lock()147 self._async_lock = asyncio.Lock()148149 def sync_auth_flow(150 self, request: Request151 ) -> typing.Generator[Request, Response, None]:152 with self._lock:153 request.headers["Authorization"] = "sync-auth"154 yield request155156 async def async_auth_flow(157 self, request: Request158 ) -> typing.AsyncGenerator[Request, Response]:159 async with self._async_lock:160 request.headers["Authorization"] = "async-auth"161 yield request162163Implement streaming multipart uploads (#857)Florimond Mancabefore adoption5829ecb
lines 164–167why, one step back164@pytest.mark.asyncio165async def test_basic_auth() -> None:166 url = "https://example.org/"167 auth = ("tomchristie", "password123")Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 168why, one step back168 app = App()Implement streaming multipart uploads (#857)Florimond Mancabefore adoption5829ecb
line 169why, one step back169Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 170why, one step back170 async with httpx.AsyncClient(transport=AsyncMockTransport(app)) as client:Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
line 171why, one step back171 response = await client.get(url, auth=auth)Implement streaming multipart uploads (#857)Florimond Mancabefore adoption5829ecb
lines 172–179why, one step back172173 assert response.status_code == 200174 assert response.json() == {"auth": "Basic dG9tY2hyaXN0aWU6cGFzc3dvcmQxMjM="}175176177@pytest.mark.asyncio178async def test_basic_auth_in_url() -> None:179 url = "https://tomchristie:password123@example.org/"Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 180why, one step back180 app = App()Implement streaming multipart uploads (#857)Florimond Mancabefore adoption5829ecb
line 181why, one step back181Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 182why, one step back182 async with httpx.AsyncClient(transport=AsyncMockTransport(app)) as client:Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
line 183why, one step back183 response = await client.get(url)Implement streaming multipart uploads (#857)Florimond Mancabefore adoption5829ecb
lines 184–192why, one step back184185 assert response.status_code == 200186 assert response.json() == {"auth": "Basic dG9tY2hyaXN0aWU6cGFzc3dvcmQxMjM="}187188189@pytest.mark.asyncio190async def test_basic_auth_on_session() -> None:191 url = "https://example.org/"192 auth = ("tomchristie", "password123")Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 193why, one step back193 app = App()Implement streaming multipart uploads (#857)Florimond Mancabefore adoption5829ecb
line 194why, one step back194Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
lines 195–197why, one step back195 async with httpx.AsyncClient(196 transport=AsyncMockTransport(app), auth=auth197 ) as client:Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
line 198why, one step back198 response = await client.get(url)Implement streaming multipart uploads (#857)Florimond Mancabefore adoption5829ecb
lines 199–206why, one step back199200 assert response.status_code == 200201 assert response.json() == {"auth": "Basic dG9tY2hyaXN0aWU6cGFzc3dvcmQxMjM="}202203204@pytest.mark.asyncio205async def test_custom_auth() -> None:206 url = "https://example.org/"Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 207why, one step back207 app = App()Implement streaming multipart uploads (#857)Florimond Mancabefore adoption5829ecb
lines 208–212why, one step back208209 def auth(request: Request) -> Request:210 request.headers["Authorization"] = "Token 123"211 return request212Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 213why, one step back213 async with httpx.AsyncClient(transport=AsyncMockTransport(app)) as client:Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
line 214why, one step back214 response = await client.get(url, auth=auth)Implement streaming multipart uploads (#857)Florimond Mancabefore adoption5829ecb
lines 215–221why, one step back215216 assert response.status_code == 200217 assert response.json() == {"auth": "Token 123"}218219220@pytest.mark.asyncio221async def test_netrc_auth() -> None:Use relative tests directory references (#1052)Florimond Mancabefore adoption09672a9
line 222why, one step back222 os.environ["NETRC"] = str(FIXTURES_DIR / ".netrc")Implement streaming multipart uploads (#857)Florimond Mancabefore adoption5829ecb
line 223why, one step back223 url = "http://netrcexample.org"Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 224why, one step back224 app = App()Implement streaming multipart uploads (#857)Florimond Mancabefore adoption5829ecb
line 225why, one step back225Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 226why, one step back226 async with httpx.AsyncClient(transport=AsyncMockTransport(app)) as client:Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
line 227why, one step back227 response = await client.get(url)Implement streaming multipart uploads (#857)Florimond Mancabefore adoption5829ecb
lines 228–236why, one step back228229 assert response.status_code == 200230 assert response.json() == {231 "auth": "Basic ZXhhbXBsZS11c2VybmFtZTpleGFtcGxlLXBhc3N3b3Jk"232 }233234235@pytest.mark.asyncio236async def test_auth_header_has_priority_over_netrc() -> None:Use relative tests directory references (#1052)Florimond Mancabefore adoption09672a9
line 237why, one step back237 os.environ["NETRC"] = str(FIXTURES_DIR / ".netrc")Implement streaming multipart uploads (#857)Florimond Mancabefore adoption5829ecb
line 238why, one step back238 url = "http://netrcexample.org"Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 239why, one step back239 app = App()Implement streaming multipart uploads (#857)Florimond Mancabefore adoption5829ecb
line 240why, one step back240Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 241why, one step back241 async with httpx.AsyncClient(transport=AsyncMockTransport(app)) as client:Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
line 242why, one step back242 response = await client.get(url, headers={"Authorization": "Override"})Implement streaming multipart uploads (#857)Florimond Mancabefore adoption5829ecb
lines 243–249why, one step back243244 assert response.status_code == 200245 assert response.json() == {"auth": "Override"}246247248@pytest.mark.asyncio249async def test_trust_env_auth() -> None:Use relative tests directory references (#1052)Florimond Mancabefore adoption09672a9
line 250why, one step back250 os.environ["NETRC"] = str(FIXTURES_DIR / ".netrc")Implement streaming multipart uploads (#857)Florimond Mancabefore adoption5829ecb
line 251why, one step back251 url = "http://netrcexample.org"Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 252why, one step back252 app = App()Implement streaming multipart uploads (#857)Florimond Mancabefore adoption5829ecb
line 253why, one step back253Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
line 254why, one step back254 async with httpx.AsyncClient(Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 255why, one step back255 transport=AsyncMockTransport(app), trust_env=FalseClosing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
lines 256–257why, one step back256 ) as client:257 response = await client.get(url)Implement streaming multipart uploads (#857)Florimond Mancabefore adoption5829ecb
lines 258–261why, one step back258259 assert response.status_code == 200260 assert response.json() == {"auth": None}261Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
line 262why, one step back262 async with httpx.AsyncClient(Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 263why, one step back263 transport=AsyncMockTransport(app), trust_env=TrueClosing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
lines 264–265why, one step back264 ) as client:265 response = await client.get(url)Implement streaming multipart uploads (#857)Florimond Mancabefore adoption5829ecb
lines 266–272why, one step back266267 assert response.status_code == 200268 assert response.json() == {269 "auth": "Basic ZXhhbXBsZS11c2VybmFtZTpleGFtcGxlLXBhc3N3b3Jk"270 }271272Allow disabling auth per-request using `auth=None` (#1115)Florimond Mancabefore adoptiona4463d0
lines 273–276why, one step back273@pytest.mark.asyncio274async def test_auth_disable_per_request() -> None:275 url = "https://example.org/"276 auth = ("tomchristie", "password123")Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 277why, one step back277 app = App()Allow disabling auth per-request using `auth=None` (#1115)Florimond Mancabefore adoptiona4463d0
line 278why, one step back278Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
lines 279–281why, one step back279 async with httpx.AsyncClient(280 transport=AsyncMockTransport(app), auth=auth281 ) as client:Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
line 282why, one step back282 response = await client.get(url, auth=None)Allow disabling auth per-request using `auth=None` (#1115)Florimond Mancabefore adoptiona4463d0
lines 283–287why, one step back283284 assert response.status_code == 200285 assert response.json() == {"auth": None}286287Implement streaming multipart uploads (#857)Florimond Mancabefore adoption5829ecb
lines 288–298why, one step back288def test_auth_hidden_url() -> None:289 url = "http://example-username:example-password@example.org/"290 expected = "URL('http://example-username:[secure]@example.org/')"291 assert url == URL(url)292 assert expected == repr(URL(url))293294295@pytest.mark.asyncio296async def test_auth_hidden_header() -> None:297 url = "https://example.org/"298 auth = ("example-username", "example-password")Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 299why, one step back299 app = App()Implement streaming multipart uploads (#857)Florimond Mancabefore adoption5829ecb
line 300why, one step back300Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 301why, one step back301 async with httpx.AsyncClient(transport=AsyncMockTransport(app)) as client:Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
line 302why, one step back302 response = await client.get(url, auth=auth)Implement streaming multipart uploads (#857)Florimond Mancabefore adoption5829ecb
lines 303–307why, one step back303304 assert "'authorization': '[secure]'" in str(response.request.headers)305306307@pytest.mark.asyncioAdd `Client.auth` setter (#1185)Florimond Mancabefore adoptioncb620e6
line 308why, one step back308async def test_auth_property() -> None:Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
lines 309–311why, one step back309 app = App()310311 async with httpx.AsyncClient(transport=AsyncMockTransport(app)) as client:Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
line 312why, one step back312 assert client.auth is NoneAdd `Client.auth` setter (#1185)Florimond Mancabefore adoptioncb620e6
line 313why, one step back313Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
lines 314–315why, one step back314 client.auth = ("tomchristie", "password123") # type: ignore315 assert isinstance(client.auth, BasicAuth)Add `Client.auth` setter (#1185)Florimond Mancabefore adoptioncb620e6
line 316why, one step back316Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
lines 317–320why, one step back317 url = "https://example.org/"318 response = await client.get(url)319 assert response.status_code == 200320 assert response.json() == {"auth": "Basic dG9tY2hyaXN0aWU6cGFzc3dvcmQxMjM="}Add `Client.auth` setter (#1185)Florimond Mancabefore adoptioncb620e6
lines 321–324why, one step back321322323@pytest.mark.asyncio324async def test_auth_invalid_type() -> None:Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
lines 325–326why, one step back325 app = App()326Implement streaming multipart uploads (#857)Florimond Mancabefore adoption5829ecb
line 327why, one step back327 with pytest.raises(TypeError):Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
line 328why, one step back328 client = httpx.AsyncClient(Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 329why, one step back329 transport=AsyncMockTransport(app),Add `Client.auth` setter (#1185)Florimond Mancabefore adoptioncb620e6
lines 330–332why, one step back330 auth="not a tuple, not a callable", # type: ignore331 )332Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 333why, one step back333 async with httpx.AsyncClient(transport=AsyncMockTransport(app)) as client:Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
lines 334–335why, one step back334 with pytest.raises(TypeError):335 await client.get(auth="not a tuple, not a callable") # type: ignoreAdd `Client.auth` setter (#1185)Florimond Mancabefore adoptioncb620e6
line 336why, one step back336Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
lines 337–338why, one step back337 with pytest.raises(TypeError):338 client.auth = "not a tuple, not a callable" # type: ignoreAdd `Client.auth` setter (#1185)Florimond Mancabefore adoptioncb620e6
line 339why, one step back339Implement streaming multipart uploads (#857)Florimond Mancabefore adoption5829ecb
lines 340–344why, one step back340341@pytest.mark.asyncio342async def test_digest_auth_returns_no_auth_if_no_digest_header_in_response() -> None:343 url = "https://example.org/"344 auth = DigestAuth(username="tomchristie", password="password123")Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 345why, one step back345 app = App()Implement streaming multipart uploads (#857)Florimond Mancabefore adoption5829ecb
line 346why, one step back346Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 347why, one step back347 async with httpx.AsyncClient(transport=AsyncMockTransport(app)) as client:Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
line 348why, one step back348 response = await client.get(url, auth=auth)Implement streaming multipart uploads (#857)Florimond Mancabefore adoption5829ecb
lines 349–354why, one step back349350 assert response.status_code == 200351 assert response.json() == {"auth": None}352 assert len(response.history) == 0353354Handle multiple auth headers correctly (#1240)Tom Christiebefore adoption33d339a
lines 355–357why, one step back355def test_digest_auth_returns_no_auth_if_alternate_auth_scheme() -> None:356 url = "https://example.org/"357 auth = DigestAuth(username="tomchristie", password="password123")Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
lines 358–359why, one step back358 auth_header = "Token ..."359 app = App(auth_header=auth_header, status_code=401)Handle multiple auth headers correctly (#1240)Tom Christiebefore adoption33d339a
line 360why, one step back360Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 361why, one step back361 client = httpx.Client(transport=MockTransport(app))Handle multiple auth headers correctly (#1240)Tom Christiebefore adoption33d339a
lines 362–368why, one step back362 response = client.get(url, auth=auth)363364 assert response.status_code == 401365 assert response.json() == {"auth": None}366 assert len(response.history) == 0367368Implement streaming multipart uploads (#857)Florimond Mancabefore adoption5829ecb
lines 369–372why, one step back369@pytest.mark.asyncio370async def test_digest_auth_200_response_including_digest_auth_header() -> None:371 url = "https://example.org/"372 auth = DigestAuth(username="tomchristie", password="password123")Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
lines 373–374why, one step back373 auth_header = 'Digest realm="realm@host.com",qop="auth",nonce="abc",opaque="xyz"'374 app = App(auth_header=auth_header, status_code=200)Implement streaming multipart uploads (#857)Florimond Mancabefore adoption5829ecb
line 375why, one step back375Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 376why, one step back376 async with httpx.AsyncClient(transport=AsyncMockTransport(app)) as client:Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
line 377why, one step back377 response = await client.get(url, auth=auth)Implement streaming multipart uploads (#857)Florimond Mancabefore adoption5829ecb
lines 378–387why, one step back378379 assert response.status_code == 200380 assert response.json() == {"auth": None}381 assert len(response.history) == 0382383384@pytest.mark.asyncio385async def test_digest_auth_401_response_without_digest_auth_header() -> None:386 url = "https://example.org/"387 auth = DigestAuth(username="tomchristie", password="password123")Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 388why, one step back388 app = App(auth_header="", status_code=401)Implement streaming multipart uploads (#857)Florimond Mancabefore adoption5829ecb
line 389why, one step back389Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 390why, one step back390 async with httpx.AsyncClient(transport=AsyncMockTransport(app)) as client:Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
line 391why, one step back391 response = await client.get(url, auth=auth)Implement streaming multipart uploads (#857)Florimond Mancabefore adoption5829ecb
lines 392–416why, one step back392393 assert response.status_code == 401394 assert response.json() == {"auth": None}395 assert len(response.history) == 0396397398@pytest.mark.parametrize(399 "algorithm,expected_hash_length,expected_response_length",400 [401 ("MD5", 64, 32),402 ("MD5-SESS", 64, 32),403 ("SHA", 64, 40),404 ("SHA-SESS", 64, 40),405 ("SHA-256", 64, 64),406 ("SHA-256-SESS", 64, 64),407 ("SHA-512", 64, 128),408 ("SHA-512-SESS", 64, 128),409 ],410)411@pytest.mark.asyncio412async def test_digest_auth(413 algorithm: str, expected_hash_length: int, expected_response_length: int414) -> None:415 url = "https://example.org/"416 auth = DigestAuth(username="tomchristie", password="password123")Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 417why, one step back417 app = DigestApp(algorithm=algorithm)Implement streaming multipart uploads (#857)Florimond Mancabefore adoption5829ecb
line 418why, one step back418Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 419why, one step back419 async with httpx.AsyncClient(transport=AsyncMockTransport(app)) as client:Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
line 420why, one step back420 response = await client.get(url, auth=auth)Implement streaming multipart uploads (#857)Florimond Mancabefore adoption5829ecb
lines 421–447why, one step back421422 assert response.status_code == 200423 assert len(response.history) == 1424425 authorization = typing.cast(dict, response.json())["auth"]426 scheme, _, fields = authorization.partition(" ")427 assert scheme == "Digest"428429 response_fields = [field.strip() for field in fields.split(",")]430 digest_data = dict(field.split("=") for field in response_fields)431432 assert digest_data["username"] == '"tomchristie"'433 assert digest_data["realm"] == '"httpx@example.org"'434 assert "nonce" in digest_data435 assert digest_data["uri"] == '"/"'436 assert len(digest_data["response"]) == expected_response_length + 2 # extra quotes437 assert len(digest_data["opaque"]) == expected_hash_length + 2438 assert digest_data["algorithm"] == algorithm439 assert digest_data["qop"] == "auth"440 assert digest_data["nc"] == "00000001"441 assert len(digest_data["cnonce"]) == 16 + 2442443444@pytest.mark.asyncio445async def test_digest_auth_no_specified_qop() -> None:446 url = "https://example.org/"447 auth = DigestAuth(username="tomchristie", password="password123")Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 448why, one step back448 app = DigestApp(qop="")Implement streaming multipart uploads (#857)Florimond Mancabefore adoption5829ecb
line 449why, one step back449Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 450why, one step back450 async with httpx.AsyncClient(transport=AsyncMockTransport(app)) as client:Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
line 451why, one step back451 response = await client.get(url, auth=auth)Implement streaming multipart uploads (#857)Florimond Mancabefore adoption5829ecb
lines 452–479why, one step back452453 assert response.status_code == 200454 assert len(response.history) == 1455456 authorization = typing.cast(dict, response.json())["auth"]457 scheme, _, fields = authorization.partition(" ")458 assert scheme == "Digest"459460 response_fields = [field.strip() for field in fields.split(",")]461 digest_data = dict(field.split("=") for field in response_fields)462463 assert "qop" not in digest_data464 assert "nc" not in digest_data465 assert "cnonce" not in digest_data466 assert digest_data["username"] == '"tomchristie"'467 assert digest_data["realm"] == '"httpx@example.org"'468 assert len(digest_data["nonce"]) == 64 + 2 # extra quotes469 assert digest_data["uri"] == '"/"'470 assert len(digest_data["response"]) == 64 + 2471 assert len(digest_data["opaque"]) == 64 + 2472 assert digest_data["algorithm"] == "SHA-256"473474475@pytest.mark.parametrize("qop", ("auth, auth-int", "auth,auth-int", "unknown,auth"))476@pytest.mark.asyncio477async def test_digest_auth_qop_including_spaces_and_auth_returns_auth(qop: str) -> None:478 url = "https://example.org/"479 auth = DigestAuth(username="tomchristie", password="password123")Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 480why, one step back480 app = DigestApp(qop=qop)Implement streaming multipart uploads (#857)Florimond Mancabefore adoption5829ecb
line 481why, one step back481Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 482why, one step back482 async with httpx.AsyncClient(transport=AsyncMockTransport(app)) as client:Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
line 483why, one step back483 response = await client.get(url, auth=auth)Implement streaming multipart uploads (#857)Florimond Mancabefore adoption5829ecb
lines 484–492why, one step back484485 assert response.status_code == 200486 assert len(response.history) == 1487488489@pytest.mark.asyncio490async def test_digest_auth_qop_auth_int_not_implemented() -> None:491 url = "https://example.org/"492 auth = DigestAuth(username="tomchristie", password="password123")Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 493why, one step back493 app = DigestApp(qop="auth-int")Implement streaming multipart uploads (#857)Florimond Mancabefore adoption5829ecb
line 494why, one step back494Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 495why, one step back495 async with httpx.AsyncClient(transport=AsyncMockTransport(app)) as client:Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
lines 496–497why, one step back496 with pytest.raises(NotImplementedError):497 await client.get(url, auth=auth)Implement streaming multipart uploads (#857)Florimond Mancabefore adoption5829ecb
lines 498–503why, one step back498499500@pytest.mark.asyncio501async def test_digest_auth_qop_must_be_auth_or_auth_int() -> None:502 url = "https://example.org/"503 auth = DigestAuth(username="tomchristie", password="password123")Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 504why, one step back504 app = DigestApp(qop="not-auth")Implement streaming multipart uploads (#857)Florimond Mancabefore adoption5829ecb
line 505why, one step back505Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 506why, one step back506 async with httpx.AsyncClient(transport=AsyncMockTransport(app)) as client:Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
lines 507–508why, one step back507 with pytest.raises(ProtocolError):508 await client.get(url, auth=auth)Implement streaming multipart uploads (#857)Florimond Mancabefore adoption5829ecb
lines 509–514why, one step back509510511@pytest.mark.asyncio512async def test_digest_auth_incorrect_credentials() -> None:513 url = "https://example.org/"514 auth = DigestAuth(username="tomchristie", password="password123")Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 515why, one step back515 app = DigestApp(send_response_after_attempt=2)Implement streaming multipart uploads (#857)Florimond Mancabefore adoption5829ecb
line 516why, one step back516Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 517why, one step back517 async with httpx.AsyncClient(transport=AsyncMockTransport(app)) as client:Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
line 518why, one step back518 response = await client.get(url, auth=auth)Implement streaming multipart uploads (#857)Florimond Mancabefore adoption5829ecb
lines 519–526why, one step back519520 assert response.status_code == 401521 assert len(response.history) == 1522523524@pytest.mark.parametrize(525 "auth_header",526 [Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
lines 527–528why, one step back527 'Digest realm="httpx@example.org", qop="auth"', # missing fields528 'Digest realm="httpx@example.org", qop="auth,au', # malformed fields listImplement streaming multipart uploads (#857)Florimond Mancabefore adoption5829ecb
lines 529–531why, one step back529 ],530)531@pytest.mark.asyncioIncrease test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067
line 532why, one step back532async def test_async_digest_auth_raises_protocol_error_on_malformed_header(Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 533why, one step back533 auth_header: str,Implement streaming multipart uploads (#857)Florimond Mancabefore adoption5829ecb
lines 534–536why, one step back534) -> None:535 url = "https://example.org/"536 auth = DigestAuth(username="tomchristie", password="password123")Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
lines 537–539why, one step back537 app = App(auth_header=auth_header, status_code=401)538539 async with httpx.AsyncClient(transport=AsyncMockTransport(app)) as client:Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
lines 540–541why, one step back540 with pytest.raises(ProtocolError):541 await client.get(url, auth=auth)Implement streaming multipart uploads (#857)Florimond Mancabefore adoption5829ecb
lines 542–543why, one step back542543Increase test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067
lines 544–546why, one step back544@pytest.mark.parametrize(545 "auth_header",546 [Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
lines 547–548why, one step back547 'Digest realm="httpx@example.org", qop="auth"', # missing fields548 'Digest realm="httpx@example.org", qop="auth,au', # malformed fields listIncrease test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067
lines 549–551why, one step back549 ],550)551def test_sync_digest_auth_raises_protocol_error_on_malformed_header(Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 552why, one step back552 auth_header: str,Increase test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067
lines 553–555why, one step back553) -> None:554 url = "https://example.org/"555 auth = DigestAuth(username="tomchristie", password="password123")Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 556why, one step back556 app = App(auth_header=auth_header, status_code=401)Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
line 557why, one step back557Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 558why, one step back558 with httpx.Client(transport=MockTransport(app)) as client:Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
lines 559–560why, one step back559 with pytest.raises(ProtocolError):560 client.get(url, auth=auth)Increase test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067
lines 561–562why, one step back561562Implement streaming multipart uploads (#857)Florimond Mancabefore adoption5829ecb
line 563why, one step back563@pytest.mark.asyncioIncrease test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067
line 564why, one step back564async def test_async_auth_history() -> None:Implement streaming multipart uploads (#857)Florimond Mancabefore adoption5829ecb
lines 565–568why, one step back565 """566 Test that intermediate requests sent as part of an authentication flow567 are recorded in the response history.568 """Increase test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067
lines 569–570why, one step back569 url = "https://example.org/"570 auth = RepeatAuth(repeat=2)Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 571why, one step back571 app = App(auth_header="abc")Implement streaming multipart uploads (#857)Florimond Mancabefore adoption5829ecb
line 572why, one step back572Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 573why, one step back573 async with httpx.AsyncClient(transport=AsyncMockTransport(app)) as client:Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
lines 574–575why, one step back574 response = await client.get(url, auth=auth)575Increase test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067
lines 576–577why, one step back576 assert response.status_code == 200577 assert response.json() == {"auth": "Repeat abc.abc"}Implement streaming multipart uploads (#857)Florimond Mancabefore adoption5829ecb
line 578why, one step back578Increase test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067
lines 579–582why, one step back579 assert len(response.history) == 2580 resp1, resp2 = response.history581 assert resp1.json() == {"auth": "Repeat 0"}582 assert resp2.json() == {"auth": "Repeat 1"}Implement streaming multipart uploads (#857)Florimond Mancabefore adoption5829ecb
line 583why, one step back583Increase test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067
lines 584–585why, one step back584 assert len(resp2.history) == 1585 assert resp2.history == [resp1]Implement streaming multipart uploads (#857)Florimond Mancabefore adoption5829ecb
line 586why, one step back586Increase test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067
line 587why, one step back587 assert len(resp1.history) == 0Implement streaming multipart uploads (#857)Florimond Mancabefore adoption5829ecb
lines 588–589why, one step back588589Increase test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067
lines 590–594why, one step back590def test_sync_auth_history() -> None:591 """592 Test that intermediate requests sent as part of an authentication flow593 are recorded in the response history.594 """Implement streaming multipart uploads (#857)Florimond Mancabefore adoption5829ecb
lines 595–596why, one step back595 url = "https://example.org/"596 auth = RepeatAuth(repeat=2)Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 597why, one step back597 app = App(auth_header="abc")Implement streaming multipart uploads (#857)Florimond Mancabefore adoption5829ecb
line 598why, one step back598Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 599why, one step back599 with httpx.Client(transport=MockTransport(app)) as client:Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
lines 600–601why, one step back600 response = client.get(url, auth=auth)601Implement streaming multipart uploads (#857)Florimond Mancabefore adoption5829ecb
lines 602–619why, one step back602 assert response.status_code == 200603 assert response.json() == {"auth": "Repeat abc.abc"}604605 assert len(response.history) == 2606 resp1, resp2 = response.history607 assert resp1.json() == {"auth": "Repeat 0"}608 assert resp2.json() == {"auth": "Repeat 1"}609610 assert len(resp2.history) == 1611 assert resp2.history == [resp1]612613 assert len(resp1.history) == 0614615616@pytest.mark.asyncio617async def test_digest_auth_unavailable_streaming_body():618 url = "https://example.org/"619 auth = DigestAuth(username="tomchristie", password="password123")Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 620why, one step back620 app = App()Implement streaming multipart uploads (#857)Florimond Mancabefore adoption5829ecb
lines 621–622why, one step back621622 async def streaming_body():Transport API (#963)Yeray Diaz Diazbefore adoptiond2816c9
line 623why, one step back623 yield b"Example request body" # pragma: nocoverImplement streaming multipart uploads (#857)Florimond Mancabefore adoption5829ecb
line 624why, one step back624Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 625why, one step back625 async with httpx.AsyncClient(transport=AsyncMockTransport(app)) as client:Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
lines 626–627why, one step back626 with pytest.raises(RequestBodyUnavailable):627 await client.post(url, data=streaming_body(), auth=auth)Implement streaming multipart uploads (#857)Florimond Mancabefore adoption5829ecb
lines 628–630why, one step back628629630@pytest.mark.asyncioIncrease test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067
line 631why, one step back631async def test_async_auth_reads_response_body() -> None:Implement streaming multipart uploads (#857)Florimond Mancabefore adoption5829ecb
lines 632–635why, one step back632 """633 Test that we can read the response body in an auth flow if `requires_response_body`634 is set.635 """Increase test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067
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
lines 638–640why, one step back638 app = App()639640 async with httpx.AsyncClient(transport=AsyncMockTransport(app)) as client:Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
line 641why, one step back641 response = await client.get(url, auth=auth)Implement streaming multipart uploads (#857)Florimond Mancabefore adoption5829ecb
line 642why, one step back642Increase test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067
lines 643–644why, one step back643 assert response.status_code == 200644 assert response.json() == {"auth": '{"auth": "xyz"}'}Implement streaming multipart uploads (#857)Florimond Mancabefore adoption5829ecb
lines 645–646why, one step back645646Increase test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067
lines 647–651why, one step back647def test_sync_auth_reads_response_body() -> None:648 """649 Test that we can read the response body in an auth flow if `requires_response_body`650 is set.651 """Implement streaming multipart uploads (#857)Florimond Mancabefore adoption5829ecb
lines 652–653why, one step back652 url = "https://example.org/"653 auth = ResponseBodyAuth("xyz")Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 654why, one step back654 app = App()Implement streaming multipart uploads (#857)Florimond Mancabefore adoption5829ecb
line 655why, one step back655Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 656why, one step back656 with httpx.Client(transport=MockTransport(app)) as client:Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
lines 657–658why, one step back657 response = client.get(url, auth=auth)658Implement streaming multipart uploads (#857)Florimond Mancabefore adoption5829ecb
lines 659–660why, one step back659 assert response.status_code == 200660 assert response.json() == {"auth": '{"auth": "xyz"}'}Add support for sync-specific or async-specific auth flows (#1217)Florimond Mancabefore adoption016e4ee
lines 661–671why, one step back661662663@pytest.mark.asyncio664async def test_async_auth() -> None:665 """666 Test that we can use an auth implementation specific to the async case, to667 support cases that require performing I/O or using concurrency primitives (such668 as checking a disk-based cache or fetching a token from a remote auth server).669 """670 url = "https://example.org/"671 auth = SyncOrAsyncAuth()Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 672why, one step back672 app = App()Add support for sync-specific or async-specific auth flows (#1217)Florimond Mancabefore adoption016e4ee
line 673why, one step back673Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 674why, one step back674 async with httpx.AsyncClient(transport=AsyncMockTransport(app)) as client:Add support for sync-specific or async-specific auth flows (#1217)Florimond Mancabefore adoption016e4ee
lines 675–686why, one step back675 response = await client.get(url, auth=auth)676677 assert response.status_code == 200678 assert response.json() == {"auth": "async-auth"}679680681def test_sync_auth() -> None:682 """683 Test that we can use an auth implementation specific to the sync case.684 """685 url = "https://example.org/"686 auth = SyncOrAsyncAuth()Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 687why, one step back687 app = App()Add support for sync-specific or async-specific auth flows (#1217)Florimond Mancabefore adoption016e4ee
line 688why, one step back688Refactor test_auth.py to use `MockTransport` class. (#1288)Tom Christiebefore adoption2d6c30d
line 689why, one step back689 with httpx.Client(transport=MockTransport(app)) as client:Add support for sync-specific or async-specific auth flows (#1217)Florimond Mancabefore adoption016e4ee
lines 690–693why, one step back690 response = client.get(url, auth=auth)691692 assert response.status_code == 200693 assert response.json() == {"auth": "sync-auth"}