Skip to content
Myr

test_auth.py

This file

Path

tests/client/test_auth.py

77 lines · 1.8 KiB · Python

Last changed by

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

History of this file

Take it

1import json23import pytest45from httpcore import (6    URL,7    Client,8    Dispatcher,9    Request,10    Response,11    SSLConfig,12    TimeoutConfig,13)141516class MockDispatch(Dispatcher):17    async def send(18        self,19        request: Request,20        stream: bool = False,21        ssl: SSLConfig = None,22        timeout: TimeoutConfig = None,23    ) -> Response:24        body = json.dumps({"auth": request.headers.get("Authorization")}).encode()25        return Response(200, content=body, request=request)262728def test_basic_auth():29    url = "https://example.org/"30    auth = ("tomchristie", "password123")3132    with Client(dispatch=MockDispatch()) as client:33        response = client.get(url, auth=auth)3435    assert response.status_code == 20036    assert json.loads(response.text) == {37        "auth": "Basic dG9tY2hyaXN0aWU6cGFzc3dvcmQxMjM="38    }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 json.loads(response.text) == {49        "auth": "Basic dG9tY2hyaXN0aWU6cGFzc3dvcmQxMjM="50    }515253def test_basic_auth_on_session():54    url = "https://example.org/"55    auth = ("tomchristie", "password123")5657    with Client(dispatch=MockDispatch(), auth=auth) as client:58        response = client.get(url)5960    assert response.status_code == 20061    assert json.loads(response.text) == {62        "auth": "Basic dG9tY2hyaXN0aWU6cGFzc3dvcmQxMjM="63    }646566def test_custom_auth():67    url = "https://example.org/"6869    def auth(request):70        request.headers["Authorization"] = "Token 123"71        return request7273    with Client(dispatch=MockDispatch()) as client:74        response = client.get(url, auth=auth)7576    assert response.status_code == 20077    assert json.loads(response.text) == {"auth": "Token 123"}