From e58935ee18c9b7d3670f6eb99223a0ef66bb3960 Mon Sep 17 00:00:00 2001 From: David Muir Sharnoff Date: Thu, 29 Sep 2022 21:17:15 -0700 Subject: [PATCH] add SetLogger() method on Request --- client.go | 1 + middleware.go | 4 ++-- request.go | 14 ++++++++++++-- request_test.go | 26 ++++++++++++++++++++++++++ resty_test.go | 14 ++++++++++---- 5 files changed, 51 insertions(+), 8 deletions(-) diff --git a/client.go b/client.go index 1a03efa3..aa960708 100644 --- a/client.go +++ b/client.go @@ -390,6 +390,7 @@ func (c *Client) R() *Request { multipartFields: []*MultipartField{}, PathParams: map[string]string{}, jsonEscapeHTML: true, + log: c.log, } return r } diff --git a/middleware.go b/middleware.go index e11e4d76..2e311d9b 100644 --- a/middleware.go +++ b/middleware.go @@ -233,7 +233,7 @@ func addCredentials(c *Client, r *Request) error { if !c.DisableWarn { if isBasicAuth && !strings.HasPrefix(r.URL, "https") { - c.log.Warnf("Using Basic Auth in HTTP mode is not secure, use HTTPS") + r.log.Warnf("Using Basic Auth in HTTP mode is not secure, use HTTPS") } } @@ -311,7 +311,7 @@ func responseLogger(c *Client, res *Response) error { } debugLog += "==============================================================================\n" - c.log.Debugf("%s", debugLog) + res.Request.log.Debugf("%s", debugLog) } return nil diff --git a/request.go b/request.go index e2f9aede..585bf93a 100644 --- a/request.go +++ b/request.go @@ -65,6 +65,7 @@ type Request struct { client *Client bodyBuf *bytes.Buffer clientTrace *clientTrace + log Logger multipartFiles []*File multipartFields []*MultipartField retryConditions []RetryConditionFunc @@ -212,7 +213,7 @@ func (r *Request) SetQueryString(query string) *Request { } } } else { - r.client.log.Errorf("%v", err) + r.log.Errorf("%v", err) } return r } @@ -596,6 +597,15 @@ func (r *Request) SetCookies(rs []*http.Cookie) *Request { return r } +// SetLogger method sets given writer for logging Resty request and response details. +// By default, requests and responses inherit their logger from the client. +// +// Compliant to interface `resty.Logger`. +func (r *Request) SetLogger(l Logger) *Request { + r.log = l + return r +} + // AddRetryCondition method adds a retry condition function to the request's // array of functions that are checked to determine if the request is retried. // The request will retry if any of the functions return true and error is nil. @@ -770,7 +780,7 @@ func (r *Request) Execute(method, url string) (*Response, error) { resp, err = r.client.execute(r) if err != nil { - r.client.log.Errorf("%v, Attempt %v", err, r.Attempt) + r.log.Errorf("%v, Attempt %v", err, r.Attempt) } return resp, err diff --git a/request_test.go b/request_test.go index 06aef360..09e57f21 100644 --- a/request_test.go +++ b/request_test.go @@ -551,6 +551,32 @@ func TestRequestBasicAuth(t *testing.T) { logResponse(t, resp) } +func TestRequestInsecureBasicAuth(t *testing.T) { + ts := createAuthServerTLSOptional(t, false) + defer ts.Close() + + var logBuf bytes.Buffer + logger := createLogger() + logger.l.SetOutput(&logBuf) + + c := dc() + c.SetHostURL(ts.URL) + + resp, err := c.R(). + SetBasicAuth("myuser", "basicauth"). + SetResult(&AuthSuccess{}). + SetLogger(logger). + Post("/login") + + assertError(t, err) + assertEqual(t, http.StatusOK, resp.StatusCode()) + assertEqual(t, true, strings.Contains(logBuf.String(), "WARN RESTY Using Basic Auth in HTTP mode is not secure, use HTTPS")) + + t.Logf("Result Success: %q", resp.Result().(*AuthSuccess)) + logResponse(t, resp) + t.Logf("captured request-level logs: %s", logBuf.String()) +} + func TestRequestBasicAuthFail(t *testing.T) { ts := createAuthServer(t) defer ts.Close() diff --git a/resty_test.go b/resty_test.go index cd7b3b15..b8154183 100644 --- a/resty_test.go +++ b/resty_test.go @@ -448,7 +448,11 @@ func createFilePostServer(t *testing.T) *httptest.Server { } func createAuthServer(t *testing.T) *httptest.Server { - ts := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + return createAuthServerTLSOptional(t, true) +} + +func createAuthServerTLSOptional(t *testing.T, useTLS bool) *httptest.Server { + handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { t.Logf("Method: %v", r.Method) t.Logf("Path: %v", r.URL.Path) t.Logf("Content-Type: %v", r.Header.Get(hdrContentTypeKey)) @@ -498,9 +502,11 @@ func createAuthServer(t *testing.T) *httptest.Server { return } - })) - - return ts + }) + if useTLS { + return httptest.NewTLSServer(handler) + } + return httptest.NewServer(handler) } func createGenServer(t *testing.T) *httptest.Server {