diff --git a/convert.go b/convert.go index 7bbad0c6..8ef22ea7 100644 --- a/convert.go +++ b/convert.go @@ -6,6 +6,7 @@ import ( "fmt" "os" "path/filepath" + "strings" "github.com/rs/zerolog/log" "gopkg.in/yaml.v3" @@ -92,6 +93,7 @@ func convertCompatTestCase(tc *TCase) (err error) { if msg, existed := validatorMap["msg"]; existed { validator.Message = msg.(string) } + validator.Check = convertCheckExpr(validator.Check) step.Validators[i] = validator } else if len(validatorMap) == 1 { // HttpRunner validator format @@ -104,6 +106,7 @@ func convertCompatTestCase(tc *TCase) (err error) { validator.Assert = assertMethod validator.Expect = checkAndExpect[1] } + validator.Check = convertCheckExpr(validator.Check) step.Validators[i] = validator } else { return fmt.Errorf("unexpected validator format: %v", validatorMap) @@ -113,6 +116,20 @@ func convertCompatTestCase(tc *TCase) (err error) { return err } +// convertCheckExpr deals with check expression including hyphen +func convertCheckExpr(checkExpr string) string { + if strings.Contains(checkExpr, textExtractorSubRegexp) { + return checkExpr + } + checkItems := strings.Split(checkExpr, ".") + for i, checkItem := range checkItems { + if strings.Contains(checkItem, "-") && !strings.Contains(checkItem, "\"") { + checkItems[i] = fmt.Sprintf("\"%s\"", checkItem) + } + } + return strings.Join(checkItems, ".") +} + func (tc *TCase) ToTestCase() (*TestCase, error) { testCase := &TestCase{ Config: tc.Config, diff --git a/convert_test.go b/convert_test.go index 53a88e56..75aea861 100644 --- a/convert_test.go +++ b/convert_test.go @@ -34,3 +34,30 @@ func TestLoadCase(t *testing.T) { t.Fail() } } + +func Test_convertCheckExpr(t *testing.T) { + exprs := []struct { + before string + after string + }{ + // normal check expression + {"a.b.c", "a.b.c"}, + {"headers.\"Content-Type\"", "headers.\"Content-Type\""}, + // check expression using regex + {"covering (.*) testing,", "covering (.*) testing,"}, + {" (.*) a-b-c", " (.*) a-b-c"}, + // abnormal check expression + {"-", "\"-\""}, + {"b-c", "\"b-c\""}, + {"a.b-c.d", "a.\"b-c\".d"}, + {"a-b.c-d", "\"a-b\".\"c-d\""}, + {"\"a-b\".c-d", "\"a-b\".\"c-d\""}, + {"headers.Content-Type", "headers.\"Content-Type\""}, + {"body.I-am-a-Key.name", "body.\"I-am-a-Key\".name"}, + } + for _, expr := range exprs { + if !assert.Equal(t, convertCheckExpr(expr.before), expr.after) { + t.Fail() + } + } +} diff --git a/response.go b/response.go index 3ed46dc0..1085c382 100644 --- a/response.go +++ b/response.go @@ -185,7 +185,6 @@ func (v *responseObject) Validate(iValidators []interface{}, variablesMapping ma } func (v *responseObject) searchJmespath(expr string) interface{} { - expr = convertJmespath(expr) checkValue, err := jmespath.Search(expr, v.respObjMeta) if err != nil { log.Error().Str("expr", expr).Err(err).Msg("search jmespath failed") @@ -201,20 +200,6 @@ func (v *responseObject) searchJmespath(expr string) interface{} { return checkValue } -// convertJmespath deals with check expression including hyphen -func convertJmespath(checkExpr string) string { - if strings.Contains(checkExpr, textExtractorSubRegexp) { - return checkExpr - } - checkItems := strings.Split(checkExpr, ".") - for i, checkItem := range checkItems { - if strings.Contains(checkItem, "-") && !strings.Contains(checkItem, "\"") { - checkItems[i] = fmt.Sprintf("\"%s\"", checkItem) - } - } - return strings.Join(checkItems, ".") -} - func (v *responseObject) searchRegexp(expr string) interface{} { respMap, ok := v.respObjMeta.(map[string]interface{}) if !ok { diff --git a/response_test.go b/response_test.go index 89aa9003..cfd3143d 100644 --- a/response_test.go +++ b/response_test.go @@ -33,30 +33,3 @@ func TestSearchRegexp(t *testing.T) { } } } - -func Test_convertJmespath(t *testing.T) { - exprs := []struct { - before string - after string - }{ - // normal check expression - {"a.b.c", "a.b.c"}, - {"headers.\"Content-Type\"", "headers.\"Content-Type\""}, - // check expression using regex - {"covering (.*) testing,", "covering (.*) testing,"}, - {" (.*) a-b-c", " (.*) a-b-c"}, - // abnormal check expression - {"-", "\"-\""}, - {"b-c", "\"b-c\""}, - {"a.b-c.d", "a.\"b-c\".d"}, - {"a-b.c-d", "\"a-b\".\"c-d\""}, - {"\"a-b\".c-d", "\"a-b\".\"c-d\""}, - {"headers.Content-Type", "headers.\"Content-Type\""}, - {"body.I-am-a-Key.name", "body.\"I-am-a-Key\".name"}, - } - for _, expr := range exprs { - if !assert.Equal(t, convertJmespath(expr.before), expr.after) { - t.Fail() - } - } -}