Skip to content

Commit

Permalink
#28 - Added multi-value query param support
Browse files Browse the repository at this point in the history
  • Loading branch information
jeevatkm committed May 7, 2016
1 parent e2dc8f5 commit afc62d8
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 4 deletions.
17 changes: 13 additions & 4 deletions middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,20 @@ func parseRequestURL(c *Client, r *Request) error {

// Adding Query Param
query := reqURL.Query()
for k := range c.QueryParam {
query.Set(k, c.QueryParam.Get(k))
for k, v := range c.QueryParam {
for _, iv := range v {
query.Add(k, iv)
}
}
for k := range r.QueryParam {
query.Set(k, r.QueryParam.Get(k))

for k, v := range r.QueryParam {
// remove query param from client level by key
// since overrides happens for that key in the request
query.Del(k)

for _, iv := range v {
query.Add(k, iv)
}
}

reqURL.RawQuery = query.Encode()
Expand Down
19 changes: 19 additions & 0 deletions request.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,25 @@ func (r *Request) SetQueryParams(params map[string]string) *Request {
return r
}

// SetMultiValueQueryParams method sets multiple paramaters with multi-value
// at one go in the current request. It will be formed as query string for the request.
// Example: `status=pending&status=approved&status=open` in the URL after `?` mark.
// resty.R().
// SetMultiValueQueryParams(url.Values{
// "status": []string{"pending", "approved", "open"},
// })
// Also you can override query params value, which was set at client instance level
//
func (r *Request) SetMultiValueQueryParams(params url.Values) *Request {
for p, v := range params {
for _, pv := range v {
r.QueryParam.Add(p, pv)
}
}

return r
}

// SetQueryString method provides ability to use string as an input to set URL query string for the request.
//
// Using String as an input
Expand Down
38 changes: 38 additions & 0 deletions resty_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1097,6 +1097,44 @@ func TestDetectContentTypeForSlice(t *testing.T) {
logResponse(t, resp)
}

func TestMuliParamQueryString(t *testing.T) {
ts1 := createGetServer(t)
defer ts1.Close()

client := dc()
req1 := client.R()

client.SetQueryParam("status", "open")

req1.SetQueryParam("status", "pending").
SetQueryParam("status", "approved").
Get(ts1.URL)

assertEqual(t, true, strings.Contains(req1.URL, "status=pending"))
assertEqual(t, true, strings.Contains(req1.URL, "status=approved"))

// because it's removed by key
assertEqual(t, false, strings.Contains(req1.URL, "status=open"))

ts2 := createGetServer(t)
defer ts2.Close()

req2 := client.R()

v := url.Values{
"status": []string{"pending", "approved", "reject"},
}

req2.SetMultiValueQueryParams(v).Get(ts2.URL)

assertEqual(t, true, strings.Contains(req2.URL, "status=pending"))
assertEqual(t, true, strings.Contains(req2.URL, "status=approved"))
assertEqual(t, true, strings.Contains(req2.URL, "status=reject"))

// because it's removed by key
assertEqual(t, false, strings.Contains(req2.URL, "status=open"))
}

func TestSetQueryStringTypical(t *testing.T) {
ts := createGetServer(t)
defer ts.Close()
Expand Down

0 comments on commit afc62d8

Please sign in to comment.