From 8cafd11162e584841ac4fae4941bc1a4b23602e3 Mon Sep 17 00:00:00 2001 From: Farhan Munshi <3207297+fm2munsh@users.noreply.github.com> Date: Wed, 4 Dec 2019 11:55:53 -0500 Subject: [PATCH] =?UTF-8?q?[MM-20620]=20Handle=20trailing=20slash=20when?= =?UTF-8?q?=20building=20marketplace=20api=E2=80=A6=20(#13273)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- services/marketplace/client.go | 3 +- services/marketplace/client_test.go | 44 +++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 services/marketplace/client_test.go 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) + }) + } +}