Skip to content

Commit a3685f9

Browse files
committed
feat: add SetDebug method at request level #651
1 parent 0a3c7c8 commit a3685f9

File tree

4 files changed

+25
-7
lines changed

4 files changed

+25
-7
lines changed

client.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -561,6 +561,8 @@ func (c *Client) SetPreRequestHook(h PreRequestHook) *Client {
561561
// For `Response` it logs information such as Status, Response Time, Headers, Body if it has one.
562562
//
563563
// client.SetDebug(true)
564+
//
565+
// Also it can be enabled at request level for particular request, see `Request.SetDebug`.
564566
func (c *Client) SetDebug(d bool) *Client {
565567
c.Debug = d
566568
return c

middleware.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ func addCredentials(c *Client, r *Request) error {
273273
}
274274

275275
func requestLogger(c *Client, r *Request) error {
276-
if c.Debug {
276+
if c.Debug || r.Debug {
277277
rr := r.RawRequest
278278
rl := &RequestLog{Header: copyHeaders(rr.Header), Body: r.fmtBodyString(c.debugBodySizeLimit)}
279279
if c.requestLog != nil {
@@ -303,7 +303,7 @@ func requestLogger(c *Client, r *Request) error {
303303
//_______________________________________________________________________
304304

305305
func responseLogger(c *Client, res *Response) error {
306-
if c.Debug {
306+
if c.Debug || res.Request.Debug {
307307
rl := &ResponseLog{Header: copyHeaders(res.Header()), Body: res.fmtBodyString(c.debugBodySizeLimit)}
308308
if c.responseLog != nil {
309309
if err := c.responseLog(rl); err != nil {

request.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ type Request struct {
4444
SRV *SRVRecord
4545
UserInfo *User
4646
Cookies []*http.Cookie
47+
Debug bool
4748

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

745+
// SetDebug method enables the debug mode on current request Resty request, It logs
746+
// the details current request and response.
747+
// For `Request` it logs information such as HTTP verb, Relative URL path, Host, Headers, Body if it has one.
748+
// For `Response` it logs information such as Status, Response Time, Headers, Body if it has one.
749+
//
750+
// client.R().SetDebug(true)
751+
func (r *Request) SetDebug(d bool) *Request {
752+
r.Debug = d
753+
return r
754+
}
755+
744756
// AddRetryCondition method adds a retry condition function to the request's
745757
// array of functions that are checked to determine if the request is retried.
746758
// The request will retry if any of the functions return true and error is nil.

request_test.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -711,11 +711,11 @@ func TestFormDataDisableWarn(t *testing.T) {
711711
c := dc()
712712
c.SetFormData(map[string]string{"zip_code": "00000", "city": "Los Angeles"}).
713713
SetContentLength(true).
714-
SetDebug(true).
715714
SetDisableWarn(true)
716715
c.outputLogTo(io.Discard)
717716

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

1751-
c := dc().SetDebug(true).
1751+
c := dc().
17521752
SetBaseURL(ts.URL).
17531753
SetPathParams(map[string]string{
17541754
"userId": "sample@sample.com",
17551755
"path": "users/developers",
17561756
})
17571757

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

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

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

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

0 commit comments

Comments
 (0)