From 69921ca6b46b77b15dc992452a5ee9a985d87560 Mon Sep 17 00:00:00 2001 From: Francisco Souza Date: Mon, 7 Dec 2015 22:50:26 -0200 Subject: [PATCH] client: avoid repeating slashes when communicating with the tsuru API --- client.go | 8 ++++++-- client_test.go | 18 ++++++++++++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/client.go b/client.go index aa6b0b3..92f5f67 100644 --- a/client.go +++ b/client.go @@ -51,7 +51,7 @@ func (c Client) registerUnit(appName string, customData TsuruYaml) ([]bind.EnvVa v := url.Values{} v.Set("hostname", hostname) v.Set("customdata", string(yamlData)) - u := fmt.Sprintf("%s/apps/%s/units/register", c.URL, appName) + u := c.url(fmt.Sprintf("/apps/%s/units/register", appName)) req, err := http.NewRequest("POST", u, strings.NewReader(v.Encode())) if err != nil { return nil, err @@ -79,7 +79,7 @@ func (c Client) sendDiffDeploy(diff, appName string) error { var err error v := url.Values{} v.Set("customdata", diff) - u := fmt.Sprintf("%s/apps/%s/diff", c.URL, appName) + u := c.url(fmt.Sprintf("/apps/%s/diff", appName)) req, err := http.NewRequest("POST", u, strings.NewReader(v.Encode())) if err != nil { return err @@ -100,3 +100,7 @@ func (c Client) sendDiffDeploy(diff, appName string) error { } return nil } + +func (c Client) url(path string) string { + return fmt.Sprintf("%s/%s", strings.TrimRight(c.URL, "/"), strings.TrimLeft(path, "/")) +} diff --git a/client_test.go b/client_test.go index 95d47a6..19806de 100644 --- a/client_test.go +++ b/client_test.go @@ -97,3 +97,21 @@ func (s *S) TestClientSendDiff(c *check.C) { err = cli.sendDiffDeploy(diff, "test") c.Assert(err, check.IsNil) } + +func (s *S) TestClientUrl(c *check.C) { + var tests = []struct { + input string + expected string + }{ + {"/", "http://localhost/"}, + {"/index", "http://localhost/index"}, + {"///index", "http://localhost/index"}, + {"/v1/register", "http://localhost/v1/register"}, + {"v1/register", "http://localhost/v1/register"}, + } + cli := Client{URL: "http://localhost/", Token: "test-token"} + for _, test := range tests { + got := cli.url(test.input) + c.Check(got, check.Equals, test.expected) + } +}