Unit Testing classes that derive from IHttpHandler

  • Thread starter Thread starter Brian Takita
  • Start date Start date
B

Brian Takita

Hello,

Is there a way to unit test a that derives from IHttpHandler that uses
the HttpResponse in the HttpContext passed to it?

When I try to call the context.Response.End() method, I get the
following error:
Unable to connect to the remote server

I am looking at
http://hyperthink.net/blog/CommentView,guid,271632d2-07e3-41af-9e58-9a7e25348b8c.aspx
which has a way to create a HttpRuntime, so IIS is not needed to unit
test an asp.net page.

Unfortunately, I can't figure out how to get the IHttpHandler attached.

Does anybody know how to do this?

Thank you,
Brian Takita
 
Problem solved. The context can be called directly if an
ApplicationInstance is assigned to the context.

Here is a snippet of my test code:

Thread.GetDomain().SetData(".appPath",
"c:\\inetpub\\wwwroot\\webapp\\");
Thread.GetDomain().SetData(".appVPath", "/webapp");
Thread.GetDomain().SetData(".hostingVirtualPath", "/webapp");
Thread.GetDomain().SetData(".hostingInstallDir",
HttpRuntime.AspInstallDirectory);

StringWriter sw = new StringWriter();
SimpleWorkerRequest wr = new SimpleWorkerRequest("default.aspx", "",
sw);
HttpContext context = new HttpContext(wr);

context.ApplicationInstance = new HttpApplication();

CustomHttpProxy p = new CustomHttpProxy ();
p.ProcessRequest(context);
 
One problem I'm running into now is reading the Response.OutputStream.

Does anybody know how to read this stream into a string variable?

Thank you,
Brian
 
Back
Top