Skip to content

Commit

Permalink
client: avoid repeating slashes when communicating with the tsuru API
Browse files Browse the repository at this point in the history
  • Loading branch information
Francisco Souza committed Dec 8, 2015
1 parent e85a09b commit 69921ca
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
8 changes: 6 additions & 2 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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, "/"))
}
18 changes: 18 additions & 0 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

0 comments on commit 69921ca

Please sign in to comment.