Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

test: exercise real SSE connections and synchronized shutdown (#43423)

Asuka Minato8071c2cbc6f6
diff --git a/api/tests/unit_tests/core/mcp/client/test_sse.py b/api/tests/unit_tests/core/mcp/client/test_sse.py
index 09f32424b2f..6ec07261818 100644
--- a/api/tests/unit_tests/core/mcp/client/test_sse.py
+++ b/api/tests/unit_tests/core/mcp/client/test_sse.py
@@ -3,19 +3,38 @@ import json
import queue
import threading
import time
-from typing import Any
-from unittest.mock import Mock, patch
+from unittest.mock import patch
import httpx
import pytest
-from httpx_sse import ServerSentEvent
+from httpx_sse import EventSource, ServerSentEvent
from sseclient import SSEClient
from core.mcp import types
from core.mcp.client.sse_client import sse_client
from core.mcp.error import MCPAuthError, MCPConnectionError
-SERVER_NAME = "test_server_for_SSE"
+SSE_ENDPOINT = "event: endpoint\ndata: /messages/?session_id=test-123\n\n"
+
+
+def install_sse_response(monkeypatch: pytest.MonkeyPatch):
+ def install(response: httpx.Response | Exception) -> list[httpx.Request]:
+ requests: list[httpx.Request] = []
+
+ def handle(request: httpx.Request) -> httpx.Response:
+ requests.append(request)
+ if isinstance(response, Exception):
+ raise response
+ return response
+
+ def client(*, headers: dict[str, str]) -> httpx.Client:
+ return httpx.Client(headers=headers, transport=httpx.MockTransport(handle))
+
+ monkeypatch.setattr("core.mcp.client.sse_client.create_ssrf_proxy_mcp_http_client", client)
+ return requests
+
+ return install
@pytest.fixture
@@ -66,132 +85,42 @@ def test_sse_message_without_id_stays_notification():
assert msg.root.jsonrpc == "2.0"
-class MockSSEClient:
- """Mock SSE client for testing."""
-
- def __init__(self, url: str, headers: dict[str, Any] | None = None):
- self.url = url
- self.headers = headers or {}
- self.connected = False
- self.read_queue: queue.Queue = queue.Queue()
- self.write_queue: queue.Queue = queue.Queue()
-
- def connect(self):
- """Simulate connection establishment."""
- self.connected = True
-
- # Send endpoint event
- endpoint_data = "/messages/?session_id=test-session-123"
- self.read_queue.put(("endpoint", endpoint_data))
-
- return self.read_queue, self.write_queue
-
- def send_initialize_response(self):
- """Send a mock initialize response."""
- response = {
- "jsonrpc": "2.0",
- "id": 1,
- "result": {
- "protocolVersion": types.LATEST_PROTOCOL_VERSION,
- "capabilities": {
- "logging": None,
- "resources": None,
- "tools": None,
- "experimental": None,
- "prompts": None,
- },
- "serverInfo": {"name": SERVER_NAME, "version": "0.1.0"},
- "instructions": "Test server instructions.",
- },
- }
- self.read_queue.put(("message", json.dumps(response)))
-
-
-def test_sse_client_message_id_handling():
- """Test SSE client properly handles message ID coercion."""
- mock_client = MockSSEClient("http://test.example/sse")
- read_queue, write_queue = mock_client.connect()
-
- # Send a message with string ID that should be coerced to int
- message_data = {
- "jsonrpc": "2.0",
- "id": "456", # String ID
- "result": {"test": "data"},
- }
- read_queue.put(("message", json.dumps(message_data)))
- read_queue.get(timeout=1.0)
- # Get the message from queue
- event_type, data = read_queue.get(timeout=1.0)
- assert event_type == "message"
-
- # Parse the message
- parsed_message = types.JSONRPCMessage.model_validate_json(data)
- # Check that it's a JSONRPCResponse and verify the ID
- assert isinstance(parsed_message.root, types.JSONRPCResponse)
- assert parsed_message.root.id == 456 # Should be converted to int
-
-
-def test_sse_client_connection_validation():
- """Test SSE client validates endpoint URLs properly."""
- test_url = "http://test.example/sse"
-
- with patch("core.mcp.client.sse_client.create_ssrf_proxy_mcp_http_client") as mock_client_factory:
- with patch("core.mcp.client.sse_client.ssrf_proxy_sse_connect") as mock_sse_connect:
- # Mock the HTTP client
- mock_client = Mock()
- mock_client_factory.return_value.__enter__.return_value = mock_client
-
- # Mock the SSE connection
- mock_event_source = Mock()
- mock_event_source.response.raise_for_status.return_value = None
- mock_sse_connect.return_value.__enter__.return_value = mock_event_source
-
- # Mock SSE events
- class MockSSEEvent:
- def __init__(self, event_type: str, data: str):
- self.event = event_type
- self.data = data
-
- # Simulate endpoint event
- endpoint_event = MockSSEEvent("endpoint", "/messages/?session_id=test-123")
- mock_event_source.iter_sse.return_value = [endpoint_event]
-
- # Test connection
- with contextlib.suppress(Exception):
- with sse_client(test_url) as (read_queue, write_queue):
- assert read_queue is not None
- assert write_queue is not None
-
-
-def test_sse_client_error_handling():
- """Test SSE client properly handles various error conditions."""
- test_url = "http://test.example/sse"
-
- # Test 401 error handling
- with patch("core.mcp.client.sse_client.create_ssrf_proxy_mcp_http_client") as mock_client_factory:
- with patch("core.mcp.client.sse_client.ssrf_proxy_sse_connect") as mock_sse_connect:
- # Mock 401 HTTP error
- mock_response = Mock(status_code=401)
- mock_response.headers = {"WWW-Authenticate": 'Bearer realm="example"'}
- mock_error = httpx.HTTPStatusError("Unauthorized", request=Mock(), response=mock_response)
- mock_sse_connect.side_effect = mock_error
-
- with pytest.raises(MCPAuthError):
- with sse_client(test_url):
- pass
-
- # Test other HTTP errors
- with patch("core.mcp.client.sse_client.create_ssrf_proxy_mcp_http_client") as mock_client_factory:
- with patch("core.mcp.client.sse_client.ssrf_proxy_sse_connect") as mock_sse_connect:
- # Mock other HTTP error
- mock_response = Mock(status_code=500)
- mock_response.headers = {}
- mock_error = httpx.HTTPStatusError("Server Error", request=Mock(), response=mock_response)
- mock_sse_connect.side_effect = mock_error
-
- with pytest.raises(MCPConnectionError):
- with sse_client(test_url):
- pass
+def test_sse_client_message_id_handling(install_sse_response):
+ """Parse numeric string IDs from actual SSE response bytes."""
+ message_data = {"jsonrpc": "2.0", "id": "456", "result": {"test": "data"}}
+ install_sse_response(
+ httpx.Response(
+ 200,
+ headers={"content-type": "text/event-stream"},
+ text=SSE_ENDPOINT + f"event: message\ndata: {json.dumps(message_data)}\n\n",
+ )
+ )
+ with sse_client("http://test.example/sse") as (read_queue, _write_queue):
+ message = read_queue.get(timeout=1)
+ assert isinstance(message, types.SessionMessage)
+ assert isinstance(message.message.root, types.JSONRPCResponse)
+ assert message.message.root.id == 456
+
+
+def test_sse_client_connection_validation(install_sse_response):
+ """Accept a same-origin endpoint parsed by the real SSE reader."""
+ requests = install_sse_response(
+ httpx.Response(200, headers={"content-type": "text/event-stream"}, text=SSE_ENDPOINT)
+ )
+ with sse_client("http://test.example/sse") as (read_queue, write_queue):
+ assert isinstance(read_queue, queue.Queue)
+ assert isinstance(write_queue, queue.Queue)
+ assert len(requests) == 1
+ assert str(requests[0].url) == "http://test.example/sse"
+
+
+def test_sse_client_error_handling(install_sse_response):
+ """Translate real HTTP status errors to the MCP error contract."""
+ for status, error in ((401, MCPAuthError), (500, MCPConnectionError)):
+ install_sse_response(httpx.Response(status, headers={"WWW-Authenticate": 'Bearer realm="example"'}))
+ with pytest.raises(error):
+ with sse_client("http://test.example/sse"):
+ pytest.fail("An HTTP error must prevent connection establishment")
@pytest.mark.parametrize(
@@ -226,36 +155,18 @@ def test_sse_client_wraps_transport_errors(transport_error: httpx.RequestError):
assert str(transport_error) in str(exc_info.value)
-def test_sse_client_timeout_configuration():
- """Test SSE client timeout configuration."""
- test_url = "http://test.example/sse"
- custom_timeout = 10.0
- custom_sse_timeout = 300.0
- custom_headers = {"Authorization": "Bearer test-token"}
-
- with patch("core.mcp.client.sse_client.create_ssrf_proxy_mcp_http_client") as mock_client_factory:
- with patch("core.mcp.client.sse_client.ssrf_proxy_sse_connect") as mock_sse_connect:
- # Mock successful connection
- mock_client = Mock()
- mock_client_factory.return_value.__enter__.return_value = mock_client
-
- mock_event_source = Mock()
- mock_event_source.response.raise_for_status.return_value = None
- mock_event_source.iter_sse.return_value = []
- mock_sse_connect.return_value.__enter__.return_value = mock_event_source
-
- with contextlib.suppress(Exception):
- with sse_client(
- test_url, headers=custom_headers, timeout=custom_timeout, sse_read_timeout=custom_sse_timeout
- ) as (read_queue, write_queue):
- # Verify the configuration was passed correctly
- mock_client_factory.assert_called_with(headers=custom_headers)
-
- # Check that timeout was configured
- call_args = mock_sse_connect.call_args
- assert call_args is not None
- timeout_arg = call_args[1]["timeout"]
- assert timeout_arg.read == custom_sse_timeout
+def test_sse_client_timeout_configuration(install_sse_response):
+ """Pass the configured timeouts through the actual HTTP request."""
+ requests = install_sse_response(
+ httpx.Response(200, headers={"content-type": "text/event-stream"}, text=SSE_ENDPOINT)
+ )
+ with sse_client(
+ "http://test.example/sse", headers={"Authorization": "Bearer test-token"}, timeout=10.0, sse_read_timeout=300.0
+ ):
+ assert len(requests) == 1
+ assert requests[0].headers["Authorization"] == "Bearer test-token"
+ assert requests[0].extensions["timeout"]["read"] == 300.0
+ assert requests[0].extensions["timeout"]["connect"] == 10.0
def test_sse_transport_endpoint_validation():
@@ -302,54 +213,44 @@ def test_sse_transport_message_parsing():
assert isinstance(error, Exception)
-def test_sse_client_queue_cleanup():
- """Test that SSE client properly cleans up queues on exit."""
- test_url = "http://test.example/sse"
-
- read_queue = None
- write_queue = None
-
- with patch("core.mcp.client.sse_client.create_ssrf_proxy_mcp_http_client") as mock_client_factory:
- with patch("core.mcp.client.sse_client.ssrf_proxy_sse_connect") as mock_sse_connect:
- # Mock connection that raises an exception
- mock_sse_connect.side_effect = Exception("Connection failed")
-
- with contextlib.suppress(Exception):
- with sse_client(test_url) as (rq, wq):
- read_queue = rq
- write_queue = wq
-
- # Queues should be cleaned up even on exception
- # Note: In real implementation, cleanup should put None to signal shutdown
-
+def test_sse_client_queue_cleanup(install_sse_response, monkeypatch: pytest.MonkeyPatch):
+ """Closing a connected client signals shutdown on both queues."""
+ from core.mcp.client.sse_client import SSETransport
-def test_sse_client_headers_propagation():
- """Test that custom headers are properly propagated in SSE client."""
- test_url = "http://test.example/sse"
- custom_headers = {
+ writer_finished = threading.Event()
+ original_writer = SSETransport.post_writer
+
+ def post_writer(self: SSETransport, client: httpx.Client, endpoint: str, messages: queue.Queue) -> None:
+ try:
+ original_writer(self, client, endpoint, messages)
+ finally:
+ writer_finished.set()
+
+ monkeypatch.setattr(SSETransport, "post_writer", post_writer)
+ install_sse_response(httpx.Response(200, headers={"content-type": "text/event-stream"}, text=SSE_ENDPOINT))
+ with sse_client("http://test.example/sse") as (read_queue, write_queue):
+ # The finite SSE body first terminates the reader itself.
+ assert read_queue.get(timeout=1) is None
+ assert read_queue.get(timeout=1) is None
+ # The writer consumes shutdown and enqueues its own final sentinel.
+ assert writer_finished.wait(timeout=1)
+ assert write_queue.get(timeout=1) is None
+
+
+def test_sse_client_headers_propagation(install_sse_response):
+ """Custom headers reach the real SSE GET request."""
+ headers = {
"Authorization": "Bearer test-token",
"X-Custom-Header": "test-value",
"User-Agent": "test-client/1.0",
}
-
- with patch("core.mcp.client.sse_client.create_ssrf_proxy_mcp_http_client") as mock_client_factory:
- with patch("core.mcp.client.sse_client.ssrf_proxy_sse_connect") as mock_sse_connect:
- # Mock the client factory to capture headers
- mock_client = Mock()
- mock_client_factory.return_value.__enter__.return_value = mock_client
-
- # Mock the SSE connection
- mock_event_source = Mock()
- mock_event_source.response.raise_for_status.return_value = None
- mock_event_source.iter_sse.return_value = []
- mock_sse_connect.return_value.__enter__.return_value = mock_event_source
-
- with contextlib.suppress(Exception):
- with sse_client(test_url, headers=custom_headers):
- pass
-
- # Verify headers were passed to client factory
- mock_client_factory.assert_called_with(headers=custom_headers)
+ requests = install_sse_response(
+ httpx.Response(200, headers={"content-type": "text/event-stream"}, text=SSE_ENDPOINT)
+ )
+ with sse_client("http://test.example/sse", headers=headers):
+ assert len(requests) == 1
+ for key, value in headers.items():
+ assert requests[0].headers[key] == value
def test_sse_client_concurrent_access():
@@ -498,8 +399,12 @@ class TestSSEReader:
read_queue: queue.Queue = queue.Queue()
status_queue: queue.Queue = queue.Queue()
- event_source = Mock()
- event_source.iter_sse.side_effect = httpx.ReadError("connection reset")
+ def fail() -> bytes:
+ raise httpx.ReadError("connection reset")
+
+ event_source = EventSource(
+ httpx.Response(200, headers={"content-type": "text/event-stream"}, content=iter(fail, b""))
+ )
transport.sse_reader(event_source, read_queue, status_queue)
@@ -515,8 +420,13 @@ class TestSSEReader:
status_queue: queue.Queue = queue.Queue()
boom = RuntimeError("unexpected!")
- event_source = Mock()
- event_source.iter_sse.side_effect = boom
+
+ def fail() -> bytes:
+ raise boom
+
+ event_source = EventSource(
+ httpx.Response(200, headers={"content-type": "text/event-stream"}, content=iter(fail, b""))
+ )
transport.sse_reader(event_source, read_queue, status_queue)
@@ -699,33 +609,29 @@ class TestWaitForEndpoint:
class TestSSEClientRuntimeError:
"""Test sse_client context manager handles RuntimeError on close()."""
- def test_runtime_error_on_close_is_suppressed(self):
- """Ensure RuntimeError raised by event_source.response.close() is caught."""
- test_url = "http://test.example/sse"
-
- class MockSSEEvent:
- def __init__(self, event_type: str, data: str):
- self.event = event_type
- self.data = data
-
- endpoint_event = MockSSEEvent("endpoint", "/messages/?session_id=test-123")
-
- with patch("core.mcp.client.sse_client.create_ssrf_proxy_mcp_http_client") as mock_cf:
- with patch("core.mcp.client.sse_client.ssrf_proxy_sse_connect") as mock_sc:
- mock_client = Mock()
- mock_cf.return_value.__enter__.return_value = mock_client
-
- mock_es = Mock()
- mock_es.response.raise_for_status.return_value = None
- mock_es.iter_sse.return_value = [endpoint_event]
- # Make close() raise RuntimeError to exercise line 307-308
- mock_es.response.close.side_effect = RuntimeError("already closed")
- mock_sc.return_value.__enter__.return_value = mock_es
-
- # Should NOT raise even though close() raises RuntimeError
- with contextlib.suppress(Exception):
- with sse_client(test_url) as (rq, wq):
- pass
+ def test_runtime_error_on_close_is_suppressed(self, install_sse_response, monkeypatch: pytest.MonkeyPatch):
+ """Suppress only RuntimeError from response.close during final cleanup."""
+ response = httpx.Response(
+ 200,
+ headers={"content-type": "text/event-stream"},
+ text=SSE_ENDPOINT,
+ request=httpx.Request("GET", "http://test.example/sse"),
+ )
+ install_sse_response(response)
+ closed: list[bool] = []
+
+ def close() -> None:
+ closed.append(True)
+ raise RuntimeError("already closed")
+
+ monkeypatch.setattr(response, "close", close)
+ monkeypatch.setattr(
+ "core.mcp.client.sse_client.ssrf_proxy_sse_connect",
+ lambda *_args, **_kwargs: contextlib.nullcontext(EventSource(response)),
+ )
+ with sse_client("http://test.example/sse") as (read_queue, _write_queue):
+ assert read_queue.get(timeout=1) is None
+ assert closed == [True]
class TestStandaloneSendMessage: