fix: unittest

This commit is contained in:
debugtalk
2022-12-26 11:20:32 +08:00
parent 9ff44d0bad
commit 57afeadc26
4 changed files with 66 additions and 18 deletions
+3 -13
View File
@@ -2,20 +2,10 @@ curl httpbin.org
curl https://httpbin.org/get?key1=value1&key2=value2 curl https://httpbin.org/get?key1=value1&key2=value2
curl -H "Content-Type: application/json" \ 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"
-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 -F "dummyName=dummyFile" -F file1=@file1.txt -F file2=@file2.txt https://httpbin.org/post
curl 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'
-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://httpbing.org/post -H "Content-Type: application/x-www-form-urlencoded" --data "key1=value+1&key2=value%3A2"
+29 -3
View File
@@ -2,10 +2,13 @@ package cmd
import ( import (
"fmt" "fmt"
"io/ioutil"
"path/filepath"
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
"github.com/spf13/cobra" "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/myexec"
"github.com/httprunner/httprunner/v4/hrp/internal/version" "github.com/httprunner/httprunner/v4/hrp/internal/version"
"github.com/httprunner/httprunner/v4/hrp/pkg/convert" "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") log.Info().Str("outputType", outputType.String()).Msg("set default")
} }
var files []string
for _, arg := range args { for _, arg := range args {
if err := caseConverter.Convert(arg, fromType, outputType); err != nil { if builtin.IsFolderPathExists(arg) {
log.Error().Err(err).Str("path", 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()). Str("outputType", outputType.String()).
Msg("convert case failed") Msg("convert case failed")
return err
} }
} }
+14 -2
View File
@@ -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 s.Request.Body = item.Request.Body.Raw
contentType := s.Request.Headers["Content-Type"] contentType := s.Request.Headers["Content-Type"]
if strings.Contains(contentType, "application/json") { if strings.Contains(contentType, "application/json") || languageType == "json" {
var iBody interface{} var iBody interface{}
err = json.Unmarshal([]byte(item.Request.Body.Raw), &iBody) err = json.Unmarshal([]byte(item.Request.Body.Raw), &iBody)
if err != nil { if err != nil {
log.Warn().Err(err).Msg("33333")
return errors.Wrap(err, "make request body (raw -> json) failed") return errors.Wrap(err, "make request body (raw -> json) failed")
} }
s.Request.Body = iBody s.Request.Body = iBody
} }
if contentType == "" {
s.Request.Headers["Content-Type"] = contentTypeMap[languageType]
}
return return
} }
+20
View File
@@ -18,6 +18,7 @@ const (
suffixYAML = ".yaml" suffixYAML = ".yaml"
suffixGoTest = ".go" suffixGoTest = ".go"
suffixPyTest = ".py" suffixPyTest = ".py"
suffixHAR = ".har"
) )
type FromType int 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 type OutputType int
const ( const (