From 48e91e9814ed0ba93c92dcbbcad69825ccf396a0 Mon Sep 17 00:00:00 2001 From: lrita Date: Fri, 28 Apr 2017 21:41:32 +0800 Subject: [PATCH 1/2] add SetCookieJar method * allow user override default CookieJar in resty client --- client.go | 11 +++++++++++ client_test.go | 11 +++++++++++ default.go | 5 +++++ 3 files changed, 27 insertions(+) diff --git a/client.go b/client.go index 32385f95..9cb934f3 100644 --- a/client.go +++ b/client.go @@ -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{ diff --git a/client_test.go b/client_test.go index 50d57618..64897c99 100644 --- a/client_test.go +++ b/client_test.go @@ -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") diff --git a/default.go b/default.go index c6c43e86..375aee69 100644 --- a/default.go +++ b/default.go @@ -89,6 +89,11 @@ func SetHeaders(headers map[string]string) *Client { return DefaultClient.SetHeaders(headers) } +// SetCookieJa 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) From cca7b203d8da0a43d74795bef69baf8f0ac542ce Mon Sep 17 00:00:00 2001 From: lrita Date: Sat, 29 Apr 2017 11:19:25 +0800 Subject: [PATCH 2/2] fix: typo in the comment of SetCookieJar --- default.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/default.go b/default.go index 375aee69..31c605ad 100644 --- a/default.go +++ b/default.go @@ -89,7 +89,7 @@ func SetHeaders(headers map[string]string) *Client { return DefaultClient.SetHeaders(headers) } -// SetCookieJa sets custom http.CookieJar. See `Client.SetCookieJar` for more information. +// SetCookieJar sets custom http.CookieJar. See `Client.SetCookieJar` for more information. func SetCookieJar(jar http.CookieJar) *Client { return DefaultClient.SetCookieJar(jar) }