test_auth.py
This file
Path
tests/client/test_auth.py
71 lines · 1.8 KiB · Python
Last changed by
Add response.json()Tom Christiebefore adoption2116d44
History of this fileTake it
Auth (#65)Tom Christiebefore adoption5afa7dd
lines 1–23why, one step back1import json23import pytest45from httpcore import (6 URL,7 Client,8 Dispatcher,9 Request,10 Response,11 SSLConfig,12 TimeoutConfig,13)141516class MockDispatch(Dispatcher):17 async def send(18 self,19 request: Request,20 stream: bool = False,21 ssl: SSLConfig = None,22 timeout: TimeoutConfig = None,23 ) -> Response:Drop unused interface on ConnectionStore (#70)Tom Christiebefore adoption3ed0885
line 24why, one step back24 body = json.dumps({"auth": request.headers.get("Authorization")}).encode()Auth (#65)Tom Christiebefore adoption5afa7dd
lines 25–29why, one step back25 return Response(200, content=body, request=request)262728def test_basic_auth():29 url = "https://example.org/"Drop unused interface on ConnectionStore (#70)Tom Christiebefore adoption3ed0885
line 30why, one step back30 auth = ("tomchristie", "password123")Auth (#65)Tom Christiebefore adoption5afa7dd
lines 31–35why, one step back3132 with Client(dispatch=MockDispatch()) as client:33 response = client.get(url, auth=auth)3435 assert response.status_code == 200Add response.json()Tom Christiebefore adoption2116d44
line 36why, one step back36 assert response.json() == {"auth": "Basic dG9tY2hyaXN0aWU6cGFzc3dvcmQxMjM="}Auth (#65)Tom Christiebefore adoption5afa7dd
lines 37–45why, one step back373839def 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 == 200Add response.json()Tom Christiebefore adoption2116d44
line 46why, one step back46 assert response.json() == {"auth": "Basic dG9tY2hyaXN0aWU6cGFzc3dvcmQxMjM="}Auth (#65)Tom Christiebefore adoption5afa7dd
lines 47–50why, one step back474849def test_basic_auth_on_session():50 url = "https://example.org/"Drop unused interface on ConnectionStore (#70)Tom Christiebefore adoption3ed0885
line 51why, one step back51 auth = ("tomchristie", "password123")Auth (#65)Tom Christiebefore adoption5afa7dd
lines 52–56why, one step back5253 with Client(dispatch=MockDispatch(), auth=auth) as client:54 response = client.get(url)5556 assert response.status_code == 200Add response.json()Tom Christiebefore adoption2116d44
line 57why, one step back57 assert response.json() == {"auth": "Basic dG9tY2hyaXN0aWU6cGFzc3dvcmQxMjM="}Auth (#65)Tom Christiebefore adoption5afa7dd
lines 58–63why, one step back585960def test_custom_auth():61 url = "https://example.org/"6263 def auth(request):Drop unused interface on ConnectionStore (#70)Tom Christiebefore adoption3ed0885
line 64why, one step back64 request.headers["Authorization"] = "Token 123"Auth (#65)Tom Christiebefore adoption5afa7dd
lines 65–70why, one step back65 return request6667 with Client(dispatch=MockDispatch()) as client:68 response = client.get(url, auth=auth)6970 assert response.status_code == 200Add response.json()Tom Christiebefore adoption2116d44
line 71why, one step back71 assert response.json() == {"auth": "Token 123"}