From c88b383ced32abaf5e07bfe3ab2c73a69d04d8dc Mon Sep 17 00:00:00 2001 From: Pavel Tisnovsky Date: Sun, 10 Aug 2025 09:00:09 +0200 Subject: [PATCH 1/2] Use proper Request object to test REST API endpoints handlers --- tests/unit/app/endpoints/test_query.py | 5 ++- .../app/endpoints/test_streaming_query.py | 44 ++++++++++++++++--- 2 files changed, 40 insertions(+), 9 deletions(-) diff --git a/tests/unit/app/endpoints/test_query.py b/tests/unit/app/endpoints/test_query.py index 5530f56c..db0ad233 100644 --- a/tests/unit/app/endpoints/test_query.py +++ b/tests/unit/app/endpoints/test_query.py @@ -66,9 +66,10 @@ async def test_query_endpoint_handler_configuration_not_loaded(mocker): ) mocker.patch("app.endpoints.query.configuration", None) - request = None + query = "What is OpenStack?" + query_request = QueryRequest(query=query) with pytest.raises(HTTPException) as e: - await query_endpoint_handler(request, auth=["test-user", "", "token"]) + await query_endpoint_handler(query_request, auth=["test-user", "", "token"]) assert e.value.status_code == status.HTTP_500_INTERNAL_SERVER_ERROR assert e.value.detail["response"] == "Configuration is not loaded" diff --git a/tests/unit/app/endpoints/test_streaming_query.py b/tests/unit/app/endpoints/test_streaming_query.py index 534c95e1..f1c2ddfe 100644 --- a/tests/unit/app/endpoints/test_streaming_query.py +++ b/tests/unit/app/endpoints/test_streaming_query.py @@ -8,7 +8,7 @@ import pytest -from fastapi import HTTPException, status +from fastapi import HTTPException, Request, status from fastapi.responses import StreamingResponse from llama_stack_client import APIConnectionError @@ -118,9 +118,14 @@ async def test_streaming_query_endpoint_handler_configuration_not_loaded(mocker) query = "What is OpenStack?" query_request = QueryRequest(query=query) + request = Request( + scope={ + "type": "http", + } + ) # await the async function with pytest.raises(HTTPException) as e: - await streaming_query_endpoint_handler(None, query_request, auth=MOCK_AUTH) + await streaming_query_endpoint_handler(request, query_request, auth=MOCK_AUTH) assert e.status_code == status.HTTP_500_INTERNAL_SERVER_ERROR assert e.detail["response"] == "Configuration is not loaded" @@ -145,9 +150,14 @@ async def test_streaming_query_endpoint_on_connection_error(mocker): mock_async_lsc = mocker.patch("client.AsyncLlamaStackClientHolder.get_client") mock_async_lsc.return_value = mock_client + request = Request( + scope={ + "type": "http", + } + ) # await the async function with pytest.raises(HTTPException) as e: - await streaming_query_endpoint_handler(None, query_request, auth=MOCK_AUTH) + await streaming_query_endpoint_handler(request, query_request, auth=MOCK_AUTH) assert e.status_code == status.HTTP_500_INTERNAL_SERVER_ERROR assert e.detail["response"] == "Configuration is not loaded" @@ -256,9 +266,14 @@ async def _test_streaming_query_endpoint_handler(mocker, store_transcript=False) query_request = QueryRequest(query=query) + request = Request( + scope={ + "type": "http", + } + ) # Await the async function response = await streaming_query_endpoint_handler( - None, query_request, auth=MOCK_AUTH + request, query_request, auth=MOCK_AUTH ) # assert the response is a StreamingResponse @@ -1259,8 +1274,13 @@ async def test_auth_tuple_unpacking_in_streaming_query_endpoint_handler(mocker): "app.endpoints.streaming_query.is_transcripts_enabled", return_value=False ) + request = Request( + scope={ + "type": "http", + } + ) await streaming_query_endpoint_handler( - None, + request, QueryRequest(query="test query"), auth=("user123", "username", "auth_token_123"), mcp_headers=None, @@ -1301,8 +1321,13 @@ async def test_streaming_query_endpoint_handler_no_tools_true(mocker): query_request = QueryRequest(query="What is OpenStack?", no_tools=True) + request = Request( + scope={ + "type": "http", + } + ) response = await streaming_query_endpoint_handler( - None, query_request, auth=MOCK_AUTH + request, query_request, auth=MOCK_AUTH ) # Assert the response is a StreamingResponse @@ -1341,8 +1366,13 @@ async def test_streaming_query_endpoint_handler_no_tools_false(mocker): query_request = QueryRequest(query="What is OpenStack?", no_tools=False) + request = Request( + scope={ + "type": "http", + } + ) response = await streaming_query_endpoint_handler( - None, query_request, auth=MOCK_AUTH + request, query_request, auth=MOCK_AUTH ) # Assert the response is a StreamingResponse From 223e3b4c724f0781fa35d7b5a20a89b8478404ee Mon Sep 17 00:00:00 2001 From: Pavel Tisnovsky Date: Sun, 10 Aug 2025 09:09:59 +0200 Subject: [PATCH 2/2] Use proper Request object to test other handlers too --- tests/unit/app/endpoints/test_config.py | 14 +++++++++++--- tests/unit/app/endpoints/test_info.py | 8 +++++++- tests/unit/app/endpoints/test_metrics.py | 9 ++++++++- tests/unit/app/endpoints/test_root.py | 8 +++++++- 4 files changed, 33 insertions(+), 6 deletions(-) diff --git a/tests/unit/app/endpoints/test_config.py b/tests/unit/app/endpoints/test_config.py index 040eedbf..48ea9603 100644 --- a/tests/unit/app/endpoints/test_config.py +++ b/tests/unit/app/endpoints/test_config.py @@ -2,7 +2,7 @@ import pytest -from fastapi import HTTPException, status +from fastapi import HTTPException, Request, status from app.endpoints.config import config_endpoint_handler from configuration import AppConfig @@ -15,7 +15,11 @@ def test_config_endpoint_handler_configuration_not_loaded(mocker): ) mocker.patch("app.endpoints.config.configuration", None) - request = None + request = Request( + scope={ + "type": "http", + } + ) with pytest.raises(HTTPException) as e: config_endpoint_handler(request) assert e.status_code == status.HTTP_500_INTERNAL_SERVER_ERROR @@ -49,7 +53,11 @@ def test_config_endpoint_handler_configuration_loaded(): } cfg = AppConfig() cfg.init_from_dict(config_dict) - request = None + request = Request( + scope={ + "type": "http", + } + ) response = config_endpoint_handler(request) assert response is not None assert response == cfg.configuration diff --git a/tests/unit/app/endpoints/test_info.py b/tests/unit/app/endpoints/test_info.py index ee45d3fa..4e8f30ca 100644 --- a/tests/unit/app/endpoints/test_info.py +++ b/tests/unit/app/endpoints/test_info.py @@ -1,5 +1,7 @@ """Unit tests for the /info REST API endpoint.""" +from fastapi import Request + from app.endpoints.info import info_endpoint_handler from configuration import AppConfig @@ -28,7 +30,11 @@ def test_info_endpoint(): } cfg = AppConfig() cfg.init_from_dict(config_dict) - request = None + request = Request( + scope={ + "type": "http", + } + ) response = info_endpoint_handler(request) assert response is not None assert response.name is not None diff --git a/tests/unit/app/endpoints/test_metrics.py b/tests/unit/app/endpoints/test_metrics.py index 1bddc3c6..8d074215 100644 --- a/tests/unit/app/endpoints/test_metrics.py +++ b/tests/unit/app/endpoints/test_metrics.py @@ -1,5 +1,7 @@ """Unit tests for the /metrics REST API endpoint.""" +from fastapi import Request + from app.endpoints.metrics import metrics_endpoint_handler @@ -8,7 +10,12 @@ async def test_metrics_endpoint(mocker): mock_setup_metrics = mocker.patch( "app.endpoints.metrics.setup_model_metrics", return_value=None ) - response = await metrics_endpoint_handler(None) + request = Request( + scope={ + "type": "http", + } + ) + response = await metrics_endpoint_handler(request) assert response is not None assert response.status_code == 200 assert "text/plain" in response.headers["Content-Type"] diff --git a/tests/unit/app/endpoints/test_root.py b/tests/unit/app/endpoints/test_root.py index 8a3af15d..c2e889df 100644 --- a/tests/unit/app/endpoints/test_root.py +++ b/tests/unit/app/endpoints/test_root.py @@ -1,10 +1,16 @@ """Unit tests for the / endpoint handler.""" +from fastapi import Request + from app.endpoints.root import root_endpoint_handler def test_root_endpoint(): """Test the root endpoint handler.""" - request = None + request = Request( + scope={ + "type": "http", + } + ) response = root_endpoint_handler(request) assert response is not None