reformat TestDownloadableURL into a table test to allow adding more URLS

This commit is contained in:
Megan Marsh 2018-01-05 11:06:26 -08:00
parent e164621bfe
commit a3d5d40f78
1 changed files with 31 additions and 28 deletions

View File

@ -38,36 +38,39 @@ func TestChooseString(t *testing.T) {
} }
func TestDownloadableURL(t *testing.T) { func TestDownloadableURL(t *testing.T) {
// Invalid URL: has hex code in host
_, err := DownloadableURL("http://what%20.com") cases := []struct {
if err == nil { InputString string
t.Fatal("expected err") OutputURL string
ErrExpected bool
}{
// Invalid URL: has hex code in host
{"http://what%20.com", "", true},
// Valid: http
{"HTTP://packer.io/path", "http://packer.io/path", false},
// No path
{"HTTP://packer.io", "http://packer.io", false},
// Invalid: unsupported scheme
{"ftp://host.com/path", "", true},
} }
// Invalid: unsupported scheme for _, tc := range cases {
_, err = DownloadableURL("ftp://host.com/path") u, err := DownloadableURL(tc.InputString)
if err == nil { if u != tc.OutputURL {
t.Fatal("expected err") t.Fatal(fmt.Sprintf("Error with URL %s: got %s but expected %s",
} tc.InputString, tc.OutputURL, u))
}
// Valid: http if (err != nil) != tc.ErrExpected {
u, err := DownloadableURL("HTTP://packer.io/path") if tc.ErrExpected == true {
if err != nil { t.Fatal(fmt.Sprintf("Error with URL %s: we expected "+
t.Fatalf("err: %s", err) "DownloadableURL to return an error but didn't get one.",
} tc.InputString))
} else {
if u != "http://packer.io/path" { t.Fatal(fmt.Sprintf("Error with URL %s: we did not expect an "+
t.Fatalf("bad: %s", u) " error from DownloadableURL but we got: %s",
} tc.InputString, err))
}
// No path }
u, err = DownloadableURL("HTTP://packer.io")
if err != nil {
t.Fatalf("err: %s", err)
}
if u != "http://packer.io" {
t.Fatalf("bad: %s", u)
} }
} }