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