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