mcp-forge/tests/mcp/test_http_transport.py

115 lines
3.3 KiB
Python
Raw Normal View History

"""Tests for HTTP and SSE transport support in MCP client."""
import pytest
from mcp_forge.mcp.client import MCPClientWrapper
def test_stdio_transport_creation():
"""Test creating a client with stdio transport."""
client = MCPClientWrapper(
name="test_stdio",
transport_type="stdio",
command="python",
args=["-m", "server"],
env={"KEY": "value"}
)
assert client.name == "test_stdio"
assert client.transport_type == "stdio"
assert client.command == "python"
assert client.args == ["-m", "server"]
assert client.env == {"KEY": "value"}
assert client.transport is not None
def test_http_transport_creation():
"""Test creating a client with HTTP transport."""
client = MCPClientWrapper(
name="test_http",
transport_type="http",
url="http://localhost:8006/mcp",
headers={"Authorization": "Bearer token123"}
)
assert client.name == "test_http"
assert client.transport_type == "http"
assert client.url == "http://localhost:8006/mcp"
assert client.headers == {"Authorization": "Bearer token123"}
assert client.transport is not None
def test_sse_transport_creation():
"""Test creating a client with SSE transport."""
client = MCPClientWrapper(
name="test_sse",
transport_type="sse",
url="http://localhost:9000/events",
headers={"X-Custom": "value"}
)
assert client.name == "test_sse"
assert client.transport_type == "sse"
assert client.url == "http://localhost:9000/events"
assert client.headers == {"X-Custom": "value"}
assert client.transport is not None
def test_stdio_without_command_raises_error():
"""Test that stdio transport requires a command."""
with pytest.raises(ValueError, match="command required for stdio transport"):
MCPClientWrapper(
name="test_stdio",
transport_type="stdio"
)
def test_http_without_url_raises_error():
"""Test that HTTP transport requires a URL."""
with pytest.raises(ValueError, match="url required for http transport"):
MCPClientWrapper(
name="test_http",
transport_type="http"
)
def test_sse_without_url_raises_error():
"""Test that SSE transport requires a URL."""
with pytest.raises(ValueError, match="url required for sse transport"):
MCPClientWrapper(
name="test_sse",
transport_type="sse"
)
def test_invalid_transport_type_raises_error():
"""Test that an invalid transport type raises an error."""
with pytest.raises(ValueError, match="Unknown transport type"):
MCPClientWrapper(
name="test_invalid",
transport_type="invalid"
)
def test_http_transport_with_empty_headers():
"""Test HTTP transport with empty headers dict."""
client = MCPClientWrapper(
name="test_http",
transport_type="http",
url="http://localhost:8006/mcp"
)
assert client.headers == {}
assert client.transport is not None
def test_default_stdio_transport():
"""Test that stdio is the default transport type."""
client = MCPClientWrapper(
name="test_default",
command="python",
args=["-m", "server"]
)
assert client.transport_type == "stdio"
assert client.command == "python"