mirror of
https://github.com/httprunner/httprunner.git
synced 2026-09-04 23:16:57 +08:00
feat: override config variables
This commit is contained in:
@@ -6,7 +6,40 @@ import (
|
|||||||
"github.com/httprunner/httpboomer"
|
"github.com/httprunner/httpboomer"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestCaseVariables(t *testing.T) {
|
func TestCaseConfigVariables(t *testing.T) {
|
||||||
|
testcase := &httpboomer.TestCase{
|
||||||
|
Config: httpboomer.TConfig{
|
||||||
|
Name: "run request with variables",
|
||||||
|
BaseURL: "https://postman-echo.com",
|
||||||
|
Variables: map[string]interface{}{
|
||||||
|
"var1": "bar1",
|
||||||
|
"agent": "HttpBoomer",
|
||||||
|
"expectedStatusCode": 200,
|
||||||
|
},
|
||||||
|
Verify: false,
|
||||||
|
},
|
||||||
|
TestSteps: []httpboomer.IStep{
|
||||||
|
httpboomer.Step("get with params").
|
||||||
|
GET("/get").
|
||||||
|
WithParams(map[string]interface{}{"foo1": "$var1", "foo2": "bar2"}).
|
||||||
|
WithHeaders(map[string]string{"User-Agent": "$agent"}).
|
||||||
|
Validate().
|
||||||
|
AssertEqual("status_code", "$expectedStatusCode", "check status code").
|
||||||
|
AssertEqual("headers.Connection", "keep-alive", "check header Connection").
|
||||||
|
AssertEqual("headers.\"Content-Type\"", "application/json; charset=utf-8", "check header Content-Type").
|
||||||
|
AssertEqual("body.args.foo1", "bar1", "check args foo1").
|
||||||
|
AssertEqual("body.args.foo2", "bar2", "check args foo2").
|
||||||
|
AssertEqual("body.headers.\"user-agent\"", "HttpBoomer", "check header user agent"),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
err := httpboomer.Test(t, testcase)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("run testcase error: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCaseStepVariables(t *testing.T) {
|
||||||
testcase := &httpboomer.TestCase{
|
testcase := &httpboomer.TestCase{
|
||||||
Config: httpboomer.TConfig{
|
Config: httpboomer.TConfig{
|
||||||
Name: "run request with variables",
|
Name: "run request with variables",
|
||||||
@@ -38,3 +71,41 @@ func TestCaseVariables(t *testing.T) {
|
|||||||
t.Fatalf("run testcase error: %v", err)
|
t.Fatalf("run testcase error: %v", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestCaseOverrideConfigVariables(t *testing.T) {
|
||||||
|
testcase := &httpboomer.TestCase{
|
||||||
|
Config: httpboomer.TConfig{
|
||||||
|
Name: "run request with variables",
|
||||||
|
BaseURL: "https://postman-echo.com",
|
||||||
|
Variables: map[string]interface{}{
|
||||||
|
"var1": "bar0",
|
||||||
|
"agent": "HttpBoomer",
|
||||||
|
"expectedStatusCode": 200,
|
||||||
|
},
|
||||||
|
Verify: false,
|
||||||
|
},
|
||||||
|
TestSteps: []httpboomer.IStep{
|
||||||
|
httpboomer.Step("get with params").
|
||||||
|
WithVariables(map[string]interface{}{
|
||||||
|
"var1": "bar1", // override config variable
|
||||||
|
"agent": "$agent", // reference config variable
|
||||||
|
// expectedStatusCode, inherit config variable
|
||||||
|
}).
|
||||||
|
GET("/get").
|
||||||
|
WithParams(map[string]interface{}{"foo1": "$var1", "foo2": "bar2"}).
|
||||||
|
WithHeaders(map[string]string{"User-Agent": "$agent"}).
|
||||||
|
Validate().
|
||||||
|
AssertEqual("status_code", "$expectedStatusCode", "check status code").
|
||||||
|
AssertEqual("headers.Connection", "keep-alive", "check header Connection").
|
||||||
|
AssertEqual("headers.\"Content-Type\"", "application/json; charset=utf-8", "check header Content-Type").
|
||||||
|
AssertEqual("body.args.foo1", "bar1", "check args foo1").
|
||||||
|
AssertEqual("body.args.foo2", "bar2", "check args foo2").
|
||||||
|
AssertEqual("body.headers.\"user-agent\"", "HttpBoomer", "check header user agent"),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
err := httpboomer.Test(t, testcase)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("run testcase error: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -139,3 +139,28 @@ func parseString(raw string, variablesMapping map[string]interface{}) interface{
|
|||||||
|
|
||||||
return parsedString
|
return parsedString
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// merge two variables mapping, the first variables have higher priority
|
||||||
|
func mergeVariables(variables, overriddenVariables map[string]interface{}) map[string]interface{} {
|
||||||
|
if overriddenVariables == nil {
|
||||||
|
return variables
|
||||||
|
}
|
||||||
|
if variables == nil {
|
||||||
|
return overriddenVariables
|
||||||
|
}
|
||||||
|
|
||||||
|
mergedVariables := make(map[string]interface{})
|
||||||
|
for k, v := range overriddenVariables {
|
||||||
|
mergedVariables[k] = v
|
||||||
|
}
|
||||||
|
for k, v := range variables {
|
||||||
|
if fmt.Sprintf("${%s}", k) == v || fmt.Sprintf("$%s", k) == v {
|
||||||
|
// e.g. {"base_url": "$base_url"}
|
||||||
|
// or {"base_url": "${base_url}"}
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
mergedVariables[k] = v
|
||||||
|
}
|
||||||
|
return mergedVariables
|
||||||
|
}
|
||||||
|
|||||||
@@ -184,3 +184,22 @@ func TestParseHeaders(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestMergeVariables(t *testing.T) {
|
||||||
|
stepVariables := map[string]interface{}{
|
||||||
|
"base_url": "$base_url",
|
||||||
|
"foo1": "bar1",
|
||||||
|
}
|
||||||
|
configVariables := map[string]interface{}{
|
||||||
|
"base_url": "https://httpbin.org",
|
||||||
|
"foo1": "bar111",
|
||||||
|
}
|
||||||
|
mergedVariables := mergeVariables(stepVariables, configVariables)
|
||||||
|
expectVariables := map[string]interface{}{
|
||||||
|
"base_url": "https://httpbin.org",
|
||||||
|
"foo1": "bar1",
|
||||||
|
}
|
||||||
|
if !assert.Equal(t, expectVariables, mergedVariables) {
|
||||||
|
t.Fail()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -48,9 +48,16 @@ func (r *Runner) Run(testcases ...*TestCase) error {
|
|||||||
func (r *Runner) runCase(testcase *TestCase) error {
|
func (r *Runner) runCase(testcase *TestCase) error {
|
||||||
config := &testcase.Config
|
config := &testcase.Config
|
||||||
log.Printf("Start to run testcase: %v", config.Name)
|
log.Printf("Start to run testcase: %v", config.Name)
|
||||||
|
|
||||||
for _, step := range testcase.TestSteps {
|
for _, step := range testcase.TestSteps {
|
||||||
|
// override variables
|
||||||
|
// step variables > extracted variables from previous steps
|
||||||
|
// step variables > testcase config variables
|
||||||
|
step.ToStruct().Variables = mergeVariables(step.ToStruct().Variables, config.Variables)
|
||||||
|
|
||||||
r.runStep(step, config)
|
r.runStep(step, config)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user