-
-
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.
feat(curl): generate curl cmd for request && example for curl cmd
- Loading branch information
Showing
9 changed files
with
373 additions
and
10 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,5 +26,6 @@ _testmain.go | |
coverage.out | ||
coverage.txt | ||
|
||
# Exclude intellij IDE folders | ||
# Exclude IDE folders | ||
.idea/* | ||
.vscode/* |
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 |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package examples | ||
|
||
import ( | ||
"net/http" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/go-resty/resty/v2" | ||
) | ||
|
||
// Example about generating curl command | ||
func TestDebugCurl(t *testing.T) { | ||
ts := createHttpbinServer(0) | ||
defer ts.Close() | ||
|
||
req := resty.New().R().SetBody(map[string]string{ | ||
"name": "Alex", | ||
}).SetCookies( | ||
[]*http.Cookie{ | ||
{ Name: "count", Value: "1", }, | ||
}, | ||
) | ||
|
||
// 1. Generate curl for request(dry-run: request isn't executed) | ||
curlCmdUnexecuted := req.GetCurlCmd() | ||
if !strings.Contains(curlCmdUnexecuted, "Cookie: count=1") || !strings.Contains(curlCmdUnexecuted, "curl -X GET") { | ||
t.Fatal("bad curl:", curlCmdUnexecuted) | ||
}else{ | ||
t.Log("curlCmdUnexecuted: \n",curlCmdUnexecuted) | ||
} | ||
|
||
// 2. Generate curl for request(request is executed) | ||
var curlCmdExecuted string | ||
req.SetResultCurlCmd(&curlCmdExecuted) | ||
if _, err := req.Post(ts.URL+"/post"); err != nil { | ||
t.Fatal(err) | ||
} | ||
if !strings.Contains(curlCmdExecuted, "Cookie: count=1") || !strings.Contains(curlCmdExecuted, "curl -X POST") { | ||
t.Fatal("bad curl:", curlCmdExecuted) | ||
}else{ | ||
t.Log("curlCmdExecuted: \n",curlCmdExecuted) | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,162 @@ | ||
package examples | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
ioutil "io" | ||
"net/http" | ||
"net/http/httptest" | ||
"net/url" | ||
"strings" | ||
) | ||
|
||
const maxMultipartMemory = 4 << 30 // 4MB | ||
|
||
// tlsCert: | ||
// | ||
// 0 No certificate | ||
// 1 With self-signed certificate | ||
// 2 With custom certificate from CA(todo) | ||
func createHttpbinServer(tlsCert int) (ts *httptest.Server) { | ||
ts = createTestServer(func(w http.ResponseWriter, r *http.Request) { | ||
httpbinHandler(w, r) | ||
}, tlsCert) | ||
|
||
return ts | ||
} | ||
|
||
func httpbinHandler(w http.ResponseWriter, r *http.Request) { | ||
w.Header().Set("Content-Type", "application/json") | ||
body, _ := ioutil.ReadAll(r.Body) | ||
r.Body = ioutil.NopCloser(bytes.NewBuffer(body)) // important!! | ||
m := map[string]interface{}{ | ||
"args": parseRequestArgs(r), | ||
"headers": dumpRequestHeader(r), | ||
"data": string(body), | ||
"json": nil, | ||
"form": map[string]string{}, | ||
"files": map[string]string{}, | ||
"method": r.Method, | ||
"origin": r.RemoteAddr, | ||
"url": r.URL.String(), | ||
} | ||
|
||
// 1. parse text/plain | ||
if strings.HasPrefix(r.Header.Get("Content-Type"), "text/plain") { | ||
m["data"] = string(body) | ||
} | ||
|
||
// 2. parse application/json | ||
if strings.HasPrefix(r.Header.Get("Content-Type"), "application/json") { | ||
var data interface{} | ||
if err := json.Unmarshal(body, &data); err != nil { | ||
m["err"] = err.Error() | ||
} else { | ||
m["json"] = data | ||
} | ||
} | ||
|
||
// 3. parse application/x-www-form-urlencoded | ||
if strings.HasPrefix(r.Header.Get("Content-Type"), "application/x-www-form-urlencoded") { | ||
m["form"] = parseQueryString(string(body)) | ||
} | ||
|
||
// 4. parse multipart/form-data | ||
if strings.HasPrefix(r.Header.Get("Content-Type"), "multipart/form-data") { | ||
form, files := readMultipartForm(r) | ||
m["form"] = form | ||
m["files"] = files | ||
} | ||
buf, _ := json.Marshal(m) | ||
_, _ = w.Write(buf) | ||
} | ||
|
||
func readMultipartForm(r *http.Request) (map[string]string, map[string]string) { | ||
if err := r.ParseMultipartForm(maxMultipartMemory); err != nil { | ||
if err != http.ErrNotMultipart { | ||
panic(fmt.Sprintf("error on parse multipart form array: %v", err)) | ||
} | ||
} | ||
// parse form data | ||
formData := make(map[string]string) | ||
for k, vs := range r.PostForm { | ||
for _, v := range vs { | ||
formData[k] = v | ||
} | ||
} | ||
// parse files | ||
files := make(map[string]string) | ||
if r.MultipartForm != nil && r.MultipartForm.File != nil { | ||
for key, fhs := range r.MultipartForm.File { | ||
// if len(fhs)>0 | ||
// f, err := fhs[0].Open() | ||
files[key] = fhs[0].Filename | ||
} | ||
} | ||
return formData, files | ||
} | ||
|
||
func dumpRequestHeader(req *http.Request) string { | ||
var res strings.Builder | ||
headers := sortHeaders(req) | ||
for _, kv := range headers { | ||
res.WriteString(kv[0] + ": " + kv[1] + "\n") | ||
} | ||
return res.String() | ||
} | ||
|
||
// sortHeaders | ||
func sortHeaders(request *http.Request) [][2]string { | ||
headers := [][2]string{} | ||
for k, vs := range request.Header { | ||
for _, v := range vs { | ||
headers = append(headers, [2]string{k, v}) | ||
} | ||
} | ||
n := len(headers) | ||
for i := 0; i < n; i++ { | ||
for j := n - 1; j > i; j-- { | ||
jj := j - 1 | ||
h1, h2 := headers[j], headers[jj] | ||
if h1[0] < h2[0] { | ||
headers[jj], headers[j] = headers[j], headers[jj] | ||
} | ||
} | ||
} | ||
return headers | ||
} | ||
|
||
func parseRequestArgs(request *http.Request) map[string]string { | ||
query := request.URL.RawQuery | ||
return parseQueryString(query) | ||
} | ||
|
||
func parseQueryString(query string) map[string]string { | ||
params := map[string]string{} | ||
paramsList, _ := url.ParseQuery(query) | ||
for key, vals := range paramsList { | ||
// params[key] = vals[len(vals)-1] | ||
params[key] = strings.Join(vals, ",") | ||
} | ||
return params | ||
} | ||
|
||
/* | ||
* | ||
- tlsCert: | ||
0 no certificate | ||
1 with self-signed certificate | ||
2 with custom certificate from CA(todo) | ||
*/ | ||
func createTestServer(fn func(w http.ResponseWriter, r *http.Request), tlsCert int) (ts *httptest.Server) { | ||
if tlsCert == 0 { | ||
// 1. http test server | ||
ts = httptest.NewServer(http.HandlerFunc(fn)) | ||
} else if tlsCert == 1{ | ||
// 2. https test server: https://stackoverflow.com/questions/54899550/create-https-test-server-for-any-client | ||
ts = httptest.NewUnstartedServer(http.HandlerFunc(fn)) | ||
ts.StartTLS() | ||
} | ||
return ts | ||
} |
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
Oops, something went wrong.