test_auth.py
This file
Path
tests/client/test_auth.py
120 lines · 3.2 KiB · Python
Last changed by
fixed __repr__ function of Headers and Url classes (#222)Can Sarıgölbefore adoptiona0aff8d
History of this fileTake it
1import json2import os34from httpx import (5 URL,6 AsyncDispatcher,7 AsyncRequest,8 AsyncResponse,9 CertTypes,10 Client,11 TimeoutTypes,12 VerifyTypes,13)141516class MockDispatch(AsyncDispatcher):17 async def send(18 self,19 request: AsyncRequest,20 verify: VerifyTypes = None,21 cert: CertTypes = None,22 timeout: TimeoutTypes = None,23 ) -> AsyncResponse:24 body = json.dumps({"auth": request.headers.get("Authorization")}).encode()25 return AsyncResponse(200, content=body, request=request)262728def test_basic_auth():29 url = "https://example.org/"30 auth = ("tomchristie", "password123")3132 with Client(dispatch=MockDispatch()) as client:33 response = client.get(url, auth=auth)3435 assert response.status_code == 20036 assert response.json() == {"auth": "Basic dG9tY2hyaXN0aWU6cGFzc3dvcmQxMjM="}373839def test_basic_auth_in_url():40 url = "https://tomchristie:password123@example.org/"4142 with Client(dispatch=MockDispatch()) as client:43 response = client.get(url)4445 assert response.status_code == 20046 assert response.json() == {"auth": "Basic dG9tY2hyaXN0aWU6cGFzc3dvcmQxMjM="}474849def test_basic_auth_on_session():50 url = "https://example.org/"51 auth = ("tomchristie", "password123")5253 with Client(dispatch=MockDispatch(), auth=auth) as client:54 response = client.get(url)5556 assert response.status_code == 20057 assert response.json() == {"auth": "Basic dG9tY2hyaXN0aWU6cGFzc3dvcmQxMjM="}585960def test_custom_auth():61 url = "https://example.org/"6263 def auth(request):64 request.headers["Authorization"] = "Token 123"65 return request6667 with Client(dispatch=MockDispatch()) as client:68 response = client.get(url, auth=auth)6970 assert response.status_code == 20071 assert response.json() == {"auth": "Token 123"}727374def test_netrc_auth():75 os.environ["NETRC"] = "tests/.netrc"76 url = "http://netrcexample.org"7778 with Client(dispatch=MockDispatch()) as client:79 response = client.get(url)8081 assert response.status_code == 20082 assert response.json() == {83 "auth": "Basic ZXhhbXBsZS11c2VybmFtZTpleGFtcGxlLXBhc3N3b3Jk"84 }858687def test_trust_env_auth():88 os.environ["NETRC"] = "tests/.netrc"89 url = "http://netrcexample.org"9091 with Client(dispatch=MockDispatch()) as client:92 response = client.get(url, trust_env=False)9394 assert response.status_code == 20095 assert response.json() == {"auth": None}9697 with Client(dispatch=MockDispatch(), trust_env=False) as client:98 response = client.get(url, trust_env=True)99100 assert response.status_code == 200101 assert response.json() == {102 "auth": "Basic ZXhhbXBsZS11c2VybmFtZTpleGFtcGxlLXBhc3N3b3Jk"103 }104105106def test_auth_hidden_url():107 url = "http://example-username:example-password@example.org/"108 expected = "URL('http://example-username:[secure]@example.org/')"109 assert url == URL(url)110 assert expected == repr(URL(url))111112113def test_auth_hidden_header():114 url = "https://example.org/"115 auth = ("example-username", "example-password")116117 with Client(dispatch=MockDispatch()) as client:118 response = client.get(url, auth=auth)119120 assert "'authorization': '[secure]'" in str(response.request.headers)