嘲笑的Htt prequest失去查询字符串 [英] Mocked HttpRequest loses the QueryString
问题描述
我需要测试其管理复杂的查询字符串的辅助类。
我用这个helper方法嘲笑的HttpContext
:
公共静态的HttpContext FakeHttpContext(URL字符串,字符串的queryString)
{
VAR HTT prequest =新的Htt prequest(,URL的queryString);
VAR的StringWriter =新的StringWriter();
VAR HTT presponse =新的Htt presponse(StringWriter的);
VAR的HttpContext =新的HttpContext(HTT prequest,HTT presponse); VAR sessionContainer =新HttpSessionStateContainer(ID,新SessionStateItemCollection()
新HttpStaticObjectsCollection(),10,真实,
HttpCookieMode.AutoDetect,
SessionStateMode.InProc,FALSE);
SessionStateUtility.AddHttpSessionStateToContext(HttpContext的,sessionContainer); 返回HttpContext的;
}
问题是,的Htt prequest
失去查询字符串:
HttpContext.Current = MockHelpers.FakeHttpContext(http://www.google.com/,NAME = gdfgd);
HttpContext.Current.Request.Url
是http://www.google.com/
和没有http://www.google.com/?name=gdfgd
预期。
如果我调试我看到刚过型Htt prequest constrctor查询字符串将丢失。
我使用的解决方法是通过URL以查询字符串的Htt的prequest构造:
HttpContext.Current = MockHelpers.FakeHttpContext(http://www.google.com/?name=gdfgd,);
感谢<一个href=\"http://stackoverflow.com/questions/19704059/mocked-htt$p$pquest-loses-the-querystring#comment29273240_19704059\">Halvard's评论我有线索找到了答案:
<一个href=\"http://msdn.microsoft.com/en-us/library/system.web.htt$p$pquest.htt$p$pquest%28v=vs.110%29.aspx\"相对=nofollow> 在它们中间断开的Htt prequest构造函数的参数。
该URL参数用于创建的Htt prequest.Url
和查询字符串用于的Htt prequest.QueryString
属性:他们被分离
要具有一致的Htt prequest与查询字符串你有一个网址:
VAR HTT prequest =新的Htt prequest
(,http://www.google.com/?name=gdfgd,名称= gdfgd);
否则,你将有URL或QueryString属性不正确。
还有就是我更新的模拟助手的方法:
公共静态的HttpContext FakeHttpContext(字符串URL)
{
VAR URI =新的URI(URL);
VAR HTT prequest =新的Htt prequest(的String.Empty,uri.ToString(),uri.Query.TrimStart()'?');
VAR的StringWriter =新的StringWriter();
VAR HTT presponse =新的Htt presponse(StringWriter的);
VAR的HttpContext =新的HttpContext(HTT prequest,HTT presponse); VAR sessionContainer =新HttpSessionStateContainer(ID,新SessionStateItemCollection()
新HttpStaticObjectsCollection(),10,真实,
HttpCookieMode.AutoDetect,
SessionStateMode.InProc,FALSE);
SessionStateUtility.AddHttpSessionStateToContext(HttpContext的,sessionContainer); 返回HttpContext的;
}
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","");
Thanks to Halvard's comment I had the clue to find the answer:
HttpRequest constructor parameters are disconnected between them.
The url parameter is used to create the HttpRequest.Url
and the queryString is used for HttpRequest.QueryString
property: they are detached
To have a consistent HttpRequest with an url with querystring you have to:
var httpRequest = new HttpRequest
("", "http://www.google.com/?name=gdfgd", "name=gdfgd");
Otherwise you'll have either the Url or the QueryString property not correctly loaded.
There is my updated Mock Helpers method:
public static HttpContext FakeHttpContext(string url)
{
var uri = new Uri(url);
var httpRequest = new HttpRequest(string.Empty, uri.ToString(), uri.Query.TrimStart('?'));
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;
}
这篇关于嘲笑的Htt prequest失去查询字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!