diff --git a/services/marketplace/client.go b/services/marketplace/client.go index 5b176b4f57..bf4595b7e6 100644 --- a/services/marketplace/client.go +++ b/services/marketplace/client.go @@ -8,6 +8,7 @@ import ( "io/ioutil" "net/http" "net/url" + "strings" "github.com/mattermost/mattermost-server/v5/model" "github.com/mattermost/mattermost-server/v5/services/httpservice" @@ -84,7 +85,7 @@ func closeBody(r *http.Response) { } func (c *Client) buildURL(urlPath string, args ...interface{}) string { - return fmt.Sprintf("%s%s", c.address, fmt.Sprintf(urlPath, args...)) + return fmt.Sprintf("%s/%s", strings.TrimRight(c.address, "/"), strings.TrimLeft(fmt.Sprintf(urlPath, args...), "/")) } func (c *Client) doGet(u string) (*http.Response, error) { diff --git a/services/marketplace/client_test.go b/services/marketplace/client_test.go new file mode 100644 index 0000000000..2c51f934a5 --- /dev/null +++ b/services/marketplace/client_test.go @@ -0,0 +1,44 @@ +// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. +// See LICENSE.txt for license information. + +package marketplace + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestBuildURL(t *testing.T) { + config := &Client{} + + testCases := map[string]struct { + base string + path string + expected string + }{ + "Base url with trailing slash and path with leading slash": { + base: "https://api.integrations.mattermost.com/", + path: "/api/v1/plugins", + expected: "https://api.integrations.mattermost.com/api/v1/plugins", + }, + "Base url without trailing slash and path with leading slash": { + base: "https://api.integrations.mattermost.com", + path: "/api/v1/plugins", + expected: "https://api.integrations.mattermost.com/api/v1/plugins", + }, + "Base url without trailing slash and path without leading slash": { + base: "https://api.integrations.mattermost.com", + path: "api/v1/plugins", + expected: "https://api.integrations.mattermost.com/api/v1/plugins", + }, + } + + for name, tt := range testCases { + t.Run(name, func(t *testing.T) { + config.address = tt.base + actual := config.buildURL(tt.path) + assert.Equal(t, tt.expected, actual) + }) + } +}