diff --git a/examples/variables_test.go b/examples/variables_test.go index f3df328a..5ce4262c 100644 --- a/examples/variables_test.go +++ b/examples/variables_test.go @@ -109,3 +109,50 @@ func TestCaseOverrideConfigVariables(t *testing.T) { t.Fatalf("run testcase error: %v", err) } } + +func TestCaseParseVariables(t *testing.T) { + testcase := &httpboomer.TestCase{ + Config: httpboomer.TConfig{ + Name: "run request with functions", + BaseURL: "https://postman-echo.com", + Verify: false, + Variables: map[string]interface{}{ + "n": 5, + "a": 12.3, + "b": 3.45, + "varFoo1": "${gen_random_string($n)}", + "varFoo2": "${max($a, $b)}", // 12.3 + }, + }, + TestSteps: []httpboomer.IStep{ + httpboomer.Step("get with params"). + WithVariables(map[string]interface{}{ + "n": 3, + "b": 34.5, + "varFoo2": "${max($a, $b)}", // 34.5 + }). + GET("/get"). + WithParams(map[string]interface{}{"foo1": "$varFoo1", "foo2": "$varFoo2"}). + WithHeaders(map[string]string{"User-Agent": "HttpBoomer"}). + Extract(). + WithJmesPath("body.args.foo1", "varFoo1"). + Validate(). + AssertEqual("status_code", 200, "check status code"). + AssertLengthEqual("body.args.foo1", 5, "check args foo1"). + AssertEqual("body.args.foo2", "34.5", "check args foo2"), // notice: request params value will be converted to string + httpboomer.Step("post json data with functions"). + POST("/post"). + WithHeaders(map[string]string{"User-Agent": "HttpBoomer"}). + WithJSON(map[string]interface{}{"foo1": "${gen_random_string($n)}", "foo2": "${max($a, $b)}"}). + Validate(). + AssertEqual("status_code", 200, "check status code"). + AssertLengthEqual("body.json.foo1", 5, "check args foo1"). + AssertEqual("body.json.foo2", 12.3, "check args foo2"), + }, + } + + err := httpboomer.Test(t, testcase) + if err != nil { + t.Fatalf("run testcase error: %v", err) + } +} diff --git a/parser_test.go b/parser_test.go index aacb5c64..a1f735e3 100644 --- a/parser_test.go +++ b/parser_test.go @@ -264,21 +264,28 @@ func TestParseHeaders(t *testing.T) { } func TestMergeVariables(t *testing.T) { - stepVariables := map[string]interface{}{ - "base_url": "$base_url", - "foo1": "bar1", + testData := []struct { + stepVariables map[string]interface{} + configVariables map[string]interface{} + expectVariables map[string]interface{} + }{ + { + map[string]interface{}{"base_url": "$base_url", "foo1": "bar1"}, + map[string]interface{}{"base_url": "https://httpbin.org", "foo1": "bar111"}, + map[string]interface{}{"base_url": "https://httpbin.org", "foo1": "bar1"}, + }, + { + map[string]interface{}{"n": 3, "b": 34.5, "varFoo2": "${max($a, $b)}"}, + map[string]interface{}{"n": 5, "a": 12.3, "b": 3.45, "varFoo1": "7a6K3", "varFoo2": 12.3}, + map[string]interface{}{"n": 3, "a": 12.3, "b": 34.5, "varFoo1": "7a6K3", "varFoo2": "${max($a, $b)}"}, + }, } - 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() + + for _, data := range testData { + mergedVariables := mergeVariables(data.stepVariables, data.configVariables) + if !assert.Equal(t, data.expectVariables, mergedVariables) { + t.Fail() + } } } @@ -439,6 +446,10 @@ func TestParseVariables(t *testing.T) { map[string]interface{}{"varA": "$varB", "varB": "$varC", "varC": "123", "a": 1, "b": 2}, map[string]interface{}{"varA": "123", "varB": "123", "varC": "123", "a": 1, "b": 2}, }, + { + map[string]interface{}{"n": 34.5, "a": 12.3, "b": "$n", "varFoo2": "${max($a, $b)}"}, + map[string]interface{}{"n": 34.5, "a": 12.3, "b": 34.5, "varFoo2": 34.5}, + }, } for _, data := range testData {