fix: Fix Query Param addition in client.go (#116)

Signed-off-by: Russell Troxel <russell.troxel@segment.com>
This commit is contained in:
Russell Troxel 2023-03-17 13:04:26 -07:00 committed by GitHub
parent 5834fa69fb
commit 05b277dec9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 6 deletions

View File

@ -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)
}

View File

@ -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()