From d92b466897928456842cad97dc196c2b892ff086 Mon Sep 17 00:00:00 2001 From: debugtalk Date: Thu, 30 Sep 2021 11:33:55 +0800 Subject: [PATCH] feat: override config variables --- examples/postman_echo/variables_test.go | 73 ++++++++++++++++++++++++- parser.go | 25 +++++++++ parser_test.go | 19 +++++++ runner.go | 7 +++ 4 files changed, 123 insertions(+), 1 deletion(-) diff --git a/examples/postman_echo/variables_test.go b/examples/postman_echo/variables_test.go index e184034..0f94cd0 100644 --- a/examples/postman_echo/variables_test.go +++ b/examples/postman_echo/variables_test.go @@ -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) + } +} diff --git a/parser.go b/parser.go index 49698a6..3dfb39b 100644 --- a/parser.go +++ b/parser.go @@ -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 +} diff --git a/parser_test.go b/parser_test.go index 7f6618b..f32ad9f 100644 --- a/parser_test.go +++ b/parser_test.go @@ -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() + } +} diff --git a/runner.go b/runner.go index 971ef76..b64230f 100644 --- a/runner.go +++ b/runner.go @@ -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 }