set DisableFlagParsing: true

This commit is contained in:
buyuxiang
2022-07-22 21:04:01 +08:00
parent 3c160aeec6
commit 971483de12
17 changed files with 66 additions and 164 deletions

View File

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

View File

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

View File

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