mardi 27 janvier 2015

Set fake locale for fake HttpContext

I am unit testing some piece of code which behaves differently on different locales. I have created a fake HttpContext but need to set the locale for it and have not been able to. here is how i am creating fake HttpContext:



public static HttpContext FakeHttpContext(string requestUrl)
{
var httpRequest = new HttpRequest("", requestUrl, "");
var stringWriter = new StringWriter();
var httpResponce = new HttpResponse(stringWriter);
var httpContext = new HttpContext(httpRequest, httpResponce);

var sessionContainer = new HttpSessionStateContainer("id", new SessionStateItemCollection(),
new HttpStaticObjectsCollection(), 10, true,
HttpCookieMode.AutoDetect,
SessionStateMode.InProc, false);

httpContext.Items["AspSession"] = typeof(HttpSessionState).GetConstructor(
BindingFlags.NonPublic | BindingFlags.Instance,
null, CallingConventions.Standard,
new[] { typeof(HttpSessionStateContainer) },
null)
.Invoke(new object[] { sessionContainer });

return httpContext;
}

Aucun commentaire:

Enregistrer un commentaire