Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion fishjam/api/_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,17 @@
from fishjam._openapi_client.types import Response
from fishjam.errors import HTTPError
from fishjam.utils import get_fishjam_url
from fishjam.version import get_version


class Client:
def __init__(self, fishjam_id: str, management_token: str):
self._fishjam_url = get_fishjam_url(fishjam_id)
self.client = AuthenticatedClient(self._fishjam_url, token=management_token)
self.client = AuthenticatedClient(
self._fishjam_url,
token=management_token,
headers={"x-fishjam-api-client": f"python-server/{get_version()}"},
)

def _request(self, method, **kwargs):
response = method.sync_detailed(client=self.client, **kwargs)
Expand Down
35 changes: 34 additions & 1 deletion tests/test_room_api.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import os
from unittest.mock import Mock, patch

import httpx
import pytest

from fishjam import (
Expand All @@ -26,6 +28,7 @@
RoomType,
VideoCodec,
)
from fishjam.version import get_version

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


class TestAPIClientHeader:
def test_x_fishjam_api_client_header_is_sent(self):
mock_response = Mock(spec=httpx.Response)
mock_response.status_code = 200
mock_response.headers = httpx.Headers({})
mock_response.json.return_value = {"data": []}

captured_headers = None

def mock_send(request, **kwargs):
nonlocal captured_headers
captured_headers = dict(request.headers)
return mock_response

room_api = FishjamClient(FISHJAM_ID, MANAGEMENT_TOKEN)

with patch.object(httpx.HTTPTransport, "handle_request", side_effect=mock_send):
try:
room_api.get_all_rooms()
except Exception:
# We don't care if the request fails, we just want to check the headers
pass

assert captured_headers is not None
assert "x-fishjam-api-client" in captured_headers

expected_header_value = f"python-server/{get_version()}"
assert captured_headers["x-fishjam-api-client"] == expected_header_value


@pytest.fixture
def room_api():
return FishjamClient(FISHJAM_ID, MANAGEMENT_TOKEN)
Expand Down Expand Up @@ -102,7 +135,7 @@ def test_valid_params(self, room_api: FishjamClient):
assert room in room_api.get_all_rooms()

def test_invalid_max_peers(self, room_api: FishjamClient):
options = RoomOptions(max_peers="10")
options = RoomOptions(max_peers="nan")

with pytest.raises(BadRequestError):
room_api.create_room(options)
Expand Down