|
| 1 | +from unittest.mock import PropertyMock, patch |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +from uipath.platform.common._config import ConfigurationManager |
| 6 | +from uipath.platform.common._http_config import get_httpx_client_kwargs |
| 7 | + |
| 8 | + |
| 9 | +@pytest.fixture(autouse=True) |
| 10 | +def _clean_env(monkeypatch: pytest.MonkeyPatch) -> None: |
| 11 | + """Ensure licensing-related env vars are clean for every test.""" |
| 12 | + monkeypatch.delenv("UIPATH_DISABLE_SSL_VERIFY", raising=False) |
| 13 | + |
| 14 | + |
| 15 | +class TestGetHttpxClientKwargsHeaders: |
| 16 | + """Tests for header merging in get_httpx_client_kwargs().""" |
| 17 | + |
| 18 | + def test_no_licensing_no_caller_headers(self) -> None: |
| 19 | + """No headers key when neither licensing nor caller headers are provided.""" |
| 20 | + with patch.object( |
| 21 | + ConfigurationManager, |
| 22 | + "licensing_context", |
| 23 | + new_callable=PropertyMock, |
| 24 | + return_value=None, |
| 25 | + ): |
| 26 | + result = get_httpx_client_kwargs() |
| 27 | + assert "headers" not in result |
| 28 | + |
| 29 | + def test_licensing_context_only(self) -> None: |
| 30 | + """Licensing header included when licensing_context is set.""" |
| 31 | + with patch.object( |
| 32 | + ConfigurationManager, |
| 33 | + "licensing_context", |
| 34 | + new_callable=PropertyMock, |
| 35 | + return_value="test-license-ctx", |
| 36 | + ): |
| 37 | + result = get_httpx_client_kwargs() |
| 38 | + assert result["headers"] == {"x-uipath-licensing-context": "test-license-ctx"} |
| 39 | + |
| 40 | + def test_caller_headers_only(self) -> None: |
| 41 | + """Caller headers included when no licensing_context is set.""" |
| 42 | + with patch.object( |
| 43 | + ConfigurationManager, |
| 44 | + "licensing_context", |
| 45 | + new_callable=PropertyMock, |
| 46 | + return_value=None, |
| 47 | + ): |
| 48 | + result = get_httpx_client_kwargs(headers={"Authorization": "Bearer tok"}) |
| 49 | + assert result["headers"] == {"Authorization": "Bearer tok"} |
| 50 | + |
| 51 | + def test_licensing_and_caller_headers_merged(self) -> None: |
| 52 | + """Both licensing and caller headers present in result.""" |
| 53 | + with patch.object( |
| 54 | + ConfigurationManager, |
| 55 | + "licensing_context", |
| 56 | + new_callable=PropertyMock, |
| 57 | + return_value="lic-ctx", |
| 58 | + ): |
| 59 | + result = get_httpx_client_kwargs(headers={"Authorization": "Bearer tok"}) |
| 60 | + assert result["headers"] == { |
| 61 | + "x-uipath-licensing-context": "lic-ctx", |
| 62 | + "Authorization": "Bearer tok", |
| 63 | + } |
| 64 | + |
| 65 | + def test_caller_headers_win_on_conflict(self) -> None: |
| 66 | + """Caller headers override licensing headers on same key.""" |
| 67 | + with patch.object( |
| 68 | + ConfigurationManager, |
| 69 | + "licensing_context", |
| 70 | + new_callable=PropertyMock, |
| 71 | + return_value="lic-ctx", |
| 72 | + ): |
| 73 | + result = get_httpx_client_kwargs( |
| 74 | + headers={"x-uipath-licensing-context": "caller-override"} |
| 75 | + ) |
| 76 | + assert result["headers"] == {"x-uipath-licensing-context": "caller-override"} |
| 77 | + |
| 78 | + def test_result_always_contains_base_config(self) -> None: |
| 79 | + """SSL, timeout, and redirect config always present regardless of headers.""" |
| 80 | + with patch.object( |
| 81 | + ConfigurationManager, |
| 82 | + "licensing_context", |
| 83 | + new_callable=PropertyMock, |
| 84 | + return_value="lic", |
| 85 | + ): |
| 86 | + result = get_httpx_client_kwargs(headers={"Authorization": "Bearer x"}) |
| 87 | + assert result["follow_redirects"] is True |
| 88 | + assert result["timeout"] == 30.0 |
| 89 | + assert "verify" in result |
| 90 | + |
| 91 | + def test_no_headers_key_when_empty(self) -> None: |
| 92 | + """Empty caller headers dict with no licensing does not add headers key.""" |
| 93 | + with patch.object( |
| 94 | + ConfigurationManager, |
| 95 | + "licensing_context", |
| 96 | + new_callable=PropertyMock, |
| 97 | + return_value=None, |
| 98 | + ): |
| 99 | + result = get_httpx_client_kwargs(headers={}) |
| 100 | + assert "headers" not in result |
0 commit comments