Skip to content

Commit

Permalink
feat: override config variables
Browse files Browse the repository at this point in the history
debugtalk committed Sep 30, 2021
1 parent e0b884a commit d92b466
Showing 4 changed files with 123 additions and 1 deletion.
73 changes: 72 additions & 1 deletion examples/postman_echo/variables_test.go
Original file line number Diff line number Diff line change
@@ -6,7 +6,40 @@ import (
"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{
Config: httpboomer.TConfig{
Name: "run request with variables",
@@ -38,3 +71,41 @@ func TestCaseVariables(t *testing.T) {
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)
}
}
25 changes: 25 additions & 0 deletions parser.go
Original file line number Diff line number Diff line change
@@ -139,3 +139,28 @@ func parseString(raw string, variablesMapping map[string]interface{}) interface{

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
}
19 changes: 19 additions & 0 deletions parser_test.go
Original file line number Diff line number Diff line change
@@ -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()
}
}
7 changes: 7 additions & 0 deletions runner.go
Original file line number Diff line number Diff line change
@@ -48,9 +48,16 @@ func (r *Runner) Run(testcases ...*TestCase) error {
func (r *Runner) runCase(testcase *TestCase) error {
config := &testcase.Config
log.Printf("Start to run testcase: %v", config.Name)

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)
}

return nil
}

0 comments on commit d92b466

Please sign in to comment.