views:

263

answers:

4

Following Jonathan Holland's suggestion in his comment for my previous question: Is there any way in .NET to programmatically listen to HTTP traffic? I've made a separate (but not exactly a duplicate) question for what I really want to know:

How do I automate a web proxy in .NET for unit tests (including set up and tear down) for spying on HTTP traffic that comes from the browser (particularly images, scripts, and XmlHttpRequests on the requested page)?

I prefer to have zero set-up (so no Fiddler installed on Windows) where everything can be unpacked from an assembly, deployed, and then removed without a trace, so to speak.

+2  A: 

Roll your own pass-through proxy, then have your test harness issue configuration commands on the admin port of the proxy. The proxy will dutifully route any normal connection to the specified ip:port, with minimal "setup."

Nescio
+1  A: 

If you want to take control of a browser-like request and look at requests and headers from a stimulated web browser to your localhost, you can use System.Net.WebClient

If you want a .NET solution, where you use complete proxy detection, then have a look at this MSDN article: http://msdn.microsoft.com/en-us/magazine/cc300743.aspx. It explains how to integrate with a proxy like Fiddler

Before using any of these solutions, I strongly recommend that you review your unit tests and what you're trying to accomplish. A full proxy solution is often times out of the scope of unit tests and you may want to scale your tests down a bit.

However, if you are writing integration tests, then these solutions should serve you well.

Ryan Guest
A: 

I'm not sure if it's what you're looking for, but here is an example of ASP.NET unit testing using selenium. http://www.stevetrefethen.com/blog/AutomatedTestingOfASPNETWebApplicationsUsingSelenium.aspx

Eduardo Campañó
I already have a browser automation framework (WebAii) but it doesn't capture outgoing HTTP traffic.
Mark Cidade
+2  A: 

WebAii 2.0 has a built-in HTTP proxy:

http://www.artoftest.com/community/blogs/09-03-25/WebAii_2_0_Beta_Released.aspx

da_ponc