From 5b5fa7ec90831b0cc7323fbd9dcef18791b28d6d Mon Sep 17 00:00:00 2001 From: Dan Luhring Date: Mon, 1 Feb 2021 13:57:40 -0500 Subject: [PATCH] Add tests for Anchore client URL intake Signed-off-by: Dan Luhring --- internal/anchore/client_test.go | 71 +++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 internal/anchore/client_test.go diff --git a/internal/anchore/client_test.go b/internal/anchore/client_test.go new file mode 100644 index 000000000..cf3d71b25 --- /dev/null +++ b/internal/anchore/client_test.go @@ -0,0 +1,71 @@ +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) + } + }) + } +}