From 765db41ec60eb886c74c5901d5369eafae2d37ce Mon Sep 17 00:00:00 2001 From: Thomas Gosteli Date: Thu, 22 Mar 2018 08:28:07 +0100 Subject: [PATCH 1/2] panic message on nil ResponseCallback fixed --- httpclient.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/httpclient.go b/httpclient.go index 8f66c3b..3687464 100644 --- a/httpclient.go +++ b/httpclient.go @@ -298,7 +298,7 @@ func (c *Client) Do(ctx context.Context, req *http.Request, v interface{}) (*htt }() if c.ResponseCallback == nil { - panic("CheckResponse is nil") + panic("ResponseCallback is nil") } resp, err = c.ResponseCallback(resp) From ed57d9f389b55b9853fea73310b3482fa7365c1c Mon Sep 17 00:00:00 2001 From: Thomas Gosteli Date: Thu, 22 Mar 2018 09:44:38 +0100 Subject: [PATCH 2/2] no longer override Authorization header when using WithHeader to set custom headers --- httpclient.go | 13 +++++++------ httpclient_test.go | 19 +++++++++++++++++++ 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/httpclient.go b/httpclient.go index 3687464..7f031d1 100644 --- a/httpclient.go +++ b/httpclient.go @@ -52,7 +52,7 @@ type Client struct { username string password string - // custom http header + // custom http header(s) header http.Header Marshaler MarshalerFunc @@ -191,7 +191,8 @@ func WithContentType(ct string) Opt { } // WithHeader is a client option for setting custom http header(s) for each request -// Content-Type and Accept headers will always be overwritten by the clients ContentType setting +// Content-Type and Accept headers will be appended by the clients ContentType setting +// Authorization header is overwritten if WithUsername/WithPassowrd was used to setup the client func WithHeader(header http.Header) Opt { return func(c *Client) error { c.header = header @@ -225,13 +226,13 @@ func (c *Client) NewRequest(method, urlStr string, body interface{}) (*http.Requ return nil, err } - if len(c.username) > 0 && len(c.password) > 0 { - req.SetBasicAuth(c.username, c.password) - } - if c.header != nil { req.Header = c.header } + + if len(c.username) > 0 && len(c.password) > 0 { + req.SetBasicAuth(c.username, c.password) + } req.Header.Add("Content-Type", contentType) req.Header.Add("Accept", contentType) diff --git a/httpclient_test.go b/httpclient_test.go index 813b638..1a37c2b 100644 --- a/httpclient_test.go +++ b/httpclient_test.go @@ -123,6 +123,25 @@ func TestClient(t *testing.T) { assert.Contains(t, req.Header, "Accept") }) + t.Run("new client with headers and basic auth", func(t *testing.T) { + username := "user1" + password := "123456" + fakeAuthHeader := http.Header{ + "Authorization": []string{"fake"}, + } + + c, err := New(baseurl, WithHeader(fakeAuthHeader), WithUsername(username), WithPassword(password)) + assert.Nil(t, err) + assert.NotNil(t, c) + + req, err := c.NewRequest(http.MethodGet, "/test", nil) + assert.Nil(t, err) + user, passwd, ok := req.BasicAuth() + assert.True(t, ok) + assert.Equal(t, user, username) + assert.Equal(t, passwd, passwd) + }) + t.Run("new client valid baseurl valid HTTP client", func(t *testing.T) { httpC := &http.Client{} c, err := New(baseurl, WithHTTPClient(httpC))