Skip to content
Myr

test_auth.py

This file

Path

tests/client/test_auth.py

72 lines · 1.8 KiB · Python

Last changed by

http3 (#86)Tom Christiebefore adoptionc9747aa

History of this file

Take it

1import json23import pytest45from http3 import (6    URL,7    AsyncDispatcher,8    AsyncRequest,9    AsyncResponse,10    CertTypes,11    Client,12    TimeoutTypes,13    VerifyTypes,14)151617class MockDispatch(AsyncDispatcher):18    async def send(19        self,20        request: AsyncRequest,21        verify: VerifyTypes = None,22        cert: CertTypes = None,23        timeout: TimeoutTypes = None,24    ) -> AsyncResponse:25        body = json.dumps({"auth": request.headers.get("Authorization")}).encode()26        return AsyncResponse(200, content=body, request=request)272829def test_basic_auth():30    url = "https://example.org/"31    auth = ("tomchristie", "password123")3233    with Client(dispatch=MockDispatch()) as client:34        response = client.get(url, auth=auth)3536    assert response.status_code == 20037    assert response.json() == {"auth": "Basic dG9tY2hyaXN0aWU6cGFzc3dvcmQxMjM="}383940def test_basic_auth_in_url():41    url = "https://tomchristie:password123@example.org/"4243    with Client(dispatch=MockDispatch()) as client:44        response = client.get(url)4546    assert response.status_code == 20047    assert response.json() == {"auth": "Basic dG9tY2hyaXN0aWU6cGFzc3dvcmQxMjM="}484950def test_basic_auth_on_session():51    url = "https://example.org/"52    auth = ("tomchristie", "password123")5354    with Client(dispatch=MockDispatch(), auth=auth) as client:55        response = client.get(url)5657    assert response.status_code == 20058    assert response.json() == {"auth": "Basic dG9tY2hyaXN0aWU6cGFzc3dvcmQxMjM="}596061def test_custom_auth():62    url = "https://example.org/"6364    def auth(request):65        request.headers["Authorization"] = "Token 123"66        return request6768    with Client(dispatch=MockDispatch()) as client:69        response = client.get(url, auth=auth)7071    assert response.status_code == 20072    assert response.json() == {"auth": "Token 123"}