I need to test an helper class which manage complex querystring.
I use this helper method to mock the HttpContext
:
public static HttpContext FakeHttpContext(string url, string queryString)
{
var httpRequest = new HttpRequest("", url, queryString);
var stringWriter = new StringWriter();
var httpResponse = new HttpResponse(stringWriter);
var httpContext = new HttpContext(httpRequest, httpResponse);
var sessionContainer = new HttpSessionStateContainer("id", new SessionStateItemCollection(),
new HttpStaticObjectsCollection(), 10, true,
HttpCookieMode.AutoDetect,
SessionStateMode.InProc, false);
SessionStateUtility.AddHttpSessionStateToContext(httpContext, sessionContainer);
return httpContext;
}
The problem is that the HttpRequest
loses the querystring:
HttpContext.Current = MockHelpers.FakeHttpContext("http://www.google.com/", "name=gdfgd");
HttpContext.Current.Request.Url
is "http://www.google.com/"
and not "http://www.google.com/?name=gdfgd"
as expected.
If I debug I see that just after the HttpRequest constrctor the querystring is lost.
The workaround I'm using is to pass the url with querystring to the HttpRequest constructor:
HttpContext.Current = MockHelpers.FakeHttpContext("http://www.google.com/?name=gdfgd","");
HttpContext
as theQueryString
property still has it. I don't know what is expected behavior though. – Halvard