Move joinURL to the transport package

Move it and make it public so it can be reused outside of alertmanager package
This commit is contained in:
Łukasz Mierzwa
2017-04-05 22:19:28 -07:00
parent 797495e63a
commit b47788d9ce
6 changed files with 64 additions and 51 deletions

View File

@@ -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
}

View File

@@ -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

View File

@@ -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

View File

@@ -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
}

17
transport/urls.go Normal file
View File

@@ -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
}

43
transport/urls_test.go Normal file
View File

@@ -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)
}
}
}