syft/internal/anchore/client_test.go
Dan Luhring b207bc8ee2
Ensure upload base path ends in /v1
Signed-off-by: Dan Luhring <dan.luhring@anchore.com>
2021-02-01 16:59:23 -05:00

110 lines
2.0 KiB
Go

package anchore
import "testing"
func TestHasScheme(t *testing.T) {
cases := []struct {
url string
expected bool
}{
{
url: "http://localhost",
expected: true,
},
{
url: "https://anchore.com:8443",
expected: true,
},
{
url: "google.com",
expected: false,
},
{
url: "",
expected: false,
},
}
for _, testCase := range cases {
t.Run(testCase.url, func(t *testing.T) {
result := hasScheme(testCase.url)
if testCase.expected != result {
t.Errorf("expected %t but got %t", testCase.expected, result)
}
})
}
}
func TestEnsureURLHasScheme(t *testing.T) {
cases := []struct {
url string
expected string
}{
{
url: "http://localhost",
expected: "http://localhost",
},
{
url: "https://anchore.com:8443",
expected: "https://anchore.com:8443",
},
{
url: "google.com:1234/v1/",
expected: "http://google.com:1234/v1/",
},
{
url: "localhost",
expected: "http://localhost",
},
}
for _, testCase := range cases {
t.Run(testCase.url, func(t *testing.T) {
result := ensureURLHasScheme(testCase.url)
if testCase.expected != result {
t.Errorf("expected '%s' but got '%s'", testCase.expected, result)
}
})
}
}
func TestEnsureURLHasSuffix(t *testing.T) {
cases := []struct {
url string
suffix string
expected string
}{
{
url: "http://localhost",
suffix: "/v1",
expected: "http://localhost/v1",
},
{
url: "http://localhost/v1",
suffix: "/v1",
expected: "http://localhost/v1",
},
{
url: "http://localhost/v1/",
suffix: "/v1",
expected: "http://localhost/v1//v1",
},
{
url: "http://localhost-v1",
suffix: "/v1",
expected: "http://localhost-v1/v1",
},
}
for _, testCase := range cases {
t.Run(testCase.url, func(t *testing.T) {
result := ensureURLHasSuffix(testCase.url, testCase.suffix)
if testCase.expected != result {
t.Errorf("expected '%s' but got '%s'", testCase.expected, result)
}
})
}
}