test_auth.py
This file
Path
tests/client/test_auth.py
77 lines · 1.8 KiB · Python
Last changed by
Drop unused interface on ConnectionStore (#70)Tom Christiebefore adoption3ed0885
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 == 200Drop unused interface on ConnectionStore (#70)Tom Christiebefore adoption3ed0885
lines 36–38why, one step back36 assert json.loads(response.text) == {37 "auth": "Basic dG9tY2hyaXN0aWU6cGFzc3dvcmQxMjM="38 }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 == 200Drop unused interface on ConnectionStore (#70)Tom Christiebefore adoption3ed0885
lines 48–50why, one step back48 assert json.loads(response.text) == {49 "auth": "Basic dG9tY2hyaXN0aWU6cGFzc3dvcmQxMjM="50 }Auth (#65)Tom Christiebefore adoption5afa7dd
lines 51–54why, one step back515253def test_basic_auth_on_session():54 url = "https://example.org/"Drop unused interface on ConnectionStore (#70)Tom Christiebefore adoption3ed0885
line 55why, one step back55 auth = ("tomchristie", "password123")Auth (#65)Tom Christiebefore adoption5afa7dd
lines 56–60why, one step back5657 with Client(dispatch=MockDispatch(), auth=auth) as client:58 response = client.get(url)5960 assert response.status_code == 200Drop unused interface on ConnectionStore (#70)Tom Christiebefore adoption3ed0885
lines 61–63why, one step back61 assert json.loads(response.text) == {62 "auth": "Basic dG9tY2hyaXN0aWU6cGFzc3dvcmQxMjM="63 }Auth (#65)Tom Christiebefore adoption5afa7dd
lines 64–69why, one step back646566def test_custom_auth():67 url = "https://example.org/"6869 def auth(request):Drop unused interface on ConnectionStore (#70)Tom Christiebefore adoption3ed0885
line 70why, one step back70 request.headers["Authorization"] = "Token 123"Auth (#65)Tom Christiebefore adoption5afa7dd
lines 71–76why, one step back71 return request7273 with Client(dispatch=MockDispatch()) as client:74 response = client.get(url, auth=auth)7576 assert response.status_code == 200Drop unused interface on ConnectionStore (#70)Tom Christiebefore adoption3ed0885
line 77why, one step back77 assert json.loads(response.text) == {"auth": "Token 123"}