mirror of
https://github.com/httprunner/httprunner.git
synced 2026-05-21 16:23:16 +08:00
feat: data-driven.
This commit is contained in:
141
parser.go
141
parser.go
@@ -3,13 +3,16 @@ package hrp
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/maja42/goval"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/rs/zerolog/log"
|
||||
"math/rand"
|
||||
"net/url"
|
||||
"reflect"
|
||||
"regexp"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/maja42/goval"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/rs/zerolog/log"
|
||||
|
||||
"github.com/httprunner/hrp/internal/builtin"
|
||||
)
|
||||
@@ -247,6 +250,15 @@ func mergeVariables(variables, overriddenVariables map[string]interface{}) map[s
|
||||
return mergedVariables
|
||||
}
|
||||
|
||||
func contains(s []string, e string) bool {
|
||||
for _, a := range s {
|
||||
if strings.EqualFold(a, e) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func getMappingFunction(funcName string) (interface{}, error) {
|
||||
if function, ok := builtin.Functions[funcName]; ok {
|
||||
// function is builtin
|
||||
@@ -505,12 +517,43 @@ func findallVariables(raw string) variableSet {
|
||||
return varSet
|
||||
}
|
||||
|
||||
func shuffleCartesianProduct(slice []map[string]interface{}) {
|
||||
if slice == nil || len(slice) == 0 {
|
||||
return
|
||||
}
|
||||
r := rand.New(rand.NewSource(time.Now().Unix()))
|
||||
for len(slice) > 0 {
|
||||
n := len(slice)
|
||||
randIndex := r.Intn(n)
|
||||
slice[n-1], slice[randIndex] = slice[randIndex], slice[n-1]
|
||||
slice = slice[:n-1]
|
||||
}
|
||||
}
|
||||
|
||||
func genCartesianProduct(params [][]map[string]interface{}) []map[string]interface{} {
|
||||
if params == nil || len(params) == 0 {
|
||||
return nil
|
||||
}
|
||||
var cartesianProduct []map[string]interface{}
|
||||
cartesianProduct = params[0]
|
||||
for i := 0; i < len(params)-1; i++ {
|
||||
var tempProduct []map[string]interface{}
|
||||
for _, param1 := range cartesianProduct {
|
||||
for _, param2 := range params[i+1] {
|
||||
tempProduct = append(tempProduct, mergeVariables(param1, param2))
|
||||
}
|
||||
}
|
||||
cartesianProduct = tempProduct
|
||||
}
|
||||
return cartesianProduct
|
||||
}
|
||||
|
||||
func getParameters(config IConfig) []map[string]interface{} {
|
||||
cfg := config.ToStruct()
|
||||
// parse config parameters
|
||||
parsedParams, err := parseParameters(cfg.Parameters, cfg.Variables)
|
||||
if err != nil {
|
||||
log.Error().Interface("params", cfg.Parameters).Err(err).Msg("parse config parameters failed")
|
||||
log.Error().Interface("parameters", cfg.Parameters).Err(err).Msg("parse config parameters failed")
|
||||
}
|
||||
if cfg.ParametersSetting["strategy"] != nil && strings.ToLower(cfg.ParametersSetting["strategy"].(string)) == "random" {
|
||||
shuffleCartesianProduct(parsedParams)
|
||||
@@ -519,10 +562,13 @@ func getParameters(config IConfig) []map[string]interface{} {
|
||||
}
|
||||
|
||||
func parseParameters(parameters map[string]interface{}, variablesMapping map[string]interface{}) ([]map[string]interface{}, error) {
|
||||
var parsedParametersList [][]map[string]interface{}
|
||||
if parameters == nil || len(parameters) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
var parsedParametersSlice [][]map[string]interface{}
|
||||
for k, v := range parameters {
|
||||
parameterNameList := strings.Split(k, "-")
|
||||
var parameterList []map[string]interface{}
|
||||
parameterNameSlice := strings.Split(k, "-")
|
||||
var parameterSlice []map[string]interface{}
|
||||
rawValue := reflect.ValueOf(v)
|
||||
switch rawValue.Kind() {
|
||||
case reflect.String:
|
||||
@@ -533,55 +579,72 @@ func parseParameters(parameters map[string]interface{}, variablesMapping map[str
|
||||
}
|
||||
parsedParameterRawValue := reflect.ValueOf(parsedParameterContent)
|
||||
if parsedParameterRawValue.Kind() != reflect.Slice {
|
||||
log.Error().Interface("parameter", parameters).Msg("[parseParameters] parameter content should be List or Text(variables or functions call), got %v")
|
||||
return nil, errors.New("parameter content should be List or Text(variables or functions call)")
|
||||
log.Error().Interface("parameter", parameters).Msg("[parseParameters] parsed parameter content should be Slice, got %v")
|
||||
return nil, errors.New("parsed parameter content should be Slice")
|
||||
}
|
||||
for i := 0; i < parsedParameterRawValue.Len(); i++ {
|
||||
parameterMap := make(map[string]interface{})
|
||||
if parsedParameterRawValue.Index(i).Kind() == reflect.Map {
|
||||
for _, key := range parameterNameList {
|
||||
parameterMap[key] = parsedParameterRawValue.Index(i).MapIndex(reflect.ValueOf(key)).Interface()
|
||||
// e.g.
|
||||
elem := reflect.ValueOf(parsedParameterRawValue.Index(i).Interface())
|
||||
if elem.Kind() == reflect.Map {
|
||||
// e.g. [{"username": "test1", "password": "passwd1", "other": "111"}, {"username": "test2", "password": "passwd2", "other": ""222}]
|
||||
// -> [{"username": "test1", "password": "passwd1"}, {"username": "test2", "password": "passwd2"}] (username, password in parameterNameSlice)
|
||||
for _, key := range parameterNameSlice {
|
||||
if _, ok := elem.Interface().(map[string]string)[key]; ok {
|
||||
parameterMap[key] = elem.MapIndex(reflect.ValueOf(key)).Interface()
|
||||
} else {
|
||||
log.Error().Interface("parameterNameSlice", parameterNameSlice).Msg("[parseParameters] parameter name not found")
|
||||
return nil, errors.New("parameter name not found")
|
||||
}
|
||||
}
|
||||
} else if parsedParameterRawValue.Index(i).Kind() == reflect.Slice {
|
||||
if len(parameterNameList) != parsedParameterRawValue.Index(i).Len() {
|
||||
log.Error().Interface("parameter", parameters).Msg("[parseParameters] parameter name list and parameter content list should have the same length")
|
||||
return nil, errors.New("parameter name list and parameter content list should have the same length")
|
||||
} else if elem.Kind() == reflect.Slice {
|
||||
// e.g. [["test1", "passwd1"], ["test2", "passwd2"]] -> [{"username": "test1", "password": "passwd1"}, {"username": "test2", "password": "passwd2"}]
|
||||
if len(parameterNameSlice) != elem.Len() {
|
||||
log.Error().Interface("parameter", parameters).Msg("[parseParameters] parameter name Slice and parameter content Slice should have the same length")
|
||||
return nil, errors.New("parameter name Slice and parameter cjntent Slice should have the same length")
|
||||
} else {
|
||||
for j := 0; j < elem.Len(); j++ {
|
||||
parameterMap[parameterNameSlice[j]] = elem.Index(j).Interface()
|
||||
}
|
||||
}
|
||||
for i := 0; i < parsedParameterRawValue.Index(i).Len(); i++ {
|
||||
parameterMap[parameterNameList[i]] = parsedParameterRawValue.Index(i).Index(i).Interface()
|
||||
}
|
||||
} else if len(parameterNameList) == 1 {
|
||||
parameterMap[parameterNameList[0]] = parsedParameterRawValue.Index(i).Interface()
|
||||
} else {
|
||||
log.Error().Interface("parameter", parameters).Msg("[parseParameters] parameter content should be List or Text(variables or functions call), got %v")
|
||||
return nil, errors.New("parameter content should be List or Text(variables or functions call)")
|
||||
// e.g. ${getAppVersion()} -> [3.1, 3.0] -> [{"app_version": 3.1}, {"app_version": 3.0}]
|
||||
if len(parameterNameSlice) != 1 {
|
||||
log.Error().Interface("parameterNameSlice", parameterNameSlice).Msg("[parseParameters] parameter name slice should have only one element when parameter content is string")
|
||||
return nil, errors.New("parameter name slice should have only one element when parameter content is string")
|
||||
}
|
||||
parameterMap[parameterNameSlice[0]] = elem.Interface()
|
||||
}
|
||||
parameterList = append(parameterList, parameterMap)
|
||||
parameterSlice = append(parameterSlice, parameterMap)
|
||||
}
|
||||
case reflect.Slice:
|
||||
for i := 0; i < rawValue.Len(); i++ {
|
||||
parameterMap := make(map[string]interface{})
|
||||
if rawValue.Index(i).Kind() == reflect.Interface || rawValue.Index(i).Kind() == reflect.String {
|
||||
parameterMap[parameterNameList[0]] = rawValue.Index(i).Interface()
|
||||
} else if rawValue.Index(i).Kind() == reflect.Slice {
|
||||
if len(parameterNameList) != rawValue.Index(i).Len() {
|
||||
log.Error().Interface("parameter", parameters).Msg("[parseParameters] parameter name list and parameter content list should have the same length")
|
||||
return nil, errors.New("parameter name list and parameter content list should have the same length")
|
||||
elem := reflect.ValueOf(rawValue.Index(i).Interface())
|
||||
if elem.Kind() == reflect.Slice {
|
||||
// e.g. username-password: [["test1", "passwd1"], ["test2", "passwd2"]]
|
||||
if len(parameterNameSlice) != elem.Len() {
|
||||
log.Error().Interface("parameter", parameters).Msg("[parseParameters] parameter name Slice and parameter content Slice should have the same length")
|
||||
return nil, errors.New("parameter name Slice and parameter content Slice should have the same length")
|
||||
}
|
||||
for i := 0; i < rawValue.Index(i).Len(); i++ {
|
||||
parameterMap[parameterNameList[i]] = rawValue.Index(i).Index(i).Interface()
|
||||
for j := 0; j < elem.Len(); j++ {
|
||||
parameterMap[parameterNameSlice[j]] = elem.Index(j).Interface()
|
||||
}
|
||||
} else {
|
||||
log.Error().Interface("parameter", parameters).Msg("[parseParameters] parameter content should be List or Text(variables or functions call), got %v")
|
||||
return nil, errors.New("parameter content should be List or Text(variables or functions call)")
|
||||
// e.g. user_agent: ["iOS/10.1", "iOS/10.2"]
|
||||
if len(parameterNameSlice) != 1 {
|
||||
log.Error().Interface("parameterNameSlice", parameterNameSlice).Msg("[parseParameters] parameter name slice should have only one element when parameter content is string")
|
||||
return nil, errors.New("parameter name slice should have only one element when parameter content is string")
|
||||
}
|
||||
parameterMap[parameterNameSlice[0]] = elem.Interface()
|
||||
}
|
||||
parameterList = append(parameterList, parameterMap)
|
||||
parameterSlice = append(parameterSlice, parameterMap)
|
||||
}
|
||||
default:
|
||||
log.Error().Interface("parameter", parameters).Msg("[parseParameters] parameter content should be List or Text(variables or functions call), got %v")
|
||||
return nil, errors.New("parameter content should be List or Text(variables or functions call)")
|
||||
log.Error().Interface("parameter", parameters).Msg("[parseParameters] parameter content should be Slice or Text(variables or functions call)")
|
||||
return nil, errors.New("parameter content should be Slice or Text(variables or functions call)")
|
||||
}
|
||||
parsedParametersList = append(parsedParametersList, parameterList)
|
||||
parsedParametersSlice = append(parsedParametersSlice, parameterSlice)
|
||||
}
|
||||
return genCartesianProduct(parsedParametersList), nil
|
||||
return genCartesianProduct(parsedParametersSlice), nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user