Skip to content
Myr

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 file

Take it

Auth (#65)Tom Christiebefore adoption5afa7dd

lines 1–6why, one step back
1import json
23import pytest45from httpcore import (6 URL,

Work on bringing API into parity with `requests`. (#76)Tom Christiebefore adoption9574041

line 7why, one step back
7 CertTypes,

Auth (#65)Tom Christiebefore adoption5afa7dd

lines 8–11why, one step back
8 Client,
9 Dispatcher,10 Request,11 Response,

Work on bringing API into parity with `requests`. (#76)Tom Christiebefore adoption9574041

lines 12–13why, one step back
12 TimeoutTypes,
13 VerifyTypes,

Auth (#65)Tom Christiebefore adoption5afa7dd

lines 14–21why, one step back
14)
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 back
22 verify: VerifyTypes = None,
23 cert: CertTypes = None,24 timeout: TimeoutTypes = None,

Auth (#65)Tom Christiebefore adoption5afa7dd

line 25why, one step back
25 ) -> Response:

Drop unused interface on ConnectionStore (#70)Tom Christiebefore adoption3ed0885

line 26why, one step back
26 body = json.dumps({"auth": request.headers.get("Authorization")}).encode()

Auth (#65)Tom Christiebefore adoption5afa7dd

lines 27–31why, one step back
27 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 back
32 auth = ("tomchristie", "password123")

Auth (#65)Tom Christiebefore adoption5afa7dd

lines 33–37why, one step back
33
34 with Client(dispatch=MockDispatch()) as client:35 response = client.get(url, auth=auth)3637 assert response.status_code == 200

Add response.json()Tom Christiebefore adoption2116d44

line 38why, one step back
38 assert response.json() == {"auth": "Basic dG9tY2hyaXN0aWU6cGFzc3dvcmQxMjM="}

Auth (#65)Tom Christiebefore adoption5afa7dd

lines 39–47why, one step back
39
4041def 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 == 200

Add response.json()Tom Christiebefore adoption2116d44

line 48why, one step back
48 assert response.json() == {"auth": "Basic dG9tY2hyaXN0aWU6cGFzc3dvcmQxMjM="}

Auth (#65)Tom Christiebefore adoption5afa7dd

lines 49–52why, one step back
49
5051def test_basic_auth_on_session():52 url = "https://example.org/"

Drop unused interface on ConnectionStore (#70)Tom Christiebefore adoption3ed0885

line 53why, one step back
53 auth = ("tomchristie", "password123")

Auth (#65)Tom Christiebefore adoption5afa7dd

lines 54–58why, one step back
54
55 with Client(dispatch=MockDispatch(), auth=auth) as client:56 response = client.get(url)5758 assert response.status_code == 200

Add response.json()Tom Christiebefore adoption2116d44

line 59why, one step back
59 assert response.json() == {"auth": "Basic dG9tY2hyaXN0aWU6cGFzc3dvcmQxMjM="}

Auth (#65)Tom Christiebefore adoption5afa7dd

lines 60–65why, one step back
60
6162def 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 back
66 request.headers["Authorization"] = "Token 123"

Auth (#65)Tom Christiebefore adoption5afa7dd

lines 67–72why, one step back
67 return request
6869 with Client(dispatch=MockDispatch()) as client:70 response = client.get(url, auth=auth)7172 assert response.status_code == 200

Add response.json()Tom Christiebefore adoption2116d44

line 73why, one step back
73 assert response.json() == {"auth": "Token 123"}