* Add support for async auth flows * Move body logic to Auth, add sync_auth_flow, add NoAuth * Update tests * Stick to next() / __anext__() * Fix undefined name errors * Add docs * Add unit tests for auth classes Co-authored-by: Tom Christie <tom@tomchristie.com>
64 lines
2.0 KiB
Python
64 lines
2.0 KiB
Python
"""
|
|
Unit tests for auth classes.
|
|
|
|
Integration tests also exist in tests/client/test_auth.py
|
|
"""
|
|
import pytest
|
|
|
|
import httpx
|
|
|
|
|
|
def test_basic_auth():
|
|
auth = httpx.BasicAuth(username="user", password="pass")
|
|
request = httpx.Request("GET", "https://www.example.com")
|
|
|
|
# The initial request should include a basic auth header.
|
|
flow = auth.sync_auth_flow(request)
|
|
request = next(flow)
|
|
assert request.headers["Authorization"].startswith("Basic")
|
|
|
|
# No other requests are made.
|
|
response = httpx.Response(content=b"Hello, world!", status_code=200)
|
|
with pytest.raises(StopIteration):
|
|
flow.send(response)
|
|
|
|
|
|
def test_digest_auth_with_200():
|
|
auth = httpx.DigestAuth(username="user", password="pass")
|
|
request = httpx.Request("GET", "https://www.example.com")
|
|
|
|
# The initial request should not include an auth header.
|
|
flow = auth.sync_auth_flow(request)
|
|
request = next(flow)
|
|
assert "Authorization" not in request.headers
|
|
|
|
# If a 200 response is returned, then no other requests are made.
|
|
response = httpx.Response(content=b"Hello, world!", status_code=200)
|
|
with pytest.raises(StopIteration):
|
|
flow.send(response)
|
|
|
|
|
|
def test_digest_auth_with_401():
|
|
auth = httpx.DigestAuth(username="user", password="pass")
|
|
request = httpx.Request("GET", "https://www.example.com")
|
|
|
|
# The initial request should not include an auth header.
|
|
flow = auth.sync_auth_flow(request)
|
|
request = next(flow)
|
|
assert "Authorization" not in request.headers
|
|
|
|
# If a 401 response is returned, then a digest auth request is made.
|
|
headers = {
|
|
"WWW-Authenticate": 'Digest realm="...", qop="auth", nonce="...", opaque="..."'
|
|
}
|
|
response = httpx.Response(
|
|
content=b"Auth required", status_code=401, headers=headers
|
|
)
|
|
request = flow.send(response)
|
|
assert request.headers["Authorization"].startswith("Digest")
|
|
|
|
# No other requests are made.
|
|
response = httpx.Response(content=b"Hello, world!", status_code=200)
|
|
with pytest.raises(StopIteration):
|
|
flow.send(response)
|