mirror of
https://github.com/httprunner/httprunner.git
synced 2026-09-05 15:37:35 +08:00
add interface request verification method.
This commit is contained in:
@@ -2,6 +2,7 @@ package builtin
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"reflect"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
@@ -18,10 +19,14 @@ var Assertions = map[string]func(t assert.TestingT, expected interface{}, actual
|
|||||||
"contains": assert.Contains,
|
"contains": assert.Contains,
|
||||||
"regex_match": assert.Regexp,
|
"regex_match": assert.Regexp,
|
||||||
// custom assertions
|
// custom assertions
|
||||||
"startswith": StartsWith, // check if string starts with substring
|
"startswith": StartsWith, // check if string starts with substring
|
||||||
"endswith": EndsWith, // check if string ends with substring
|
"endswith": EndsWith, // check if string ends with substring
|
||||||
"length_equals": EqualLength,
|
"length_equals": EqualLength,
|
||||||
"length_equal": EqualLength, // alias for length_equals
|
"length_equal": EqualLength, // alias for length_equals
|
||||||
|
"length_less_than": LessThanLength,
|
||||||
|
"length_less_or_equals": LessOrEqualsLength,
|
||||||
|
"length_greater_than": GreaterThanLength,
|
||||||
|
"length_greater_or_equals": GreaterOrEqualsLength,
|
||||||
}
|
}
|
||||||
|
|
||||||
func StartsWith(t assert.TestingT, expected, actual interface{}, msgAndArgs ...interface{}) bool {
|
func StartsWith(t assert.TestingT, expected, actual interface{}, msgAndArgs ...interface{}) bool {
|
||||||
@@ -57,6 +62,66 @@ func EqualLength(t assert.TestingT, expected, actual interface{}, msgAndArgs ...
|
|||||||
return assert.Len(t, actual, length, msgAndArgs...)
|
return assert.Len(t, actual, length, msgAndArgs...)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func GreaterThanLength(t assert.TestingT, expected, actual interface{}, msgAndArgs ...interface{}) bool {
|
||||||
|
length, err := convertInt(expected)
|
||||||
|
if err != nil {
|
||||||
|
return assert.Fail(t, fmt.Sprintf("expected type is not int, got %#v", expected), msgAndArgs...)
|
||||||
|
}
|
||||||
|
ok, l := getLen(actual)
|
||||||
|
if !ok {
|
||||||
|
return assert.Fail(t, fmt.Sprintf("\"%s\" could not be applied builtin len()", actual), msgAndArgs...)
|
||||||
|
}
|
||||||
|
if l > length {
|
||||||
|
return assert.Fail(t, fmt.Sprintf("\"%s\" should be more than %d item(s), but has %d", actual, length, l), msgAndArgs...)
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
func GreaterOrEqualsLength(t assert.TestingT, expected, actual interface{}, msgAndArgs ...interface{}) bool {
|
||||||
|
length, err := convertInt(expected)
|
||||||
|
if err != nil {
|
||||||
|
return assert.Fail(t, fmt.Sprintf("expected type is not int, got %#v", expected), msgAndArgs...)
|
||||||
|
}
|
||||||
|
ok, l := getLen(actual)
|
||||||
|
if !ok {
|
||||||
|
return assert.Fail(t, fmt.Sprintf("\"%s\" could not be applied builtin len()", actual), msgAndArgs...)
|
||||||
|
}
|
||||||
|
if l >= length {
|
||||||
|
return assert.Fail(t, fmt.Sprintf("\"%s\" should be no less than %d item(s), but has %d", actual, length, l), msgAndArgs...)
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
func LessThanLength(t assert.TestingT, expected, actual interface{}, msgAndArgs ...interface{}) bool {
|
||||||
|
length, err := convertInt(expected)
|
||||||
|
if err != nil {
|
||||||
|
return assert.Fail(t, fmt.Sprintf("expected type is not int, got %#v", expected), msgAndArgs...)
|
||||||
|
}
|
||||||
|
ok, l := getLen(actual)
|
||||||
|
if !ok {
|
||||||
|
return assert.Fail(t, fmt.Sprintf("\"%s\" could not be applied builtin len()", actual), msgAndArgs...)
|
||||||
|
}
|
||||||
|
if l < length {
|
||||||
|
return assert.Fail(t, fmt.Sprintf("\"%s\" should be less than %d item(s), but has %d", actual, length, l), msgAndArgs...)
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
func LessOrEqualsLength(t assert.TestingT, expected, actual interface{}, msgAndArgs ...interface{}) bool {
|
||||||
|
length, err := convertInt(expected)
|
||||||
|
if err != nil {
|
||||||
|
return assert.Fail(t, fmt.Sprintf("expected type is not int, got %#v", expected), msgAndArgs...)
|
||||||
|
}
|
||||||
|
ok, l := getLen(actual)
|
||||||
|
if !ok {
|
||||||
|
return assert.Fail(t, fmt.Sprintf("\"%s\" could not be applied builtin len()", actual), msgAndArgs...)
|
||||||
|
}
|
||||||
|
if l <= length {
|
||||||
|
return assert.Fail(t, fmt.Sprintf("\"%s\" should be no more than %d item(s), but has %d", actual, length, l), msgAndArgs...)
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
func convertInt(value interface{}) (int, error) {
|
func convertInt(value interface{}) (int, error) {
|
||||||
switch v := value.(type) {
|
switch v := value.(type) {
|
||||||
case int:
|
case int:
|
||||||
@@ -83,3 +148,15 @@ func convertInt(value interface{}) (int, error) {
|
|||||||
return 0, fmt.Errorf("unsupported int convertion for %v(%T)", v, v)
|
return 0, fmt.Errorf("unsupported int convertion for %v(%T)", v, v)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// getLen try to get length of object.
|
||||||
|
// return (false, 0) if impossible.
|
||||||
|
func getLen(x interface{}) (ok bool, length int) {
|
||||||
|
v := reflect.ValueOf(x)
|
||||||
|
defer func() {
|
||||||
|
if e := recover(); e != nil {
|
||||||
|
ok = false
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
return true, v.Len()
|
||||||
|
}
|
||||||
|
|||||||
+29
-9
@@ -625,8 +625,11 @@ func TestParseParameters(t *testing.T) {
|
|||||||
expectVars []map[string]interface{}
|
expectVars []map[string]interface{}
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
map[string]interface{}{"username-password": "${parameterize(examples/account.csv)}", "user_agent": []interface{}{"IOS/10.1", "IOS/10.2"}},
|
map[string]interface{}{
|
||||||
[]map[string]interface{}{{"username": "test1", "password": "111111", "user_agent": "IOS/10.1"},
|
"username-password": "${parameterize(examples/account.csv)}",
|
||||||
|
"user_agent": []interface{}{"IOS/10.1", "IOS/10.2"}},
|
||||||
|
[]map[string]interface{}{
|
||||||
|
{"username": "test1", "password": "111111", "user_agent": "IOS/10.1"},
|
||||||
{"username": "test1", "password": "111111", "user_agent": "IOS/10.2"},
|
{"username": "test1", "password": "111111", "user_agent": "IOS/10.2"},
|
||||||
{"username": "test2", "password": "222222", "user_agent": "IOS/10.1"},
|
{"username": "test2", "password": "222222", "user_agent": "IOS/10.1"},
|
||||||
{"username": "test2", "password": "222222", "user_agent": "IOS/10.2"},
|
{"username": "test2", "password": "222222", "user_agent": "IOS/10.2"},
|
||||||
@@ -634,12 +637,23 @@ func TestParseParameters(t *testing.T) {
|
|||||||
{"username": "test3", "password": "333333", "user_agent": "IOS/10.2"}},
|
{"username": "test3", "password": "333333", "user_agent": "IOS/10.2"}},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
map[string]interface{}{},
|
map[string]interface{}{
|
||||||
nil,
|
"username-password": [][]interface{}{{"test1", "111111"}, {"test2", "222222"}, {"test3", "333333"}},
|
||||||
|
"user_agent": []interface{}{"IOS/10.1", "IOS/10.2"},
|
||||||
|
"app_version": []interface{}{0.3}},
|
||||||
|
[]map[string]interface{}{
|
||||||
|
{"username": "test1", "password": "111111", "user_agent": "IOS/10.1", "app_version": 0.3},
|
||||||
|
{"username": "test1", "password": "111111", "user_agent": "IOS/10.2", "app_version": 0.3},
|
||||||
|
{"username": "test2", "password": "222222", "user_agent": "IOS/10.1", "app_version": 0.3},
|
||||||
|
{"username": "test2", "password": "222222", "user_agent": "IOS/10.2", "app_version": 0.3},
|
||||||
|
{"username": "test3", "password": "333333", "user_agent": "IOS/10.1", "app_version": 0.3},
|
||||||
|
{"username": "test3", "password": "333333", "user_agent": "IOS/10.2", "app_version": 0.3}},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
nil,
|
map[string]interface{}{}, nil,
|
||||||
nil,
|
},
|
||||||
|
{
|
||||||
|
nil, nil,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
for _, data := range testData {
|
for _, data := range testData {
|
||||||
@@ -655,13 +669,19 @@ func TestParseParametersError(t *testing.T) {
|
|||||||
rawVars map[string]interface{}
|
rawVars map[string]interface{}
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
map[string]interface{}{"username_password": "${parameterize(examples/account.csv)}", "user_agent": []interface{}{"IOS/10.1", "IOS/10.2"}},
|
map[string]interface{}{
|
||||||
|
"username_password": "${parameterize(examples/account.csv)}",
|
||||||
|
"user_agent": []interface{}{"IOS/10.1", "IOS/10.2"}},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
map[string]interface{}{"username-password": "${parameterize(examples/account.csv)}", "user-agent": []interface{}{"IOS/10.1", "IOS/10.2"}},
|
map[string]interface{}{
|
||||||
|
"username-password": "${parameterize(examples/account.csv)}",
|
||||||
|
"user-agent": []interface{}{"IOS/10.1", "IOS/10.2"}},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
map[string]interface{}{"username-password": "${param(examples/account.csv)}", "user_agent": []interface{}{"IOS/10.1", "IOS/10.2"}},
|
map[string]interface{}{
|
||||||
|
"username-password": "${param(examples/account.csv)}",
|
||||||
|
"user_agent": []interface{}{"IOS/10.1", "IOS/10.2"}},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
for _, data := range testData {
|
for _, data := range testData {
|
||||||
|
|||||||
Reference in New Issue
Block a user