fix: func convertCompatTestCase

This commit is contained in:
buyuxiang
2022-02-28 16:51:31 +08:00
parent 344aba681c
commit b5629071e1
2 changed files with 23 additions and 33 deletions
+22 -33
View File
@@ -6,7 +6,6 @@ import (
"fmt" "fmt"
"os" "os"
"path/filepath" "path/filepath"
"reflect"
"strings" "strings"
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
@@ -71,25 +70,9 @@ func convertCompatTestCase(tc *TCase) (err error) {
// 1. deal with body compatible with HttpRunner // 1. deal with body compatible with HttpRunner
if step.Request.Body == nil { if step.Request.Body == nil {
if step.Request.Json != nil { if step.Request.Json != nil {
// Content-Type is "application/json; charset=UTF-8"
step.Request.Body = step.Request.Json step.Request.Body = step.Request.Json
} else if step.Request.Data != nil { } else if step.Request.Data != nil {
dataValue := reflect.ValueOf(step.Request.Data) step.Request.Body = step.Request.Data
switch dataValue.Kind() {
case reflect.String:
// Content-Type is "text/plain"
step.Request.Body = dataValue.String()
case reflect.Map:
// Content-Type is "application/x-www-form-urlencoded"
var paramsList []string
mapRange := dataValue.MapRange()
for mapRange.Next() {
paramsList = append(paramsList, fmt.Sprintf("%s=%s", mapRange.Key(), mapRange.Value()))
}
step.Request.Body = strings.Join(paramsList, "&")
default:
log.Error().Msgf("[convert compat testcase] unexpected body type: %v", dataValue.Kind())
}
} }
} }
@@ -110,31 +93,37 @@ func convertCompatTestCase(tc *TCase) (err error) {
if msg, exist := validatorMap["msg"]; exist { if msg, exist := validatorMap["msg"]; exist {
validator.Message = msg.(string) validator.Message = msg.(string)
} }
convertCompatHeader(&validator)
step.Validators = append(step.Validators, validator)
} else if len(validatorMap) == 1 { } else if len(validatorMap) == 1 {
// HttpRunner validator format // HttpRunner validator format
validatorValue := reflect.ValueOf(validatorMap) for assertMethod, iValidatorContent := range validatorMap {
assertMethod := validatorValue.MapKeys()[0] checkAndExpect := iValidatorContent.([]interface{})
iValidatorContent := validatorValue.MapIndex(assertMethod).Interface().([]interface{}) validator.Check = checkAndExpect[0].(string)
validator.Check = iValidatorContent[0].(string) validator.Assert = assertMethod
validator.Assert = assertMethod.String() validator.Expect = checkAndExpect[1]
validator.Expect = iValidatorContent[1] }
convertCompatHeader(&validator)
step.Validators = append(step.Validators, validator)
} else { } else {
log.Error().Msgf("[convert compat testcase] unexpected validator format: %v", validatorMap) log.Error().Msgf("[convert compat testcase] unexpected validator format: %v", validatorMap)
} }
// deal with headers format in HttpRunner
// e.g. headers.Content-Type => headers.\"Content-Type\"
if strings.Contains(validator.Check, "headers.") &&
!strings.Contains(validator.Check, "\"") &&
strings.Contains(validator.Check, "-") {
replacedHeader := fmt.Sprintf("headers.\"%s\"", validator.Check[len("headers."):])
validator.Check = replacedHeader
}
step.Validators = append(step.Validators, validator)
} }
} }
return err return err
} }
// convertCompatHeader deals with headers format in HttpRunner
// e.g. headers.Content-Type => headers.\"Content-Type\"
func convertCompatHeader(validator *Validator) {
if strings.Contains(validator.Check, "headers.") &&
!strings.Contains(validator.Check, "\"") &&
strings.Contains(validator.Check, "-") {
replacedHeader := fmt.Sprintf("headers.\"%s\"", validator.Check[len("headers."):])
validator.Check = replacedHeader
}
}
func (tc *TCase) ToTestCase() (*TestCase, error) { func (tc *TCase) ToTestCase() (*TestCase, error) {
testCase := &TestCase{ testCase := &TestCase{
Config: tc.Config, Config: tc.Config,
+1
View File
@@ -3,6 +3,7 @@
## v0.6.3 (2022-02-22) ## v0.6.3 (2022-02-22)
- feat: support customized setup/teardown hooks (variable assignment not supported) - feat: support customized setup/teardown hooks (variable assignment not supported)
- compat: support testcase generated by HttpRunner
## v0.6.2 (2022-02-22) ## v0.6.2 (2022-02-22)