mirror of
https://github.com/httprunner/httprunner.git
synced 2026-05-12 02:21:29 +08:00
move convertCheckExpr to convertCompatTestCase
This commit is contained in:
17
convert.go
17
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,
|
||||
|
||||
@@ -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()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
15
response.go
15
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 {
|
||||
|
||||
@@ -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