473,625 Members | 3,318 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

"faking" an HTTPContext when running unit tests

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.Cur rent. 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.Cur rent
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.Cur rent, 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.Cur rent to this context - but I fear it may not be that simple.

Thanks
Nov 18 '05 #1
3 3687
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=tru e

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
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
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.m icrosoft.com> wrote in message
news:o0******** ******@cpmsftng xa07.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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
17298
by: Csaba2000 | last post by:
I have set onmousedown to change the cursor, but this setting is ignored (IE 5.5; NN 6.1 on Win 2K Pro) until the mouse is either moved or the mouse button is released. On Opera 7.01, the setting seems to be ignored completely, even when I try with window.setTimeout. So my two questions are: (1) Most important: Is there anything I can do so that I don't have to wait for the next mouse event before the cursor gets repainted. (2) Why...
7
6245
by: czechboy | last post by:
Hi, I would like to send DELETE request to google calendar but nothing of this works: xmlhttp.open("POST",del,false); xmlhttp.setRequestHeader("X-HTTP-Method-Override", "DELETE"); xmlhttp.send(); or xmlhttp.open("DELETE",del,false); xmlhttp.send();
4
1281
by: Ronald S. Cook | last post by:
I have an old ASP.NET app that I need to rewrite. It uses frames to have an embedded Windows Media Player playing content that is controlled from other frames on the page. And, of course, the user can navigate around other frames without disrupting what is playing in the frame that contains the player. What am I supposed to use to get this functionality these days if frames are taboo/the old way of doing things?
0
8256
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8694
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8635
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8356
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8497
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6118
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4089
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2621
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1803
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.