繁体   English   中英

如何在代码测试中获得响应主体?

[英]how to get the response body in code webtest?

我写了一个称为网络服务的网络测试。

我想获取响应主体并对其进行一些验证。

    public override IEnumerator<WebTestRequest> GetRequestEnumerator()
    {


        WebTestRequest request2 = new WebTestRequest("webservice");

        request2.Headers.Add("Content-Type", "application/json");
        request2.Method = "POST";
        request2.Encoding = System.Text.Encoding.GetEncoding("utf-8");
        StringHttpBody request2Body = new StringHttpBody();

        request2Body.ContentType = "application/json";
        request2Body.InsertByteOrderMark = false;
        request2Body.BodyString = @"{                                       <body>}";
        request2.Body = request2Body;


        WebTestResponse res = new WebTestResponse();
        console.WriteLine(res.BodyBytes);

       yield return request2;

       request2 = null;
    }

当我运行上面的代码时,我的控制台上没有任何响应。

如何使用编码的Webtest获得响应正文?

问题中的代码至少存在三个问题

  1. 问题中的代码在执行WriteLine之前不会执行请求。 这两个语句WebTestResponse res = new WebTestResponse(); console.WriteLine(res.BodyBytes); 只需创建一个新的WebTestResponse对象(具有所有默认值),然后尝试打印其部分内容即可。 该请求是由调用GetRequestEnumerator方法的代码发出的。

  2. 未定义console对象。 普通控制台的首字母大写,即Console

  3. 当执行网络测试时,我不确定其“控制台”输出将到达何处。 据我所知,Web测试的标准输出不是定义明确的东西。

获得响应正文的一种简单方法是使用WebTestRequestPluginPostRequest方法。 作为一个开始

public class BodyContentsDemo : WebTestRequestPlugin
{
    public override void PostRequest(object sender, PostRequestEventArgs e)
    {
        byte[] bb = e.Response.BodyBytes;
        string ss = e.Response.BodyString;

        e.WebTest.AddCommentToResult(
            "BodyBytes is " +
            bb == null ? " null"
            : bb.Length.ToString() + " bytes");

        e.WebTest.AddCommentToResult(
            "BodyString is " +
            ss == null ? "null"
            : ss.Length.ToString() + " chars");

        // Use bb or ss.
    }
}

请注意,使用AddCommentToResult将日志记录信息提供给Web测试结果日志。

最终,我能够在过去几天努力寻找从Web Performance测试中获取响应文本的解决方案。 希望这可以帮助

公共重写IEnumerator GetRequestEnumerator(){

    WebTestRequest request2 = new WebTestRequest("webservice");

    request2.Headers.Add("Content-Type", "application/json");
    request2.Method = "POST";
    request2.Encoding = System.Text.Encoding.GetEncoding("utf-8");
    StringHttpBody request2Body = new StringHttpBody();

    request2Body.ContentType = "application/json";
    request2Body.InsertByteOrderMark = false;
    request2Body.BodyString = @"{<body>}";
    request2.Body = request2Body;


    WebTestResponse res = new WebTestResponse();
    console.WriteLine(res.BodyBytes);

   yield return request2;
   /*This will generate a new string which can be part of your filename when      you run performance tests*/
   String randomNo = DateTime.Now.ToString("MM/dd/yyyy HH:mm:ss").Replace("-", "").Replace(" ", "").Replace(":", "");
   /*This will generate a new file each time your WebRequest runs so you know what the server is returning when you perform webtests*/
   /*You can use some Json parser if your response is Json and capture and validate the response*/
   System.IO.File.WriteAllText(@"C:\Users\XXXX\PerformanceTestRequests\LastResponse" + randomNo+ ".txt", this.LastResponse.BodyString);
   request2 = null;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM