Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion internal/client/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
34 changes: 34 additions & 0 deletions internal/client/request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{})

Expand Down
2 changes: 1 addition & 1 deletion internal/transport/round_trippers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}

Expand Down