Skip to content

Commit

Permalink
fix(curl): getCurlCmd should not send request
Browse files Browse the repository at this point in the history
  • Loading branch information
ahuigo committed Oct 10, 2023
1 parent fa336ff commit 0edc400
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 37 deletions.
23 changes: 15 additions & 8 deletions client.go
Expand Up @@ -1129,9 +1129,7 @@ func (c *Client) GetClient() *http.Client {
// Client Unexported methods
//_______________________________________________________________________

// Executes method executes the given `Request` object and returns response
// error.
func (c *Client) execute(req *Request) (*Response, error) {
func (c *Client) executeBefore(req *Request) (error) {
// Lock the user-defined pre-request hooks.
c.udBeforeRequestLock.RLock()
defer c.udBeforeRequestLock.RUnlock()
Expand All @@ -1147,22 +1145,22 @@ func (c *Client) execute(req *Request) (*Response, error) {
// to modify the *resty.Request object
for _, f := range c.udBeforeRequest {
if err = f(c, req); err != nil {
return nil, wrapNoRetryErr(err)
return wrapNoRetryErr(err)
}
}

// If there is a rate limiter set for this client, the Execute call
// will return an error if the rate limit is exceeded.
if req.client.rateLimiter != nil {
if !req.client.rateLimiter.Allow() {
return nil, wrapNoRetryErr(ErrRateLimitExceeded)
return wrapNoRetryErr(ErrRateLimitExceeded)
}
}

// resty middlewares
for _, f := range c.beforeRequest {
if err = f(c, req); err != nil {
return nil, wrapNoRetryErr(err)
return wrapNoRetryErr(err)
}
}

Expand All @@ -1173,15 +1171,24 @@ func (c *Client) execute(req *Request) (*Response, error) {
// call pre-request if defined
if c.preReqHook != nil {
if err = c.preReqHook(c, req.RawRequest); err != nil {
return nil, wrapNoRetryErr(err)
return wrapNoRetryErr(err)
}
}

if err = requestLogger(c, req); err != nil {
return nil, wrapNoRetryErr(err)
return wrapNoRetryErr(err)
}

req.RawRequest.Body = newRequestBodyReleaser(req.RawRequest.Body, req.bodyBuf)
return nil
}

// Executes method executes the given `Request` object and returns response
// error.
func (c *Client) execute(req *Request) (*Response, error) {
if err:= c.executeBefore(req);err!=nil{
return nil, err
}

req.Time = time.Now()
resp, err := c.httpClient.Do(req.RawRequest)
Expand Down
24 changes: 11 additions & 13 deletions examples/debug_curl_test.go
Expand Up @@ -19,23 +19,21 @@ func TestDebugCurl(t *testing.T) {
[]*http.Cookie{
{ Name: "count", Value: "1", },
},
).EnableCurl()
)

// 1. generate curl before request
curl_before_req := req.GetCurlCmd()
if !strings.Contains(curl_before_req, "Cookie: count=1") || !strings.Contains(curl_before_req, "curl -X GET") {
t.Fatal("bad curl:", curl_before_req)
// 1. generate curl for request(not executed)
curlCmdUnexecuted := req.GetCurlCmd()
if !strings.Contains(curlCmdUnexecuted, "Cookie: count=1") || !strings.Contains(curlCmdUnexecuted, "curl -X GET") {
t.Fatal("bad curl:", curlCmdUnexecuted)
}

// 2. send request
resp, err := req.Post(ts.URL+"/post",)
if err != nil {
// 2. generate curl for request(executed)
var curlCmdExecuted string
req.SetResultCurlCmd(&curlCmdExecuted)
if _, err := req.Post(ts.URL+"/post"); err != nil {
t.Fatal(err)
}

// 3. generate curl after request
curl_after_req := resp.Request.GetCurlCmd()
if !strings.Contains(curl_after_req, "Cookie: count=1") || !strings.Contains(curl_after_req, "curl -X POST") {
t.Fatal("bad curl:", curl_after_req)
if !strings.Contains(curlCmdExecuted, "Cookie: count=1") || !strings.Contains(curlCmdExecuted, "curl -X POST") {
t.Fatal("bad curl:", curlCmdExecuted)
}
}
4 changes: 2 additions & 2 deletions middleware.go
Expand Up @@ -308,8 +308,8 @@ func addCredentials(c *Client, r *Request) error {
}

func createCurlCmd(c *Client, r *Request) (err error) {
if r.enableCurl{
r.curlCmd = BuildCurlRequest(r.RawRequest, c.httpClient.Jar)
if r.resultCurlCmd!=nil{
*r.resultCurlCmd = BuildCurlRequest(r.RawRequest, c.httpClient.Jar)
}
return nil
}
Expand Down
32 changes: 18 additions & 14 deletions request.go
Expand Up @@ -39,6 +39,7 @@ type Request struct {
Time time.Time
Body interface{}
Result interface{}
resultCurlCmd *string
Error interface{}
RawRequest *http.Request
SRV *SRVRecord
Expand Down Expand Up @@ -71,23 +72,20 @@ type Request struct {
multipartFiles []*File
multipartFields []*MultipartField
retryConditions []RetryConditionFunc
enableCurl bool
curlCmd string
}

func (r *Request) EnableCurl() *Request{
r.enableCurl = true
return r
}

func (r *Request) GetCurlCmd() string{
if r.curlCmd == ""{
if r.RawRequest == nil{
r.Get("/")
func (r *Request) GetCurlCmd() string {
// trigger beforeRequest from middleware
if r.RawRequest == nil {
r.client.executeBefore(r) // mock r.Get("/")
}
r.curlCmd = BuildCurlRequest(r.RawRequest, r.client.httpClient.Jar)
}
return r.curlCmd
if r.resultCurlCmd == nil {
r.resultCurlCmd = new(string)
}
if *r.resultCurlCmd == "" {
*r.resultCurlCmd = BuildCurlRequest(r.RawRequest, r.client.httpClient.Jar)
}
return *r.resultCurlCmd
}

// Context method returns the Context if its already set in request
Expand Down Expand Up @@ -350,6 +348,12 @@ func (r *Request) SetResult(res interface{}) *Request {
return r
}

// This method is to register curl cmd for request executed.
func (r *Request) SetResultCurlCmd(curlCmd *string) *Request {
r.resultCurlCmd = curlCmd
return r
}

// SetError method is to register the request `Error` object for automatic unmarshalling for the request,
// if response status code is greater than 399 and content type either JSON or XML.
//
Expand Down

0 comments on commit 0edc400

Please sign in to comment.