在Web测试中强制执行基本授权以获得性能

时间:2016-06-23 20:33:13

标签: performance visual-studio-2015 authorization webtest

我正在使用Visual Studio 2015进行性能测试。我正在使用Web测试来调用API端点。使用fiddler和HTTPRequester我可以毫无问题地连接到API。当我使用webtest时,我收到401未授权。 webtest和其他一切之间的区别在于webtest使用的是Authorization:Negotiate而不是Authorization:Basic。

如何在Visual Studio 2015中强制授权到Basic而不是协商?

以下是webtest当前创建的标题:

POST /Foo/api.asp?$api=FooBarAPI HTTP/1.1 
Accept : application/xml 
User-Agent : Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)
Accept-Language : en-US
Accept-Encoding : GZIP
Content-Type : application/xml
Authorization : Negotiate (Base64 Encoded Login Information)
Host : Foo.Bar.net
Content-Length : 0

如果这是重复,我道歉。我一整天都在寻找有关这方面的信息,但我没有发现任何有用的信息。

2 个答案:

答案 0 :(得分:0)

通过向请求添加适当的标头字段来完成基本身份验证。您可以使用基于以下代码的插件。

public string UserName { get; set; }
public string Password { get; set; }

public override void PreRequest(object sender, PreRequestEventArgs e)
{
    e.Request.Headers.Add("Authorization", "Basic " + GetEncodedAuthorisation(UserName, Password));
}

private string GetEncodedAuthorisation(string userName, string password)
{
    return Encode8BitStringInBase64(userName + ":" + password);
}

private string Encode8BitStringInBase64(string source)
{
    byte[] outBytes = new byte[source.Length];

    for (int ix = 0; ix < source.Length; ix++)
    {
        char ch = source[ix];
        outBytes[ix] = (byte)(((int)ch) & 0xFF);
    }

    return Convert.ToBase64String(outBytes);
}

另见this page

评论说&#34;我有数百个性能测试我现在需要修改&#34; 。 &#34; .webtest&#34;文件只是XML。您可以尝试将上述代码转换为WebTestPlugin,即为测试中的每个请求调用的代码。然后手动将其添加到一个测试中,并确切地查看对该测试的XML所做的更改。然后,它应该是一个简单的脚本(或编辑)任务来修改所有文件。

答案 1 :(得分:0)

其他选项是 - 您可以使用Postman获取基本身份验证令牌并将其传递到标头中。它对我有用。

Graphic showing auth token passed as header