github.com/weaveworks/common@v0.0.0-20230728070032-dd9e68f319d5/sanitize/sanitize_test.go (about) 1 package sanitize_test 2 3 import ( 4 "testing" 5 6 "github.com/weaveworks/common/sanitize" 7 ) 8 9 func TestSanitizeURL(t *testing.T) { 10 for _, input := range []struct { 11 scheme string 12 port int 13 path string 14 input string 15 want string 16 }{ 17 {"", 0, "", "", ""}, 18 {"", 0, "", "foo", "http://foo"}, 19 {"", 80, "", "foo", "http://foo:80"}, 20 {"", 0, "some/path", "foo", "http://foo/some/path"}, 21 {"", 0, "/some/path", "foo", "http://foo/some/path"}, 22 {"https://", 0, "", "foo", "https://foo"}, 23 {"https://", 80, "", "foo", "https://foo:80"}, 24 {"https://", 0, "some/path", "foo", "https://foo/some/path"}, 25 {"https://", 0, "", "http://foo", "http://foo"}, // specified scheme beats default... 26 {"", 0, "", "https://foo", "https://foo"}, // https can be a specified scheme without default... 27 {"http://", 0, "", "https://foo", "https://foo"}, // https can be a specified scheme with default... 28 {"", 9999, "", "foo:80", "http://foo:80"}, // specified port beats default... 29 {"", 0, "/bar", "foo/baz", "http://foo/bar"}, // ...but default path beats specified! 30 {"", 0, "", "foo:443", "https://foo:443"}, // port 443 addrs default to https scheme 31 } { 32 if want, have := input.want, sanitize.URL(input.scheme, input.port, input.path)(input.input); want != have { 33 t.Errorf("sanitize.URL(%q, %d, %q)(%q): want %q, have %q", input.scheme, input.port, input.path, input.input, want, have) 34 continue 35 } 36 } 37 }