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 1a0acab
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 11 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
4 changes: 2 additions & 2 deletions examples/debug_curl_test.go
Expand Up @@ -21,7 +21,7 @@ func TestDebugCurl(t *testing.T) {
},
).EnableCurl()

// 1. generate curl before request
// 1. generate curl for request(not executed)
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)
Expand All @@ -33,7 +33,7 @@ func TestDebugCurl(t *testing.T) {
t.Fatal(err)
}

// 3. generate curl after request
// 3. generate curl for request(executed)
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)
Expand Down
3 changes: 2 additions & 1 deletion request.go
Expand Up @@ -83,7 +83,8 @@ func (r *Request) EnableCurl() *Request{
func (r *Request) GetCurlCmd() string{
if r.curlCmd == ""{
if r.RawRequest == nil{
r.Get("/")
// trigger beforeRequest from middleware
r.client.executeBefore(r) // mock r.Get("/")
}
r.curlCmd = BuildCurlRequest(r.RawRequest, r.client.httpClient.Jar)
}
Expand Down

0 comments on commit 1a0acab

Please sign in to comment.