Skip to content

Commit d7fcedc

Browse files
committed
test(session-manager): cover helper branch paths for 100% coverage
Add tests for _extract_session_id and _extract_status that exercise the "skip non-start messages" and "not found" paths, replacing pragma suppressions with actual coverage.
1 parent d4bd472 commit d7fcedc

1 file changed

Lines changed: 26 additions & 2 deletions

File tree

tests/server/test_streamable_http_manager.py

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -425,15 +425,39 @@ def _extract_session_id(messages: list[Message]) -> str | None:
425425
for header_name, header_value in msg.get("headers", []):
426426
if header_name.decode().lower() == MCP_SESSION_ID_HEADER.lower():
427427
return header_value.decode()
428-
return None # pragma: no cover
428+
return None
429429

430430

431431
def _extract_status(messages: list[Message]) -> int | None:
432432
"""Extract the HTTP status code from ASGI response messages."""
433433
for msg in messages:
434434
if msg["type"] == "http.response.start":
435435
return msg["status"]
436-
return None # pragma: no cover
436+
return None
437+
438+
439+
def test_extract_session_id_skips_non_start_messages():
440+
"""_extract_session_id skips non-start messages and returns None when no ID found."""
441+
body_msg: Message = {"type": "http.response.body", "body": b"data"}
442+
start_no_header: Message = {"type": "http.response.start", "status": 200, "headers": []}
443+
444+
# Only body messages → None
445+
assert _extract_session_id([body_msg]) is None
446+
# Start message without session header → None
447+
assert _extract_session_id([body_msg, start_no_header]) is None
448+
449+
450+
def test_extract_status_skips_non_start_messages():
451+
"""_extract_status skips non-start messages and returns None when empty."""
452+
body_msg: Message = {"type": "http.response.body", "body": b"data"}
453+
start_msg: Message = {"type": "http.response.start", "status": 200, "headers": []}
454+
455+
# Only body messages → None
456+
assert _extract_status([body_msg]) is None
457+
# Body then start → returns status from start
458+
assert _extract_status([body_msg, start_msg]) == 200
459+
# Empty list → None
460+
assert _extract_status([]) is None
437461

438462

439463
def _make_scope(session_id: str | None = None) -> dict[str, Any]:

0 commit comments

Comments
 (0)