gpt4 book ai didi

c# - 当测试 C# 时从响应中删除 Cookie 时,请求中的 cookie 会消失

转载 作者:行者123 更新时间:2023-12-02 06:56:16 29 4
gpt4 key购买 nike

我尝试模拟系统以测试 Controller 。

        [TestMethod]
public static HttpContextBase TestController()
{
Container _container = GetContainer();
// Setup Moq config for controller context
var moqContext = new Mock<HttpContextBase>();
var moqRequest = new Mock<HttpRequestBase>();
var moqResponse = new Mock<HttpResponseBase>();
var moqSession = new Mock<HttpSessionStateBase>();
var moqServer = new Mock<HttpServerUtilityBase>();
var fakeIdentity = new GenericIdentity("User");
var principal = new GenericPrincipal(fakeIdentity, null);

moqRequest.SetupGet(x => x.Url).Returns(new Uri("http://10.00.00:11111/", UriKind.RelativeOrAbsolute));
moqRequest.SetupGet(x => x.ServerVariables).Returns(new NameValueCollection());
//moq Context
moqContext.SetupGet(x => x.Request).Returns(moqRequest.Object);
moqContext.SetupGet(x => x.Request.Cookies).Returns(new HttpCookieCollection());
moqContext.SetupGet(x => x.Response).Returns(moqResponse.Object);
moqContext.SetupGet(x => x.Response.Cookies).Returns(new HttpCookieCollection());
moqContext.SetupGet(x => x.Session).Returns(moqSession.Object);
moqContext.SetupGet(x => x.Server).Returns(moqServer.Object);
moqContext.SetupGet(x => x.User).Returns(principal);
moqContext.SetupGet(x => x.User.Identity.Name).Returns("UserName");
var contextRequest = new HttpRequest("", "http://10.0.0.00:11111/User/Login", "");
var contextResponse = new HttpResponse(new StringWriter());
HttpContext.Current = new HttpContext(contextRequest, contextResponse);
HttpContext.Current.User = principal;
// Authenticate User
IUsuarioAppService _usuarioApp = _container.GetInstance<IUsuarioAppService>();
var usuario = _usuarioApp.AutenticarUsuario(username, password);
var usuarioController = _container.GetInstance<UsuariosController>();
usuarioController.ControllerContext = new ControllerContext(moqContext.Object, new RouteData(), usuarioController);
usuarioController.Login(true, "", usuario);

foreach (var key in moqContext.Object.Response.Cookies.AllKeys)
{
var cookieItem = moqContext.Object.Response.Cookies[key];
var cookie = new HttpCookie(key, cookieItem.Value);
cookie.Expires = DateTime.Now.AddMinutes(30);
HttpContext.Current.Request.Cookies.Add(cookie);
moqContext.Object.Request.Cookies.Add(cookie);
}
var controller = _container.GetInstance<HomeController>();
controller.ControllerContext = new ControllerContext(moqContext.Object, new RouteData(), controller);
}

var scenario1 = controller.New(model1) as ViewResult;

问题是在HomeController中的方法New中我删除了Response的cookie,然后Request的所有cookie都消失了。通常它不会发生,只有当我测试时。

        internal static void SetCookie(string key, object value)
{
var cookie = new HttpCookie(key, value != null ? value.ToString() : null);
cookie.Path = HttpRuntime.AppDomainAppVirtualPath;
cookie.Expires = DateTime.UtcNow.AddDays(3);
cookie.HttpOnly = true;

if (value == null)
cookie.Expires = DateTime.Now.AddDays(-1d);

if (HttpContext.Current.Response.Cookies.AllKeys.Contains(key))
{
HttpContext.Current.Response.Cookies.Remove(key);
HttpContext.Current.Response.AppendCookie(cookie);
}
else
{
HttpContext.Current.Response.AppendCookie(cookie);
}
}

这可能是什么原因?我是否正确地将 cookie 添加到 HttpContext?

最佳答案

是什么让您认为 cookie 出现在请求中?当我读到它时

 moqContext.SetupGet(x => x.Request).Returns(moqRequest.Object);
moqContext.SetupGet(x => x.Request.Cookies).Returns(new HttpCookieCollection());

意味着不会在请求上设置 cookie

关于c# - 当测试 C# 时从响应中删除 Cookie 时,请求中的 cookie 会消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59968308/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com