Something went wrong. Try again.
mirror of github.com/PrefectHQ/fastmcp
Something went wrong. Try again.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495import pytestfrom mcp.server.auth.middleware.bearer_auth import RequireAuthMiddlewarefrom starlette.routing import Routefrom starlette.testclient import TestClient
from fastmcp.server import FastMCPfrom fastmcp.server.auth.providers.jwt import JWTVerifier, RSAKeyPairfrom fastmcp.server.http import create_streamable_http_app
class TestStreamableHTTPAppResourceMetadataURL: """Test resource_metadata_url logic in create_streamable_http_app."""
@pytest.fixture def rsa_key_pair(self) -> RSAKeyPair: """Generate RSA key pair for testing.""" return RSAKeyPair.generate()
@pytest.fixture def bearer_auth_provider(self, rsa_key_pair): provider = JWTVerifier( public_key=rsa_key_pair.public_key, issuer="https://issuer", audience="https://audience", base_url="https://resource.example.com", ) return provider
def test_auth_endpoint_wrapped_with_require_auth_middleware( self, bearer_auth_provider ): """Test that auth-protected endpoints use RequireAuthMiddleware.""" server = FastMCP(name="TestServer")
app = create_streamable_http_app( server=server, streamable_http_path="/mcp", auth=bearer_auth_provider, )
route = next(r for r in app.routes if isinstance(r, Route) and r.path == "/mcp")
# When auth is enabled, endpoint should use RequireAuthMiddleware assert isinstance(route.endpoint, RequireAuthMiddleware)
def test_auth_endpoint_has_correct_methods(self, rsa_key_pair): """Test that auth-protected endpoints have correct HTTP methods.""" provider = JWTVerifier( public_key=rsa_key_pair.public_key, issuer="https://issuer", audience="https://audience", base_url="https://resource.example.com/", ) server = FastMCP(name="TestServer") app = create_streamable_http_app( server=server, streamable_http_path="/mcp", auth=provider, ) route = next(r for r in app.routes if isinstance(r, Route) and r.path == "/mcp")
# Verify RequireAuthMiddleware is applied assert isinstance(route.endpoint, RequireAuthMiddleware) # Verify methods include GET, POST, DELETE for streamable-http expected_methods = {"GET", "POST", "DELETE"} assert route.methods is not None assert expected_methods.issubset(set(route.methods))
def test_no_auth_provider_mounts_without_middleware(self, rsa_key_pair): """Test that endpoints without auth are not wrapped with middleware.""" server = FastMCP(name="TestServer") app = create_streamable_http_app( server=server, streamable_http_path="/mcp", auth=None, ) route = next(r for r in app.routes if isinstance(r, Route) and r.path == "/mcp") # Without auth, no RequireAuthMiddleware should be applied assert not isinstance(route.endpoint, RequireAuthMiddleware)
def test_authenticated_requests_still_require_auth(self, bearer_auth_provider): """Test that actual requests (not OPTIONS) still require authentication.""" server = FastMCP(name="TestServer") app = create_streamable_http_app( server=server, streamable_http_path="/mcp", auth=bearer_auth_provider, )
# Test POST request without auth - should fail with 401 with TestClient(app) as client: response = client.post("/mcp") assert response.status_code == 401 assert "www-authenticate" in response.headers