Skip to content

Commit 467eaee

Browse files
committed
add test
1 parent b8016b7 commit 467eaee

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

fishjam/api/_client.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@
1111
class Client:
1212
def __init__(self, fishjam_id: str, management_token: str):
1313
self._fishjam_url = get_fishjam_url(fishjam_id)
14-
self.client = AuthenticatedClient(self._fishjam_url, token=management_token, headers={"x-fishjam-api-client": f"python-server-{get_version()}"})
14+
self.client = AuthenticatedClient(
15+
self._fishjam_url,
16+
token=management_token,
17+
headers={"x-fishjam-api-client": f"python-server-{get_version()}"},
18+
)
1519

1620
def _request(self, method, **kwargs):
1721
response = method.sync_detailed(client=self.client, **kwargs)

tests/test_room_api.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import os
2+
from unittest.mock import Mock, patch
23

4+
import httpx
35
import pytest
46

57
from fishjam import (
@@ -26,6 +28,7 @@
2628
RoomType,
2729
VideoCodec,
2830
)
31+
from fishjam.version import get_version
2932

3033
HOST = "proxy" if os.getenv("DOCKER_TEST") == "TRUE" else "localhost"
3134
FISHJAM_ID = f"http://{HOST}:5555"
@@ -54,6 +57,36 @@ def test_valid_token(self):
5457
assert room in all_rooms
5558

5659

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+
5790
@pytest.fixture
5891
def room_api():
5992
return FishjamClient(FISHJAM_ID, MANAGEMENT_TOKEN)

0 commit comments

Comments
 (0)