Forge an httpcontext, session, and httpheader for non-webbase projects such as unit tests

Source: Internet
Author: User

Httpcontext. Current is null during unit testing.

Some DLL files are bound to httpcontext (the major reason is that most of ...net files are used for web projects)

Or try to use these DLL in Windows form or console...

Of course, httpcontext. current can be assigned a value... the simplest way is to directly add an httpcontext to it.

  httprequest request  =    New   httprequest ( " " , "   http: // localhost  "  , " " ); 
httpcontext. current = New httpcontext (request, New httpresponse ( New system. io. stringwriter ();

Well, if you need a session, add the followingCode

 System. Web. sessionstate. sessionstateutility. addhttpsessionstatetocontext (
Httpcontext. Current, New Httpsessionstatecontainer
( "" ,
New Sessionstateitemcollection (),
New Httpstaticobjectscollection (),
20000 ,
True ,
Httpcookiemode. usecookies,
Sessionstatemode. Off,
False
)
);

The problem arises again... some code will read httpheader... for example, what Ip is often stored here

Even more depressing is that the class of httpcontext is often unable to set httpheader .....

Because this is read-only .......

In fact, read-only is basically a lie in. net, so it is a bit useful during compilation.

When running, do not read-only connections to private can be obtained through reflection.

Use reflection to assign values to httpheader.

 VaR instance  =  Httpcontext. Current. Request. servervariables;
Type type = Instance. GetType ();
Bindingflags temp = Bindingflags. nonpublic | Bindingflags. Instance | Bindingflags. Static;

Methodinfo addstatic = Null ;
Methodinfo makereadonly = Null ;
Methodinfo makereadwrite = Null ;


Methodinfo [] Methods = Type. getmethods (temp );
Foreach (Methodinfo Method In Methods)
{
Switch (Method. Name)
{
Case " Makereadwrite " : Makereadwrite = Method;
Break ;
Case " Makereadonly " : Makereadonly = Method;
Break ;
Case " Addstatic " : Addstatic = Method;
Break ;
}
}

Makereadwrite. Invoke (instance, Null );
List < String [] > List = New List < String [] > ();
String IP = Servicecontext. IPaddress;
List. Add ( New String [] { " Http_x_forwarded_for " , IP });
List. Add ( New String [] { " Http_client_ip " , IP });
List. Add ( New String [] { " Http_x_forwarded " , IP });
List. Add ( New String [] { " Http_x_cluster_client_ip " , IP });
List. Add ( New String [] { " Http_forwarded_for " , IP });
List. Add ( New String [] { " Http_forwarded " , IP });
List. Add ( New String [] { " Remote_addr " , IP });
Foreach ( String [] Values In List)
Addstatic. Invoke (instance, values );
Makereadonly. Invoke (instance, Null );

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.