diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index be5924ff..cc54b050 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -1,5 +1,9 @@ # Release History +## v0.6.3 (2022-02-22) + +- feat: support customized setup/teardown hooks (variable assignment not supported) + ## v0.6.2 (2022-02-22) - feat: support text/html extraction with regex diff --git a/examples/demo.json b/examples/demo.json index f18347e6..da332faf 100644 --- a/examples/demo.json +++ b/examples/demo.json @@ -34,8 +34,15 @@ "variables": { "b": 34.5, "n": 3, + "name": "get with params", "varFoo2": "${max($a, $b)}" }, + "setup_hooks": [ + "${setup_hook_example($name)}" + ], + "teardown_hooks": [ + "${teardown_hook_example($name)}" + ], "extract": { "varFoo1": "body.args.foo1" }, diff --git a/examples/demo.yaml b/examples/demo.yaml index ac157382..387b9345 100644 --- a/examples/demo.yaml +++ b/examples/demo.yaml @@ -24,7 +24,12 @@ teststeps: variables: b: 34.5 "n": 3 + name: get with params varFoo2: ${max($a, $b)} + setup_hooks: + - ${setup_hook_example($name)} + teardown_hooks: + - ${teardown_hook_example($name)} extract: varFoo1: body.args.foo1 validate: diff --git a/examples/plugin/debugtalk.go b/examples/plugin/debugtalk.go index ff8d115f..64cdc946 100644 --- a/examples/plugin/debugtalk.go +++ b/examples/plugin/debugtalk.go @@ -55,3 +55,11 @@ func Concatenate(args ...interface{}) (interface{}, error) { } return result, nil } + +func SetupHookExample(args string) string { + return fmt.Sprintf("step name: %v, setup...", args) +} + +func TeardownHookExample(args string) string { + return fmt.Sprintf("step name: %v, teardown...", args) +} diff --git a/examples/plugin/hashicorp.go b/examples/plugin/hashicorp.go index 8bae45a3..1d3f78e2 100644 --- a/examples/plugin/hashicorp.go +++ b/examples/plugin/hashicorp.go @@ -10,5 +10,7 @@ func main() { plugin.Register("sum_two_string", SumTwoString) plugin.Register("sum_strings", SumStrings) plugin.Register("concatenate", Concatenate) + plugin.Register("setup_hook_example", SetupHookExample) + plugin.Register("teardown_hook_example", TeardownHookExample) plugin.Serve() } diff --git a/internal/scaffold/demo.go b/internal/scaffold/demo.go index 19a62fac..de6e8448 100644 --- a/internal/scaffold/demo.go +++ b/internal/scaffold/demo.go @@ -19,8 +19,11 @@ var demoTestCase = &hrp.TestCase{ "n": 3, // inherit config level variables if not set in step level, a/varFoo1 "b": 34.5, // override config level variable if existed, n/b/varFoo2 "varFoo2": "${max($a, $b)}", // 34.5; override variable b and eval again + "name": "get with params", }). + SetupHook("${setup_hook_example($name)}"). GET("/get"). + TeardownHook("${teardown_hook_example($name)}"). WithParams(map[string]interface{}{"foo1": "$varFoo1", "foo2": "$varFoo2"}). // request with params WithHeaders(map[string]string{"User-Agent": "HttpRunnerPlus"}). // request with headers Extract(). @@ -99,10 +102,20 @@ func Sum(args ...interface{}) (interface{}, error) { return sum, nil } +func SetupHookExample(args string) string { + return fmt.Sprintf("step name: %v, setup...", args) +} + +func TeardownHookExample(args string) string { + return fmt.Sprintf("step name: %v, teardown...", args) +} + func main() { plugin.Register("sum_ints", SumInts) plugin.Register("sum_two_int", SumTwoInt) plugin.Register("sum", Sum) + plugin.Register("setup_hook_example", SetupHookExample) + plugin.Register("teardown_hook_example", TeardownHookExample) plugin.Serve() } ` diff --git a/runner.go b/runner.go index 7f6778c1..c67c2400 100644 --- a/runner.go +++ b/runner.go @@ -616,6 +616,14 @@ func (r *caseRunner) runStepRequest(step *TStep) (stepResult *stepData, err erro } sessionData := newSessionData() + // deal with setup hooks + for _, setupHook := range step.SetupHooks { + _, err = r.parser.parseData(setupHook, step.Variables) + if err != nil { + return stepResult, errors.Wrap(err, "run setup hooks failed") + } + } + // convert request struct to map jsonRequest, _ := json.Marshal(&step.Request) var requestMap map[string]interface{} @@ -659,7 +667,7 @@ func (r *caseRunner) runStepRequest(step *TStep) (stepResult *stepData, err erro if len(step.Request.Params) > 0 { params, err := r.parser.parseData(step.Request.Params, step.Variables) if err != nil { - return stepResult, errors.Wrap(err, "parse data failed") + return stepResult, errors.Wrap(err, "parse request params failed") } parsedParams := params.(map[string]interface{}) requestMap["params"] = parsedParams @@ -797,6 +805,14 @@ func (r *caseRunner) runStepRequest(step *TStep) (stepResult *stepData, err erro } stepResult.ContentSize = resp.ContentLength stepResult.Data = sessionData + + // deal with teardown hooks + for _, teardownHook := range step.TeardownHooks { + _, err = r.parser.parseData(teardownHook, step.Variables) + if err != nil { + return stepResult, errors.Wrap(err, "run teardown hooks failed") + } + } return stepResult, err }