Unit Testing classes that derive from IHttpHandler

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
 
B

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);
 
B

Brian Takita

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top