diff --git a/internal/client/request.go b/internal/client/request.go index 7b0103df..6eb203f9 100644 --- a/internal/client/request.go +++ b/internal/client/request.go @@ -23,6 +23,7 @@ import ( "io" "net/http" "net/url" + "strings" ) // Request wrap the http.Request configuration providing functions for configure it in an easier and contained way @@ -134,7 +135,7 @@ func (r *Request) Error() error { func (r *Request) URL() *url.URL { url := *r.restClient.baseURL - url.Path = r.apiPath + url.Path = strings.TrimSuffix(url.Path, "/") + r.apiPath url.RawQuery = r.params.Encode() return &url diff --git a/internal/client/request_test.go b/internal/client/request_test.go index d06fb0fd..684bba2f 100644 --- a/internal/client/request_test.go +++ b/internal/client/request_test.go @@ -98,6 +98,40 @@ func TestSetParams(t *testing.T) { assert.Equal(t, url.Values{"foo": []string{"bar"}, "baz": []string{"1", "2"}}, r.params) } +func TestRequestURL(t *testing.T) { + testCases := map[string]struct { + baseURL string + apiPath string + expectedURL string + }{ + "no base path": { + baseURL: "http://host/", + apiPath: "/api/backend/projects/", + expectedURL: "http://host/api/backend/projects/", + }, + "with base path prefix": { + baseURL: "http://host/mia/", + apiPath: "/api/backend/projects/", + expectedURL: "http://host/mia/api/backend/projects/", + }, + "base path without trailing slash": { + baseURL: "http://host/mia", + apiPath: "/api/backend/projects/", + expectedURL: "http://host/mia/api/backend/projects/", + }, + } + + for testName, testCase := range testCases { + t.Run(testName, func(t *testing.T) { + baseURL, err := url.Parse(testCase.baseURL) + require.NoError(t, err) + client := newAPIClient(baseURL, contentConfig{}, http.DefaultClient) + requestURL := NewRequest(client).APIPath(testCase.apiPath).URL() + assert.Equal(t, testCase.expectedURL, requestURL.String()) + }) + } +} + func TestSetAPIPath(t *testing.T) { r := (&Request{}) diff --git a/internal/transport/round_trippers.go b/internal/transport/round_trippers.go index e131e89f..044f641e 100644 --- a/internal/transport/round_trippers.go +++ b/internal/transport/round_trippers.go @@ -115,7 +115,7 @@ func printCurl(r *http.Request) string { for key, values := range r.Header { for _, value := range values { value = maskSensibleHeaderValue(key, value) - builder.WriteString(fmt.Sprintf("\t-H %q\n", fmt.Sprintf("%s: %s", key, value))) + fmt.Fprintf(&builder, "\t-H %q\n", fmt.Sprintf("%s: %s", key, value)) } }