Skip to content

Commit 370d744

Browse files
authored
fix(enhancement): add explicit option to enable generate curl command in conjunction with debug mode and few clean ups #828 (#842)
1 parent 08e6170 commit 370d744

File tree

8 files changed

+98
-210
lines changed

8 files changed

+98
-210
lines changed

client.go

+25-6
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ type Client struct {
154154
invalidHooks []ErrorHook
155155
panicHooks []ErrorHook
156156
rateLimiter RateLimiter
157+
generateCurlOnDebug bool
157158
}
158159

159160
// User type is to hold an username and password information
@@ -443,12 +444,13 @@ func (c *Client) R() *Request {
443444
RawPathParams: map[string]string{},
444445
Debug: c.Debug,
445446

446-
client: c,
447-
multipartFiles: []*File{},
448-
multipartFields: []*MultipartField{},
449-
jsonEscapeHTML: c.jsonEscapeHTML,
450-
log: c.log,
451-
responseBodyLimit: c.ResponseBodyLimit,
447+
client: c,
448+
multipartFiles: []*File{},
449+
multipartFields: []*MultipartField{},
450+
jsonEscapeHTML: c.jsonEscapeHTML,
451+
log: c.log,
452+
responseBodyLimit: c.ResponseBodyLimit,
453+
generateCurlOnDebug: c.generateCurlOnDebug,
452454
}
453455
return r
454456
}
@@ -1130,6 +1132,23 @@ func (c *Client) DisableTrace() *Client {
11301132
return c
11311133
}
11321134

1135+
// EnableGenerateCurlOnDebug method enables the generation of CURL commands in the debug log.
1136+
// It works in conjunction with debug mode.
1137+
//
1138+
// NOTE: Use with care.
1139+
// - Potential to leak sensitive data in the debug log from [Request] and [Response].
1140+
// - Beware of memory usage since the request body is reread.
1141+
func (c *Client) EnableGenerateCurlOnDebug() *Client {
1142+
c.generateCurlOnDebug = true
1143+
return c
1144+
}
1145+
1146+
// DisableGenerateCurlOnDebug method disables the option set by [Client.EnableGenerateCurlOnDebug].
1147+
func (c *Client) DisableGenerateCurlOnDebug() *Client {
1148+
c.generateCurlOnDebug = false
1149+
return c
1150+
}
1151+
11331152
// IsProxySet method returns the true is proxy is set from resty client otherwise
11341153
// false. By default proxy is set from environment, refer to `http.ProxyFromEnvironment`.
11351154
func (c *Client) IsProxySet() bool {

examples/debug_curl_test.go renamed to curl_cmd_test.go

+20-16
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,16 @@
1-
package examples
1+
package resty
22

33
import (
44
"io"
55
"net/http"
66
"os"
77
"strings"
88
"testing"
9-
10-
"github.com/go-resty/resty/v2"
119
)
1210

1311
// 1. Generate curl for unexecuted request(dry-run)
14-
func TestGenerateUnexcutedCurl(t *testing.T) {
15-
ts := createHttpbinServer(0)
16-
defer ts.Close()
17-
18-
req := resty.New().R().
12+
func TestGenerateUnexecutedCurl(t *testing.T) {
13+
req := dclr().
1914
SetBody(map[string]string{
2015
"name": "Alex",
2116
}).
@@ -25,7 +20,8 @@ func TestGenerateUnexcutedCurl(t *testing.T) {
2520
},
2621
)
2722

28-
curlCmdUnexecuted := req.GenerateCurlCommand()
23+
curlCmdUnexecuted := req.EnableGenerateCurlOnDebug().GenerateCurlCommand()
24+
req.DisableGenerateCurlOnDebug()
2925

3026
if !strings.Contains(curlCmdUnexecuted, "Cookie: count=1") ||
3127
!strings.Contains(curlCmdUnexecuted, "curl -X GET") ||
@@ -39,28 +35,32 @@ func TestGenerateUnexcutedCurl(t *testing.T) {
3935

4036
// 2. Generate curl for executed request
4137
func TestGenerateExecutedCurl(t *testing.T) {
42-
ts := createHttpbinServer(0)
38+
ts := createPostServer(t)
4339
defer ts.Close()
4440

4541
data := map[string]string{
4642
"name": "Alex",
4743
}
48-
req := resty.New().R().
44+
c := dcl()
45+
req := c.R().
4946
SetBody(data).
5047
SetCookies(
5148
[]*http.Cookie{
5249
{Name: "count", Value: "1"},
5350
},
5451
)
5552

56-
url := ts.URL + "/post"
53+
url := ts.URL + "/curl-cmd-post"
5754
resp, err := req.
58-
EnableTrace().
55+
EnableGenerateCurlOnDebug().
5956
Post(url)
6057
if err != nil {
6158
t.Fatal(err)
6259
}
6360
curlCmdExecuted := resp.Request.GenerateCurlCommand()
61+
62+
c.DisableGenerateCurlOnDebug()
63+
req.DisableGenerateCurlOnDebug()
6464
if !strings.Contains(curlCmdExecuted, "Cookie: count=1") ||
6565
!strings.Contains(curlCmdExecuted, "curl -X POST") ||
6666
!strings.Contains(curlCmdExecuted, `-d '{"name":"Alex"}'`) ||
@@ -73,15 +73,16 @@ func TestGenerateExecutedCurl(t *testing.T) {
7373

7474
// 3. Generate curl in debug mode
7575
func TestDebugModeCurl(t *testing.T) {
76-
ts := createHttpbinServer(0)
76+
ts := createPostServer(t)
7777
defer ts.Close()
7878

7979
// 1. Capture stderr
8080
getOutput, restore := captureStderr()
8181
defer restore()
8282

8383
// 2. Build request
84-
req := resty.New().R().
84+
c := New()
85+
req := c.EnableGenerateCurlOnDebug().R().
8586
SetBody(map[string]string{
8687
"name": "Alex",
8788
}).
@@ -92,12 +93,15 @@ func TestDebugModeCurl(t *testing.T) {
9293
)
9394

9495
// 3. Execute request: set debug mode
95-
url := ts.URL + "/post"
96+
url := ts.URL + "/curl-cmd-post"
9697
_, err := req.SetDebug(true).Post(url)
9798
if err != nil {
9899
t.Fatal(err)
99100
}
100101

102+
c.DisableGenerateCurlOnDebug()
103+
req.DisableGenerateCurlOnDebug()
104+
101105
// 4. test output curl
102106
output := getOutput()
103107
if !strings.Contains(output, "Cookie: count=1") ||

examples/BUILD.bazel

-10
This file was deleted.

examples/server_test.go

-162
This file was deleted.

middleware.go

+9-5
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ func addCredentials(c *Client, r *Request) error {
308308
}
309309

310310
func createCurlCmd(c *Client, r *Request) (err error) {
311-
if r.trace {
311+
if r.Debug && r.generateCurlOnDebug {
312312
if r.resultCurlCmd == nil {
313313
r.resultCurlCmd = new(string)
314314
}
@@ -338,10 +338,14 @@ func requestLogger(c *Client, r *Request) error {
338338
}
339339
}
340340

341-
reqLog := "\n==============================================================================\n" +
342-
"~~~ REQUEST(curl) ~~~\n" +
343-
fmt.Sprintf("CURL:\n %v\n", buildCurlRequest(r.RawRequest, r.client.httpClient.Jar)) +
344-
"~~~ REQUEST ~~~\n" +
341+
reqLog := "\n==============================================================================\n"
342+
343+
if r.Debug && r.generateCurlOnDebug {
344+
reqLog += "~~~ REQUEST(CURL) ~~~\n" +
345+
fmt.Sprintf(" %v\n", *r.resultCurlCmd)
346+
}
347+
348+
reqLog += "~~~ REQUEST ~~~\n" +
345349
fmt.Sprintf("%s %s %s\n", r.Method, rr.URL.RequestURI(), rr.Proto) +
346350
fmt.Sprintf("HOST : %s\n", rr.URL.Host) +
347351
fmt.Sprintf("HEADERS:\n%s\n", composeHeaders(c, r, rl.Header)) +

0 commit comments

Comments
 (0)