|
| 1 | +""" |
| 2 | +Unit tests for the default client metadata headers sent by HeimdallApiClient. |
| 3 | +
|
| 4 | +Covers: |
| 5 | +- x-client-version reflects the installed distribution version (not a hardcoded value) |
| 6 | +- Fallback to "0.0.0" when the distribution is not installed (running from source) |
| 7 | +- User-supplied client_metadata overrides the defaults |
| 8 | +""" |
| 9 | + |
| 10 | +from importlib.metadata import PackageNotFoundError |
| 11 | + |
| 12 | +from heimdall_api_client.client import _SDK_VERSION, HeimdallApiClient |
| 13 | + |
| 14 | + |
| 15 | +def _make_client(**kwargs) -> HeimdallApiClient: |
| 16 | + return HeimdallApiClient(client_id="fake-id", client_secret="fake-secret", **kwargs) |
| 17 | + |
| 18 | + |
| 19 | +class TestClientMetadata: |
| 20 | + def test_default_metadata_uses_sdk_version(self): |
| 21 | + client = _make_client() |
| 22 | + |
| 23 | + assert client.client_metadata["x-client-name"] == "python-sdk" |
| 24 | + assert client.client_metadata["x-client-version"] == _SDK_VERSION |
| 25 | + |
| 26 | + def test_sdk_version_matches_installed_distribution(self): |
| 27 | + from importlib.metadata import version |
| 28 | + |
| 29 | + try: |
| 30 | + expected = version("heimdallpower-api-client") |
| 31 | + except PackageNotFoundError: |
| 32 | + expected = "0.0.0" |
| 33 | + |
| 34 | + assert _SDK_VERSION == expected |
| 35 | + |
| 36 | + def test_user_metadata_overrides_defaults(self): |
| 37 | + client = _make_client(client_metadata={"x-client-version": "9.9.9", "x-custom": "abc"}) |
| 38 | + |
| 39 | + assert client.client_metadata["x-client-version"] == "9.9.9" |
| 40 | + assert client.client_metadata["x-custom"] == "abc" |
| 41 | + assert client.client_metadata["x-client-name"] == "python-sdk" |
0 commit comments