问题
I'm using Visual Studio 2015 for our performance testing. I'm using a web test to make a call to an API endpoint. Using fiddler and HTTPRequester I can connect to the API with no problem. When I use the webtest I receive a 401 unauthorized. The difference between the webtest and everything else is the webtest is using Authorization: Negotiate instead of Authorization: Basic.
How, in Visual Studio 2015 can I force the Authorization to Basic instead of negotiate?
Here is the header as the webtest currently creates it:
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
If this is a duplicate I apologize. I've been looking all day for information on this and I have found nothing that helps.
回答1:
Basic authentication is done by adding the appropriate header field to the requests. You can use a plugin based on the following code.
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);
}
See also this page.
A comment says "I have hundreds of performance tests I have to modify now". The ".webtest" file is just XML. You can try making the above code into a WebTestPlugin
, ie one that is called for every request in the test. Then manually add that to one test and see exactly what changes are made to the XML of that test. It should then be a simple scripting (or editing) task to modify all the files.
回答2:
Other option is - you can grab basic auth token using Postman and pass it along in the headers. It worked for me.
来源:https://stackoverflow.com/questions/38001310/force-authorization-to-basic-in-web-test-for-performance