Skip to content
Myr

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 file

Take it

Auth (#65)Tom Christiebefore adoption5afa7dd

lines 1–23why, one step back
1import json
23import 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 back
24 body = json.dumps({"auth": request.headers.get("Authorization")}).encode()

Auth (#65)Tom Christiebefore adoption5afa7dd

lines 25–29why, one step back
25 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 back
30 auth = ("tomchristie", "password123")

Auth (#65)Tom Christiebefore adoption5afa7dd

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

Add response.json()Tom Christiebefore adoption2116d44

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

Auth (#65)Tom Christiebefore adoption5afa7dd

lines 37–45why, one step back
37
3839def 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 == 200

Add response.json()Tom Christiebefore adoption2116d44

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

Auth (#65)Tom Christiebefore adoption5afa7dd

lines 47–50why, one step back
47
4849def test_basic_auth_on_session():50 url = "https://example.org/"

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

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

Auth (#65)Tom Christiebefore adoption5afa7dd

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

Add response.json()Tom Christiebefore adoption2116d44

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

Auth (#65)Tom Christiebefore adoption5afa7dd

lines 58–63why, one step back
58
5960def 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 back
64 request.headers["Authorization"] = "Token 123"

Auth (#65)Tom Christiebefore adoption5afa7dd

lines 65–70why, one step back
65 return request
6667 with Client(dispatch=MockDispatch()) as client:68 response = client.get(url, auth=auth)6970 assert response.status_code == 200

Add response.json()Tom Christiebefore adoption2116d44

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