@@ -464,3 +464,31 @@ def test_sse_message_id_coercion():
464464 json_message = '{"jsonrpc": "2.0", "id": "123", "method": "ping", "params": null}'
465465 msg = types .JSONRPCMessage .model_validate_json (json_message )
466466 assert msg == snapshot (types .JSONRPCMessage (root = types .JSONRPCRequest (method = "ping" , jsonrpc = "2.0" , id = 123 )))
467+
468+
469+ @pytest .mark .parametrize (
470+ "endpoint, expected_result" ,
471+ [
472+ # Valid endpoints - should normalize and work
473+ ("/messages/" , "/messages/" ),
474+ ("messages/" , "/messages/" ),
475+ ("/" , "/" ),
476+ # Invalid endpoints - should raise ValueError
477+ ("http://example.com/messages/" , ValueError ),
478+ ("//example.com/messages/" , ValueError ),
479+ ("ftp://example.com/messages/" , ValueError ),
480+ ("/messages/?param=value" , ValueError ),
481+ ("/messages/#fragment" , ValueError ),
482+ ],
483+ )
484+ def test_sse_server_transport_endpoint_validation (endpoint : str , expected_result : str | type [Exception ]):
485+ """Test that SseServerTransport properly validates and normalizes endpoints."""
486+ if isinstance (expected_result , type ) and issubclass (expected_result , Exception ):
487+ # Test invalid endpoints that should raise an exception
488+ with pytest .raises (expected_result , match = "is not a relative path.*expecting a relative path" ):
489+ SseServerTransport (endpoint )
490+ else :
491+ # Test valid endpoints that should normalize correctly
492+ sse = SseServerTransport (endpoint )
493+ assert sse ._endpoint == expected_result
494+ assert sse ._endpoint .startswith ("/" )
0 commit comments