34 lines
911 B
Go
34 lines
911 B
Go
package monitor
|
|
|
|
import "testing"
|
|
|
|
func TestHostAndPort(t *testing.T) {
|
|
var cases = []struct {
|
|
target string
|
|
defaultPort string
|
|
wantHost string
|
|
wantPort string
|
|
}{
|
|
{"example.com", "443", "example.com", "443"},
|
|
{"example.com:8443", "443", "example.com", "8443"},
|
|
{"https://example.com/health", "443", "example.com", "443"},
|
|
{"https://example.com:9000/x", "443", "example.com", "9000"},
|
|
{"db.example.com:5432", "", "db.example.com", "5432"},
|
|
{"10.0.0.5:22", "443", "10.0.0.5", "22"},
|
|
}
|
|
var c struct {
|
|
target string
|
|
defaultPort string
|
|
wantHost string
|
|
wantPort string
|
|
}
|
|
var host string
|
|
var port string
|
|
for _, c = range cases {
|
|
host, port = hostAndPort(c.target, c.defaultPort)
|
|
if host != c.wantHost || port != c.wantPort {
|
|
t.Errorf("hostAndPort(%q, %q) = (%q, %q), want (%q, %q)", c.target, c.defaultPort, host, port, c.wantHost, c.wantPort)
|
|
}
|
|
}
|
|
}
|