mirror of
https://github.com/httprunner/httprunner.git
synced 2026-06-02 22:39:42 +08:00
merge main
This commit is contained in:
77
models.go
77
models.go
@@ -1,5 +1,11 @@
|
||||
package hrp
|
||||
|
||||
import (
|
||||
"math/rand"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
httpGET string = "GET"
|
||||
httpHEAD string = "HEAD"
|
||||
@@ -13,14 +19,69 @@ const (
|
||||
// TConfig represents config data structure for testcase.
|
||||
// Each testcase should contain one config part.
|
||||
type TConfig struct {
|
||||
Name string `json:"name" yaml:"name"` // required
|
||||
Verify bool `json:"verify,omitempty" yaml:"verify,omitempty"`
|
||||
BaseURL string `json:"base_url,omitempty" yaml:"base_url,omitempty"`
|
||||
Variables map[string]interface{} `json:"variables,omitempty" yaml:"variables,omitempty"`
|
||||
Parameters map[string]interface{} `json:"parameters,omitempty" yaml:"parameters,omitempty"`
|
||||
Export []string `json:"export,omitempty" yaml:"export,omitempty"`
|
||||
Weight int `json:"weight,omitempty" yaml:"weight,omitempty"`
|
||||
Path string `json:"path,omitempty" yaml:"path,omitempty"` // testcase file path
|
||||
Name string `json:"name" yaml:"name"` // required
|
||||
Verify bool `json:"verify,omitempty" yaml:"verify,omitempty"`
|
||||
BaseURL string `json:"base_url,omitempty" yaml:"base_url,omitempty"`
|
||||
Variables map[string]interface{} `json:"variables,omitempty" yaml:"variables,omitempty"`
|
||||
Parameters map[string]interface{} `json:"parameters,omitempty" yaml:"parameters,omitempty"`
|
||||
ParametersSetting *TParamsConfig `json:"parameters_setting,omitempty" yaml:"parameters_setting,omitempty"`
|
||||
Export []string `json:"export,omitempty" yaml:"export,omitempty"`
|
||||
Weight int `json:"weight,omitempty" yaml:"weight,omitempty"`
|
||||
Path string `json:"path,omitempty" yaml:"path,omitempty"` // testcase file path
|
||||
}
|
||||
|
||||
type TParamsConfig struct {
|
||||
Strategy interface{} `json:"strategy,omitempty" yaml:"strategy,omitempty"`
|
||||
Iteration int `json:"iteration,omitempty" yaml:"iteration,omitempty"`
|
||||
Iterators []*Iterator `json:"parameterIterator,omitempty" yaml:"parameterIterator,omitempty"` //保存参数的迭代器
|
||||
}
|
||||
|
||||
const (
|
||||
strategyRandom string = "random"
|
||||
strategySequential string = "Sequential"
|
||||
)
|
||||
|
||||
type paramsType []map[string]interface{}
|
||||
|
||||
type Iterator struct {
|
||||
sync.Mutex
|
||||
data paramsType
|
||||
strategy string // random, sequential
|
||||
iteration int
|
||||
index int
|
||||
}
|
||||
|
||||
func (params paramsType) Iterator() *Iterator {
|
||||
return &Iterator{
|
||||
data: params,
|
||||
iteration: len(params),
|
||||
index: 0,
|
||||
}
|
||||
}
|
||||
|
||||
func (iter *Iterator) HasNext() bool {
|
||||
if iter.iteration == -1 {
|
||||
return true
|
||||
}
|
||||
return iter.index < iter.iteration
|
||||
}
|
||||
|
||||
func (iter *Iterator) Next() (value map[string]interface{}) {
|
||||
iter.Lock()
|
||||
defer iter.Unlock()
|
||||
if len(iter.data) == 0 {
|
||||
iter.index++
|
||||
return map[string]interface{}{}
|
||||
}
|
||||
if iter.strategy == strategyRandom {
|
||||
randSource := rand.New(rand.NewSource(time.Now().Unix()))
|
||||
randIndex := randSource.Intn(len(iter.data))
|
||||
value = iter.data[randIndex]
|
||||
} else {
|
||||
value = iter.data[iter.index%len(iter.data)]
|
||||
}
|
||||
iter.index++
|
||||
return value
|
||||
}
|
||||
|
||||
// Request represents HTTP request data structure.
|
||||
|
||||
Reference in New Issue
Block a user