Skip to content

Commit

Permalink
add SetCookieJar method to override default CookieJar (#69)
Browse files Browse the repository at this point in the history
  • Loading branch information
lrita authored and jeevatkm committed Apr 29, 2017
1 parent 97effac commit d2d648a
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
11 changes: 11 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,17 @@ func (c *Client) SetHeaders(headers map[string]string) *Client {
return c
}

// SetCookieJar method sets custom http.CookieJar in the resty client. Its way to override default.
// Example: sometimes we don't want to save cookies in api contacting, we can remove the default
// CookieJar in resty client.
//
// resty.SetCookieJar(nil)
//
func (c *Client) SetCookieJar(jar http.CookieJar) *Client {
c.httpClient.Jar = jar
return c
}

// SetCookie method sets 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{
Expand Down
11 changes: 11 additions & 0 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,17 @@ func TestSetScheme(t *testing.T) {
assertEqual(t, true, DefaultClient.scheme == "http")
}

func TestSetCookieJar(t *testing.T) {
DefaultClient = dc()
backupJar := DefaultClient.httpClient.Jar

SetCookieJar(nil)
assertEqual(t, true, DefaultClient.httpClient.Jar == nil)

SetCookieJar(backupJar)
assertEqual(t, true, DefaultClient.httpClient.Jar == backupJar)
}

func TestClientOptions(t *testing.T) {
SetHTTPMode().SetContentLength(true)
assertEqual(t, Mode(), "http")
Expand Down
5 changes: 5 additions & 0 deletions default.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@ func SetHeaders(headers map[string]string) *Client {
return DefaultClient.SetHeaders(headers)
}

// SetCookieJar sets custom http.CookieJar. See `Client.SetCookieJar` for more information.
func SetCookieJar(jar http.CookieJar) *Client {
return DefaultClient.SetCookieJar(jar)
}

// SetCookie sets single cookie object. See `Client.SetCookie` for more information.
func SetCookie(hc *http.Cookie) *Client {
return DefaultClient.SetCookie(hc)
Expand Down

0 comments on commit d2d648a

Please sign in to comment.