Skip to content

Commit

Permalink
fix: Fix Query Param addition in client.go (#116)
Browse files Browse the repository at this point in the history
Signed-off-by: Russell Troxel <[email protected]>
  • Loading branch information
rtrox authored Mar 17, 2023
1 parent 5834fa6 commit 05b277d
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
11 changes: 7 additions & 4 deletions internal/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
5 changes: 3 additions & 2 deletions internal/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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()
Expand Down

0 comments on commit 05b277d

Please sign in to comment.