// SPDX-License-Identifier: Apache-2.0 package netutil import "testing" func TestEnsurePort(t *testing.T) { const dp = "9090" cases := []struct { in, want string }{ {"", ""}, {"chbtl2", "chbtl2:9090"}, {"chbtl2:9090", "chbtl2:9090"}, {"chbtl2:1234", "chbtl2:1234"}, {"lb-ams.example.com", "lb-ams.example.com:9090"}, {"lb-ams.example.com:9090", "lb-ams.example.com:9090"}, {"192.0.2.1", "192.0.2.1:9090"}, {"192.0.2.1:9090", "192.0.2.1:9090"}, {"::1", "[::1]:9090"}, {"2001:db8::1", "[2001:db8::1]:9090"}, {"[::1]:9090", "[::1]:9090"}, {"[2001:db8::1]:443", "[2001:db8::1]:443"}, // Already-bracketed but missing port: net.SplitHostPort rejects // it, ParseIP rejects the bracketed textual form, so the plain // concat path adds ":9090" and yields a valid host:port. {"[::1]", "[::1]:9090"}, } for _, tc := range cases { got := EnsurePort(tc.in, dp) if got != tc.want { t.Errorf("EnsurePort(%q) = %q, want %q", tc.in, got, tc.want) } } }