Skip to content

Commit 9b50b0d

Browse files
committed
test: verify warning log for unknown session ID
1 parent b0ee1a4 commit 9b50b0d

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

tests/server/test_streamable_http_manager.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""Tests for StreamableHTTPSessionManager."""
22

33
import json
4+
import logging
45
from typing import Any
56
from unittest.mock import AsyncMock, patch
67

@@ -269,7 +270,7 @@ async def mock_receive():
269270

270271

271272
@pytest.mark.anyio
272-
async def test_unknown_session_id_returns_404():
273+
async def test_unknown_session_id_returns_404(caplog: pytest.LogCaptureFixture):
273274
"""Test that requests with unknown session IDs return HTTP 404 per MCP spec."""
274275
app = Server("test-unknown-session")
275276
manager = StreamableHTTPSessionManager(app=app)
@@ -299,7 +300,8 @@ async def mock_send(message: Message):
299300
async def mock_receive():
300301
return {"type": "http.request", "body": b"{}", "more_body": False} # pragma: no cover
301302

302-
await manager.handle_request(scope, mock_receive, mock_send)
303+
with caplog.at_level(logging.WARNING, logger="mcp.server.streamable_http_manager"):
304+
await manager.handle_request(scope, mock_receive, mock_send)
303305

304306
# Find the response start message
305307
response_start = next(
@@ -316,6 +318,11 @@ async def mock_receive():
316318
assert error_data["error"]["code"] == INVALID_REQUEST
317319
assert error_data["error"]["message"] == "Session not found"
318320

321+
# Verify warning was logged with the session ID
322+
assert any(
323+
"non-existent-session-id" in record.message for record in caplog.records
324+
), "Should log a warning with the unknown session ID"
325+
319326

320327
@pytest.mark.anyio
321328
async def test_e2e_streamable_http_server_cleanup():

0 commit comments

Comments
 (0)