test_auth.py
This file
Path
tests/client/test_auth.py
69 lines · 1.8 KiB · Python
Last changed by
Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
History of this fileTake it
1import json23from httpx import (4 AsyncDispatcher,5 AsyncRequest,6 AsyncResponse,7 CertTypes,8 Client,9 TimeoutTypes,10 VerifyTypes,11)121314class MockDispatch(AsyncDispatcher):15 async def send(16 self,17 request: AsyncRequest,18 verify: VerifyTypes = None,19 cert: CertTypes = None,20 timeout: TimeoutTypes = None,21 ) -> AsyncResponse:22 body = json.dumps({"auth": request.headers.get("Authorization")}).encode()23 return AsyncResponse(200, content=body, request=request)242526def test_basic_auth():27 url = "https://example.org/"28 auth = ("tomchristie", "password123")2930 with Client(dispatch=MockDispatch()) as client:31 response = client.get(url, auth=auth)3233 assert response.status_code == 20034 assert response.json() == {"auth": "Basic dG9tY2hyaXN0aWU6cGFzc3dvcmQxMjM="}353637def test_basic_auth_in_url():38 url = "https://tomchristie:password123@example.org/"3940 with Client(dispatch=MockDispatch()) as client:41 response = client.get(url)4243 assert response.status_code == 20044 assert response.json() == {"auth": "Basic dG9tY2hyaXN0aWU6cGFzc3dvcmQxMjM="}454647def test_basic_auth_on_session():48 url = "https://example.org/"49 auth = ("tomchristie", "password123")5051 with Client(dispatch=MockDispatch(), auth=auth) as client:52 response = client.get(url)5354 assert response.status_code == 20055 assert response.json() == {"auth": "Basic dG9tY2hyaXN0aWU6cGFzc3dvcmQxMjM="}565758def test_custom_auth():59 url = "https://example.org/"6061 def auth(request):62 request.headers["Authorization"] = "Token 123"63 return request6465 with Client(dispatch=MockDispatch()) as client:66 response = client.get(url, auth=auth)6768 assert response.status_code == 20069 assert response.json() == {"auth": "Token 123"}