diff --git a/internal/client/client.go b/internal/client/client.go index bc98682..b59b893 100644 --- a/internal/client/client.go +++ b/internal/client/client.go @@ -94,15 +94,18 @@ func NewClient(c *cli.Context, cf *model.Config) (*Client, error) { // DoRequest - Take a HTTP Request and return Unmarshaled data func (c *Client) DoRequest(endpoint string, target interface{}, queryParams ...map[string]string) error { + values := c.URL.Query() for _, m := range queryParams { for k, v := range m { - c.URL.Query().Add(k, v) + values.Add(k, v) } } - url := c.URL.JoinPath(endpoint).String() - log.Infof("Sending HTTP request to %s", url) + url := c.URL.JoinPath(endpoint) + url.RawQuery = values.Encode() - req, err := http.NewRequest("GET", url, nil) + log.Infof("Sending HTTP request to %s", url.String()) + + req, err := http.NewRequest("GET", url.String(), nil) if err != nil { return fmt.Errorf("Failed to create HTTP Request(%s): %w", url, err) } diff --git a/internal/client/client_test.go b/internal/client/client_test.go index 2e80d82..cd0feae 100644 --- a/internal/client/client_test.go +++ b/internal/client/client_test.go @@ -82,7 +82,7 @@ func TestDoRequest(t *testing.T) { { name: "noParams", endpoint: "queue", - expectedURL: "http://localhost:7878/api/v3/queue", + expectedURL: "/api/v3/queue", }, { name: "params", @@ -91,13 +91,14 @@ func TestDoRequest(t *testing.T) { "page": "1", "testParam": "asdf", }, - expectedURL: "http://localhost:7878/api/v3/test?page=1&testParam=asdf", + expectedURL: "/api/v3/test?page=1&testParam=asdf", }, } for _, param := range parameters { t.Run(param.name, func(t *testing.T) { require := require.New(t) ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + require.Equal(param.expectedURL, r.URL.String(), "DoRequest should use the correct URL") fmt.Fprintln(w, "{\"test\": \"asdf2\"}") })) defer ts.Close()