test_auth.py
This file
Path
tests/client/test_auth.py
72 lines · 1.8 KiB · Python
Last changed by
http3 (#86)Tom Christiebefore adoptionc9747aa
History of this fileTake it
Auth (#65)Tom Christiebefore adoption5afa7dd
lines 1–4why, one step back1import json23import pytest4http3 (#86)Tom Christiebefore adoptionc9747aa
line 5why, one step back5from http3 import (Auth (#65)Tom Christiebefore adoption5afa7dd
line 6why, one step back6 URL,Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7
lines 7–9why, one step back7 AsyncDispatcher,8 AsyncRequest,9 AsyncResponse,Work on bringing API into parity with `requests`. (#76)Tom Christiebefore adoption9574041
line 10why, one step back10 CertTypes,Auth (#65)Tom Christiebefore adoption5afa7dd
line 11why, one step back11 Client,Work on bringing API into parity with `requests`. (#76)Tom Christiebefore adoption9574041
lines 12–13why, one step back12 TimeoutTypes,13 VerifyTypes,Auth (#65)Tom Christiebefore adoption5afa7dd
lines 14–16why, one step back14)1516Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7
line 17why, one step back17class MockDispatch(AsyncDispatcher):Auth (#65)Tom Christiebefore adoption5afa7dd
lines 18–19why, one step back18 async def send(19 self,Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7
line 20why, one step back20 request: AsyncRequest,Work on bringing API into parity with `requests`. (#76)Tom Christiebefore adoption9574041
lines 21–23why, one step back21 verify: VerifyTypes = None,22 cert: CertTypes = None,23 timeout: TimeoutTypes = None,Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7
line 24why, one step back24 ) -> AsyncResponse:Drop unused interface on ConnectionStore (#70)Tom Christiebefore adoption3ed0885
line 25why, one step back25 body = json.dumps({"auth": request.headers.get("Authorization")}).encode()Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7
line 26why, one step back26 return AsyncResponse(200, content=body, request=request)Auth (#65)Tom Christiebefore adoption5afa7dd
lines 27–30why, one step back272829def test_basic_auth():30 url = "https://example.org/"Drop unused interface on ConnectionStore (#70)Tom Christiebefore adoption3ed0885
line 31why, one step back31 auth = ("tomchristie", "password123")Auth (#65)Tom Christiebefore adoption5afa7dd
lines 32–36why, one step back3233 with Client(dispatch=MockDispatch()) as client:34 response = client.get(url, auth=auth)3536 assert response.status_code == 200Add response.json()Tom Christiebefore adoption2116d44
line 37why, one step back37 assert response.json() == {"auth": "Basic dG9tY2hyaXN0aWU6cGFzc3dvcmQxMjM="}Auth (#65)Tom Christiebefore adoption5afa7dd
lines 38–46why, one step back383940def test_basic_auth_in_url():41 url = "https://tomchristie:password123@example.org/"4243 with Client(dispatch=MockDispatch()) as client:44 response = client.get(url)4546 assert response.status_code == 200Add response.json()Tom Christiebefore adoption2116d44
line 47why, one step back47 assert response.json() == {"auth": "Basic dG9tY2hyaXN0aWU6cGFzc3dvcmQxMjM="}Auth (#65)Tom Christiebefore adoption5afa7dd
lines 48–51why, one step back484950def test_basic_auth_on_session():51 url = "https://example.org/"Drop unused interface on ConnectionStore (#70)Tom Christiebefore adoption3ed0885
line 52why, one step back52 auth = ("tomchristie", "password123")Auth (#65)Tom Christiebefore adoption5afa7dd
lines 53–57why, one step back5354 with Client(dispatch=MockDispatch(), auth=auth) as client:55 response = client.get(url)5657 assert response.status_code == 200Add response.json()Tom Christiebefore adoption2116d44
line 58why, one step back58 assert response.json() == {"auth": "Basic dG9tY2hyaXN0aWU6cGFzc3dvcmQxMjM="}Auth (#65)Tom Christiebefore adoption5afa7dd
lines 59–64why, one step back596061def test_custom_auth():62 url = "https://example.org/"6364 def auth(request):Drop unused interface on ConnectionStore (#70)Tom Christiebefore adoption3ed0885
line 65why, one step back65 request.headers["Authorization"] = "Token 123"Auth (#65)Tom Christiebefore adoption5afa7dd
lines 66–71why, one step back66 return request6768 with Client(dispatch=MockDispatch()) as client:69 response = client.get(url, auth=auth)7071 assert response.status_code == 200Add response.json()Tom Christiebefore adoption2116d44
line 72why, one step back72 assert response.json() == {"auth": "Token 123"}