mirror of
https://github.com/httprunner/httprunner.git
synced 2026-08-28 19:47:14 +08:00
move convertCheckExpr to convertCompatTestCase
This commit is contained in:
+17
@@ -6,6 +6,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/rs/zerolog/log"
|
"github.com/rs/zerolog/log"
|
||||||
"gopkg.in/yaml.v3"
|
"gopkg.in/yaml.v3"
|
||||||
@@ -92,6 +93,7 @@ func convertCompatTestCase(tc *TCase) (err error) {
|
|||||||
if msg, existed := validatorMap["msg"]; existed {
|
if msg, existed := validatorMap["msg"]; existed {
|
||||||
validator.Message = msg.(string)
|
validator.Message = msg.(string)
|
||||||
}
|
}
|
||||||
|
validator.Check = convertCheckExpr(validator.Check)
|
||||||
step.Validators[i] = validator
|
step.Validators[i] = validator
|
||||||
} else if len(validatorMap) == 1 {
|
} else if len(validatorMap) == 1 {
|
||||||
// HttpRunner validator format
|
// HttpRunner validator format
|
||||||
@@ -104,6 +106,7 @@ func convertCompatTestCase(tc *TCase) (err error) {
|
|||||||
validator.Assert = assertMethod
|
validator.Assert = assertMethod
|
||||||
validator.Expect = checkAndExpect[1]
|
validator.Expect = checkAndExpect[1]
|
||||||
}
|
}
|
||||||
|
validator.Check = convertCheckExpr(validator.Check)
|
||||||
step.Validators[i] = validator
|
step.Validators[i] = validator
|
||||||
} else {
|
} else {
|
||||||
return fmt.Errorf("unexpected validator format: %v", validatorMap)
|
return fmt.Errorf("unexpected validator format: %v", validatorMap)
|
||||||
@@ -113,6 +116,20 @@ func convertCompatTestCase(tc *TCase) (err error) {
|
|||||||
return err
|
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) {
|
func (tc *TCase) ToTestCase() (*TestCase, error) {
|
||||||
testCase := &TestCase{
|
testCase := &TestCase{
|
||||||
Config: tc.Config,
|
Config: tc.Config,
|
||||||
|
|||||||
@@ -34,3 +34,30 @@ func TestLoadCase(t *testing.T) {
|
|||||||
t.Fail()
|
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()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
-15
@@ -185,7 +185,6 @@ func (v *responseObject) Validate(iValidators []interface{}, variablesMapping ma
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (v *responseObject) searchJmespath(expr string) interface{} {
|
func (v *responseObject) searchJmespath(expr string) interface{} {
|
||||||
expr = convertJmespath(expr)
|
|
||||||
checkValue, err := jmespath.Search(expr, v.respObjMeta)
|
checkValue, err := jmespath.Search(expr, v.respObjMeta)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error().Str("expr", expr).Err(err).Msg("search jmespath failed")
|
log.Error().Str("expr", expr).Err(err).Msg("search jmespath failed")
|
||||||
@@ -201,20 +200,6 @@ func (v *responseObject) searchJmespath(expr string) interface{} {
|
|||||||
return checkValue
|
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{} {
|
func (v *responseObject) searchRegexp(expr string) interface{} {
|
||||||
respMap, ok := v.respObjMeta.(map[string]interface{})
|
respMap, ok := v.respObjMeta.(map[string]interface{})
|
||||||
if !ok {
|
if !ok {
|
||||||
|
|||||||
@@ -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()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user