Creating context

S

Seth

I have been trying to create a mock httpcontext for the
purpose of unit testing. I have a class, Customer, that
uses cookies. I have set it up to be able to take a
httpcontext as a parameter in an overloaded constructor.
I have seen a few references to people that have created
mock httpcontexts but I can not seem to get it to work.

This is what I have right now, it is the result of
working backwards from httpcontext and trial and error.
It compiles fine but the swrq is not set to an instance
of an object at runtime.

SimpleWorkerRequest swrq;
TextWriter output = new StringWriter();
using(TextWriter streamWriter = new StreamWriter
("InvalidPathChars.txt"))

swrq = new System.Web.Hosting.SimpleWorkerRequest
( "page", "query", output );

return new HttpContext( swrq );

Does anyone have any links to information about this?
 
R

Rick Strahl [MVP]

Seth,

I'm not sure that you can do this. A simple Worker Request by itself doesn't
make for an HTTP Context until it's fed back into the HTTPRuntime via
ProcessRequest, so I'm not surprised this isn't working in fact surprised
that hte cast is working. The sole purpose of HttpWorkerRequest is to be an
input to an instance of the ASP.Net runtime which in turn generates the
context.

Can't you host the ASP.net runtime and run the request through it to get
your Context and hook from inside there?
+++ Rick ---

--

Rick Strahl
West Wind Technologies
http://www.west-wind.com/
http://www.west-wind.com/webblog/
 
M

MSFT

Hi Seth,

Thank you for posting in Microsoft Newsgroup. Regarding on the issue, I am
finding proper resource to assist you and we will update as soon as posible.

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.)
 
S

Steven Cheng[MSFT]

Hi Seth,


Based on your description, you are wanting to manually create a HttpContext
to with a SimpleWorkRequest object, how ever when you run the app, you
found that the SimpleWorkRequest couldn't be created correctly. Is this the
problem you encountered?

If so, I've reviewed some references on the ASP.NET runtime. In ASP.NET ,
for a client request beccome from a httprequest to a html output stream, it
need to pass through many processes. At the highest level of abstraction,
the ASP.NET worker process accomplishes one main task¡ªhanding the request
over to a chain of managed objects dubbed the HTTP pipeline. The HTTP
pipeline is activated by creating a new instance of the HttpRuntime class
and then calling its ProcessRequest method. As mentioned, in ASP.NET you
have a single worker process running all the time (except that the Web
garden model is enabled) that manages all Web applications in distinct
AppDomains. Each AppDomain has its own instance of the HttpRuntime class¡
ªthe entry point in the pipeline. 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 about the ASP.NET Runtime and the pipeline,
you can visit this tech article via the following weblink:
http://msdn.microsoft.com/library/en-us/dnaspp/html/dngrfTheASPNETHTTPRuntim
e.asp?frame=true

So if you need to generate the required internals objects for the
HttpRuntime, otherwise, the HTTP pipline can't start working since the
working enviroment is not prepared well. Also, I've found some one else's
sample for using the HttpRuntime and SimpleWorkRequest outside the IIS(in a
winform application), you can visit the link below to get the sample and
its tutorial aritcle:

http://www.codeproject.com/dotnet/usingaspruntime.asp

This article has provided a complete example for how to Using ASP.NET
Runtime in Desktop Applications(visit a aspx page). I think it maybe
helpful to you.


If you have any new findings please also let me know.


Merry Christmas!!

Steven Cheng
Microsoft Online Support

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

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,774
Messages
2,569,599
Members
45,163
Latest member
Sasha15427
Top