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

1import json2import os34import pytest56from httpx import (7    URL,8    AsyncDispatcher,9    AsyncRequest,10    AsyncResponse,11    CertTypes,12    Client,13    TimeoutTypes,14    VerifyTypes,15)161718class MockDispatch(AsyncDispatcher):19    async def send(20        self,21        request: AsyncRequest,22        verify: VerifyTypes = None,23        cert: CertTypes = None,24        timeout: TimeoutTypes = None,25    ) -> AsyncResponse:26        body = json.dumps({"auth": request.headers.get("Authorization")}).encode()27        return AsyncResponse(200, content=body, request=request)282930def test_basic_auth():31    url = "https://example.org/"32    auth = ("tomchristie", "password123")3334    with Client(dispatch=MockDispatch()) as client:35        response = client.get(url, auth=auth)3637    assert response.status_code == 20038    assert response.json() == {"auth": "Basic dG9tY2hyaXN0aWU6cGFzc3dvcmQxMjM="}394041def 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 == 20048    assert response.json() == {"auth": "Basic dG9tY2hyaXN0aWU6cGFzc3dvcmQxMjM="}495051def test_basic_auth_on_session():52    url = "https://example.org/"53    auth = ("tomchristie", "password123")5455    with Client(dispatch=MockDispatch(), auth=auth) as client:56        response = client.get(url)5758    assert response.status_code == 20059    assert response.json() == {"auth": "Basic dG9tY2hyaXN0aWU6cGFzc3dvcmQxMjM="}606162def test_custom_auth():63    url = "https://example.org/"6465    def auth(request):66        request.headers["Authorization"] = "Token 123"67        return request6869    with Client(dispatch=MockDispatch()) as client:70        response = client.get(url, auth=auth)7172    assert response.status_code == 20073    assert response.json() == {"auth": "Token 123"}747576def 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}9899    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    }106107108def 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)123124125def 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)