Skip to content
Myr

test_auth.py

This file

Path

tests/client/test_auth.py

69 lines · 1.8 KiB · Python

Last changed by

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

History of this file

Take it

Auth (#65)Tom Christiebefore adoption5afa7dd

lines 1–2why, one step back
1import json
2

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

line 3why, one step back
3from httpx import (

Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7

lines 4–6why, one step back
4 AsyncDispatcher,
5 AsyncRequest,6 AsyncResponse,

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

line 7why, one step back
7 CertTypes,

Auth (#65)Tom Christiebefore adoption5afa7dd

line 8why, one step back
8 Client,

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

lines 9–10why, one step back
9 TimeoutTypes,
10 VerifyTypes,

Auth (#65)Tom Christiebefore adoption5afa7dd

lines 11–13why, one step back
11)
1213

Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7

line 14why, one step back
14class MockDispatch(AsyncDispatcher):

Auth (#65)Tom Christiebefore adoption5afa7dd

lines 15–16why, one step back
15 async def send(
16 self,

Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7

line 17why, one step back
17 request: AsyncRequest,

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

lines 18–20why, one step back
18 verify: VerifyTypes = None,
19 cert: CertTypes = None,20 timeout: TimeoutTypes = None,

Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7

line 21why, one step back
21 ) -> AsyncResponse:

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

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

Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7

line 23why, one step back
23 return AsyncResponse(200, content=body, request=request)

Auth (#65)Tom Christiebefore adoption5afa7dd

lines 24–27why, one step back
24
2526def test_basic_auth():27 url = "https://example.org/"

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

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

Auth (#65)Tom Christiebefore adoption5afa7dd

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

Add response.json()Tom Christiebefore adoption2116d44

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

Auth (#65)Tom Christiebefore adoption5afa7dd

lines 35–43why, one step back
35
3637def test_basic_auth_in_url():38 url = "https://tomchristie:password123@example.org/"3940 with Client(dispatch=MockDispatch()) as client:41 response = client.get(url)4243 assert response.status_code == 200

Add response.json()Tom Christiebefore adoption2116d44

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

Auth (#65)Tom Christiebefore adoption5afa7dd

lines 45–48why, one step back
45
4647def test_basic_auth_on_session():48 url = "https://example.org/"

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

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

Auth (#65)Tom Christiebefore adoption5afa7dd

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

Add response.json()Tom Christiebefore adoption2116d44

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

Auth (#65)Tom Christiebefore adoption5afa7dd

lines 56–61why, one step back
56
5758def test_custom_auth():59 url = "https://example.org/"6061 def auth(request):

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

line 62why, one step back
62 request.headers["Authorization"] = "Token 123"

Auth (#65)Tom Christiebefore adoption5afa7dd

lines 63–68why, one step back
63 return request
6465 with Client(dispatch=MockDispatch()) as client:66 response = client.get(url, auth=auth)6768 assert response.status_code == 200

Add response.json()Tom Christiebefore adoption2116d44

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