diff --git a/examples/function_test.go b/examples/function_test.go index cd2c2d98..44c8c590 100644 --- a/examples/function_test.go +++ b/examples/function_test.go @@ -19,14 +19,18 @@ func TestCaseCallFunction(t *testing.T) { TestSteps: []hrp.IStep{ hrp.NewStep("get with params"). GET("/get"). - WithParams(map[string]interface{}{"foo1": "${gen_random_string($n)}", "foo2": "${max($a, $b)}"}). + WithParams(map[string]interface{}{"foo1": "${gen_random_string($n)}", "foo2": "${max($a, $b)}", "foo3": "Foo3"}). WithHeaders(map[string]string{"User-Agent": "HttpRunnerPlus"}). Extract(). WithJmesPath("body.args.foo1", "varFoo1"). Validate(). AssertEqual("status_code", 200, "check status code"). AssertLengthEqual("body.args.foo1", 5, "check args foo1"). - AssertEqual("body.args.foo2", "12.3", "check args foo2"), // notice: request params value will be converted to string + AssertEqual("body.args.foo2", "12.3", "check args foo2"). + AssertTypeMatch("body.args.foo3", "str", "check args foo3 is type string"). + AssertStringEqual("body.args.foo3", "foo3", "check args foo3 case-insensitivity"). + AssertContains("body.args.foo3", "Foo", "check contains "). + AssertContainedBy("body.args.foo3", "this is Foo3 test", "check contained by"), // notice: request params value will be converted to string hrp.NewStep("post json data with functions"). POST("/post"). WithHeaders(map[string]string{"User-Agent": "HttpRunnerPlus"}). diff --git a/internal/builtin/assertion.go b/internal/builtin/assertion.go index 31110221..87e4709b 100644 --- a/internal/builtin/assertion.go +++ b/internal/builtin/assertion.go @@ -15,13 +15,16 @@ var Assertions = map[string]func(t assert.TestingT, expected interface{}, actual "greater_or_equals": assert.GreaterOrEqual, "less_or_equals": assert.LessOrEqual, "not_equal": assert.NotEqual, - "contains": assert.Contains, + "contained_by": assert.Contains, "regex_match": assert.Regexp, + "type_match": assert.IsType, // custom assertions "startswith": StartsWith, // check if string starts with substring "endswith": EndsWith, // check if string ends with substring "length_equals": EqualLength, "length_equal": EqualLength, // alias for length_equals + "contains": Contains, + "string_equals": EqualString, } func StartsWith(t assert.TestingT, expected, actual interface{}, msgAndArgs ...interface{}) bool { @@ -83,3 +86,20 @@ func convertInt(value interface{}) (int, error) { return 0, fmt.Errorf("unsupported int convertion for %v(%T)", v, v) } } + +// Contains assert whether actual +func Contains(t assert.TestingT, expected, actual interface{}, msgAndArgs ...interface{}) bool { + return assert.Contains(t, actual, expected, msgAndArgs) +} + +func EqualString(t assert.TestingT, expected, actual interface{}, msgAndArgs ...interface{}) bool { + if !assert.IsType(t, "string", actual, msgAndArgs) { + return false + } + if !assert.IsType(t, "string", expected, msgAndArgs) { + return false + } + actualString := actual.(string) + expectedString := expected.(string) + return assert.True(t, strings.EqualFold(actualString, expectedString), msgAndArgs) +} diff --git a/validate.go b/validate.go index f5d67a8a..9748ac05 100644 --- a/validate.go +++ b/validate.go @@ -35,6 +35,28 @@ func (s *StepRequestValidation) AssertEqual(jmesPath string, expected interface{ return s } +func (s *StepRequestValidation) AssertContains(jmesPath string, expected interface{}, msg string) *StepRequestValidation { + v := Validator{ + Check: jmesPath, + Assert: "contains", + Expect: expected, + Message: msg, + } + s.step.Validators = append(s.step.Validators, v) + return s +} + +func (s *StepRequestValidation) AssertTypeMatch(jmesPath string, expected interface{}, msg string) *StepRequestValidation { + v := Validator{ + Check: jmesPath, + Assert: "type_match", + Expect: expected, + Message: msg, + } + s.step.Validators = append(s.step.Validators, v) + return s +} + func (s *StepRequestValidation) AssertStartsWith(jmesPath string, expected interface{}, msg string) *StepRequestValidation { v := Validator{ Check: jmesPath, @@ -67,3 +89,25 @@ func (s *StepRequestValidation) AssertLengthEqual(jmesPath string, expected inte s.step.Validators = append(s.step.Validators, v) return s } + +func (s *StepRequestValidation) AssertContainedBy(jmesPath string, expected interface{}, msg string) *StepRequestValidation { + v := Validator{ + Check: jmesPath, + Assert: "contained_by", + Expect: expected, + Message: msg, + } + s.step.Validators = append(s.step.Validators, v) + return s +} + +func (s *StepRequestValidation) AssertStringEqual(jmesPath string, expected interface{}, msg string) *StepRequestValidation { + v := Validator{ + Check: jmesPath, + Assert: "string_equals", + Expect: expected, + Message: msg, + } + s.step.Validators = append(s.step.Validators, v) + return s +} \ No newline at end of file