Skip to content
Myr

test_auth.py

This file

Path

tests/client/test_auth.py

120 lines · 3.2 KiB · Python

Last changed by

fixed __repr__ function of Headers and Url classes (#222)Can Sarıgölbefore adoptiona0aff8d

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 (

fixed __repr__ function of Headers and Url classes (#222)Can Sarıgölbefore adoptiona0aff8d

line 5why, one step back
5 URL,

Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7

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

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

line 9why, one step back
9 CertTypes,

Auth (#65)Tom Christiebefore adoption5afa7dd

line 10why, one step back
10 Client,

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

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

Auth (#65)Tom Christiebefore adoption5afa7dd

lines 13–15why, one step back
13)
1415

Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7

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

Auth (#65)Tom Christiebefore adoption5afa7dd

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

Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7

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

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

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

Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7

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

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

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

Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7

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

Auth (#65)Tom Christiebefore adoption5afa7dd

lines 26–29why, one step back
26
2728def 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"}

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

lines 72–95why, one step back
72
7374def test_netrc_auth():75 os.environ["NETRC"] = "tests/.netrc"76 url = "http://netrcexample.org"7778 with Client(dispatch=MockDispatch()) as client:79 response = client.get(url)8081 assert response.status_code == 20082 assert response.json() == {83 "auth": "Basic ZXhhbXBsZS11c2VybmFtZTpleGFtcGxlLXBhc3N3b3Jk"84 }858687def test_trust_env_auth():88 os.environ["NETRC"] = "tests/.netrc"89 url = "http://netrcexample.org"9091 with Client(dispatch=MockDispatch()) as client:92 response = client.get(url, trust_env=False)9394 assert response.status_code == 20095 assert response.json() == {"auth": None}

Add trust_env property to BaseClient (#187)Can Sarıgölbefore adoption04d5ffb

lines 96–103why, one step back
96
97 with Client(dispatch=MockDispatch(), trust_env=False) as client:98 response = client.get(url, trust_env=True)99100 assert response.status_code == 200101 assert response.json() == {102 "auth": "Basic ZXhhbXBsZS11c2VybmFtZTpleGFtcGxlLXBhc3N3b3Jk"103 }

fixed __repr__ function of Headers and Url classes (#222)Can Sarıgölbefore adoptiona0aff8d

lines 104–120why, one step back
104
105106def test_auth_hidden_url():107 url = "http://example-username:example-password@example.org/"108 expected = "URL('http://example-username:[secure]@example.org/')"109 assert url == URL(url)110 assert expected == repr(URL(url))111112113def test_auth_hidden_header():114 url = "https://example.org/"115 auth = ("example-username", "example-password")116117 with Client(dispatch=MockDispatch()) as client:118 response = client.get(url, auth=auth)119120 assert "'authorization': '[secure]'" in str(response.request.headers)