-
-
Notifications
You must be signed in to change notification settings - Fork 724
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(enhancement): use std lib functions (#912)
- curl cmd request body error scenario and removed pointer on string - redirect add error prefix and remove default header - test case additions
- Loading branch information
Showing
12 changed files
with
102 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,8 @@ | ||
// Copyright (c) 2015-present Jeevanandam M ([email protected]), All rights reserved. | ||
// resty source code and usage is governed by a MIT style | ||
// license that can be found in the LICENSE file. | ||
// SPDX-License-Identifier: MIT | ||
|
||
package resty | ||
|
||
import ( | ||
|
@@ -19,22 +24,22 @@ func buildCurlCmd(req *Request) string { | |
} | ||
|
||
// 2. Generate curl cookies | ||
// TODO validate this block of code, I think its not required since cookie captured via Headers | ||
if cookieJar := req.client.CookieJar(); cookieJar != nil { | ||
if cookies := cookieJar.Cookies(req.RawRequest.URL); len(cookies) > 0 { | ||
curl += "-H " + cmdQuote(dumpCurlCookies(cookies)) + " " | ||
} | ||
} | ||
|
||
// 3. Generate curl body | ||
// 3. Generate curl body except for io.Reader and multipart request | ||
if req.RawRequest.GetBody != nil { | ||
body, err := req.RawRequest.GetBody() | ||
if err != nil { | ||
if err == nil { | ||
buf, _ := io.ReadAll(body) | ||
curl += "-d " + cmdQuote(string(bytes.TrimRight(buf, "\n"))) + " " | ||
} else { | ||
req.log.Errorf("curl: %v", err) | ||
return "" | ||
curl += "-d ''" | ||
} | ||
buf, _ := io.ReadAll(body) | ||
curl += "-d " + cmdQuote(string(bytes.TrimRight(buf, "\n"))) + " " | ||
} | ||
|
||
urlString := cmdQuote(req.RawRequest.URL.String()) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,14 @@ | ||
// Copyright (c) 2015-present Jeevanandam M ([email protected]), All rights reserved. | ||
// resty source code and usage is governed by a MIT style | ||
// license that can be found in the LICENSE file. | ||
// SPDX-License-Identifier: MIT | ||
|
||
package resty | ||
|
||
import ( | ||
"bytes" | ||
"errors" | ||
"io" | ||
"net/http" | ||
"net/http/cookiejar" | ||
"strings" | ||
|
@@ -206,6 +213,45 @@ func TestCurl_buildCurlCmd(t *testing.T) { | |
} | ||
} | ||
|
||
func TestCurlRequestGetBodyError(t *testing.T) { | ||
c := dcnl(). | ||
EnableDebug(). | ||
SetRequestMiddlewares( | ||
PrepareRequestMiddleware, | ||
func(_ *Client, r *Request) error { | ||
r.RawRequest.GetBody = func() (io.ReadCloser, error) { | ||
return nil, errors.New("test case error") | ||
} | ||
return nil | ||
}, | ||
) | ||
|
||
req := c.R(). | ||
SetBody(map[string]string{ | ||
"name": "Resty", | ||
}). | ||
SetCookies( | ||
[]*http.Cookie{ | ||
{Name: "count", Value: "1"}, | ||
}, | ||
). | ||
SetMethod(MethodPost) | ||
|
||
assertEqual(t, "", req.GenerateCurlCommand()) | ||
|
||
curlCmdUnexecuted := req.EnableGenerateCurlOnDebug().GenerateCurlCommand() | ||
req.DisableGenerateCurlOnDebug() | ||
|
||
if !strings.Contains(curlCmdUnexecuted, "Cookie: count=1") || | ||
!strings.Contains(curlCmdUnexecuted, "curl -X POST") || | ||
!strings.Contains(curlCmdUnexecuted, `-d ''`) { | ||
t.Fatal("Incomplete curl:", curlCmdUnexecuted) | ||
} else { | ||
t.Log("curlCmdUnexecuted: \n", curlCmdUnexecuted) | ||
} | ||
|
||
} | ||
|
||
func TestCurlMiscTestCoverage(t *testing.T) { | ||
cookieStr := dumpCurlCookies([]*http.Cookie{ | ||
{Name: "count", Value: "1"}, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
// Copyright (c) 2015-present Jeevanandam M ([email protected]), All rights reserved. | ||
// resty source code and usage is governed by a MIT style | ||
// license that can be found in the LICENSE file. | ||
// SPDX-License-Identifier: MIT | ||
|
||
package resty | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
// Copyright (c) 2015-present Jeevanandam M ([email protected]), All rights reserved. | ||
// resty source code and usage is governed by a MIT style | ||
// license that can be found in the LICENSE file. | ||
// SPDX-License-Identifier: MIT | ||
|
||
package resty | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
// Copyright (c) 2015-present Jeevanandam M ([email protected]), All rights reserved. | ||
// resty source code and usage is governed by a MIT style | ||
// license that can be found in the LICENSE file. | ||
// SPDX-License-Identifier: MIT | ||
|
||
package resty | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters