test_auth.py
This file
Path
tests/client/test_auth.py
94 lines · 2.4 KiB · Python
Last changed by
added netrc support (#177)Can Sarıgölbefore adoption919e8d3
History of this fileTake it
Auth (#65)Tom Christiebefore adoption5afa7dd
line 1why, one step back1import jsonadded netrc support (#177)Can Sarıgölbefore adoption919e8d3
line 2why, one step back2import osAuth (#65)Tom Christiebefore adoption5afa7dd
line 3why, one step back3Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 4why, one step back4from httpx import (Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7
lines 5–7why, one step back5 AsyncDispatcher,6 AsyncRequest,7 AsyncResponse,Work on bringing API into parity with `requests`. (#76)Tom Christiebefore adoption9574041
line 8why, one step back8 CertTypes,Auth (#65)Tom Christiebefore adoption5afa7dd
line 9why, one step back9 Client,Work on bringing API into parity with `requests`. (#76)Tom Christiebefore adoption9574041
lines 10–11why, one step back10 TimeoutTypes,11 VerifyTypes,Auth (#65)Tom Christiebefore adoption5afa7dd
lines 12–14why, one step back12)1314Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7
line 15why, one step back15class MockDispatch(AsyncDispatcher):Auth (#65)Tom Christiebefore adoption5afa7dd
lines 16–17why, one step back16 async def send(17 self,Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7
line 18why, one step back18 request: AsyncRequest,Work on bringing API into parity with `requests`. (#76)Tom Christiebefore adoption9574041
lines 19–21why, one step back19 verify: VerifyTypes = None,20 cert: CertTypes = None,21 timeout: TimeoutTypes = None,Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7
line 22why, one step back22 ) -> AsyncResponse:Drop unused interface on ConnectionStore (#70)Tom Christiebefore adoption3ed0885
line 23why, one step back23 body = json.dumps({"auth": request.headers.get("Authorization")}).encode()Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7
line 24why, one step back24 return AsyncResponse(200, content=body, request=request)Auth (#65)Tom Christiebefore adoption5afa7dd
lines 25–28why, one step back252627def test_basic_auth():28 url = "https://example.org/"Drop unused interface on ConnectionStore (#70)Tom Christiebefore adoption3ed0885
line 29why, one step back29 auth = ("tomchristie", "password123")Auth (#65)Tom Christiebefore adoption5afa7dd
lines 30–34why, one step back3031 with Client(dispatch=MockDispatch()) as client:32 response = client.get(url, auth=auth)3334 assert response.status_code == 200Add response.json()Tom Christiebefore adoption2116d44
line 35why, one step back35 assert response.json() == {"auth": "Basic dG9tY2hyaXN0aWU6cGFzc3dvcmQxMjM="}Auth (#65)Tom Christiebefore adoption5afa7dd
lines 36–44why, one step back363738def test_basic_auth_in_url():39 url = "https://tomchristie:password123@example.org/"4041 with Client(dispatch=MockDispatch()) as client:42 response = client.get(url)4344 assert response.status_code == 200Add response.json()Tom Christiebefore adoption2116d44
line 45why, one step back45 assert response.json() == {"auth": "Basic dG9tY2hyaXN0aWU6cGFzc3dvcmQxMjM="}Auth (#65)Tom Christiebefore adoption5afa7dd
lines 46–49why, one step back464748def test_basic_auth_on_session():49 url = "https://example.org/"Drop unused interface on ConnectionStore (#70)Tom Christiebefore adoption3ed0885
line 50why, one step back50 auth = ("tomchristie", "password123")Auth (#65)Tom Christiebefore adoption5afa7dd
lines 51–55why, one step back5152 with Client(dispatch=MockDispatch(), auth=auth) as client:53 response = client.get(url)5455 assert response.status_code == 200Add response.json()Tom Christiebefore adoption2116d44
line 56why, one step back56 assert response.json() == {"auth": "Basic dG9tY2hyaXN0aWU6cGFzc3dvcmQxMjM="}Auth (#65)Tom Christiebefore adoption5afa7dd
lines 57–62why, one step back575859def test_custom_auth():60 url = "https://example.org/"6162 def auth(request):Drop unused interface on ConnectionStore (#70)Tom Christiebefore adoption3ed0885
line 63why, one step back63 request.headers["Authorization"] = "Token 123"Auth (#65)Tom Christiebefore adoption5afa7dd
lines 64–69why, one step back64 return request6566 with Client(dispatch=MockDispatch()) as client:67 response = client.get(url, auth=auth)6869 assert response.status_code == 200Add response.json()Tom Christiebefore adoption2116d44
line 70why, one step back70 assert response.json() == {"auth": "Token 123"}added netrc support (#177)Can Sarıgölbefore adoption919e8d3
lines 71–94why, one step back717273def test_netrc_auth():74 os.environ["NETRC"] = "tests/.netrc"75 url = "http://netrcexample.org"7677 with Client(dispatch=MockDispatch()) as client:78 response = client.get(url)7980 assert response.status_code == 20081 assert response.json() == {82 "auth": "Basic ZXhhbXBsZS11c2VybmFtZTpleGFtcGxlLXBhc3N3b3Jk"83 }848586def test_trust_env_auth():87 os.environ["NETRC"] = "tests/.netrc"88 url = "http://netrcexample.org"8990 with Client(dispatch=MockDispatch()) as client:91 response = client.get(url, trust_env=False)9293 assert response.status_code == 20094 assert response.json() == {"auth": None}