简体   繁体   English

如何在Visual Studio负载测试中接受HTTP 400和HTTP 500作为有效响应代码?

[英]How to accept HTTP 400 and HTTP 500 as valid response codes in Visual Studio Load Test?

I am using the Visual Studio 2013 web testing framework to stress-test a REST service. 我正在使用Visual Studio 2013 Web测试框架来对REST服务进行压力测试。 I want to measure how well the application performs when presented with pathological/malformed JSON in POST request. 我想测量在POST请求中出现病态/格式错误的JSON时应用程序的执行情况。 The server is responding with either HTTP 400 or HTTP 500 and I would like to consider both a success response. 服务器使用HTTP 400或HTTP 500进行响应,我想同时考虑成功响应。

I tried validating the response with the pass-all event handler: 我尝试使用pass-all事件处理程序验证响应:

public class MalformedBodyTest : WebTest 
{
    private void Validate(object sender, ValidationEventArgs e)
    {
        e.IsValid = true;
    }

    public MalformedBodyTest()
    {
        this.ValidateResponse += Validate;
    }

    ...

}

This does not seem to have any difference, the responses are still showing up as failed. 这似乎没有任何区别,响应仍然显示为失败。 Using ExpectedHttpStatusCode property of WebTestRequest works fine but only enables me to test for a single HTTP status code. 使用WebTestRequest ExpectedHttpStatusCode属性工作正常但只允许我测试单个HTTP状态代码。

I would like the request to be considered failed just if the timeout is reached. 我希望在达到超时时将请求视为失败。 Is there any way to achieve this functionality? 有没有办法实现这个功能?

Ok, that was actually quite simple. 好的,这实际上非常简单。 I missed that there is also a property IgnoreHttpStatusCode in WebTestRequest . 我错过了WebTestRequest还有一个属性IgnoreHttpStatusCode By setting it to true in the request generator 通过在请求生成器中将其设置为true

public override IEnumerator<WebTestRequest> GetRequestEnumerator()
{
    for (var i = 0; i < Iterations; ++i)
    {
        var request = new WebTestRequest(_url);
        request.IgnoreHttpStatusCode = true;

        ...

        yield return request;
    }
}

I was then able to precisely set which HTTP status codes to accept and which to reject in the validation handler: 然后我能够精确设置接受哪些HTTP状态代码以及在验证处理程序中拒绝哪些HTTP状态代码:

private void Validate(object sender, ValidationEventArgs e)
{
    e.IsValid = (e.Response.StatusCode == HttpStatusCode.BadRequest ||
                 e.Response.StatusCode == HttpStatusCode.InternalServerError);
}

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

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