Testing an ASMX Web Service Using NUnit and Session State Transfer
I have a test class NUnit that starts an ASP.NET web service (using Microsoft.VisualStudio.WebHost.Server) that is running at http: // localhost: 1070
The problem I am running into is that I want to create a session state in an NUnit test accessible by an ASP.NET web service at localhost: 1070.
I did the following and the session state can be successfully created inside NUnit Test but lost when calling a web service:
//Create a new HttpContext for NUnit Testing based on:
//http://blogs.imeta.co.uk/jallderidge/archive/2008/10/19/456.aspx
HttpContext.Current = new HttpContext(
new HttpRequest("", "http://localhost:1070/", ""), new HttpResponse(
new System.IO.StringWriter()));
//Create a new HttpContext.Current for NUnit Testing
System.Web.SessionState.SessionStateUtility.AddHttpSessionStateToContext(
HttpContext.Current, new HttpSessionStateContainer("",
new SessionStateItemCollection(),
new HttpStaticObjectsCollection(), 20000, true,
HttpCookieMode.UseCookies, SessionStateMode.Off, false));
HttpContext.Current.Session["UserName"] = "testUserName";
testwebService.testMethod();
I want to get session state generated in NUnit test for session ["UserName"] in ASP.NET Web Service:
[WebMethod(EnableSession=true)]
public int testMethod()
{
string user;
if(Session["UserName"] != null)
{
user = (string)Session["UserName"];
//Do some processing of the user
//user is validated against with value stored in database
return 1;
}
else
return 0;
}
The web.config file has the following configuration for session state configuration and would like to continue to use InProc and not StateServer or SQLServer:
<sessionState mode="InProc" stateConnectionString="tcpip=127.0.0.1:42424" cookieless="false" timeout="20"/>
a source to share
Instead of using the actual HttpContext class, you can use HttpContextBase . And then mock your session, response, request, etc. on.
a source to share