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 fileTake it
Auth (#65)Tom Christiebefore adoption5afa7dd
line 1why, one step back1import jsonadded netrc support (#177)Can Sarıgölbefore adoption919e8d3
line 2why, one step back2import osAuth (#65)Tom Christiebefore adoption5afa7dd
line 3why, one step back3Refactor client functionality into middleware (#268)Florimond Mancabefore adoption40e6e8c
lines 4–5why, one step back4import pytest5Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 6why, one step back6from httpx import (fixed __repr__ function of Headers and Url classes (#222)Can Sarıgölbefore adoptiona0aff8d
line 7why, one step back7 URL,Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7
lines 8–10why, one step back8 AsyncDispatcher,9 AsyncRequest,10 AsyncResponse,Work on bringing API into parity with `requests`. (#76)Tom Christiebefore adoption9574041
line 11why, one step back11 CertTypes,Auth (#65)Tom Christiebefore adoption5afa7dd
line 12why, one step back12 Client,Work on bringing API into parity with `requests`. (#76)Tom Christiebefore adoption9574041
lines 13–14why, one step back13 TimeoutTypes,14 VerifyTypes,Auth (#65)Tom Christiebefore adoption5afa7dd
lines 15–17why, one step back15)1617Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7
line 18why, one step back18class MockDispatch(AsyncDispatcher):Auth (#65)Tom Christiebefore adoption5afa7dd
lines 19–20why, one step back19 async def send(20 self,Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7
line 21why, one step back21 request: AsyncRequest,Work on bringing API into parity with `requests`. (#76)Tom Christiebefore adoption9574041
lines 22–24why, one step back22 verify: VerifyTypes = None,23 cert: CertTypes = None,24 timeout: TimeoutTypes = None,Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7
line 25why, one step back25 ) -> AsyncResponse:Drop unused interface on ConnectionStore (#70)Tom Christiebefore adoption3ed0885
line 26why, one step back26 body = json.dumps({"auth": request.headers.get("Authorization")}).encode()Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7
line 27why, one step back27 return AsyncResponse(200, content=body, request=request)Auth (#65)Tom Christiebefore adoption5afa7dd
lines 28–31why, one step back282930def test_basic_auth():31 url = "https://example.org/"Drop unused interface on ConnectionStore (#70)Tom Christiebefore adoption3ed0885
line 32why, one step back32 auth = ("tomchristie", "password123")Auth (#65)Tom Christiebefore adoption5afa7dd
lines 33–37why, one step back3334 with Client(dispatch=MockDispatch()) as client:35 response = client.get(url, auth=auth)3637 assert response.status_code == 200Add response.json()Tom Christiebefore adoption2116d44
line 38why, one step back38 assert response.json() == {"auth": "Basic dG9tY2hyaXN0aWU6cGFzc3dvcmQxMjM="}Auth (#65)Tom Christiebefore adoption5afa7dd
lines 39–47why, one step back394041def 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 == 200Add response.json()Tom Christiebefore adoption2116d44
line 48why, one step back48 assert response.json() == {"auth": "Basic dG9tY2hyaXN0aWU6cGFzc3dvcmQxMjM="}Auth (#65)Tom Christiebefore adoption5afa7dd
lines 49–52why, one step back495051def test_basic_auth_on_session():52 url = "https://example.org/"Drop unused interface on ConnectionStore (#70)Tom Christiebefore adoption3ed0885
line 53why, one step back53 auth = ("tomchristie", "password123")Auth (#65)Tom Christiebefore adoption5afa7dd
lines 54–58why, one step back5455 with Client(dispatch=MockDispatch(), auth=auth) as client:56 response = client.get(url)5758 assert response.status_code == 200Add response.json()Tom Christiebefore adoption2116d44
line 59why, one step back59 assert response.json() == {"auth": "Basic dG9tY2hyaXN0aWU6cGFzc3dvcmQxMjM="}Auth (#65)Tom Christiebefore adoption5afa7dd
lines 60–65why, one step back606162def 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 back66 request.headers["Authorization"] = "Token 123"Auth (#65)Tom Christiebefore adoption5afa7dd
lines 67–72why, one step back67 return request6869 with Client(dispatch=MockDispatch()) as client:70 response = client.get(url, auth=auth)7172 assert response.status_code == 200Add response.json()Tom Christiebefore adoption2116d44
line 73why, one step back73 assert response.json() == {"auth": "Token 123"}added netrc support (#177)Can Sarıgölbefore adoption919e8d3
lines 74–97why, one step back747576def 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 back9899 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 back106107108def 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 back123124125def 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)