From 971483de12f7fe6ce552ee159f7730c87f609303 Mon Sep 17 00:00:00 2001 From: buyuxiang <347586493@qq.com> Date: Fri, 22 Jul 2022 21:04:01 +0800 Subject: [PATCH] set DisableFlagParsing: true --- docs/CHANGELOG.md | 2 + docs/cmd/hrp.md | 2 +- docs/cmd/hrp_boom.md | 4 +- docs/cmd/hrp_boom_curl.md | 13 +--- docs/cmd/hrp_build.md | 2 +- docs/cmd/hrp_convert.md | 4 +- docs/cmd/hrp_convert_curl.md | 13 +--- docs/cmd/hrp_har2case.md | 27 -------- docs/cmd/hrp_pytest.md | 2 +- docs/cmd/hrp_run.md | 4 +- docs/cmd/hrp_run_curl.md | 13 +--- docs/cmd/hrp_startproject.md | 2 +- docs/cmd/hrp_wiki.md | 2 +- hrp/cmd/curl.go | 87 +++++++------------------- hrp/internal/convert/converter.go | 30 ++++----- hrp/internal/convert/from_curl.go | 19 ++---- hrp/internal/convert/from_curl_test.go | 4 +- 17 files changed, 66 insertions(+), 164 deletions(-) delete mode 100644 docs/cmd/hrp_har2case.md diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index cf16caf4..bf0ea847 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -10,6 +10,8 @@ - feat: support omitting websocket url if not necessary - feat: support multiple websocket connections each session - fix: optimize websocket step initialization +- feat: support convert curl command(s) to testcase(s) +- feat: support run curl as subcommand of run/boom/convert ## v4.1.6 (2022-07-04) diff --git a/docs/cmd/hrp.md b/docs/cmd/hrp.md index 83eef0fe..b7aab39f 100644 --- a/docs/cmd/hrp.md +++ b/docs/cmd/hrp.md @@ -37,4 +37,4 @@ Copyright 2017 debugtalk * [hrp startproject](hrp_startproject.md) - create a scaffold project * [hrp wiki](hrp_wiki.md) - visit https://httprunner.com -###### Auto generated by spf13/cobra on 6-Jul-2022 +###### Auto generated by spf13/cobra on 22-Jul-2022 diff --git a/docs/cmd/hrp_boom.md b/docs/cmd/hrp_boom.md index d4bf9c1d..85ceb90e 100644 --- a/docs/cmd/hrp_boom.md +++ b/docs/cmd/hrp_boom.md @@ -41,6 +41,6 @@ hrp boom [flags] ### SEE ALSO * [hrp](hrp.md) - Next-Generation API Testing Solution. -* [hrp boom curl](hrp_boom_curl.md) - run load test with boomer using converted curl testcase +* [hrp boom curl](hrp_boom_curl.md) - run load test with boomer by curl command -###### Auto generated by spf13/cobra on 6-Jul-2022 +###### Auto generated by spf13/cobra on 22-Jul-2022 diff --git a/docs/cmd/hrp_boom_curl.md b/docs/cmd/hrp_boom_curl.md index b2aaa474..23b442aa 100644 --- a/docs/cmd/hrp_boom_curl.md +++ b/docs/cmd/hrp_boom_curl.md @@ -1,6 +1,6 @@ ## hrp boom curl -run load test with boomer using converted curl testcase +run load test with boomer by curl command ``` hrp boom curl URLs [flags] @@ -9,18 +9,11 @@ hrp boom curl URLs [flags] ### Options ``` - -b, --cookie strings -b, --cookie in curl - -d, --data strings -d, --data in curl - -F, --form strings -F, --form in curl - -G, --get -G, --get in curl - -I, --head -I, --head in curl - -H, --header strings -H, --header in curl - -h, --help help for curl - -X, --request string -X, --request in curl + -h, --help help for curl ``` ### SEE ALSO * [hrp boom](hrp_boom.md) - run load test with boomer -###### Auto generated by spf13/cobra on 6-Jul-2022 +###### Auto generated by spf13/cobra on 22-Jul-2022 diff --git a/docs/cmd/hrp_build.md b/docs/cmd/hrp_build.md index c85bded0..9e861f9b 100644 --- a/docs/cmd/hrp_build.md +++ b/docs/cmd/hrp_build.md @@ -28,4 +28,4 @@ hrp build $path ... [flags] * [hrp](hrp.md) - Next-Generation API Testing Solution. -###### Auto generated by spf13/cobra on 6-Jul-2022 +###### Auto generated by spf13/cobra on 22-Jul-2022 diff --git a/docs/cmd/hrp_convert.md b/docs/cmd/hrp_convert.md index b9e55141..2e6d1f57 100644 --- a/docs/cmd/hrp_convert.md +++ b/docs/cmd/hrp_convert.md @@ -21,6 +21,6 @@ hrp convert $path... [flags] ### SEE ALSO * [hrp](hrp.md) - Next-Generation API Testing Solution. -* [hrp convert curl](hrp_convert_curl.md) - convert curl command(s) to httprunner testcase +* [hrp convert curl](hrp_convert_curl.md) - convert curl command to httprunner testcase -###### Auto generated by spf13/cobra on 6-Jul-2022 +###### Auto generated by spf13/cobra on 22-Jul-2022 diff --git a/docs/cmd/hrp_convert_curl.md b/docs/cmd/hrp_convert_curl.md index 8490123c..87b089bf 100644 --- a/docs/cmd/hrp_convert_curl.md +++ b/docs/cmd/hrp_convert_curl.md @@ -1,6 +1,6 @@ ## hrp convert curl -convert curl command(s) to httprunner testcase +convert curl command to httprunner testcase ``` hrp convert curl URLs [flags] @@ -9,18 +9,11 @@ hrp convert curl URLs [flags] ### Options ``` - -b, --cookie strings -b, --cookie in curl - -d, --data strings -d, --data in curl - -F, --form strings -F, --form in curl - -G, --get -G, --get in curl - -I, --head -I, --head in curl - -H, --header strings -H, --header in curl - -h, --help help for curl - -X, --request string -X, --request in curl + -h, --help help for curl ``` ### SEE ALSO * [hrp convert](hrp_convert.md) - convert to JSON/YAML/gotest/pytest testcases -###### Auto generated by spf13/cobra on 6-Jul-2022 +###### Auto generated by spf13/cobra on 22-Jul-2022 diff --git a/docs/cmd/hrp_har2case.md b/docs/cmd/hrp_har2case.md deleted file mode 100644 index 0919562e..00000000 --- a/docs/cmd/hrp_har2case.md +++ /dev/null @@ -1,27 +0,0 @@ -## hrp har2case - -convert HAR to json/yaml testcase files - -### Synopsis - -convert HAR to json/yaml testcase files - -``` -hrp har2case $har_path... [flags] -``` - -### Options - -``` - -h, --help help for har2case - -d, --output-dir string specify output directory, default to the same dir with har file - -p, --profile string specify profile path to override headers and cookies - -j, --to-json convert to JSON format (default) - -y, --to-yaml convert to YAML format -``` - -### SEE ALSO - -* [hrp](hrp.md) - Next-Generation API Testing Solution. - -###### Auto generated by spf13/cobra on 29-May-2022 diff --git a/docs/cmd/hrp_pytest.md b/docs/cmd/hrp_pytest.md index f50e22a7..c3c9bc27 100644 --- a/docs/cmd/hrp_pytest.md +++ b/docs/cmd/hrp_pytest.md @@ -16,4 +16,4 @@ hrp pytest $path ... [flags] * [hrp](hrp.md) - Next-Generation API Testing Solution. -###### Auto generated by spf13/cobra on 6-Jul-2022 +###### Auto generated by spf13/cobra on 22-Jul-2022 diff --git a/docs/cmd/hrp_run.md b/docs/cmd/hrp_run.md index 1eb95f2b..a64b6a76 100644 --- a/docs/cmd/hrp_run.md +++ b/docs/cmd/hrp_run.md @@ -34,6 +34,6 @@ hrp run $path... [flags] ### SEE ALSO * [hrp](hrp.md) - Next-Generation API Testing Solution. -* [hrp run curl](hrp_run_curl.md) - run API test with go engine using converted curl testcase +* [hrp run curl](hrp_run_curl.md) - run API test with go engine by curl command -###### Auto generated by spf13/cobra on 6-Jul-2022 +###### Auto generated by spf13/cobra on 22-Jul-2022 diff --git a/docs/cmd/hrp_run_curl.md b/docs/cmd/hrp_run_curl.md index ff6906bb..78df21be 100644 --- a/docs/cmd/hrp_run_curl.md +++ b/docs/cmd/hrp_run_curl.md @@ -1,6 +1,6 @@ ## hrp run curl -run API test with go engine using converted curl testcase +run API test with go engine by curl command ``` hrp run curl URLs [flags] @@ -9,18 +9,11 @@ hrp run curl URLs [flags] ### Options ``` - -b, --cookie strings -b, --cookie in curl - -d, --data strings -d, --data in curl - -F, --form strings -F, --form in curl - -G, --get -G, --get in curl - -I, --head -I, --head in curl - -H, --header strings -H, --header in curl - -h, --help help for curl - -X, --request string -X, --request in curl + -h, --help help for curl ``` ### SEE ALSO * [hrp run](hrp_run.md) - run API test with go engine -###### Auto generated by spf13/cobra on 6-Jul-2022 +###### Auto generated by spf13/cobra on 22-Jul-2022 diff --git a/docs/cmd/hrp_startproject.md b/docs/cmd/hrp_startproject.md index ef96dd1f..e5877aab 100644 --- a/docs/cmd/hrp_startproject.md +++ b/docs/cmd/hrp_startproject.md @@ -21,4 +21,4 @@ hrp startproject $project_name [flags] * [hrp](hrp.md) - Next-Generation API Testing Solution. -###### Auto generated by spf13/cobra on 6-Jul-2022 +###### Auto generated by spf13/cobra on 22-Jul-2022 diff --git a/docs/cmd/hrp_wiki.md b/docs/cmd/hrp_wiki.md index fd27525a..41754c3b 100644 --- a/docs/cmd/hrp_wiki.md +++ b/docs/cmd/hrp_wiki.md @@ -16,4 +16,4 @@ hrp wiki [flags] * [hrp](hrp.md) - Next-Generation API Testing Solution. -###### Auto generated by spf13/cobra on 6-Jul-2022 +###### Auto generated by spf13/cobra on 22-Jul-2022 diff --git a/hrp/cmd/curl.go b/hrp/cmd/curl.go index bc67fb79..8f5970a9 100644 --- a/hrp/cmd/curl.go +++ b/hrp/cmd/curl.go @@ -1,6 +1,7 @@ package cmd import ( + "fmt" "os" "strings" @@ -13,9 +14,10 @@ import ( ) var runCurlCmd = &cobra.Command{ - Use: "curl URLs", - Short: "run API test with go engine by curl command", - Args: cobra.MinimumNArgs(1), + Use: "curl URLs", + Short: "run API test with go engine by curl command", + Args: cobra.MinimumNArgs(1), + DisableFlagParsing: true, PreRun: func(cmd *cobra.Command, args []string) { setLogLevel(logLevel) }, @@ -28,11 +30,12 @@ var runCurlCmd = &cobra.Command{ } var boomCurlCmd = &cobra.Command{ - Use: "curl URLs", - Short: "run load test with boomer by curl command", - Args: cobra.MinimumNArgs(1), + Use: "curl URLs", + Short: "run load test with boomer by curl command", + Args: cobra.MinimumNArgs(1), + DisableFlagParsing: true, PreRun: func(cmd *cobra.Command, args []string) { - boomer.SetUlimit(10240) // ulimit -n 10240 + boomer.SetUlimit(10240) if !strings.EqualFold(logLevel, "DEBUG") { logLevel = "WARN" // disable info logs for load testing } @@ -45,9 +48,10 @@ var boomCurlCmd = &cobra.Command{ } var convertCurlCmd = &cobra.Command{ - Use: "curl URLs", - Short: "convert curl command to httprunner testcase", - Args: cobra.MinimumNArgs(1), + Use: "curl URLs", + Short: "convert curl command to httprunner testcase", + Args: cobra.MinimumNArgs(1), + DisableFlagParsing: true, PreRun: func(cmd *cobra.Command, args []string) { setLogLevel(logLevel) }, @@ -57,35 +61,10 @@ var convertCurlCmd = &cobra.Command{ }, } -var ( - cookieSlice []string - dataSlice []string - formSlice []string - get bool - head bool - headerSlice []string - request string -) - func init() { runCmd.AddCommand(runCurlCmd) - addCurlFlags(runCurlCmd) - boomCmd.AddCommand(boomCurlCmd) - addCurlFlags(boomCurlCmd) - convertCmd.AddCommand(convertCurlCmd) - addCurlFlags(convertCurlCmd) -} - -func addCurlFlags(cmd *cobra.Command) { - cmd.Flags().StringSliceVarP(&cookieSlice, "cookie", "b", nil, "-b, --cookie in curl") - cmd.Flags().StringSliceVarP(&dataSlice, "data", "d", nil, "-d, --data in curl") - cmd.Flags().StringSliceVarP(&formSlice, "form", "F", nil, "-F, --form in curl") - cmd.Flags().BoolVarP(&get, "get", "G", false, "-G, --get in curl") - cmd.Flags().BoolVarP(&head, "head", "I", false, "-I, --head in curl") - cmd.Flags().StringSliceVarP(&headerSlice, "header", "H", nil, "-H, --header in curl") - cmd.Flags().StringVarP(&request, "request", "X", "", "-X, --request in curl") } func makeCurlTestCase(args []string) *hrp.TestCase { @@ -95,11 +74,7 @@ func makeCurlTestCase(args []string) *hrp.TestCase { log.Error().Err(err).Msg("convert curl command failed") os.Exit(1) } - casePath, err := os.Getwd() - if err != nil { - casePath = "" - log.Error().Err(err).Msg("get working directory failed") - } + casePath, _ := os.Getwd() testCase, err := tCase.ToTestCase(casePath) if err != nil { log.Error().Err(err).Msg("convert testcase to failed") @@ -109,29 +84,13 @@ func makeCurlTestCase(args []string) *hrp.TestCase { } func makeCurlCommand(args []string) string { - var cmdList []string - cmdList = append(cmdList, "curl") - for _, c := range cookieSlice { - cmdList = append(cmdList, "--cookie", c) + for i := 0; i < len(args); i++ { + if !strings.HasPrefix(args[i], "-") { + args[i] = fmt.Sprintf("\"%s\"", args[i]) + } } - for _, d := range dataSlice { - cmdList = append(cmdList, "--data", d) - } - for _, f := range formSlice { - cmdList = append(cmdList, "--form", f) - } - if get { - cmdList = append(cmdList, "--get") - } - if head { - cmdList = append(cmdList, "--head") - } - for _, h := range headerSlice { - cmdList = append(cmdList, "--header", h) - } - if request != "" { - cmdList = append(cmdList, "--request", request) - } - cmdList = append(cmdList, args...) - return strings.Join(cmdList, " ") + var curlCmd []string + curlCmd = append(curlCmd, "curl") + curlCmd = append(curlCmd, args...) + return strings.Join(curlCmd, " ") } diff --git a/hrp/internal/convert/converter.go b/hrp/internal/convert/converter.go index e1613679..4c69296e 100644 --- a/hrp/internal/convert/converter.go +++ b/hrp/internal/convert/converter.go @@ -104,22 +104,22 @@ func Run(outputType OutputType, outputDir, profilePath string, args []string) { } // LoadTCase loads source file and convert to TCase type -func LoadTCase(path string) (*hrp.TCase, error) { - if strings.HasPrefix(path, "curl ") { +func LoadTCase(inputSample string) (*hrp.TCase, error) { + if strings.HasPrefix(inputSample, "curl ") { // 'path' contains curl command - curlCase, err := LoadSingleCurlCase(path) + curlCase, err := LoadSingleCurlCase(inputSample) if err != nil { return nil, err } return curlCase, nil } - extName := filepath.Ext(path) + extName := filepath.Ext(inputSample) if extName == "" { return nil, errors.New("file extension is not specified") } switch extName { case ".har": - tCase, err := LoadHARCase(path) + tCase, err := LoadHARCase(inputSample) if err != nil { return nil, err } @@ -127,19 +127,19 @@ func LoadTCase(path string) (*hrp.TCase, error) { case ".json": // priority: hrp JSON case > postman > swagger // check if hrp JSON case - tCase, err := LoadJSONCase(path) + tCase, err := LoadJSONCase(inputSample) if err == nil { return tCase, nil } // check if postman format - casePostman, err := LoadPostmanCase(path) + casePostman, err := LoadPostmanCase(inputSample) if err == nil { return casePostman, nil } // check if swagger format - caseSwagger, err := LoadSwaggerCase(path) + caseSwagger, err := LoadSwaggerCase(inputSample) if err == nil { return caseSwagger, nil } @@ -148,13 +148,13 @@ func LoadTCase(path string) (*hrp.TCase, error) { case ".yaml", ".yml": // priority: hrp YAML case > swagger // check if hrp YAML case - tCase, err := NewYAMLCase(path) + tCase, err := NewYAMLCase(inputSample) if err == nil { return tCase, nil } // check if swagger format - caseSwagger, err := LoadSwaggerCase(path) + caseSwagger, err := LoadSwaggerCase(inputSample) if err == nil { return caseSwagger, nil } @@ -167,7 +167,7 @@ func LoadTCase(path string) (*hrp.TCase, error) { case ".jmx": // TODO return nil, errors.New("convert JMeter jmx is not implemented") case ".txt": - curlCase, err := LoadCurlCase(path) + curlCase, err := LoadCurlCase(inputSample) if err != nil { return nil, err } @@ -186,16 +186,12 @@ type TCaseConverter struct { func (c *TCaseConverter) genOutputPath(suffix string) string { var outFileFullName string - if curlCmd := strings.TrimSpace(c.InputSample); strings.HasPrefix(curlCmd, "curl") { + if curlCmd := strings.TrimSpace(c.InputSample); strings.HasPrefix(curlCmd, "curl ") { outFileFullName = fmt.Sprintf("curl_%v_test%v", time.Now().Format("20060102150405"), suffix) if c.OutputDir != "" { return filepath.Join(c.OutputDir, outFileFullName) } else { - curWorkDir, err := os.Getwd() - if err != nil { - log.Error().Err(err).Msg("get current working direction failed") - os.Exit(1) - } + curWorkDir, _ := os.Getwd() return filepath.Join(curWorkDir, outFileFullName) } } diff --git a/hrp/internal/convert/from_curl.go b/hrp/internal/convert/from_curl.go index ca73b564..7c60bf76 100644 --- a/hrp/internal/convert/from_curl.go +++ b/hrp/internal/convert/from_curl.go @@ -198,9 +198,9 @@ func parseCaseCurl(cmd string) (CaseCurl, error) { type CaseCurl map[string][]string -// GetByIndex gets the value by index associated with the given key. -// If there are no value by index associated with the key, GetByIndex returns the empty string. -func (c CaseCurl) GetByIndex(key string, index int) string { +// Get gets the first value associated with the given key. +// If there are no values associated with the key, Get returns the empty string. +func (c CaseCurl) Get(key string, index int) string { if c == nil { return "" } @@ -301,7 +301,7 @@ type stepFromCurl struct { } func (s *stepFromCurl) makeRequestName(c CaseCurl) error { - s.Name = c.GetByIndex(originCmdKey, 0) + s.Name = c.Get(originCmdKey, 0) return nil } @@ -315,7 +315,7 @@ func (s *stepFromCurl) makeRequestMethod(c CaseCurl) error { s.Request.Method = http.MethodHead } if c.HaveKey("--request") { - s.Request.Method = hrp.HTTPMethod(strings.ToUpper(c.GetByIndex("--request", 0))) + s.Request.Method = hrp.HTTPMethod(strings.ToUpper(c.Get("--request", 0))) } return nil } @@ -375,8 +375,6 @@ func (s *stepFromCurl) makeRequestHeader(headerExpr string) error { var headerValue string if i < len(headerExpr)-1 { headerValue = strings.TrimSpace(headerExpr[i+1:]) - } else { - headerValue = "" } if strings.ToLower(headerKey) == "host" { // headerExpr modifying internal header like "Host:" @@ -433,8 +431,6 @@ func (s *stepFromCurl) makeRequestCookie(cookieExpr string) error { var cookieValue string if i < len(cookie)-1 { cookieValue = strings.TrimSpace(cookie[i+1:]) - } else { - cookieValue = "" } s.Request.Cookies[cookieKey] = cookieValue } @@ -497,11 +493,8 @@ func (s *stepFromCurl) makeRequestForm(formList []string) error { var formValue string if i < len(formExpr)-1 { formValue = strings.TrimSpace(formExpr[i+1:]) - } else { - formValue = "" } - filePath := strings.TrimLeft(formValue, "@") - s.Request.Upload[formKey] = strings.Trim(filePath, "\"") + s.Request.Upload[formKey] = strings.Trim(formValue, "\"") } } return nil diff --git a/hrp/internal/convert/from_curl_test.go b/hrp/internal/convert/from_curl_test.go index dacf10c7..a9b5f4f2 100644 --- a/hrp/internal/convert/from_curl_test.go +++ b/hrp/internal/convert/from_curl_test.go @@ -66,8 +66,8 @@ func TestLoadCurlCase(t *testing.T) { // curl -F "dummyName=dummyFile" -F file1=@file1.txt -F file2=@file2.txt https://httpbin.org/post if !assert.Equal(t, map[string]interface{}{ "dummyName": "dummyFile", - "file1": "file1.txt", - "file2": "file2.txt", + "file1": "@file1.txt", + "file2": "@file2.txt", }, tCase.TestSteps[3].Request.Upload) { t.Fatal() }