diff --git a/middleware.go b/middleware.go index a3ac21d4..445f7360 100644 --- a/middleware.go +++ b/middleware.go @@ -134,32 +134,31 @@ func parseRequestURL(c *Client, r *Request) error { } // Adding Query Param - query := make(url.Values) - for k, v := range c.QueryParam { - for _, iv := range v { - query.Add(k, iv) + if l := len(c.QueryParam) + len(r.QueryParam); l > 0 { + query := make(url.Values, l) + for k, v := range r.QueryParam { + query[k] = v[:] } - } - 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 k, v := range c.QueryParam { + // skip query parameter if it was set from request + if _, ok := query[k]; ok { + continue + } - for _, iv := range v { - query.Add(k, iv) + query[k] = v[:] } - } - // GitHub #123 Preserve query string order partially. - // Since not feasible in `SetQuery*` resty methods, because - // standard package `url.Encode(...)` sorts the query params - // alphabetically - if len(query) > 0 { - if IsStringEmpty(reqURL.RawQuery) { - reqURL.RawQuery = query.Encode() - } else { - reqURL.RawQuery = reqURL.RawQuery + "&" + query.Encode() + // GitHub #123 Preserve query string order partially. + // Since not feasible in `SetQuery*` resty methods, because + // standard package `url.Encode(...)` sorts the query params + // alphabetically + if len(query) > 0 { + if IsStringEmpty(reqURL.RawQuery) { + reqURL.RawQuery = query.Encode() + } else { + reqURL.RawQuery = reqURL.RawQuery + "&" + query.Encode() + } } } diff --git a/middleware_test.go b/middleware_test.go index 9a8e523c..dd1a0880 100644 --- a/middleware_test.go +++ b/middleware_test.go @@ -200,13 +200,32 @@ func Test_parseRequestURL(t *testing.T) { "foo": "1", // ignored, because of the "foo" parameter in request "bar": "2", }) - c.SetQueryParams(map[string]string{ + r.SetQueryParams(map[string]string{ "foo": "3", }) r.URL = "https://example.com/" }, expectedURL: "https://example.com/?foo=3&bar=2", }, + { + name: "adding query parameters by request to URL with existent", + init: func(c *Client, r *Request) { + r.SetQueryParams(map[string]string{ + "bar": "2", + }) + r.URL = "https://example.com/?foo=1" + }, + expectedURL: "https://example.com/?foo=1&bar=2", + }, + { + name: "adding query parameters by request with multiple values", + init: func(c *Client, r *Request) { + r.QueryParam.Add("foo", "1") + r.QueryParam.Add("foo", "2") + r.URL = "https://example.com/" + }, + expectedURL: "https://example.com/?foo=1&foo=2", + }, } { t.Run(tt.name, func(t *testing.T) { c := New()