diff --git a/examples/extract_test.go b/examples/extract_test.go index 1fb1df8c..6f5d5dc2 100644 --- a/examples/extract_test.go +++ b/examples/extract_test.go @@ -6,7 +6,8 @@ import ( "github.com/httprunner/httpboomer" ) -func TestCaseExtractStep(t *testing.T) { +// reference extracted variables for validation in the same step +func TestCaseExtractStepSingle(t *testing.T) { testcase := &httpboomer.TestCase{ Config: httpboomer.TConfig{ Name: "run request with variables", @@ -28,10 +29,9 @@ func TestCaseExtractStep(t *testing.T) { WithJmesPath("headers.\"Content-Type\"", "contentType"). WithJmesPath("body.args.foo1", "varFoo1"). Validate(). - AssertEqual("$statusCode", "$expectedStatusCode", "check status code"). // assert with extracted variable - AssertEqual("headers.Connection", "keep-alive", "check header Connection"). - AssertEqual("$contentType", "application/json; charset=utf-8", "check header Content-Type"). // assert with extracted variable - AssertEqual("$varFoo1", "bar1", "check args foo1"). // assert with extracted variable + AssertEqual("$statusCode", "$expectedStatusCode", "check status code"). // assert with extracted variable from current step + AssertEqual("$contentType", "application/json; charset=utf-8", "check header Content-Type"). // assert with extracted variable from current step + AssertEqual("$varFoo1", "bar1", "check args foo1"). // assert with extracted variable from current step AssertEqual("body.args.foo2", "bar2", "check args foo2"). AssertEqual("body.headers.\"user-agent\"", "HttpBoomer", "check header user agent"), }, @@ -42,3 +42,48 @@ func TestCaseExtractStep(t *testing.T) { t.Fatalf("run testcase error: %v", err) } } + +// reference extracted variables from previous step +func TestCaseExtractStepAssociation(t *testing.T) { + testcase := &httpboomer.TestCase{ + Config: httpboomer.TConfig{ + Name: "run request with variables", + BaseURL: "https://postman-echo.com", + Verify: false, + }, + TestSteps: []httpboomer.IStep{ + httpboomer.Step("get with params"). + WithVariables(map[string]interface{}{ + "var1": "bar1", + "agent": "HttpBoomer", + }). + GET("/get"). + WithParams(map[string]interface{}{"foo1": "$var1", "foo2": "bar2"}). + WithHeaders(map[string]string{"User-Agent": "$agent"}). + Extract(). + WithJmesPath("status_code", "statusCode"). + WithJmesPath("headers.\"Content-Type\"", "contentType"). + WithJmesPath("body.args.foo1", "varFoo1"). + Validate(). + AssertEqual("$statusCode", 200, "check status code"). + AssertEqual("headers.Connection", "keep-alive", "check header Connection"). + AssertEqual("$contentType", "application/json; charset=utf-8", "check header Content-Type"). + AssertEqual("$varFoo1", "bar1", "check args foo1"). + AssertEqual("body.args.foo2", "bar2", "check args foo2"). + AssertEqual("body.headers.\"user-agent\"", "HttpBoomer", "check header user agent"), + httpboomer.Step("post json data"). + POST("/post"). + WithHeaders(map[string]string{"User-Agent": "HttpBoomer"}). + WithJSON(map[string]interface{}{"foo1": "bar1", "foo2": "bar2"}). + Validate(). + AssertEqual("status_code", "$statusCode", "check status code"). // assert with extracted variable from previous step + AssertEqual("$varFoo1", "bar1", "check json foo1"). // assert with extracted variable from previous step + AssertEqual("body.json.foo2", "bar2", "check json foo2"), + }, + } + + err := httpboomer.Test(t, testcase) + if err != nil { + t.Fatalf("run testcase error: %v", err) + } +} diff --git a/response.go b/response.go index a61abc92..816246ad 100644 --- a/response.go +++ b/response.go @@ -89,7 +89,8 @@ func (v *ResponseObject) Extract(extractors map[string]string) map[string]interf extractMapping := make(map[string]interface{}) for key, value := range extractors { extractedValue := v.searchJmespath(value) - log.Printf("extract %s => %v", value, extractedValue) + log.Printf("[extract] %s => %v", value, extractedValue) + log.Printf("[setVariable] %s = %v", key, extractedValue) extractMapping[key] = extractedValue } @@ -117,7 +118,7 @@ func (v *ResponseObject) Validate(validators []TValidator, variablesMapping map[ // do assertion result := assertFunc(v.t, expectValue, checkValue) - log.Printf("assert %s %s %v => %v", checkItem, assertMethod, expectValue, result) + log.Printf("[assert] %s <%s> %v => %v", checkItem, assertMethod, expectValue, result) if !result { v.t.Fail() }