From 98f2cab4a2a752b29f87992fc37ee86da2217432 Mon Sep 17 00:00:00 2001 From: Renan DelValle Date: Thu, 12 Sep 2019 11:04:52 -0700 Subject: [PATCH] Renamed Aurora address validator to be less redudnant. Added tests cribbed from version 1. --- realis_test.go | 44 ++++++++++++++++++++++++++++++++++++++++++++ util.go | 8 ++++---- 2 files changed, 48 insertions(+), 4 deletions(-) diff --git a/realis_test.go b/realis_test.go index 8530e74..b48e88f 100644 --- a/realis_test.go +++ b/realis_test.go @@ -26,3 +26,47 @@ func TestGetCACerts(t *testing.T) { require.NoError(t, err) assert.Equal(t, len(certs.Subjects()), 2) } + +func TestAuroraURLValidator(t *testing.T) { + t.Run("badURL", func(t *testing.T) { + url, err := validateAuroraAddress("http://badurl.com/badpath") + assert.Empty(t, url) + assert.Error(t, err) + }) + + t.Run("URLHttp", func(t *testing.T) { + url, err := validateAuroraAddress("http://goodurl.com:8081/api") + assert.Equal(t, "http://goodurl.com:8081/api", url) + assert.NoError(t, err) + }) + + t.Run("URLHttps", func(t *testing.T) { + url, err := validateAuroraAddress("https://goodurl.com:8081/api") + assert.Equal(t, "https://goodurl.com:8081/api", url) + assert.NoError(t, err) + }) + + t.Run("URLNoPath", func(t *testing.T) { + url, err := validateAuroraAddress("http://goodurl.com:8081") + assert.Equal(t, "http://goodurl.com:8081/api", url) + assert.NoError(t, err) + }) + + t.Run("ipAddrNoPath", func(t *testing.T) { + url, err := validateAuroraAddress("http://192.168.1.33:8081") + assert.Equal(t, "http://192.168.1.33:8081/api", url) + assert.NoError(t, err) + }) + + t.Run("URLNoProtocol", func(t *testing.T) { + url, err := validateAuroraAddress("goodurl.com:8081/api") + assert.Equal(t, "http://goodurl.com:8081/api", url) + assert.NoError(t, err) + }) + + t.Run("URLNoProtocolNoPathNoPort", func(t *testing.T) { + url, err := validateAuroraAddress("goodurl.com") + assert.Equal(t, "http://goodurl.com:8081/api", url) + assert.NoError(t, err) + }) +} diff --git a/util.go b/util.go index caf7baa..c1bba48 100644 --- a/util.go +++ b/util.go @@ -40,14 +40,14 @@ func init() { } } -func validateAndPopulateAuroraURL(urlStr string) (string, error) { +func validateAuroraAddress(address string) (string, error) { // If no protocol defined, assume http - if !strings.Contains(urlStr, "://") { - urlStr = "http://" + urlStr + if !strings.Contains(address, "://") { + address = "http://" + address } - u, err := url.Parse(urlStr) + u, err := url.Parse(address) if err != nil { return "", errors.Wrap(err, "error parsing url")