How To Authenticate to a Proxy Server within a Visual Studio .WebTest using Visual Studio 2008 SP1

So you record a webtest in Visual Studio and during the playback you notice that you receive an HTTP response with the following, and all of your tests fail.

HTTP/1.1 407 Proxy Authentication Required ( The ISA Server requires authorization to fulfill the request. Access to the Web Proxy filter is denied. )
Proxy-Authenticate : Negotiate,Kerberos,NTLM,Basic realm="EXAMPLE.EXAMPLE.COM"

Now what do you do?

 

I see this question come up from time to time, so I thought I would blog about it to make things easier for folks who run into this problem.  

 

What is the HTTP 407 response and how do I solve this?

First, your proxy is forcing you to authenticate to it *before* you are allowed to make your connections outbound from it and over to the system you want to test.

First question I would have - is Do you really have to go through the proxy? Downside is that the proxy is going to skew your test results if you are running a load test, because the proxy may breakdown or delay your responses, or it could end up caching files, and improve your responses. Either way it is a variable that if you can eliminate would be preferrable if you want reliable results in a Visual Studio LoadTest.

So, lets assume you cant get around the proxy issue.

1) You must have VS2008 Service Pack 1, there were some bugs fixed around the proxy implementation explicitly.
2) You can create a coded test or you can implement a WebTest plug-in, becuase you are going to need to explicity authenticate to the proxy, before you deal with anything else.

 

So, why do I need to do this in code in a plugin?

The short answer is that the WebProxy is not exposed in the User Interface within a Visual Studio .WebTest, so you will need to set the WebProxy in code. This is actually not that difficult and I have a sample plugin that you can use below to get you started.

1 using System;  
2 using Microsoft.VisualStudio.TestTools.WebTesting;  
3 using Microsoft.VisualStudio.TestTools.WebTesting.Rules;  
4 using System.Net;  
5  
6 namespace WebTestPluginNamespace  
7 {  
8     public class MyWebTestPlugin : WebTestPlugin  
9     {  
10  
11  
12         public override void PreWebTest(object sender, PreWebTestEventArgs e)  
13         {  
14             // Create a WebProxy object for your proxy  
15             WebProxy webProxy = new WebProxy("<https://yourproxy>");  
16  
17             //Set the WebProxy so that even local addresses use the proxy  
18             // webProxy.BypassProxyOnLocal = false;  
19  
20             // Use this WebProxy for the Web test  
21             e.WebTest.WebProxy = webProxy;  
22  
23  
24  
25             e.WebTest.PreAuthenticate = true;  
26             NetworkCredential proxyCredentials;  
27  
28             proxyCredentials = new NetworkCredential();  
29  
30             proxyCredentials.Domain = "yourDomain";  
31             proxyCredentials.UserName = "yourUserName";  
32             proxyCredentials.Password = "yourPassword";  
33             e.WebTest.WebProxy.Credentials = proxyCredentials;  
34  
35  
36  
37  
38         }  
39     }  
40 }  
41