hrp_step_
通过源码得知 SetupHooks 和 TeardownHooks 并没有返回值,只要加上返回值,然后给 rb.requestMap 重新赋值,同时也要给 rb.req 赋值,rb.req 是实际请求数据,在之前就处理过了,所以这里需要再进行处理
package hrp
func runStepRequest(r *SessionRunner, step *TStep) (stepResult *StepResult, err error) {
// ... 省略
// add request object to step variables, could be used in setup hooks
stepVariables["hrp_step_name"] = step.Name
stepVariables["hrp_step_request"] = rb.requestMap
// deal with setup hooks
for _, setupHook := range step.SetupHooks {
_, err = parser.Parse(setupHook, stepVariables)
if err != nil {
return stepResult, errors.Wrap(err, "run setup hooks failed")
}
}
// ... 省略
}
以 SetupHooks 为例
- 函数内修改数据,但是不能改变 request 的结构
python def setup_hook_encryption(request): request["body"]["setup_hook_encryption_request"] = "setup_hook_encryption_request" return request
- stepVariables 增加 request,兼容 v3 的写法
- 数据写入 rb.requestMap
- 数据写入 rb.req.Body,修改后需要修改 ContentLength,否则会因为长度不一致导致报错
package hrp
func runStepRequest(r *SessionRunner, step *TStep) (stepResult *StepResult, err error) {
// ... 省略
// add request object to step variables, could be used in setup hooks
stepVariables["hrp_step_name"] = step.Name
stepVariables["hrp_step_request"] = rb.requestMap
stepVariables["request"] = rb.requestMap
// deal with setup hooks
for _, setupHook := range step.SetupHooks {
req, err := parser.Parse(setupHook, stepVariables)
if err != nil {
return stepResult, errors.Wrap(err, "run setup hooks failed")
}
reqMap, ok := req.(map[string]interface{})
if ok {
rb.requestMap = reqMap
stepVariables["request"] = reqMap
}
}
if len(step.SetupHooks) > 0 {
requestBody, ok := rb.requestMap["body"].(map[string]interface{})
if ok {
body, err := json.Marshal(requestBody)
if err == nil {
rb.req.Body = io.NopCloser(bytes.NewReader(body))
rb.req.ContentLength = int64(len(body))
}
}
}
// ... 省略
}