gpt4 book ai didi

asp.net-mvc - 帮助为测试响应 header 创建单元测试,特别是 Cache-Control,以确定缓存是否已禁用

转载 作者:行者123 更新时间:2023-12-02 13:25:12 26 4
gpt4 key购买 nike

场景:

我有一个基本 Controller ,它在 OnActionExecuting 覆盖中禁用缓存。

protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
filterContext.HttpContext.Response.Cache.SetExpires(DateTime.UtcNow.AddDays(-1));
filterContext.HttpContext.Response.Cache.SetValidUntilExpires(false);
filterContext.HttpContext.Response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches);
filterContext.HttpContext.Response.Cache.SetCacheability(HttpCacheability.NoCache); //IE
filterContext.HttpContext.Response.Cache.SetNoStore(); //FireFox
}

如何创建单元测试来测试此行为?

最佳答案

尝试做同样的事情。迄今为止我拥有的最好的就是这个......

    [TestCase]
public void ResponseNotFromCache()
{

System.Net.WebRequest rq = System.Net.WebRequest.Create("testmethod");
System.Net.HttpWebResponse rs = rq.GetResponse() as System.Net.HttpWebResponse;

Assert.IsFalse(rs.IsFromCache);
}

一定有更好的方法!

更新:How can I test an event of a MVC controller

关于asp.net-mvc - 帮助为测试响应 header 创建单元测试,特别是 Cache-Control,以确定缓存是否已禁用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2282337/

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