Skip to content
Myr

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

1 file+37 added−34 removed

modified:tests/client/test_auth.py+37 added-34 removed
11 unmodified lines
1213import pytest1415import httpx16from httpx import URL, Auth, BasicAuth, DigestAuth, ProtocolError, Request, Response1617from ..common import FIXTURES_DIR1819
53 unmodified lines
7374 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)787981class RepeatAuth(Auth):80class 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' headers
4 unmodified lines
8990 def __init__(self, repeat: int):91 self.repeat = repeat9294 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 = []9798 for index in range(self.repeat):99 request.headers["Authorization"] = f"Repeat {index}"
4 unmodified lines
104 request.headers["Authorization"] = f"Repeat {key}"105 yield request106107107class ResponseBodyAuth(Auth):108class 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 lines
116117 def __init__(self, token: str) -> None:118 self.token = token119119 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 request128129127class SyncOrAsyncAuth(Auth):130class SyncOrAsyncAuth(httpx.Auth):131 """132 A mock authentication scheme that uses a different implementation for the133 sync and async cases.134 """
2 unmodified lines
137 self._lock = threading.Lock()138 self._async_lock = asyncio.Lock()139140 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 request146147 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 lines
215async def test_custom_auth() -> None:216 url = "https://example.org/"217 app = App()218216 def auth(request: Request) -> Request:219 def auth(request: httpx.Request) -> httpx.Request:220 request.headers["Authorization"] = "Token 123"221 return request222223 async with httpx.AsyncClient(transport=httpx.MockTransport(app)) as client:
73 unmodified lines
297298def 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))303304305@pytest.mark.asyncio306async def test_auth_hidden_header() -> None:
14 unmodified lines
321 async with httpx.AsyncClient(transport=httpx.MockTransport(app)) as client:322 assert client.auth is None323324 client.auth = ("user", "password123") # type: ignore322 assert isinstance(client.auth, BasicAuth)325 assert isinstance(client.auth, httpx.BasicAuth)326327 url = "https://example.org/"328 response = await client.get(url)329 assert response.status_code == 200
20 unmodified lines
350351@pytest.mark.asyncio352async 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()356357 async with httpx.AsyncClient(transport=httpx.MockTransport(app)) as client:358 response = await client.get(url, auth=auth)
4 unmodified lines
363364365def 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)370371 client = httpx.Client(transport=httpx.MockTransport(app))
6 unmodified lines
378379@pytest.mark.asyncio380async 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)385386 async with httpx.AsyncClient(transport=httpx.MockTransport(app)) as client:
6 unmodified lines
393394@pytest.mark.asyncio395async 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)399400 async with httpx.AsyncClient(transport=httpx.MockTransport(app)) as client:401 response = await client.get(url, auth=auth)
20 unmodified lines
422async 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)428429 async with httpx.AsyncClient(transport=httpx.MockTransport(app)) as client:430 response = await client.get(url, auth=auth)
22 unmodified lines
453454@pytest.mark.asyncio455async 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="")459460 async with httpx.AsyncClient(transport=httpx.MockTransport(app)) as client:461 response = await client.get(url, auth=auth)
23 unmodified lines
485@pytest.mark.parametrize("qop", ("auth, auth-int", "auth,auth-int", "unknown,auth"))486@pytest.mark.asyncio487async 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)491492 async with httpx.AsyncClient(transport=httpx.MockTransport(app)) as client:493 response = await client.get(url, auth=auth)
4 unmodified lines
498499@pytest.mark.asyncio500async 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")504505 async with httpx.AsyncClient(transport=httpx.MockTransport(app)) as client:506 with pytest.raises(NotImplementedError):
2 unmodified lines
509510@pytest.mark.asyncio511async 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")515516 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)519520521@pytest.mark.asyncio522async 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)526527 async with httpx.AsyncClient(transport=httpx.MockTransport(app)) as client:528 response = await client.get(url, auth=auth)
4 unmodified lines
533534@pytest.mark.asyncio535async 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()539540 async with httpx.AsyncClient(transport=httpx.MockTransport(app)) as client:541 response_1 = await client.get(url, auth=auth)
8 unmodified lines
550551@pytest.mark.asyncio552async 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()556557 async with httpx.AsyncClient(transport=httpx.MockTransport(app)) as client:558 response_1 = await client.get(url, auth=auth)
39 unmodified lines
598async 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)604605 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)608609610@pytest.mark.parametrize(
6 unmodified lines
617def 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)623624 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)627628629@pytest.mark.asyncio
49 unmodified lines
679 assert len(resp1.history) == 0680681682class 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)687688689@pytest.mark.asyncio690async 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()694695 async def streaming_body() -> typing.AsyncIterator[bytes]:696 yield b"Example request body" # pragma: no cover
70 unmodified lines