Skip to content

Commit

Permalink
Rewire set methods to actual url.Values' set methods (#71)
Browse files Browse the repository at this point in the history
Rewire set methods to actual url.Values' set methods for:
- SetFormData
- SetQueryParams
- godoc update
  • Loading branch information
alexmontecucco authored and jeevatkm committed May 1, 2017
1 parent d2d648a commit 2b803d0
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 29 deletions.
18 changes: 9 additions & 9 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ func (c *Client) SetCookieJar(jar http.CookieJar) *Client {
return c
}

// SetCookie method sets a single cookie in the client instance.
// SetCookie method appends a single cookie in the client instance.
// These cookies will be added to all the request raised from this client instance.
// resty.SetCookie(&http.Cookie{
// Name:"go-resty",
Expand Down Expand Up @@ -210,7 +210,7 @@ func (c *Client) SetCookies(cs []*http.Cookie) *Client {
return c
}

// SetQueryParam method sets single paramater and its value in the client instance.
// SetQueryParam method sets single parameter and its value in the client instance.
// It will be formed as query string for the request. For example: `search=kitchen%20papers&size=large`
// in the URL after `?` mark. These query params will be added to all the request raised from
// this client instance. Also it can be overridden at request level Query Param options,
Expand All @@ -220,11 +220,11 @@ func (c *Client) SetCookies(cs []*http.Cookie) *Client {
// SetQueryParam("size", "large")
//
func (c *Client) SetQueryParam(param, value string) *Client {
c.QueryParam.Add(param, value)
c.QueryParam.Set(param, value)
return c
}

// SetQueryParams method sets multiple parameters and its values at one go in the client instance.
// SetQueryParams method sets multiple parameters and their values at one go in the client instance.
// It will be formed as query string for the request. For example: `search=kitchen%20papers&size=large`
// in the URL after `?` mark. These query params will be added to all the request raised from this
// client instance. Also it can be overridden at request level Query Param options,
Expand All @@ -236,13 +236,13 @@ func (c *Client) SetQueryParam(param, value string) *Client {
//
func (c *Client) SetQueryParams(params map[string]string) *Client {
for p, v := range params {
c.QueryParam.Add(p, v)
c.SetQueryParam(p, v)
}

return c
}

// SetFormData method sets Form parameters and its values in the client instance.
// SetFormData method sets Form parameters and their values in the client instance.
// It's applicable only HTTP method `POST` and `PUT` and requets content type would be set as
// `application/x-www-form-urlencoded`. These form data will be added to all the request raised from
// this client instance. Also it can be overridden at request level form data, see `resty.R().SetFormData`.
Expand All @@ -253,7 +253,7 @@ func (c *Client) SetQueryParams(params map[string]string) *Client {
//
func (c *Client) SetFormData(data map[string]string) *Client {
for k, v := range data {
c.FormData.Add(k, v)
c.FormData.Set(k, v)
}

return c
Expand Down Expand Up @@ -308,7 +308,7 @@ func (c *Client) R() *Request {
return r
}

// OnBeforeRequest method sets request middleware into the before request chain.
// OnBeforeRequest method appends request middleware into the before request chain.
// Its gets applied after default `go-resty` request middlewares and before request
// been sent from `go-resty` to host server.
// resty.OnBeforeRequest(func(c *resty.Client, r *resty.Request) error {
Expand All @@ -323,7 +323,7 @@ func (c *Client) OnBeforeRequest(m func(*Client, *Request) error) *Client {
return c
}

// OnAfterResponse method sets response middleware into the after response chain.
// OnAfterResponse method appends response middleware into the after response chain.
// Once we receive response from host server, default `go-resty` response middleware
// gets applied and then user assigened response middlewares applied.
// resty.OnAfterResponse(func(c *resty.Client, r *resty.Response) error {
Expand Down
2 changes: 1 addition & 1 deletion default.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func SetCookies(cs []*http.Cookie) *Client {
return DefaultClient.SetCookies(cs)
}

// SetQueryParam method sets single paramater and its value. See `Client.SetQueryParam` for more information.
// SetQueryParam method sets single parameter and its value. See `Client.SetQueryParam` for more information.
func SetQueryParam(param, value string) *Client {
return DefaultClient.SetQueryParam(param, value)
}
Expand Down
18 changes: 9 additions & 9 deletions request.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,13 @@ func (r *Request) SetHeader(header, value string) *Request {
//
func (r *Request) SetHeaders(headers map[string]string) *Request {
for h, v := range headers {
r.Header.Set(h, v)
r.SetHeader(h, v)
}

return r
}

// SetQueryParam method sets single paramater and its value in the current request.
// SetQueryParam method sets single parameter and its value in the current request.
// It will be formed as query string for the request.
// Example: `search=kitchen%20papers&size=large` in the URL after `?` mark.
// resty.R().
Expand All @@ -63,7 +63,7 @@ func (r *Request) SetHeaders(headers map[string]string) *Request {
// Also you can override query params value, which was set at client instance level
//
func (r *Request) SetQueryParam(param, value string) *Request {
r.QueryParam.Add(param, value)
r.QueryParam.Set(param, value)
return r
}

Expand All @@ -79,13 +79,13 @@ func (r *Request) SetQueryParam(param, value string) *Request {
//
func (r *Request) SetQueryParams(params map[string]string) *Request {
for p, v := range params {
r.QueryParam.Add(p, v)
r.SetQueryParam(p, v)
}

return r
}

// SetMultiValueQueryParams method sets multiple parameters with multi-value
// SetMultiValueQueryParams method appends multiple parameters 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().
Expand Down Expand Up @@ -122,8 +122,8 @@ func (r *Request) SetQueryString(query string) *Request {
return r
}

// SetFormData method sets Form parameters and its values in the current request.
// It's applicable only HTTP method `POST` and `PUT` and requets content type would be set as
// SetFormData method sets Form parameters and their values in the current request.
// It's applicable only HTTP method `POST` and `PUT` and requests content type would be set as
// `application/x-www-form-urlencoded`.
// resty.R().
// SetFormData(map[string]string{
Expand All @@ -134,13 +134,13 @@ func (r *Request) SetQueryString(query string) *Request {
//
func (r *Request) SetFormData(data map[string]string) *Request {
for k, v := range data {
r.FormData.Add(k, v)
r.FormData.Set(k, v)
}

return r
}

// SetMultiValueFormData method sets multiple form parameters with multi-value
// SetMultiValueFormData method appends multiple form parameters with multi-value
// at one go in the current request.
// resty.R().
// SetMultiValueFormData(url.Values{
Expand Down
24 changes: 14 additions & 10 deletions resty_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ func TestPostJSONMapSuccess(t *testing.T) {
logResponse(t, resp)
}

func TestPostJSONMapInvaildResponseJson(t *testing.T) {
func TestPostJSONMapInvalidResponseJson(t *testing.T) {
ts := createPostServer(t)
defer ts.Close()

Expand Down Expand Up @@ -369,7 +369,7 @@ func TestPostXMLStructInvalidLogin(t *testing.T) {
logResponse(t, resp)
}

func TestPostXMLStructInvaildResponseXml(t *testing.T) {
func TestPostXMLStructInvalidResponseXml(t *testing.T) {
ts := createPostServer(t)
defer ts.Close()

Expand Down Expand Up @@ -779,7 +779,7 @@ func TestNoAutoRedirect(t *testing.T) {
assertEqual(t, "Get /redirect-2: Auto redirect is disabled", err.Error())
}

func TestHTTPAutoRedirectUpto10(t *testing.T) {
func TestHTTPAutoRedirectUpTo10(t *testing.T) {
ts := createRedirectServer(t)
defer ts.Close()

Expand Down Expand Up @@ -994,7 +994,7 @@ func TestDetectContentTypeForSlice(t *testing.T) {
logResponse(t, resp)
}

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

Expand All @@ -1004,15 +1004,19 @@ func TestMuliParamQueryString(t *testing.T) {
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
// pending overrides open
assertEqual(t, false, strings.Contains(req1.URL, "status=open"))

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

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

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

Expand Down Expand Up @@ -1537,14 +1541,14 @@ func createRedirectServer(t *testing.T) *httptest.Server {
if cnt >= 5 {
http.Redirect(w, r, "http://httpbin.org/get", http.StatusTemporaryRedirect)
} else {
http.Redirect(w, r, fmt.Sprintf("/redirect-host-check-%d", (cnt+1)), http.StatusTemporaryRedirect)
http.Redirect(w, r, fmt.Sprintf("/redirect-host-check-%d", (cnt + 1)), http.StatusTemporaryRedirect)
}
}
} else if strings.HasPrefix(r.URL.Path, "/redirect-") {
cntStr := strings.SplitAfter(r.URL.Path, "-")[1]
cnt, _ := strconv.Atoi(cntStr)

http.Redirect(w, r, fmt.Sprintf("/redirect-%d", (cnt+1)), http.StatusTemporaryRedirect)
http.Redirect(w, r, fmt.Sprintf("/redirect-%d", (cnt + 1)), http.StatusTemporaryRedirect)
}
}
})
Expand Down

0 comments on commit 2b803d0

Please sign in to comment.