From 57afeadc2694532969f5345f4727071210d96ec1 Mon Sep 17 00:00:00 2001 From: debugtalk Date: Mon, 26 Dec 2022 00:35:03 +0800 Subject: [PATCH] fix: unittest --- examples/data/curl/curl_examples.txt | 16 +++----------- hrp/cmd/convert.go | 32 +++++++++++++++++++++++++--- hrp/pkg/convert/from_postman.go | 16 ++++++++++++-- hrp/pkg/convert/main.go | 20 +++++++++++++++++ 4 files changed, 66 insertions(+), 18 deletions(-) diff --git a/examples/data/curl/curl_examples.txt b/examples/data/curl/curl_examples.txt index d6aa3d9f..6d8e6a7e 100644 --- a/examples/data/curl/curl_examples.txt +++ b/examples/data/curl/curl_examples.txt @@ -2,20 +2,10 @@ curl httpbin.org curl https://httpbin.org/get?key1=value1&key2=value2 -curl -H "Content-Type: application/json" \ - -H "Authorization: Bearer b7d03a6947b217efb6f3ec3bd3504582" \ - -d '{"type":"A","name":"www","data":"162.10.66.0","priority":null,"port":null,"weight":null}' \ - "https://httpbin.org/post" +curl -H "Content-Type: application/json" -H "Authorization: Bearer b7d03a6947b217efb6f3ec3bd3504582" -d '{"type":"A","name":"www","data":"162.10.66.0","priority":null,"port":null,"weight":null}' "https://httpbin.org/post" curl -F "dummyName=dummyFile" -F file1=@file1.txt -F file2=@file2.txt https://httpbin.org/post -curl https://httpbin.org/post \ - -d 'shipment[to_address][id]=adr_HrBKVA85' \ - -d 'shipment[from_address][id]=adr_VtuTOj7o' \ - -d 'shipment[parcel][id]=prcl_WDv2VzHp' \ - -d 'shipment[is_return]=true' \ - -d 'shipment[customs_info][id]=cstinfo_bl5sE20Y' - -curl https://httpbing.org/post -H "Content-Type: application/x-www-form-urlencoded" \ - --data "key1=value+1&key2=value%3A2" +curl https://httpbin.org/post -d 'shipment[to_address][id]=adr_HrBKVA85' -d 'shipment[from_address][id]=adr_VtuTOj7o' -d 'shipment[parcel][id]=prcl_WDv2VzHp' -d 'shipment[is_return]=true' -d 'shipment[customs_info][id]=cstinfo_bl5sE20Y' +curl https://httpbing.org/post -H "Content-Type: application/x-www-form-urlencoded" --data "key1=value+1&key2=value%3A2" diff --git a/hrp/cmd/convert.go b/hrp/cmd/convert.go index ac6617c9..203896f5 100644 --- a/hrp/cmd/convert.go +++ b/hrp/cmd/convert.go @@ -2,10 +2,13 @@ package cmd import ( "fmt" + "io/ioutil" + "path/filepath" "github.com/rs/zerolog/log" "github.com/spf13/cobra" + "github.com/httprunner/httprunner/v4/hrp/internal/builtin" "github.com/httprunner/httprunner/v4/hrp/internal/myexec" "github.com/httprunner/httprunner/v4/hrp/internal/version" "github.com/httprunner/httprunner/v4/hrp/pkg/convert" @@ -55,12 +58,35 @@ var convertCmd = &cobra.Command{ log.Info().Str("outputType", outputType.String()).Msg("set default") } + var files []string for _, arg := range args { - if err := caseConverter.Convert(arg, fromType, outputType); err != nil { - log.Error().Err(err).Str("path", arg). + if builtin.IsFolderPathExists(arg) { + fs, err := ioutil.ReadDir(arg) + if err != nil { + log.Error().Err(err).Str("path", arg).Msg("read dir failed") + continue + } + for _, f := range fs { + files = append(files, filepath.Join(arg, f.Name())) + } + } else { + files = append(files, arg) + } + } + + for _, file := range files { + extName := filepath.Ext(file) + if !builtin.Contains(fromType.Extensions(), extName) { + log.Warn().Str("path", file). + Strs("expectExtensions", fromType.Extensions()). + Msg("skip file") + continue + } + + if err := caseConverter.Convert(file, fromType, outputType); err != nil { + log.Error().Err(err).Str("path", file). Str("outputType", outputType.String()). Msg("convert case failed") - return err } } diff --git a/hrp/pkg/convert/from_postman.go b/hrp/pkg/convert/from_postman.go index 99774bea..8518789b 100644 --- a/hrp/pkg/convert/from_postman.go +++ b/hrp/pkg/convert/from_postman.go @@ -330,17 +330,29 @@ func (s *stepFromPostman) makeRequestBodyRaw(item *TItem) (err error) { } }() + languageType := "text" + iOptions := item.Request.Body.Options + if iOptions != nil { + iLanguage := iOptions.(map[string]interface{})["raw"] + if iLanguage != nil { + languageType = iLanguage.(map[string]interface{})["language"].(string) + } + } + s.Request.Body = item.Request.Body.Raw contentType := s.Request.Headers["Content-Type"] - if strings.Contains(contentType, "application/json") { + if strings.Contains(contentType, "application/json") || languageType == "json" { var iBody interface{} err = json.Unmarshal([]byte(item.Request.Body.Raw), &iBody) if err != nil { - log.Warn().Err(err).Msg("33333") return errors.Wrap(err, "make request body (raw -> json) failed") } s.Request.Body = iBody } + + if contentType == "" { + s.Request.Headers["Content-Type"] = contentTypeMap[languageType] + } return } diff --git a/hrp/pkg/convert/main.go b/hrp/pkg/convert/main.go index 602489fe..7ac5a238 100644 --- a/hrp/pkg/convert/main.go +++ b/hrp/pkg/convert/main.go @@ -18,6 +18,7 @@ const ( suffixYAML = ".yaml" suffixGoTest = ".go" suffixPyTest = ".py" + suffixHAR = ".har" ) type FromType int @@ -54,6 +55,25 @@ func (fromType FromType) String() string { } } +func (fromType FromType) Extensions() []string { + switch fromType { + case FromTypeYAML: + return []string{suffixYAML, ".yml"} + case FromTypeHAR: + return []string{suffixHAR} + case FromTypePostman, FromTypeSwagger: + return []string{suffixJSON} + case FromTypeCurl: + return []string{".txt", ".curl"} + case FromTypeGotest: + return []string{suffixGoTest} + case FromTypePyest: + return []string{suffixPyTest} + default: + return []string{suffixJSON} + } +} + type OutputType int const (