diff --git a/cmd/notify-amqp.go b/cmd/notify-amqp.go index 8e3b4652e..04965d954 100644 --- a/cmd/notify-amqp.go +++ b/cmd/notify-amqp.go @@ -44,7 +44,7 @@ func (a *amqpNotify) Validate() error { if !a.Enable { return nil } - if _, err := checkNetURL(a.URL); err != nil { + if _, err := checkURL(a.URL); err != nil { return err } return nil diff --git a/cmd/notify-elasticsearch.go b/cmd/notify-elasticsearch.go index 4773feaf2..e35eef0cd 100644 --- a/cmd/notify-elasticsearch.go +++ b/cmd/notify-elasticsearch.go @@ -37,7 +37,7 @@ func (e *elasticSearchNotify) Validate() error { if !e.Enable { return nil } - if _, err := checkNetURL(e.URL); err != nil { + if _, err := checkURL(e.URL); err != nil { return err } return nil diff --git a/cmd/notify-nats.go b/cmd/notify-nats.go index 8fe0654d7..1659eefc2 100644 --- a/cmd/notify-nats.go +++ b/cmd/notify-nats.go @@ -52,7 +52,7 @@ func (n *natsNotify) Validate() error { if !n.Enable { return nil } - if _, err := checkNetURL(n.Address); err != nil { + if _, err := checkURL(n.Address); err != nil { return err } return nil diff --git a/cmd/notify-postgresql.go b/cmd/notify-postgresql.go index 1ea092f5b..fadecd332 100644 --- a/cmd/notify-postgresql.go +++ b/cmd/notify-postgresql.go @@ -88,7 +88,7 @@ func (p *postgreSQLNotify) Validate() error { if !p.Enable { return nil } - if _, err := checkNetURL(p.Host); err != nil { + if _, err := checkURL(p.Host); err != nil { return err } return nil diff --git a/cmd/notify-redis.go b/cmd/notify-redis.go index ca2aca93b..1f385d472 100644 --- a/cmd/notify-redis.go +++ b/cmd/notify-redis.go @@ -36,7 +36,7 @@ func (r *redisNotify) Validate() error { if !r.Enable { return nil } - if _, err := checkNetURL(r.Addr); err != nil { + if _, err := checkURL(r.Addr); err != nil { return err } return nil diff --git a/cmd/notify-webhook.go b/cmd/notify-webhook.go index b028c8e4e..c64421996 100644 --- a/cmd/notify-webhook.go +++ b/cmd/notify-webhook.go @@ -36,7 +36,7 @@ func (w *webhookNotify) Validate() error { if !w.Enable { return nil } - if _, err := checkNetURL(w.Endpoint); err != nil { + if _, err := checkURL(w.Endpoint); err != nil { return err } return nil diff --git a/cmd/utils.go b/cmd/utils.go index d644731ed..744e7bd08 100644 --- a/cmd/utils.go +++ b/cmd/utils.go @@ -19,6 +19,7 @@ package cmd import ( "encoding/base64" "encoding/xml" + "errors" "fmt" "io" "net/http" @@ -263,15 +264,14 @@ func isFile(path string) bool { return false } -// checkNetURL - checks if passed address correspond -// to a network address (and not file system path) -func checkNetURL(address string) (*url.URL, error) { +// checkURL - checks if passed address correspond +func checkURL(address string) (*url.URL, error) { + if address == "" { + return nil, errors.New("Address cannot be empty") + } u, err := url.Parse(address) if err != nil { return nil, fmt.Errorf("`%s` invalid: %s", address, err.Error()) } - if u.Host == "" { - return nil, fmt.Errorf("`%s` invalid network URL", address) - } return u, nil } diff --git a/cmd/utils_test.go b/cmd/utils_test.go index d0d11c3f4..fa06e8e09 100644 --- a/cmd/utils_test.go +++ b/cmd/utils_test.go @@ -388,3 +388,30 @@ func TestLocalAddress(t *testing.T) { } } + +// TestCheckURL tests valid address +func TestCheckURL(t *testing.T) { + testCases := []struct { + addr string + shouldPass bool + }{ + {"", false}, + {":", false}, + {"localhost", true}, + {"127.0.0.1", true}, + {"http://localhost/", true}, + {"http://127.0.0.1/", true}, + {"proto://myhostname/path", true}, + } + + // Validates fetching local address. + for i, testCase := range testCases { + _, err := checkURL(testCase.addr) + if testCase.shouldPass && err != nil { + t.Errorf("Test %d: expected to pass but got an error: %v\n", i+1, err) + } + if !testCase.shouldPass && err == nil { + t.Errorf("Test %d: expected to fail but passed.", i+1) + } + } +}