Skip to content
Myr

test_auth.py

This file

Path

tests/test_auth.py

63 lines · 2.0 KiB · Python

Last changed by

Not known.

History of this file

Take it

1"""2Unit tests for auth classes.34Integration tests also exist in tests/client/test_auth.py5"""6import pytest78import httpx91011def test_basic_auth():12    auth = httpx.BasicAuth(username="user", password="pass")13    request = httpx.Request("GET", "https://www.example.com")1415    # The initial request should include a basic auth header.16    flow = auth.sync_auth_flow(request)17    request = next(flow)18    assert request.headers["Authorization"].startswith("Basic")1920    # No other requests are made.21    response = httpx.Response(content=b"Hello, world!", status_code=200)22    with pytest.raises(StopIteration):23        flow.send(response)242526def test_digest_auth_with_200():27    auth = httpx.DigestAuth(username="user", password="pass")28    request = httpx.Request("GET", "https://www.example.com")2930    # The initial request should not include an auth header.31    flow = auth.sync_auth_flow(request)32    request = next(flow)33    assert "Authorization" not in request.headers3435    # If a 200 response is returned, then no other requests are made.36    response = httpx.Response(content=b"Hello, world!", status_code=200)37    with pytest.raises(StopIteration):38        flow.send(response)394041def test_digest_auth_with_401():42    auth = httpx.DigestAuth(username="user", password="pass")43    request = httpx.Request("GET", "https://www.example.com")4445    # The initial request should not include an auth header.46    flow = auth.sync_auth_flow(request)47    request = next(flow)48    assert "Authorization" not in request.headers4950    # If a 401 response is returned, then a digest auth request is made.51    headers = {52        "WWW-Authenticate": 'Digest realm="...", qop="auth", nonce="...", opaque="..."'53    }54    response = httpx.Response(55        content=b"Auth required", status_code=401, headers=headers56    )57    request = flow.send(response)58    assert request.headers["Authorization"].startswith("Digest")5960    # No other requests are made.61    response = httpx.Response(content=b"Hello, world!", status_code=200)62    with pytest.raises(StopIteration):63        flow.send(response)