test_auth.py
This file
Path
tests/client/test_auth.py
73 lines · 1.8 KiB · Python
Last changed by
Work on bringing API into parity with `requests`. (#76)Tom Christiebefore adoption9574041
History of this fileTake it
Auth (#65)Tom Christiebefore adoption5afa7dd
lines 1–6why, one step back1import json23import pytest45from httpcore import (6 URL,Work on bringing API into parity with `requests`. (#76)Tom Christiebefore adoption9574041
line 7why, one step back7 CertTypes,Auth (#65)Tom Christiebefore adoption5afa7dd
lines 8–11why, one step back8 Client,9 Dispatcher,10 Request,11 Response,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–21why, one step back14)151617class MockDispatch(Dispatcher):18 async def send(19 self,20 request: Request,21 stream: bool = False,Work on bringing API into parity with `requests`. (#76)Tom Christiebefore adoption9574041
lines 22–24why, one step back22 verify: VerifyTypes = None,23 cert: CertTypes = None,24 timeout: TimeoutTypes = None,Auth (#65)Tom Christiebefore adoption5afa7dd
line 25why, one step back25 ) -> Response:Drop unused interface on ConnectionStore (#70)Tom Christiebefore adoption3ed0885
line 26why, one step back26 body = json.dumps({"auth": request.headers.get("Authorization")}).encode()Auth (#65)Tom Christiebefore adoption5afa7dd
lines 27–31why, one step back27 return Response(200, content=body, request=request)282930def test_basic_auth():31 url = "https://example.org/"Drop unused interface on ConnectionStore (#70)Tom Christiebefore adoption3ed0885
line 32why, one step back32 auth = ("tomchristie", "password123")Auth (#65)Tom Christiebefore adoption5afa7dd
lines 33–37why, one step back3334 with Client(dispatch=MockDispatch()) as client:35 response = client.get(url, auth=auth)3637 assert response.status_code == 200Add response.json()Tom Christiebefore adoption2116d44
line 38why, one step back38 assert response.json() == {"auth": "Basic dG9tY2hyaXN0aWU6cGFzc3dvcmQxMjM="}Auth (#65)Tom Christiebefore adoption5afa7dd
lines 39–47why, one step back394041def test_basic_auth_in_url():42 url = "https://tomchristie:password123@example.org/"4344 with Client(dispatch=MockDispatch()) as client:45 response = client.get(url)4647 assert response.status_code == 200Add response.json()Tom Christiebefore adoption2116d44
line 48why, one step back48 assert response.json() == {"auth": "Basic dG9tY2hyaXN0aWU6cGFzc3dvcmQxMjM="}Auth (#65)Tom Christiebefore adoption5afa7dd
lines 49–52why, one step back495051def test_basic_auth_on_session():52 url = "https://example.org/"Drop unused interface on ConnectionStore (#70)Tom Christiebefore adoption3ed0885
line 53why, one step back53 auth = ("tomchristie", "password123")Auth (#65)Tom Christiebefore adoption5afa7dd
lines 54–58why, one step back5455 with Client(dispatch=MockDispatch(), auth=auth) as client:56 response = client.get(url)5758 assert response.status_code == 200Add response.json()Tom Christiebefore adoption2116d44
line 59why, one step back59 assert response.json() == {"auth": "Basic dG9tY2hyaXN0aWU6cGFzc3dvcmQxMjM="}Auth (#65)Tom Christiebefore adoption5afa7dd
lines 60–65why, one step back606162def test_custom_auth():63 url = "https://example.org/"6465 def auth(request):Drop unused interface on ConnectionStore (#70)Tom Christiebefore adoption3ed0885
line 66why, one step back66 request.headers["Authorization"] = "Token 123"Auth (#65)Tom Christiebefore adoption5afa7dd
lines 67–72why, one step back67 return request6869 with Client(dispatch=MockDispatch()) as client:70 response = client.get(url, auth=auth)7172 assert response.status_code == 200Add response.json()Tom Christiebefore adoption2116d44
line 73why, one step back73 assert response.json() == {"auth": "Token 123"}