fix: unittest

This commit is contained in:
debugtalk
2022-12-26 00:35:03 +08:00
parent 53fbfd3ef0
commit 45190462b6
4 changed files with 66 additions and 18 deletions

View File

@@ -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
}
}

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
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
}

View File

@@ -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 (