From b47788d9ce021cfd20eaf2e55e578fd8ee3a3602 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Mierzwa?= Date: Wed, 5 Apr 2017 22:19:28 -0700 Subject: [PATCH] Move joinURL to the transport package Move it and make it public so it can be reused outside of alertmanager package --- alertmanager/alerts.go | 3 ++- alertmanager/remote.go | 13 ----------- alertmanager/remote_test.go | 36 ------------------------------- alertmanager/silences.go | 3 ++- transport/urls.go | 17 +++++++++++++++ transport/urls_test.go | 43 +++++++++++++++++++++++++++++++++++++ 6 files changed, 64 insertions(+), 51 deletions(-) create mode 100644 transport/urls.go create mode 100644 transport/urls_test.go diff --git a/alertmanager/alerts.go b/alertmanager/alerts.go index 2c46a3469..d7d379439 100644 --- a/alertmanager/alerts.go +++ b/alertmanager/alerts.go @@ -6,6 +6,7 @@ import ( "github.com/cloudflare/unsee/config" "github.com/cloudflare/unsee/models" + "github.com/cloudflare/unsee/transport" log "github.com/Sirupsen/logrus" ) @@ -22,7 +23,7 @@ type AlertGroupsAPIResponse struct { func (response *AlertGroupsAPIResponse) Get() error { start := time.Now() - url, err := joinURL(config.Config.AlertmanagerURI, "api/v1/alerts/groups") + url, err := transport.JoinURL(config.Config.AlertmanagerURI, "api/v1/alerts/groups") if err != nil { return err } diff --git a/alertmanager/remote.go b/alertmanager/remote.go index da44c9f41..ed04f5d47 100644 --- a/alertmanager/remote.go +++ b/alertmanager/remote.go @@ -6,24 +6,11 @@ import ( "fmt" "io" "net/http" - "net/url" - "path" "time" log "github.com/Sirupsen/logrus" ) -// joinURL can be used to join a base url (http(s)://domain.com) and a path (/my/path) -// it will return a joined string or an error (if you supply invalid url) -func joinURL(base string, sub string) (string, error) { - u, err := url.Parse(base) - if err != nil { - return "", err - } - u.Path = path.Join(u.Path, sub) - return u.String(), nil -} - // getJSONFromURL is a helper function that takesan URL, request timeout // and target structure, it will make a HTTP request and decode JSON response // onto the structure provided diff --git a/alertmanager/remote_test.go b/alertmanager/remote_test.go index 2f5ab3146..8f4b86b4b 100644 --- a/alertmanager/remote_test.go +++ b/alertmanager/remote_test.go @@ -9,42 +9,6 @@ import ( httpmock "gopkg.in/jarcoal/httpmock.v1" ) -type joinURLTest struct { - base string - sub string - url string -} - -var joinURLTests = []joinURLTest{ - joinURLTest{ - base: "http://localhost", - sub: "/sub", - url: "http://localhost/sub", - }, - joinURLTest{ - base: "http://localhost", - sub: "/sub/", - url: "http://localhost/sub", - }, - joinURLTest{ - base: "http://am.example.com", - sub: "/api/v1/alerts", - url: "http://am.example.com/api/v1/alerts", - }, -} - -func TestJoinURL(t *testing.T) { - for _, testCase := range joinURLTests { - url, err := joinURL(testCase.base, testCase.sub) - if err != nil { - t.Errorf("joinURL(%v, %v) failed: %s", testCase.base, testCase.sub, err.Error()) - } - if url != testCase.url { - t.Errorf("Invalid joined url from '%s' + '%s', expected '%s', got '%s'", testCase.base, testCase.sub, testCase.url, url) - } - } -} - type mockJSONResponse struct { status string integer int diff --git a/alertmanager/silences.go b/alertmanager/silences.go index e04eb3af7..0833b30d4 100644 --- a/alertmanager/silences.go +++ b/alertmanager/silences.go @@ -8,6 +8,7 @@ import ( "github.com/cloudflare/unsee/config" "github.com/cloudflare/unsee/models" + "github.com/cloudflare/unsee/transport" log "github.com/Sirupsen/logrus" ) @@ -24,7 +25,7 @@ type SilenceAPIResponse struct { func (response *SilenceAPIResponse) Get() error { start := time.Now() - url, err := joinURL(config.Config.AlertmanagerURI, "api/v1/silences") + url, err := transport.JoinURL(config.Config.AlertmanagerURI, "api/v1/silences") if err != nil { return err } diff --git a/transport/urls.go b/transport/urls.go new file mode 100644 index 000000000..374e746ba --- /dev/null +++ b/transport/urls.go @@ -0,0 +1,17 @@ +package transport + +import ( + "net/url" + "path" +) + +// JoinURL can be used to join a base url (http(s)://domain.com) and a path (/my/path) +// it will return a joined string or an error (if you supply invalid url) +func JoinURL(base string, sub string) (string, error) { + u, err := url.Parse(base) + if err != nil { + return "", err + } + u.Path = path.Join(u.Path, sub) + return u.String(), nil +} diff --git a/transport/urls_test.go b/transport/urls_test.go new file mode 100644 index 000000000..a7b2d63e8 --- /dev/null +++ b/transport/urls_test.go @@ -0,0 +1,43 @@ +package transport_test + +import ( + "testing" + + "github.com/cloudflare/unsee/transport" +) + +type joinURLTest struct { + base string + sub string + url string +} + +var joinURLTests = []joinURLTest{ + joinURLTest{ + base: "http://localhost", + sub: "/sub", + url: "http://localhost/sub", + }, + joinURLTest{ + base: "http://localhost", + sub: "/sub/", + url: "http://localhost/sub", + }, + joinURLTest{ + base: "http://am.example.com", + sub: "/api/v1/alerts", + url: "http://am.example.com/api/v1/alerts", + }, +} + +func TestJoinURL(t *testing.T) { + for _, testCase := range joinURLTests { + url, err := transport.JoinURL(testCase.base, testCase.sub) + if err != nil { + t.Errorf("joinURL(%v, %v) failed: %s", testCase.base, testCase.sub, err.Error()) + } + if url != testCase.url { + t.Errorf("Invalid joined url from '%s' + '%s', expected '%s', got '%s'", testCase.base, testCase.sub, testCase.url, url) + } + } +}