@@ -28,10 +28,28 @@ def test_resource_url_from_server_url_preserves_query():
2828 assert resource_url_from_server_url ("https://example.com/?key=value" ) == "https://example.com/?key=value"
2929
3030
31+ def test_resource_url_from_server_url_removes_default_ports ():
32+ """Explicit HTTP and HTTPS default ports should be removed from canonical resource URLs."""
33+ assert resource_url_from_server_url ("http://example.com:80/mcp" ) == "http://example.com/mcp"
34+ assert resource_url_from_server_url ("https://example.com:443/mcp" ) == "https://example.com/mcp"
35+
36+
3137def test_resource_url_from_server_url_preserves_port ():
3238 """Non-default ports should be preserved."""
3339 assert resource_url_from_server_url ("https://example.com:8443/path" ) == "https://example.com:8443/path"
3440 assert resource_url_from_server_url ("http://example.com:8080/" ) == "http://example.com:8080/"
41+ assert resource_url_from_server_url ("ftp://example.com:443/path" ) == "ftp://example.com:443/path"
42+
43+
44+ def test_resource_url_from_server_url_removes_default_port_from_ipv6_literal ():
45+ """Default-port removal should preserve bracketed IPv6 authority syntax."""
46+ assert resource_url_from_server_url ("https://[2001:DB8::1]:443/mcp" ) == "https://[2001:db8::1]/mcp"
47+
48+
49+ def test_resource_url_from_server_url_preserves_malformed_port ():
50+ """Malformed ports should retain the existing canonicalization behavior."""
51+ assert resource_url_from_server_url ("https://example.com:abc/mcp" ) == "https://example.com:abc/mcp"
52+ assert resource_url_from_server_url ("https://:443/mcp" ) == "https://:443/mcp"
3553
3654
3755def test_resource_url_from_server_url_lowercase_scheme_and_host ():
@@ -121,3 +139,9 @@ def test_check_resource_allowed_empty_paths():
121139 assert check_resource_allowed ("https://example.com" , "https://example.com" ) is True
122140 assert check_resource_allowed ("https://example.com/" , "https://example.com" ) is True
123141 assert check_resource_allowed ("https://example.com/api" , "https://example.com" ) is True
142+
143+
144+ def test_check_resource_allowed_accepts_canonicalized_default_port ():
145+ """Canonicalized explicit default ports should match equivalent metadata URLs."""
146+ canonical = resource_url_from_server_url ("https://example.com:443/mcp" )
147+ assert check_resource_allowed (canonical , "https://example.com/mcp" ) is True
0 commit comments