|
| 1 | +# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import pytest |
| 16 | +from datetime import datetime |
| 17 | +from fastmcp.server.middleware import MiddlewareContext |
| 18 | +from mcp import types as mt |
| 19 | +from mcp_proxy_for_aws.context import get_client_info, set_client_info |
| 20 | +from mcp_proxy_for_aws.middleware.client_info import ClientInfoMiddleware |
| 21 | + |
| 22 | + |
| 23 | +@pytest.fixture |
| 24 | +def middleware(): |
| 25 | + """Create a ClientInfoMiddleware instance.""" |
| 26 | + return ClientInfoMiddleware() |
| 27 | + |
| 28 | + |
| 29 | +@pytest.fixture |
| 30 | +def mock_context_with_client_info(): |
| 31 | + """Create a mock context with client_info.""" |
| 32 | + params = mt.InitializeRequestParams( |
| 33 | + protocolVersion='2024-11-05', |
| 34 | + capabilities=mt.ClientCapabilities(), |
| 35 | + clientInfo=mt.Implementation(name='test-client', version='1.0.0'), |
| 36 | + ) |
| 37 | + message = mt.InitializeRequest( |
| 38 | + method='initialize', |
| 39 | + params=params, |
| 40 | + ) |
| 41 | + return MiddlewareContext( |
| 42 | + message=message, |
| 43 | + fastmcp_context=None, |
| 44 | + source='client', |
| 45 | + type='request', |
| 46 | + method='initialize', |
| 47 | + timestamp=datetime.now(), |
| 48 | + ) |
| 49 | + |
| 50 | + |
| 51 | +@pytest.mark.asyncio |
| 52 | +async def test_captures_client_info(middleware, mock_context_with_client_info): |
| 53 | + """Test that middleware captures client_info from initialize request.""" |
| 54 | + # Reset context variable |
| 55 | + set_client_info(None) |
| 56 | + |
| 57 | + async def call_next(ctx): |
| 58 | + pass |
| 59 | + |
| 60 | + await middleware.on_initialize(mock_context_with_client_info, call_next) |
| 61 | + |
| 62 | + # Verify client_info was captured |
| 63 | + info = get_client_info() |
| 64 | + assert info is not None |
| 65 | + assert info.name == 'test-client' |
| 66 | + assert info.version == '1.0.0' |
| 67 | + |
| 68 | + |
| 69 | +@pytest.mark.asyncio |
| 70 | +async def test_calls_next_middleware(middleware, mock_context_with_client_info): |
| 71 | + """Test that middleware calls the next middleware in chain.""" |
| 72 | + called = False |
| 73 | + |
| 74 | + async def call_next(ctx): |
| 75 | + nonlocal called |
| 76 | + called = True |
| 77 | + |
| 78 | + await middleware.on_initialize(mock_context_with_client_info, call_next) |
| 79 | + |
| 80 | + assert called is True |
| 81 | + |
| 82 | + |
| 83 | +@pytest.mark.asyncio |
| 84 | +async def test_captures_different_client_info(middleware): |
| 85 | + """Test that middleware captures different client_info values.""" |
| 86 | + # Reset context variable |
| 87 | + set_client_info(None) |
| 88 | + |
| 89 | + params = mt.InitializeRequestParams( |
| 90 | + protocolVersion='2024-11-05', |
| 91 | + capabilities=mt.ClientCapabilities(), |
| 92 | + clientInfo=mt.Implementation(name='another-client', version='2.5.3'), |
| 93 | + ) |
| 94 | + message = mt.InitializeRequest( |
| 95 | + method='initialize', |
| 96 | + params=params, |
| 97 | + ) |
| 98 | + context = MiddlewareContext( |
| 99 | + message=message, |
| 100 | + fastmcp_context=None, |
| 101 | + source='client', |
| 102 | + type='request', |
| 103 | + method='initialize', |
| 104 | + timestamp=datetime.now(), |
| 105 | + ) |
| 106 | + |
| 107 | + async def call_next(ctx): |
| 108 | + pass |
| 109 | + |
| 110 | + await middleware.on_initialize(context, call_next) |
| 111 | + |
| 112 | + # Verify client_info was captured with correct values |
| 113 | + info = get_client_info() |
| 114 | + assert info is not None |
| 115 | + assert info.name == 'another-client' |
| 116 | + assert info.version == '2.5.3' |
0 commit comments