467,211 Members | 1,224 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,211 developers. It's quick & easy.

Creating context

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?

Nov 18 '05 #1
  • viewed: 1651
Share:
3 Replies
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/
----------------------------------
Making waves on the Web
"Seth" <ss*****@hotmail.com> wrote in message
news:00****************************@phx.gbl...
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?

Nov 18 '05 #2
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.)

Nov 18 '05 #3
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...PNETHTTPRuntim
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.)

Nov 18 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by vande | last post: by
reply views Thread by Matt Warner | last post: by
3 posts views Thread by Ivonne Riedel | last post: by
4 posts views Thread by tshad | last post: by
1 post views Thread by Frijoles | last post: by
3 posts views Thread by Allan Ebdrup | last post: by
9 posts views Thread by =?Utf-8?B?YmJn?= | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.