Repository: encode/httpx
File: tests/client/test_auth.py (Python)

Code before:
    with pytest.raises(TypeError):
        await client.get(auth="not a tuple, not a callable")  # type: ignore

    with pytest.raises(TypeError):
        client.auth = "not a tuple, not a callable"  # type: ignore


@pytest.mark.asyncio
async def test_digest_auth_returns_no_auth_if_no_digest_header_in_response() -> None:
    url = "https://example.org/"
    auth = DigestAuth(username="tomchristie", password="password123")

    client = AsyncClient(transport=AsyncMockTransport())
    response = await client.get(url, auth=auth)

    assert response.status_code == 200
    assert response.json() == {"auth": None}
    assert len(response.history) == 0


def test_digest_auth_returns_no_auth_if_alternate_auth_scheme() -> None:
    url = "https://example.org/"
    auth = DigestAuth(username="tomchristie", password="password123")
    auth_header = b"Token ..."

    client = Client(
        transport=SyncMockTransport(auth_header=auth_header, status_code=401)
    )
    response = client.get(url, auth=auth)

    assert response.status_code == 401
    assert response.json() == {"auth": None}
    assert len(response.history) == 0


@pytest.mark.asyncio
async def test_digest_auth_200_response_including_digest_auth_header() -> None:
    url = "https://example.org/"
    auth = DigestAuth(username="tomchristie", password="password123")
    auth_header = b'Digest realm="realm@host.com",qop="auth",nonce="abc",opaque="xyz"'

    client = AsyncClient(
        transport=AsyncMockTransport(auth_header=auth_header, status_code=200)
    )
    response = await client.get(url, auth=auth)

    assert response.status_code == 200
    assert response.json() == {"auth": None}
    assert len(response.history) == 0



Review comment (bug):
CI is failing, I guess this should be `httpx.Client`?

Code after:

    async with httpx.AsyncClient(transport=AsyncMockTransport()) as client:
        with pytest.raises(TypeError):
            await client.get(auth="not a tuple, not a callable")  # type: ignore

        with pytest.raises(TypeError):
            client.auth = "not a tuple, not a callable"  # type: ignore


@pytest.mark.asyncio
async def test_digest_auth_returns_no_auth_if_no_digest_header_in_response() -> None:
    url = "https://example.org/"
    auth = DigestAuth(username="tomchristie", password="password123")

    async with httpx.AsyncClient(transport=AsyncMockTransport()) as client:
        response = await client.get(url, auth=auth)

    assert response.status_code == 200
    assert response.json() == {"auth": None}
    assert len(response.history) == 0


def test_digest_auth_returns_no_auth_if_alternate_auth_scheme() -> None:
    url = "https://example.org/"
    auth = DigestAuth(username="tomchristie", password="password123")
    auth_header = b"Token ..."

    client = httpx.Client(
        transport=SyncMockTransport(auth_header=auth_header, status_code=401)
    )
    response = client.get(url, auth=auth)

    assert response.status_code == 401
    assert response.json() == {"auth": None}
    assert len(response.history) == 0


@pytest.mark.asyncio
async def test_digest_auth_200_response_including_digest_auth_header() -> None:
    url = "https://example.org/"
    auth = DigestAuth(username="tomchristie", password="password123")
    auth_header = b'Digest realm="realm@host.com",qop="auth",nonce="abc",opaque="xyz"'

    async with httpx.AsyncClient(
        transport=AsyncMockTransport(auth_header=auth_header, status_code=200)
    ) as client:
        response = await client.get(url, auth=auth)

    assert response.status_code == 200
    assert response.json() == {"auth": None}
    assert len(response.history) == 0