CodeBetter.Com
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @CodeBetter

Karl Seguin

.NET From Ottawa, Ontario

HttpHandler Unit Testing

The pains of unit testing the front-end portion of ASP.NET far outweigh the benefits. It's easier and more worthwhile to stick with your domain and data layers.

Lately, I've been working on a project that makes heavy use of HttpHandlers. A significant amount of code has nothing to do with classes in the System.Web.UI namespace - so no controls, no viewstate, no postback. Since those are all the things that make unit testing ASP.NET a nasty affair, I figured it might be worthwhile to try and unit test the code in question.

It ended up being really easy, and although I've built a mini-base test class for my test fixtures to inherit, it really all comes down to 1 very simple method:
 

protected string RawRequest(string fileName, string queryString)
{
   StringBuilder output = new StringBuilder();
   using (StringWriter sw = new StringWriter(output))
   {
      HttpResponse response = new HttpResponse(sw);
      HttpRequest request = new HttpRequest(fileName, "http://fueltest.net/" + fileName, queryString);
      HttpContext context = new HttpContext(request, response);
      new RequestHandler().ProcessRequest(context);
   }
   return output.ToString();
}

where RequestHandler() is my core HttpHandler.

My tests are able to validate that the output, given the querystring inputs, are correct.
 


Published Jan 25 2007, 10:16 AM by karl
Filed under:

Comments

Bryan Wilhite said:

Yes, clearly Scott Guthrie has no spare time to promote the use of ASHX files.

I wrote about this topic last year in "More Unsung Microsoft Technology: the HTTP Handler (.ashx) Story Is Rarely Told ‘Correctly’" here:

http://www.kintespace.com/rasxlog/?p=326

And this year we have "Random Screen Shots: The New Songhay System ASP.NET Design Pattern" here:

http://www.kintespace.com/rasxlog/?p=536

Not to rain on the Atlas parade but ASHX files and AJAX with YUI is far more portable than the ASP.NET AJAX.

# January 26, 2007 12:41 AM

Jeremy D. Miller said:

Karl,  

What does the HttpHandler actually do?  If they do anything that's significant, the easiest way to unit test is going to be by composition.  Break off any piece of logic that you can and move that to "POO" classes and slice the HttpHandler itself as thinly as possible.  Either write a wrapper interface for the actual ASP.Net runtime stuff or use the HttpHandler just as a Mediator to the POO class that does the processing.

Jeremy

# January 26, 2007 8:24 AM

Elijah Manor said:

I have been looking for a good free unit tester tool as well. I have been using Selenium, but I am on the lookout for something else.

# February 4, 2007 10:51 PM

karl said:

Jeremy:

They don't do a lot. They mostly deal with very strict input and output which is all handled by separate testable classes.

The main reason I've set this up, which I realize I wasn't clear on, is to do end-to-end testing. It's for full integration testing. I submit my input to the above method and validate the output.

It's actually shown me that my real unit tests have a number of wholes in them. They'll often all pass, while these integration tests fail - meaning I failed to properly cover something. They've proven to be invaluable to catch bugs and help me better understand the more subtle aspects of unit testing I'm still missing.

# February 5, 2007 10:45 PM

Leave a Comment

(required)  
(optional)
(required)  

Enter the numbers above:
Add
Check out Devlicio.us!

Our Sponsors

Free Tech Publications