Skip to content
Myr

test_auth.py

This file

Path

tests/client/test_auth.py

129 lines · 3.4 KiB · Python

Last changed by

Refactor client functionality into middleware (#268)Florimond Mancabefore adoption40e6e8c

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

Refactor client functionality into middleware (#268)Florimond Mancabefore adoption40e6e8c

lines 4–5why, one step back
4import pytest
5

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

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

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

line 7why, one step back
7 URL,

Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7

lines 8–10why, one step back
8 AsyncDispatcher,
9 AsyncRequest,10 AsyncResponse,

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

line 11why, one step back
11 CertTypes,

Auth (#65)Tom Christiebefore adoption5afa7dd

line 12why, one step back
12 Client,

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

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

Auth (#65)Tom Christiebefore adoption5afa7dd

lines 15–17why, one step back
15)
1617

Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7

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

Auth (#65)Tom Christiebefore adoption5afa7dd

lines 19–20why, one step back
19 async def send(
20 self,

Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7

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

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,

Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7

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

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

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

Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7

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

Auth (#65)Tom Christiebefore adoption5afa7dd

lines 28–31why, one step back
28
2930def 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"}

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

lines 74–97why, one step back
74
7576def test_netrc_auth():77 os.environ["NETRC"] = "tests/.netrc"78 url = "http://netrcexample.org"7980 with Client(dispatch=MockDispatch()) as client:81 response = client.get(url)8283 assert response.status_code == 20084 assert response.json() == {85 "auth": "Basic ZXhhbXBsZS11c2VybmFtZTpleGFtcGxlLXBhc3N3b3Jk"86 }878889def test_trust_env_auth():90 os.environ["NETRC"] = "tests/.netrc"91 url = "http://netrcexample.org"9293 with Client(dispatch=MockDispatch()) as client:94 response = client.get(url, trust_env=False)9596 assert response.status_code == 20097 assert response.json() == {"auth": None}

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

lines 98–105why, one step back
98
99 with Client(dispatch=MockDispatch(), trust_env=False) as client:100 response = client.get(url, trust_env=True)101102 assert response.status_code == 200103 assert response.json() == {104 "auth": "Basic ZXhhbXBsZS11c2VybmFtZTpleGFtcGxlLXBhc3N3b3Jk"105 }

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

lines 106–122why, one step back
106
107108def test_auth_hidden_url():109 url = "http://example-username:example-password@example.org/"110 expected = "URL('http://example-username:[secure]@example.org/')"111 assert url == URL(url)112 assert expected == repr(URL(url))113114115def test_auth_hidden_header():116 url = "https://example.org/"117 auth = ("example-username", "example-password")118119 with Client(dispatch=MockDispatch()) as client:120 response = client.get(url, auth=auth)121122 assert "'authorization': '[secure]'" in str(response.request.headers)

Refactor client functionality into middleware (#268)Florimond Mancabefore adoption40e6e8c

lines 123–129why, one step back
123
124125def test_auth_invalid_type():126 url = "https://example.org/"127 with Client(dispatch=MockDispatch(), auth="not a tuple, not a callable") as client:128 with pytest.raises(TypeError):129 client.get(url)