Skip to content
Myr

test_auth.py

This file

Path

tests/client/test_auth.py

94 lines · 2.4 KiB · Python

Last changed by

added netrc support (#177)Can Sarıgölbefore adoption919e8d3

History of this file

Take it

Auth (#65)Tom Christiebefore adoption5afa7dd

line 1why, one step back
1import json

added netrc support (#177)Can Sarıgölbefore adoption919e8d3

line 2why, one step back
2import os

Auth (#65)Tom Christiebefore adoption5afa7dd

line 3why, one step back
3

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

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

Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7

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

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

line 8why, one step back
8 CertTypes,

Auth (#65)Tom Christiebefore adoption5afa7dd

line 9why, one step back
9 Client,

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

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

Auth (#65)Tom Christiebefore adoption5afa7dd

lines 12–14why, one step back
12)
1314

Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7

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

Auth (#65)Tom Christiebefore adoption5afa7dd

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

Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7

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

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

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

Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7

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

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

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

Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7

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

Auth (#65)Tom Christiebefore adoption5afa7dd

lines 25–28why, one step back
25
2627def test_basic_auth():28 url = "https://example.org/"

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

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

Auth (#65)Tom Christiebefore adoption5afa7dd

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

Add response.json()Tom Christiebefore adoption2116d44

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

Auth (#65)Tom Christiebefore adoption5afa7dd

lines 36–44why, one step back
36
3738def test_basic_auth_in_url():39 url = "https://tomchristie:password123@example.org/"4041 with Client(dispatch=MockDispatch()) as client:42 response = client.get(url)4344 assert response.status_code == 200

Add response.json()Tom Christiebefore adoption2116d44

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

Auth (#65)Tom Christiebefore adoption5afa7dd

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

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

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

Auth (#65)Tom Christiebefore adoption5afa7dd

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

Add response.json()Tom Christiebefore adoption2116d44

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

Auth (#65)Tom Christiebefore adoption5afa7dd

lines 57–62why, one step back
57
5859def test_custom_auth():60 url = "https://example.org/"6162 def auth(request):

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

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

Auth (#65)Tom Christiebefore adoption5afa7dd

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

Add response.json()Tom Christiebefore adoption2116d44

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

added netrc support (#177)Can Sarıgölbefore adoption919e8d3

lines 71–94why, one step back
71
7273def test_netrc_auth():74 os.environ["NETRC"] = "tests/.netrc"75 url = "http://netrcexample.org"7677 with Client(dispatch=MockDispatch()) as client:78 response = client.get(url)7980 assert response.status_code == 20081 assert response.json() == {82 "auth": "Basic ZXhhbXBsZS11c2VybmFtZTpleGFtcGxlLXBhc3N3b3Jk"83 }848586def test_trust_env_auth():87 os.environ["NETRC"] = "tests/.netrc"88 url = "http://netrcexample.org"8990 with Client(dispatch=MockDispatch()) as client:91 response = client.get(url, trust_env=False)9293 assert response.status_code == 20094 assert response.json() == {"auth": None}