Skip to content

Commit

Permalink
feat: add SetDebug method at request level #651
Browse files Browse the repository at this point in the history
  • Loading branch information
jeevatkm committed Sep 17, 2023
1 parent 0a3c7c8 commit a3685f9
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 7 deletions.
2 changes: 2 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,8 @@ func (c *Client) SetPreRequestHook(h PreRequestHook) *Client {
// For `Response` it logs information such as Status, Response Time, Headers, Body if it has one.
//
// client.SetDebug(true)
//
// Also it can be enabled at request level for particular request, see `Request.SetDebug`.
func (c *Client) SetDebug(d bool) *Client {
c.Debug = d
return c
Expand Down
4 changes: 2 additions & 2 deletions middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ func addCredentials(c *Client, r *Request) error {
}

func requestLogger(c *Client, r *Request) error {
if c.Debug {
if c.Debug || r.Debug {
rr := r.RawRequest
rl := &RequestLog{Header: copyHeaders(rr.Header), Body: r.fmtBodyString(c.debugBodySizeLimit)}
if c.requestLog != nil {
Expand Down Expand Up @@ -303,7 +303,7 @@ func requestLogger(c *Client, r *Request) error {
//_______________________________________________________________________

func responseLogger(c *Client, res *Response) error {
if c.Debug {
if c.Debug || res.Request.Debug {
rl := &ResponseLog{Header: copyHeaders(res.Header()), Body: res.fmtBodyString(c.debugBodySizeLimit)}
if c.responseLog != nil {
if err := c.responseLog(rl); err != nil {
Expand Down
12 changes: 12 additions & 0 deletions request.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ type Request struct {
SRV *SRVRecord
UserInfo *User
Cookies []*http.Cookie
Debug bool

// Attempt is to represent the request attempt made during a Resty
// request execution flow, including retry count.
Expand Down Expand Up @@ -741,6 +742,17 @@ func (r *Request) SetLogger(l Logger) *Request {
return r
}

// SetDebug method enables the debug mode on current request Resty request, It logs
// the details current request and response.
// For `Request` it logs information such as HTTP verb, Relative URL path, Host, Headers, Body if it has one.
// For `Response` it logs information such as Status, Response Time, Headers, Body if it has one.
//
// client.R().SetDebug(true)
func (r *Request) SetDebug(d bool) *Request {
r.Debug = d
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.
Expand Down
14 changes: 9 additions & 5 deletions request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -711,11 +711,11 @@ func TestFormDataDisableWarn(t *testing.T) {
c := dc()
c.SetFormData(map[string]string{"zip_code": "00000", "city": "Los Angeles"}).
SetContentLength(true).
SetDebug(true).
SetDisableWarn(true)
c.outputLogTo(io.Discard)

resp, err := c.R().
SetDebug(true).
SetFormData(map[string]string{"first_name": "Jeevanandam", "last_name": "M", "zip_code": "00001"}).
SetBasicAuth("myuser", "mypass").
Post(ts.URL + "/profile")
Expand Down Expand Up @@ -1748,14 +1748,15 @@ func TestPathParamURLInput(t *testing.T) {
ts := createGetServer(t)
defer ts.Close()

c := dc().SetDebug(true).
c := dc().
SetBaseURL(ts.URL).
SetPathParams(map[string]string{
"userId": "[email protected]",
"path": "users/developers",
})

resp, err := c.R().
SetDebug(true).
SetPathParams(map[string]string{
"subAccountId": "100002",
"website": "https://example.com",
Expand Down Expand Up @@ -1900,7 +1901,8 @@ func TestDebugLoggerRequestBodyTooLarge(t *testing.T) {

// upload a text file with no more than 512 bytes
output = bytes.NewBufferString("")
resp, err = New().SetDebug(true).outputLogTo(output).SetDebugBodyLimit(debugBodySizeLimit).R().
resp, err = New().outputLogTo(output).SetDebugBodyLimit(debugBodySizeLimit).R().
SetDebug(true).
SetFile("file", filepath.Join(getTestDataPath(), "text-file.txt")).
SetHeader("Content-Type", "text/plain").
Post(ts.URL + "/upload")
Expand All @@ -1927,7 +1929,8 @@ func TestDebugLoggerRequestBodyTooLarge(t *testing.T) {

// post form with no more than 512 bytes data
output = bytes.NewBufferString("")
resp, err = New().SetDebug(true).outputLogTo(output).SetDebugBodyLimit(debugBodySizeLimit).R().
resp, err = New().outputLogTo(output).SetDebugBodyLimit(debugBodySizeLimit).R().
SetDebug(true).
SetFormData(map[string]string{
"first_name": "Alex",
"last_name": "C",
Expand All @@ -1954,7 +1957,8 @@ func TestDebugLoggerRequestBodyTooLarge(t *testing.T) {

// post slice with more than 512 bytes data
output = bytes.NewBufferString("")
resp, err = New().SetDebug(true).outputLogTo(output).SetDebugBodyLimit(debugBodySizeLimit).R().
resp, err = New().outputLogTo(output).SetDebugBodyLimit(debugBodySizeLimit).R().
SetDebug(true).
SetBody([]string{strings.Repeat("C", int(debugBodySizeLimit))}).
SetBasicAuth("myuser", "mypass").
Post(formTs.URL + "/profile")
Expand Down

0 comments on commit a3685f9

Please sign in to comment.