|
| 1 | +from base64 import b64encode |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +import tls_requests |
| 6 | + |
| 7 | +auth = ("user", "pass") |
| 8 | +AUTH_TOKEN = "Basic %s" % b64encode(b":".join([s.encode() for s in auth])).decode() |
| 9 | +AUTH_HEADERS = {"authorization": AUTH_TOKEN} |
| 10 | +AUTH_FUNCTION_KEY = "x-authorization" |
| 11 | +AUTH_FUNCTION_VALUE = "123456" |
| 12 | +AUTH_FUNCTION_HEADERS = {AUTH_FUNCTION_KEY: AUTH_FUNCTION_VALUE} |
| 13 | + |
| 14 | + |
| 15 | +def auth_function(request): |
| 16 | + request.headers.update(AUTH_FUNCTION_HEADERS) |
| 17 | + |
| 18 | + |
| 19 | +@pytest.fixture |
| 20 | +def auth_url(httpserver): |
| 21 | + return httpserver.url_for('/auth') |
| 22 | + |
| 23 | + |
| 24 | +@pytest.fixture |
| 25 | +def http_auth_function(httpserver): |
| 26 | + httpserver.expect_request("/auth", headers=AUTH_FUNCTION_HEADERS).respond_with_data() |
| 27 | + return httpserver |
| 28 | + |
| 29 | + |
| 30 | +@pytest.fixture |
| 31 | +def http_auth(httpserver): |
| 32 | + httpserver.expect_request("/auth", headers=AUTH_HEADERS).respond_with_data() |
| 33 | + return httpserver |
| 34 | + |
| 35 | + |
| 36 | +def test_auth(http_auth, auth_url): |
| 37 | + response = tls_requests.get(auth_url, auth=auth) |
| 38 | + assert response.status_code == 200 |
| 39 | + assert response.request.headers["Authorization"] == AUTH_TOKEN |
| 40 | + |
| 41 | + |
| 42 | +def test_auth_function(http_auth_function, auth_url): |
| 43 | + response = tls_requests.get(auth_url, auth=auth_function) |
| 44 | + assert response.status_code == 200 |
| 45 | + assert response.request.headers[AUTH_FUNCTION_KEY] == AUTH_FUNCTION_VALUE |
| 46 | + |
| 47 | + |
| 48 | +def test_client_auth(http_auth, auth_url): |
| 49 | + with tls_requests.Client(auth=auth) as client: |
| 50 | + response = client.get(auth_url) |
| 51 | + |
| 52 | + assert response.status_code == 200 |
| 53 | + assert bool(response.closed == client.closed) is True |
| 54 | + assert response.request.headers["Authorization"] == AUTH_TOKEN |
| 55 | + |
| 56 | + |
| 57 | +def test_client_auth_cross_sharing(http_auth, auth_url): |
| 58 | + with tls_requests.Client(auth=('1', '2')) as client: |
| 59 | + response = client.get(auth_url, auth=auth) |
| 60 | + |
| 61 | + assert response.status_code == 200 |
| 62 | + assert bool(response.closed == client.closed) is True |
| 63 | + assert response.request.headers["Authorization"] == AUTH_TOKEN |
| 64 | + |
| 65 | + |
| 66 | +def test_client_auth_function_cross_sharing(http_auth_function, auth_url): |
| 67 | + with tls_requests.Client(auth=auth) as client: |
| 68 | + response = client.get(auth_url, auth=auth_function) |
| 69 | + |
| 70 | + assert response.status_code == 200 |
| 71 | + assert bool(response.closed == client.closed) is True |
| 72 | + assert response.request.headers[AUTH_FUNCTION_KEY] == AUTH_FUNCTION_VALUE |
| 73 | + |
| 74 | + |
| 75 | +@pytest.mark.asyncio |
| 76 | +async def test_async_auth(http_auth, auth_url): |
| 77 | + async with tls_requests.AsyncClient(auth=auth) as client: |
| 78 | + response = await client.get(auth_url) |
| 79 | + |
| 80 | + assert response.status_code == 200 |
| 81 | + assert bool(response.closed == client.closed) is True |
| 82 | + assert response.request.headers["Authorization"] == AUTH_TOKEN |
| 83 | + |
| 84 | + |
| 85 | +@pytest.mark.asyncio |
| 86 | +async def test_async_auth_function(http_auth_function, auth_url): |
| 87 | + async with tls_requests.AsyncClient(auth=auth_function) as client: |
| 88 | + response = await client.get(auth_url) |
| 89 | + |
| 90 | + assert response.status_code == 200 |
| 91 | + assert bool(response.closed == client.closed) is True |
| 92 | + assert response.request.headers[AUTH_FUNCTION_KEY] == AUTH_FUNCTION_VALUE |
| 93 | + |
| 94 | + |
| 95 | +@pytest.mark.asyncio |
| 96 | +async def test_async_auth_function_cross_sharing(http_auth_function, auth_url): |
| 97 | + async with tls_requests.AsyncClient(auth=auth) as client: |
| 98 | + response = await client.get(auth_url, auth=auth_function) |
| 99 | + |
| 100 | + assert response.status_code == 200 |
| 101 | + assert bool(response.closed == client.closed) is True |
| 102 | + assert response.request.headers[AUTH_FUNCTION_KEY] == AUTH_FUNCTION_VALUE |
0 commit comments