mirror of
https://github.com/httprunner/httprunner.git
synced 2026-09-05 15:37:35 +08:00
fix: extract variables for reference
This commit is contained in:
@@ -0,0 +1,44 @@
|
|||||||
|
package postman_echo
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/httprunner/httpboomer"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestCaseExtractStep(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",
|
||||||
|
"expectedStatusCode": 200,
|
||||||
|
}).
|
||||||
|
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", "$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("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)
|
||||||
|
}
|
||||||
|
}
|
||||||
+12
-2
@@ -3,6 +3,7 @@ package httpboomer
|
|||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"log"
|
"log"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/imroc/req"
|
"github.com/imroc/req"
|
||||||
@@ -99,12 +100,21 @@ func (v *ResponseObject) Validate(validators []TValidator, variablesMapping map[
|
|||||||
for _, validator := range validators {
|
for _, validator := range validators {
|
||||||
// parse check value
|
// parse check value
|
||||||
checkItem := validator.Check
|
checkItem := validator.Check
|
||||||
checkValue := v.searchJmespath(checkItem)
|
var checkValue interface{}
|
||||||
|
if strings.Contains(checkItem, "$") {
|
||||||
|
// reference variable
|
||||||
|
checkValue = parseData(checkItem, variablesMapping)
|
||||||
|
} else {
|
||||||
|
checkValue = v.searchJmespath(checkItem)
|
||||||
|
}
|
||||||
|
|
||||||
// get assert method
|
// get assert method
|
||||||
assertMethod := validator.Assert
|
assertMethod := validator.Assert
|
||||||
assertFunc := assertFunctionsMap[assertMethod]
|
assertFunc := assertFunctionsMap[assertMethod]
|
||||||
|
|
||||||
// parse expected value
|
// parse expected value
|
||||||
expectValue := parseData(validator.Expect, variablesMapping)
|
expectValue := parseData(validator.Expect, variablesMapping)
|
||||||
|
|
||||||
// do assertion
|
// do assertion
|
||||||
result := assertFunc(v.t, expectValue, checkValue)
|
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)
|
||||||
@@ -119,7 +129,7 @@ func (v *ResponseObject) searchJmespath(expr string) interface{} {
|
|||||||
checkValue, err := jmespath.Search(expr, v.respObjMeta)
|
checkValue, err := jmespath.Search(expr, v.respObjMeta)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("[searchJmespath] jmespath.Search error: %v", err)
|
log.Printf("[searchJmespath] jmespath.Search error: %v", err)
|
||||||
return nil
|
return expr // jmespath not found, return the expression
|
||||||
}
|
}
|
||||||
return checkValue
|
return checkValue
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user