chevron-thin-right chevron-thin-left brand cancel-circle search youtube-icon google-plus-icon linkedin-icon facebook-icon twitter-icon toolbox download check linkedin phone twitter-old google-plus facebook profile-male chat calendar profile-male
Welcome to Typemock Community! Here you can ask and receive answers from other community members. If you liked or disliked an answer or thread: react with an up- or downvote.
0 votes
Hi,

I'm having a problem with disappearing session state between requests.

There is an ancient topic regarding the subject (How to simulate session?) and test case presented there works fine.

However, test fails when I issue an extra request. It seems that Session_Start event is triggered one more time and test fails:
[Test, Isolated]
public void KeepsSessionValuesAcrossMultipleRequests2()
{
   TestSession session = new TestSession();
   WebRequest request = new WebRequest("test/Page1.aspx");
   request.EventHandlers.Page_PreInit = delegate(object sender, System.EventArgs e)
   {
      HttpSessionState httpsession = HttpContext.Current.Session;
      httpsession["test"] = "testvalue";
   };
   session.ProcessRequest(request); //1st Session_Start

   request = new WebRequest("test/Page1.aspx");
   request.EventHandlers.Page_PreInit = delegate(object sender, System.EventArgs e)
   {
      HttpSessionState httpsession = HttpContext.Current.Session;
      Assert.AreEqual("testvalue", httpsession["test"], "Invalid session value");
   };
   session.ProcessRequest(request);

   WebRequest thirdRequest = new WebRequest("test/Page1.aspx");
   thirdRequest.EventHandlers.Page_PreInit = delegate(object sender, System.EventArgs e)
   {
      HttpSessionState httpsession = HttpContext.Current.Session;
      Assert.AreEqual("testvalue", httpsession["test"], "Invalid session value on 3rd request");
   };

   session.ProcessRequest(thirdRequest); //2nd Session_Start, session is empty - test fails
}


It seems that cookie containing session state id is not sent to server. Am I doing something wrong here?

I'm running Ivonna 2.1.1.22205 and TypeMock Isolator 6.0.3.0.

Thanks,
asked by pma (2.3k points)

3 Answers

0 votes
Hi,

You have discovered a bug in Ivonna. The problem is that the authentication cookie is not being sent with the third request.

I'll publish the fix today and let you know.

Artem
answered by ulu (1.7k points)
0 votes
You can download the fixed version at http://budurl.com/ivdl
answered by ulu (1.7k points)
0 votes
Hi,

Fix works perfectly, but there seems to be a problem when application adds additional cookies to response.

Please take a look at test:
[Test, Isolated]
public void CookiesWrittenOnFirstRequestShouldBeAvailableOnSecondRequest()
{
   const string url = "/testpage.aspx";         
   const string additionalCookieName = "mc_1";
   var session = new TestSession();
   session.SetUser("foo", new string[0]);

   var sessionKeyFromFirstRequest = "";
   var firstRequest = new WebRequest(url);
   firstRequest.EventHandlers.Page_PreRender += (s, e) =>
   {
      var page = (Page) s;
      sessionKeyFromFirstRequest = page.Session.SessionID;

      var newCookie = new HttpCookie(additionalCookieName);
      newCookie.Values.Add("10394", "1");
      page.Response.Cookies.Add(newCookie);
   };

   session.ProcessRequest(firstRequest);

   var secondRequest = new WebRequest(url);
   secondRequest.EventHandlers.Page_Init += (s, e) =>
   {
      var page = (Page)s;
      var cookieHeaders = "Cookie header: " + page.Request.Headers["Cookie"].ToString();
      Assert.AreEqual(sessionKeyFromFirstRequest, page.Session.SessionID, cookieHeaders);
      Assert.NotNull(page.Request.Cookies[additionalCookieName], cookieHeaders);            
   };
   session.ProcessRequest(secondRequest);
}



It seems like there is a problem when cookies are sent back to ASP .NET.

E.g. cookie header instead of:
Cookie: ASP.NET_SessionId=51pncynqyhc3iy452ny1cnmm; mc_1=10394

looks like:
Cookie: ASP.NET_SessionId=51pncynqyhc3iy452ny1cnmm:mc_1=10394

Note ':' instead of '; '
This totally confuses ASP .NET, session starts again and 'mc_1' cookie gets overwritten.
answered by pma (2.3k points)
...