Use consistent import style (#2493)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Tom Christie · committed by GitHub · parent 40a0da0 · Browse files · 1 file·+37 added−34 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+37 added−34 removed
- tests:+37 added−34 removed
Provenance
- Commit
7985f68- Tree
b34eeb4- Author
Tom Christie <tom@tomchristie.com>- Committer
GitHub <noreply@github.com>- Parent
40a0da0
1 file changed+37 added−34 removed
modified:tests/client/test_auth.py+37 added-34 removed
11 unmodified lines12 13 import pytest14 15 import httpx16-from httpx import URL, Auth, BasicAuth, DigestAuth, ProtocolError, Request, Response16 17 from ..common import FIXTURES_DIR18 19 53 unmodified lines73 74 headers = {75 "www-authenticate": f'Digest realm="httpx@example.org", {challenge_str}',76 }78- return Response(401, headers=headers)77+ return httpx.Response(401, headers=headers)78 79 81-class RepeatAuth(Auth):80+class RepeatAuth(httpx.Auth):81 """82 A mock authentication scheme that requires clients to send83 the request a fixed number of times, and then send a last request containing84 an aggregation of nonces that the server sent in 'WWW-Authenticate' headers4 unmodified lines89 90 def __init__(self, repeat: int):91 self.repeat = repeat92 94- def auth_flow(self, request: Request) -> typing.Generator[Request, Response, None]:93+ def auth_flow(94+ self, request: httpx.Request95+ ) -> typing.Generator[httpx.Request, httpx.Response, None]:96 nonces = []97 98 for index in range(self.repeat):99 request.headers["Authorization"] = f"Repeat {index}"4 unmodified lines104 request.headers["Authorization"] = f"Repeat {key}"105 yield request106 107 107-class ResponseBodyAuth(Auth):108+class ResponseBodyAuth(httpx.Auth):109 """110 A mock authentication scheme that requires clients to send an 'Authorization'111 header, then send back the contents of the response in the 'Authorization'112 header.3 unmodified lines116 117 def __init__(self, token: str) -> None:118 self.token = token119 119- def auth_flow(self, request: Request) -> typing.Generator[Request, Response, None]:120+ def auth_flow(121+ self, request: httpx.Request122+ ) -> typing.Generator[httpx.Request, httpx.Response, None]:123 request.headers["Authorization"] = self.token124 response = yield request125 data = response.text126 request.headers["Authorization"] = data127 yield request128 129 127-class SyncOrAsyncAuth(Auth):130+class SyncOrAsyncAuth(httpx.Auth):131 """132 A mock authentication scheme that uses a different implementation for the133 sync and async cases.134 """2 unmodified lines137 self._lock = threading.Lock()138 self._async_lock = asyncio.Lock()139 140 def sync_auth_flow(138- self, request: Request139- ) -> typing.Generator[Request, Response, None]:141+ self, request: httpx.Request142+ ) -> typing.Generator[httpx.Request, httpx.Response, None]:143 with self._lock:144 request.headers["Authorization"] = "sync-auth"145 yield request146 147 async def async_auth_flow(145- self, request: Request146- ) -> typing.AsyncGenerator[Request, Response]:148+ self, request: httpx.Request149+ ) -> typing.AsyncGenerator[httpx.Request, httpx.Response]:150 async with self._async_lock:151 request.headers["Authorization"] = "async-auth"152 yield request153 61 unmodified lines215 async def test_custom_auth() -> None:216 url = "https://example.org/"217 app = App()218 216- def auth(request: Request) -> Request:219+ def auth(request: httpx.Request) -> httpx.Request:220 request.headers["Authorization"] = "Token 123"221 return request222 223 async with httpx.AsyncClient(transport=httpx.MockTransport(app)) as client:73 unmodified lines297 298 def test_auth_hidden_url() -> None:299 url = "http://example-username:example-password@example.org/"300 expected = "URL('http://example-username:[secure]@example.org/')"298- assert url == URL(url)299- assert expected == repr(URL(url))301+ assert url == httpx.URL(url)302+ assert expected == repr(httpx.URL(url))303 304 305 @pytest.mark.asyncio306 async def test_auth_hidden_header() -> None:14 unmodified lines321 async with httpx.AsyncClient(transport=httpx.MockTransport(app)) as client:322 assert client.auth is None323 324 client.auth = ("user", "password123") # type: ignore322- assert isinstance(client.auth, BasicAuth)325+ assert isinstance(client.auth, httpx.BasicAuth)326 327 url = "https://example.org/"328 response = await client.get(url)329 assert response.status_code == 20020 unmodified lines350 351 @pytest.mark.asyncio352 async def test_digest_auth_returns_no_auth_if_no_digest_header_in_response() -> None:353 url = "https://example.org/"351- auth = DigestAuth(username="user", password="password123")354+ auth = httpx.DigestAuth(username="user", password="password123")355 app = App()356 357 async with httpx.AsyncClient(transport=httpx.MockTransport(app)) as client:358 response = await client.get(url, auth=auth)4 unmodified lines363 364 365 def test_digest_auth_returns_no_auth_if_alternate_auth_scheme() -> None:366 url = "https://example.org/"364- auth = DigestAuth(username="user", password="password123")367+ auth = httpx.DigestAuth(username="user", password="password123")368 auth_header = "Token ..."369 app = App(auth_header=auth_header, status_code=401)370 371 client = httpx.Client(transport=httpx.MockTransport(app))6 unmodified lines378 379 @pytest.mark.asyncio380 async def test_digest_auth_200_response_including_digest_auth_header() -> None:381 url = "https://example.org/"379- auth = DigestAuth(username="user", password="password123")382+ auth = httpx.DigestAuth(username="user", password="password123")383 auth_header = 'Digest realm="realm@host.com",qop="auth",nonce="abc",opaque="xyz"'384 app = App(auth_header=auth_header, status_code=200)385 386 async with httpx.AsyncClient(transport=httpx.MockTransport(app)) as client:6 unmodified lines393 394 @pytest.mark.asyncio395 async def test_digest_auth_401_response_without_digest_auth_header() -> None:396 url = "https://example.org/"394- auth = DigestAuth(username="user", password="password123")397+ auth = httpx.DigestAuth(username="user", password="password123")398 app = App(auth_header="", status_code=401)399 400 async with httpx.AsyncClient(transport=httpx.MockTransport(app)) as client:401 response = await client.get(url, auth=auth)20 unmodified lines422 async def test_digest_auth(423 algorithm: str, expected_hash_length: int, expected_response_length: int424 ) -> None:425 url = "https://example.org/"423- auth = DigestAuth(username="user", password="password123")426+ auth = httpx.DigestAuth(username="user", password="password123")427 app = DigestApp(algorithm=algorithm)428 429 async with httpx.AsyncClient(transport=httpx.MockTransport(app)) as client:430 response = await client.get(url, auth=auth)22 unmodified lines453 454 @pytest.mark.asyncio455 async def test_digest_auth_no_specified_qop() -> None:456 url = "https://example.org/"454- auth = DigestAuth(username="user", password="password123")457+ auth = httpx.DigestAuth(username="user", password="password123")458 app = DigestApp(qop="")459 460 async with httpx.AsyncClient(transport=httpx.MockTransport(app)) as client:461 response = await client.get(url, auth=auth)23 unmodified lines485 @pytest.mark.parametrize("qop", ("auth, auth-int", "auth,auth-int", "unknown,auth"))486 @pytest.mark.asyncio487 async def test_digest_auth_qop_including_spaces_and_auth_returns_auth(qop: str) -> None:488 url = "https://example.org/"486- auth = DigestAuth(username="user", password="password123")489+ auth = httpx.DigestAuth(username="user", password="password123")490 app = DigestApp(qop=qop)491 492 async with httpx.AsyncClient(transport=httpx.MockTransport(app)) as client:493 response = await client.get(url, auth=auth)4 unmodified lines498 499 @pytest.mark.asyncio500 async def test_digest_auth_qop_auth_int_not_implemented() -> None:501 url = "https://example.org/"499- auth = DigestAuth(username="user", password="password123")502+ auth = httpx.DigestAuth(username="user", password="password123")503 app = DigestApp(qop="auth-int")504 505 async with httpx.AsyncClient(transport=httpx.MockTransport(app)) as client:506 with pytest.raises(NotImplementedError):2 unmodified lines509 510 @pytest.mark.asyncio511 async def test_digest_auth_qop_must_be_auth_or_auth_int() -> None:512 url = "https://example.org/"510- auth = DigestAuth(username="user", password="password123")513+ auth = httpx.DigestAuth(username="user", password="password123")514 app = DigestApp(qop="not-auth")515 516 async with httpx.AsyncClient(transport=httpx.MockTransport(app)) as client:514- with pytest.raises(ProtocolError):517+ with pytest.raises(httpx.ProtocolError):518 await client.get(url, auth=auth)519 520 521 @pytest.mark.asyncio522 async def test_digest_auth_incorrect_credentials() -> None:523 url = "https://example.org/"521- auth = DigestAuth(username="user", password="password123")524+ auth = httpx.DigestAuth(username="user", password="password123")525 app = DigestApp(send_response_after_attempt=2)526 527 async with httpx.AsyncClient(transport=httpx.MockTransport(app)) as client:528 response = await client.get(url, auth=auth)4 unmodified lines533 534 @pytest.mark.asyncio535 async def test_digest_auth_reuses_challenge() -> None:536 url = "https://example.org/"534- auth = DigestAuth(username="user", password="password123")537+ auth = httpx.DigestAuth(username="user", password="password123")538 app = DigestApp()539 540 async with httpx.AsyncClient(transport=httpx.MockTransport(app)) as client:541 response_1 = await client.get(url, auth=auth)8 unmodified lines550 551 @pytest.mark.asyncio552 async def test_digest_auth_resets_nonce_count_after_401() -> None:553 url = "https://example.org/"551- auth = DigestAuth(username="user", password="password123")554+ auth = httpx.DigestAuth(username="user", password="password123")555 app = DigestApp()556 557 async with httpx.AsyncClient(transport=httpx.MockTransport(app)) as client:558 response_1 = await client.get(url, auth=auth)39 unmodified lines598 async def test_async_digest_auth_raises_protocol_error_on_malformed_header(599 auth_header: str,600 ) -> None:601 url = "https://example.org/"599- auth = DigestAuth(username="user", password="password123")602+ auth = httpx.DigestAuth(username="user", password="password123")603 app = App(auth_header=auth_header, status_code=401)604 605 async with httpx.AsyncClient(transport=httpx.MockTransport(app)) as client:603- with pytest.raises(ProtocolError):606+ with pytest.raises(httpx.ProtocolError):607 await client.get(url, auth=auth)608 609 610 @pytest.mark.parametrize(6 unmodified lines617 def test_sync_digest_auth_raises_protocol_error_on_malformed_header(618 auth_header: str,619 ) -> None:620 url = "https://example.org/"618- auth = DigestAuth(username="user", password="password123")621+ auth = httpx.DigestAuth(username="user", password="password123")622 app = App(auth_header=auth_header, status_code=401)623 624 with httpx.Client(transport=httpx.MockTransport(app)) as client:622- with pytest.raises(ProtocolError):625+ with pytest.raises(httpx.ProtocolError):626 client.get(url, auth=auth)627 628 629 @pytest.mark.asyncio49 unmodified lines679 assert len(resp1.history) == 0680 681 682 class ConsumeBodyTransport(httpx.MockTransport):680- async def handle_async_request(self, request: Request) -> Response:683+ async def handle_async_request(self, request: httpx.Request) -> httpx.Response:684 assert isinstance(request.stream, httpx.AsyncByteStream)685 [_ async for _ in request.stream]686 return self.handler(request)687 688 689 @pytest.mark.asyncio690 async def test_digest_auth_unavailable_streaming_body():691 url = "https://example.org/"689- auth = DigestAuth(username="user", password="password123")692+ auth = httpx.DigestAuth(username="user", password="password123")693 app = DigestApp()694 695 async def streaming_body() -> typing.AsyncIterator[bytes]:696 yield b"Example request body" # pragma: no cover70 unmodified lines