By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,173 Members | 1,686 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,173 IT Pros & Developers. It's quick & easy.

"faking" an HTTPContext when running unit tests

P: n/a
I am working on a web/webservice application that has a service layer. Most
service methods will perform an access-check before executing. This check
uses the IPrincipal credentials available in HttpContext.Current. This works
great.

My issue is that I need to write unit tests for those service methods. These
tests then run outside of IIS, thus there is no HttpContext.Current
available - and the servicemethod will not execute.

The two courses of action that seem possible:

a) somehow "forge" an HttpContext, so that the code will work unaltered
or
b) wrap the call to HttpContext.Current, so that I can supply a different
credential for testing purposes.

is a) a viable solution? or is there a substantial amount code that must be
written in order to set something like that up? The nice thing would be if
there was some simple way to create an HttpContext and set the
HttpContext.Current to this context - but I fear it may not be that simple.

Thanks
Nov 18 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Hi,
Thanks for posting in the community!
From your description, you'd like to manually generate the HttpContext
instance so as to do some unit test for ASP.NET web application with out
being host in IIS. And you're looking for some suggestions or information
on this.
If there is anything I misunderstood, please feel free to let me know.

Based on my research. Since the ASP.NET runtime process a certain web
request through a serials of progress which is called the ASP.NET runtime
pipeline. The pipeline start from the HttpRuntime which has a
processRequest method. And during the processRequest method, the ASP.NET
runtime finishs the whole progress of dealing with the certain request. The
HttpRuntime will generated an HttpContext instance which contains many
other helper compoent instances, here is the description in MSDN:
-------------------------------
The HttpRuntime object initializes a number of internal objects that will
help carry the request out. Helper objects include the cache manager (the
Cache object) and the internal file system monitor used to detect changes
in the source files that form the application. The HttpRuntime creates the
context for the request and fills it up with any HTTP information specific
to the request. The context is represented by an instance of the
HttpContext class
-------------------------------
For more detailed information, you may view the following reference in MSDN:
#The ASP.NET HTTP Runtime
http://msdn.microsoft.com/library/en...PNETHTTPRuntim
e.asp?frame=true

So if we'd like to manually faking the HttpContext, we need to first faking
the ASP.NET dotnet's runtime pipeline's task, that 'll be a complex work.
However, there're several tech articles which focus on Hosting the ASP.NET
Runtime outside the IIS such as in a winform or console application. Here
is some weblinks to them, you may have a look to see whether they'll help
you:
##Tech Articles on hosting ASP.NET runtime outside IIS

#Hosting ASP.NET Outside of IIS(especially for ASP.NET unit test)
http://hyperthink.net/blog/PermaLink...e58-9a7e25348b
8c.aspx

#Using ASP.NET Runtime in Desktop Applications
http://www.codeproject.com/dotnet/usingaspruntime.asp
#Using the ASP.Net Runtime for extending desktop applications with dynamic
HTML Scripts
http://www.west-wind.com/presentatio...netruntime.asp

Also, there're some thirty-party Unit Testing tools for ASP.NET web
application, some of them provide the convenient interfaces for ASP.NET web
application's unit test. Here is some web links to them:

##ASP.NET Unit Test Tools
#Get Test Infected with NUnit: Unit Test Your .NET Data Access Layer
http://msdn.microsoft.com/library/en...twithnunit.asp
?frame=true

#NUnitAsp ASP.NET unit testing
http://nunitasp.sourceforge.net/

Please check out the preceding items. If you need any further help, please
feel free to post here.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Nov 18 '05 #2

P: n/a
Hi,
Have you had a chance to check out my suggestions or have you got any good
ideas on this issue? If you need any further help, please feel free to let
me know.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Nov 18 '05 #3

P: n/a
Thank you for your response. It was most valuable.

With regards to the development effort needed to host outside IIS, that is
something we will consider.

Presently, we went with the somewhat less elegant solution - of using a
predetermined identity in the code, when the httpcontext is unavailable.
This is not the ideal approach, but it did provide a quick-fix in the short
term (i.e. if no context, we assume that the component is run for testing,
and create a dummy principal, that has predictable rights.

In a unit test scenario, this will work for most of the testing, with user
rights testing being more difficult to do. no decision has been made on the
long-term solution.

Regards,

Inge

"Steven Cheng[MSFT]" <v-******@online.microsoft.com> wrote in message
news:o0**************@cpmsftngxa07.phx.gbl...
Hi,
Have you had a chance to check out my suggestions or have you got any good
ideas on this issue? If you need any further help, please feel free to let
me know.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Nov 18 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.