Spaces:
Running
Running
File size: 29,133 Bytes
8c5bdf2 151d030 8c5bdf2 151d030 8c5bdf2 151d030 8c5bdf2 151d030 8c5bdf2 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 | """Comprehensive tests for OpenAPI new implementation."""
import json
from unittest.mock import AsyncMock, Mock
import httpx
import pytest
from httpx import Response
from fastmcp.client import Client
from fastmcp.experimental.server.openapi import FastMCPOpenAPI
class TestOpenAPIComprehensive:
"""Comprehensive tests ensuring no functionality is lost."""
@pytest.fixture
def comprehensive_openapi_spec(self):
"""Comprehensive OpenAPI spec covering all major features."""
return {
"openapi": "3.0.0",
"info": {"title": "Comprehensive API", "version": "1.0.0"},
"servers": [{"url": "https://api.example.com"}],
"components": {
"schemas": {
"User": {
"type": "object",
"properties": {
"id": {"type": "integer"},
"name": {"type": "string"},
"email": {"type": "string", "format": "email"},
"age": {"type": "integer", "minimum": 0},
},
"required": ["name", "email"],
},
"Error": {
"type": "object",
"properties": {
"code": {"type": "integer"},
"message": {"type": "string"},
},
},
},
"parameters": {
"UserId": {
"name": "id",
"in": "path",
"required": True,
"schema": {"type": "integer"},
"description": "User identifier",
}
},
},
"paths": {
# Basic CRUD operations
"/users": {
"get": {
"operationId": "list_users",
"summary": "List all users",
"parameters": [
{
"name": "limit",
"in": "query",
"schema": {
"type": "integer",
"default": 10,
"minimum": 1,
"maximum": 100,
},
"description": "Number of users to return",
},
{
"name": "offset",
"in": "query",
"schema": {
"type": "integer",
"default": 0,
"minimum": 0,
},
"description": "Number of users to skip",
},
{
"name": "sort",
"in": "query",
"schema": {
"type": "string",
"enum": ["name", "email", "age"],
},
"description": "Sort field",
},
],
"responses": {
"200": {
"description": "List of users",
"content": {
"application/json": {
"schema": {
"type": "array",
"items": {
"$ref": "#/components/schemas/User"
},
}
}
},
}
},
},
"post": {
"operationId": "create_user",
"summary": "Create a new user",
"requestBody": {
"required": True,
"content": {
"application/json": {
"schema": {"$ref": "#/components/schemas/User"}
}
},
},
"responses": {
"201": {
"description": "User created",
"content": {
"application/json": {
"schema": {"$ref": "#/components/schemas/User"}
}
},
},
"400": {
"description": "Invalid input",
"content": {
"application/json": {
"schema": {"$ref": "#/components/schemas/Error"}
}
},
},
},
},
},
"/users/{id}": {
"parameters": [{"$ref": "#/components/parameters/UserId"}],
"get": {
"operationId": "get_user",
"summary": "Get user by ID",
"responses": {
"200": {
"description": "User details",
"content": {
"application/json": {
"schema": {"$ref": "#/components/schemas/User"}
}
},
},
"404": {
"description": "User not found",
"content": {
"application/json": {
"schema": {"$ref": "#/components/schemas/Error"}
}
},
},
},
},
"put": {
"operationId": "update_user",
"summary": "Update user",
"requestBody": {
"required": True,
"content": {
"application/json": {
"schema": {"$ref": "#/components/schemas/User"}
}
},
},
"responses": {
"200": {
"description": "User updated",
"content": {
"application/json": {
"schema": {"$ref": "#/components/schemas/User"}
}
},
},
},
},
"delete": {
"operationId": "delete_user",
"summary": "Delete user",
"responses": {
"204": {"description": "User deleted"},
"404": {
"description": "User not found",
"content": {
"application/json": {
"schema": {"$ref": "#/components/schemas/Error"}
}
},
},
},
},
},
# Complex parameter scenarios
"/search": {
"get": {
"operationId": "search_users",
"summary": "Search users with complex filters",
"parameters": [
{
"name": "q",
"in": "query",
"required": True,
"schema": {"type": "string"},
"description": "Search query",
},
{
"name": "filter",
"in": "query",
"style": "deepObject",
"explode": True,
"schema": {
"type": "object",
"properties": {
"age": {
"type": "object",
"properties": {
"min": {"type": "integer"},
"max": {"type": "integer"},
},
},
"name": {"type": "string"},
"active": {"type": "boolean"},
},
},
},
{
"name": "X-Request-ID",
"in": "header",
"schema": {"type": "string"},
"description": "Request identifier for tracing",
},
],
"responses": {
"200": {
"description": "Search results",
"content": {
"application/json": {
"schema": {
"type": "object",
"properties": {
"results": {
"type": "array",
"items": {
"$ref": "#/components/schemas/User"
},
},
"total": {"type": "integer"},
"page": {"type": "integer"},
},
}
}
},
}
},
}
},
# Parameter collision scenario
"/collision/{id}": {
"patch": {
"operationId": "collision_test",
"summary": "Test parameter collision handling",
"parameters": [
{
"name": "id",
"in": "path",
"required": True,
"schema": {"type": "string"},
"description": "Resource ID",
},
{
"name": "version",
"in": "query",
"schema": {"type": "integer", "default": 1},
},
{
"name": "version",
"in": "header",
"schema": {"type": "string"},
},
],
"requestBody": {
"required": True,
"content": {
"application/json": {
"schema": {
"type": "object",
"properties": {
"id": {
"type": "integer",
"description": "Internal ID",
},
"version": {
"type": "string",
"description": "Data version",
},
"data": {"type": "object"},
},
}
}
},
},
"responses": {"200": {"description": "Updated"}},
}
},
},
}
@pytest.fixture
def openapi_31_spec(self):
"""OpenAPI 3.1 spec to test compatibility."""
return {
"openapi": "3.1.0",
"info": {"title": "OpenAPI 3.1 Test", "version": "1.0.0"},
"paths": {
"/items/{id}": {
"get": {
"operationId": "get_item_31",
"parameters": [
{
"name": "id",
"in": "path",
"required": True,
"schema": {"type": "string"},
}
],
"responses": {
"200": {
"description": "Item details",
"content": {
"application/json": {
"schema": {
"type": "object",
"properties": {
"id": {"type": "string"},
"name": {"type": "string"},
},
}
}
},
}
},
}
}
},
}
@pytest.mark.asyncio
async def test_comprehensive_server_initialization(
self, comprehensive_openapi_spec
):
"""Test server initialization with comprehensive spec."""
async with httpx.AsyncClient(base_url="https://api.example.com") as client:
server = FastMCPOpenAPI(
openapi_spec=comprehensive_openapi_spec,
client=client,
name="Comprehensive Test Server",
)
# Should initialize successfully
assert server.name == "Comprehensive Test Server"
assert hasattr(server, "_director")
assert hasattr(server, "_spec")
# Test with in-memory client
async with Client(server) as mcp_client:
tools = await mcp_client.list_tools()
# Should have created tools for all operations
tool_names = {tool.name for tool in tools}
expected_operations = {
"list_users",
"create_user",
"get_user",
"update_user",
"delete_user",
"search_users",
"collision_test",
}
assert tool_names == expected_operations
@pytest.mark.asyncio
async def test_openapi_31_compatibility(self, openapi_31_spec):
"""Test that OpenAPI 3.1 specs work correctly."""
async with httpx.AsyncClient(base_url="https://api.example.com") as client:
server = FastMCPOpenAPI(
openapi_spec=openapi_31_spec,
client=client,
name="OpenAPI 3.1 Test",
)
async with Client(server) as mcp_client:
tools = await mcp_client.list_tools()
assert len(tools) == 1
tool = tools[0]
assert tool.name == "get_item_31"
@pytest.mark.asyncio
async def test_parameter_collision_handling(self, comprehensive_openapi_spec):
"""Test that parameter collisions are handled correctly."""
async with httpx.AsyncClient(base_url="https://api.example.com") as client:
server = FastMCPOpenAPI(
openapi_spec=comprehensive_openapi_spec,
client=client,
)
async with Client(server) as mcp_client:
tools = await mcp_client.list_tools()
collision_tool = next(
tool for tool in tools if tool.name == "collision_test"
)
schema = collision_tool.inputSchema
properties = schema["properties"]
# Should have unique parameter names for colliding parameters
param_names = list(properties.keys())
# Should have some form of id parameters (path and body)
id_params = [name for name in param_names if "id" in name]
assert len(id_params) >= 2
# Should have some form of version parameters (query, header, body)
version_params = [name for name in param_names if "version" in name]
assert len(version_params) >= 3
# Should have other parameters
assert "data" in param_names
@pytest.mark.asyncio
async def test_deep_object_parameters(self, comprehensive_openapi_spec):
"""Test deepObject parameter handling."""
async with httpx.AsyncClient(base_url="https://api.example.com") as client:
server = FastMCPOpenAPI(
openapi_spec=comprehensive_openapi_spec,
client=client,
)
async with Client(server) as mcp_client:
tools = await mcp_client.list_tools()
search_tool = next(
tool for tool in tools if tool.name == "search_users"
)
schema = search_tool.inputSchema
properties = schema["properties"]
# Should have flattened deepObject parameters
# The exact flattening depends on implementation
assert "q" in properties # Regular query parameter
# Should have some form of filter parameters
filter_params = [name for name in properties.keys() if "filter" in name]
assert len(filter_params) > 0
@pytest.mark.asyncio
async def test_request_building_and_execution(self, comprehensive_openapi_spec):
"""Test that requests are built and executed correctly."""
# Create a mock client that tracks requests
mock_client = Mock(spec=httpx.AsyncClient)
mock_client.base_url = "https://api.example.com"
mock_client.headers = None
# Mock successful response
mock_response = Mock(spec=Response)
mock_response.status_code = 200
mock_response.json.return_value = {
"id": 123,
"name": "Test User",
"email": "test@example.com",
}
mock_response.text = json.dumps(
{"id": 123, "name": "Test User", "email": "test@example.com"}
)
mock_response.raise_for_status = Mock()
mock_client.send = AsyncMock(return_value=mock_response)
server = FastMCPOpenAPI(
openapi_spec=comprehensive_openapi_spec,
client=mock_client,
)
async with Client(server) as mcp_client:
# Test GET request with path parameter
await mcp_client.call_tool("get_user", {"id": 123})
# Should have made a request
mock_client.send.assert_called_once()
request = mock_client.send.call_args[0][0]
# Verify request details
assert request.method == "GET"
assert "123" in str(request.url)
assert "users/123" in str(request.url)
@pytest.mark.asyncio
async def test_complex_request_with_body_and_parameters(
self, comprehensive_openapi_spec
):
"""Test complex request with both parameters and body."""
mock_client = Mock(spec=httpx.AsyncClient)
mock_client.base_url = "https://api.example.com"
mock_client.headers = None
mock_response = Mock(spec=Response)
mock_response.status_code = 201
mock_response.json.return_value = {
"id": 456,
"name": "New User",
"email": "new@example.com",
}
mock_response.raise_for_status = Mock()
mock_client.send = AsyncMock(return_value=mock_response)
server = FastMCPOpenAPI(
openapi_spec=comprehensive_openapi_spec,
client=mock_client,
)
async with Client(server) as mcp_client:
# Test POST request with body
await mcp_client.call_tool(
"create_user",
{
"name": "New User",
"email": "new@example.com",
"age": 25,
},
)
# Should have made a request
mock_client.send.assert_called_once()
request = mock_client.send.call_args[0][0]
# Verify request details
assert request.method == "POST"
assert "users" in str(request.url)
# Should have JSON body
assert request.content is not None
body_data = json.loads(request.content)
assert body_data["name"] == "New User"
assert body_data["email"] == "new@example.com"
assert body_data["age"] == 25
@pytest.mark.asyncio
async def test_query_parameters(self, comprehensive_openapi_spec):
"""Test query parameter handling."""
mock_client = Mock(spec=httpx.AsyncClient)
mock_client.base_url = "https://api.example.com"
mock_client.headers = None
mock_response = Mock(spec=Response)
mock_response.status_code = 200
mock_response.json.return_value = []
mock_response.raise_for_status = Mock()
mock_client.send = AsyncMock(return_value=mock_response)
server = FastMCPOpenAPI(
openapi_spec=comprehensive_openapi_spec,
client=mock_client,
)
async with Client(server) as mcp_client:
# Test GET request with query parameters
await mcp_client.call_tool(
"list_users",
{
"limit": 20,
"offset": 10,
"sort": "name",
},
)
mock_client.send.assert_called_once()
request = mock_client.send.call_args[0][0]
# Verify query parameters in URL
url_str = str(request.url)
assert "limit=20" in url_str
assert "offset=10" in url_str
assert "sort=name" in url_str
@pytest.mark.asyncio
async def test_error_handling(self, comprehensive_openapi_spec):
"""Test error handling for HTTP errors."""
mock_client = Mock(spec=httpx.AsyncClient)
mock_client.base_url = "https://api.example.com"
mock_client.headers = None
# Mock HTTP error response
mock_response = Mock(spec=Response)
mock_response.status_code = 404
mock_response.reason_phrase = "Not Found"
mock_response.json.return_value = {"code": 404, "message": "User not found"}
mock_response.text = json.dumps({"code": 404, "message": "User not found"})
# Configure raise_for_status to raise HTTPStatusError
def raise_for_status():
raise httpx.HTTPStatusError(
"404 Not Found", request=Mock(), response=mock_response
)
mock_response.raise_for_status = raise_for_status
mock_client.send = AsyncMock(return_value=mock_response)
server = FastMCPOpenAPI(
openapi_spec=comprehensive_openapi_spec,
client=mock_client,
)
async with Client(server) as mcp_client:
# Should handle HTTP errors gracefully
with pytest.raises(Exception) as exc_info:
await mcp_client.call_tool("get_user", {"id": 999})
# Error should be wrapped appropriately
error_message = str(exc_info.value)
assert "404" in error_message
@pytest.mark.asyncio
async def test_schema_refs_resolution(self, comprehensive_openapi_spec):
"""Test that schema references are resolved correctly."""
async with httpx.AsyncClient(base_url="https://api.example.com") as client:
server = FastMCPOpenAPI(
openapi_spec=comprehensive_openapi_spec,
client=client,
)
async with Client(server) as mcp_client:
tools = await mcp_client.list_tools()
# Find create_user tool which uses schema refs
create_tool = next(tool for tool in tools if tool.name == "create_user")
schema = create_tool.inputSchema
properties = schema["properties"]
# Should have resolved User schema properties
assert "name" in properties
assert "email" in properties
# May also have id and age depending on implementation
@pytest.mark.asyncio
async def test_optional_vs_required_parameters(self, comprehensive_openapi_spec):
"""Test handling of optional vs required parameters."""
async with httpx.AsyncClient(base_url="https://api.example.com") as client:
server = FastMCPOpenAPI(
openapi_spec=comprehensive_openapi_spec,
client=client,
)
async with Client(server) as mcp_client:
tools = await mcp_client.list_tools()
# Check list_users tool - has optional query parameters
list_tool = next(tool for tool in tools if tool.name == "list_users")
schema = list_tool.inputSchema
# Query parameters should be optional
# (may not appear in required list)
# This test just ensures the schema is well-formed
assert "properties" in schema
# Check search_users tool - has required query parameter
search_tool = next(
tool for tool in tools if tool.name == "search_users"
)
search_schema = search_tool.inputSchema
# Should have some required parameters
assert len(search_schema["properties"]) > 0
@pytest.mark.asyncio
async def test_server_performance_no_latency(self, comprehensive_openapi_spec):
"""Test that server initialization is fast (no code generation latency)."""
import time
# Time the server creation
start_time = time.time()
async with httpx.AsyncClient(base_url="https://api.example.com") as client:
server = FastMCPOpenAPI(
openapi_spec=comprehensive_openapi_spec,
client=client,
)
end_time = time.time()
# Should be very fast (no code generation)
initialization_time = end_time - start_time
assert initialization_time < 0.1 # Should be under 100ms
# Verify server was created correctly
assert server is not None
assert hasattr(server, "_director")
assert hasattr(server, "_spec")
|