|
1 | 1 | import os |
| 2 | +from unittest.mock import Mock, patch |
2 | 3 |
|
| 4 | +import httpx |
3 | 5 | import pytest |
4 | 6 |
|
5 | 7 | from fishjam import ( |
|
26 | 28 | RoomType, |
27 | 29 | VideoCodec, |
28 | 30 | ) |
| 31 | +from fishjam.version import get_version |
29 | 32 |
|
30 | 33 | HOST = "proxy" if os.getenv("DOCKER_TEST") == "TRUE" else "localhost" |
31 | 34 | FISHJAM_ID = f"http://{HOST}:5555" |
@@ -54,6 +57,36 @@ def test_valid_token(self): |
54 | 57 | assert room in all_rooms |
55 | 58 |
|
56 | 59 |
|
| 60 | +class TestAPIClientHeader: |
| 61 | + def test_x_fishjam_api_client_header_is_sent(self): |
| 62 | + mock_response = Mock(spec=httpx.Response) |
| 63 | + mock_response.status_code = 200 |
| 64 | + mock_response.headers = httpx.Headers({}) |
| 65 | + mock_response.json.return_value = {"data": []} |
| 66 | + |
| 67 | + captured_headers = None |
| 68 | + |
| 69 | + def mock_send(request, **kwargs): |
| 70 | + nonlocal captured_headers |
| 71 | + captured_headers = dict(request.headers) |
| 72 | + return mock_response |
| 73 | + |
| 74 | + room_api = FishjamClient(FISHJAM_ID, MANAGEMENT_TOKEN) |
| 75 | + |
| 76 | + with patch.object(httpx.HTTPTransport, "handle_request", side_effect=mock_send): |
| 77 | + try: |
| 78 | + room_api.get_all_rooms() |
| 79 | + except Exception: |
| 80 | + # We don't care if the request fails, we just want to check the headers |
| 81 | + pass |
| 82 | + |
| 83 | + assert captured_headers is not None |
| 84 | + assert "x-fishjam-api-client" in captured_headers |
| 85 | + |
| 86 | + expected_header_value = f"python-server-{get_version()}" |
| 87 | + assert captured_headers["x-fishjam-api-client"] == expected_header_value |
| 88 | + |
| 89 | + |
57 | 90 | @pytest.fixture |
58 | 91 | def room_api(): |
59 | 92 | return FishjamClient(FISHJAM_ID, MANAGEMENT_TOKEN) |
|
0 commit comments