|
| 1 | +import os |
| 2 | +import platform |
| 3 | +from unittest import TestCase |
| 4 | +from unittest.mock import patch |
| 5 | + |
| 6 | +from src.superannotate.lib.infrastructure.services.http_client import HttpClient |
| 7 | + |
| 8 | + |
| 9 | +class TestHttpClient(TestCase): |
| 10 | + def setUp(self): |
| 11 | + self.api_url = "https://api.example.com" |
| 12 | + self.team_id = 123 |
| 13 | + self.token = f"test_token={self.team_id}" |
| 14 | + |
| 15 | + @patch.dict(os.environ, {"sa_version": "1.0.0", "SA_ENV": "test"}) |
| 16 | + def test_default_headers_with_env(self): |
| 17 | + client = HttpClient(self.api_url, self.token) |
| 18 | + headers = client.default_headers |
| 19 | + |
| 20 | + expected_user_agent = ( |
| 21 | + f"Python-SDK-Version: 1.0.0; Python: {platform.python_version()};" |
| 22 | + f"OS: {platform.system()}; Team: {self.team_id}; Env: test" |
| 23 | + ) |
| 24 | + |
| 25 | + assert headers["Authorization"] == self.token |
| 26 | + assert headers["authtype"] == "sdk" |
| 27 | + assert headers["Content-Type"] == "application/json" |
| 28 | + assert headers["User-Agent"] == expected_user_agent |
| 29 | + |
| 30 | + @patch.dict(os.environ, {"sa_version": "2.0.0"}, clear=True) |
| 31 | + def test_default_headers_without_env(self): |
| 32 | + client = HttpClient(self.api_url, self.token) |
| 33 | + headers = client.default_headers |
| 34 | + |
| 35 | + expected_user_agent = ( |
| 36 | + f"Python-SDK-Version: 2.0.0; Python: {platform.python_version()};" |
| 37 | + f"OS: {platform.system()}; Team: {self.team_id}" |
| 38 | + ) |
| 39 | + |
| 40 | + assert headers["User-Agent"] == expected_user_agent |
| 41 | + assert "Env:" not in headers["User-Agent"] |
| 42 | + |
| 43 | + def test_default_headers_no_version(self): |
| 44 | + with patch.dict(os.environ, {}, clear=True): |
| 45 | + client = HttpClient(self.api_url, self.token) |
| 46 | + headers = client.default_headers |
| 47 | + |
| 48 | + expected_user_agent = ( |
| 49 | + f"Python-SDK-Version: None; Python: {platform.python_version()};" |
| 50 | + f"OS: {platform.system()}; Team: {self.team_id}" |
| 51 | + ) |
| 52 | + assert headers["User-Agent"] == expected_user_agent |
0 commit comments