代码之家  ›  专栏  ›  技术社区  ›  AdrianHHH

如何从插件读取、更新和替换web测试中的“字符串体”字段?

  •  6
  • AdrianHHH  · 技术社区  · 10 年前

    我录制的web性能测试有几个“字符串体”字段,我需要在运行时从web测试请求插件中修改它们的内容。

    “字符串正文”字段不能直接从 PreRequestEventArgs .

    如何将“字符串正文”字段读入 string 修改后再写回去?

    1 回复  |  直到 10 年前
        1
  •  5
  •   AdrianHHH    10 年前

    要读取“字符串正文”字段,请将请求正文转换为 StringHttpBody 这使得字符串可用。要将其写回,请新建 字符串HttpBody 对象以包含更新的字符串,然后将其写入请求中。

    使用插件,我需要在web性能测试中修改请求的“字符串体”字段。我可以使用以下代码访问内容:

    public override void PreRequest(object sender, PreRequestEventArgs e)
    {
        if ( e.Request.Body == null ) { return; }
        StringHttpBody httpBody = e.Request.Body as StringHttpBody;
        if ( httpBody == null ) { return; }
        string body = httpBody.BodyString;
    
        string updatedBody = UpdateBody(body);
    
        StringHttpBody newBody = new StringHttpBody();
        newBody.BodyString = updatedBody;
        e.Request.Body = newBody;
    }