Help create a unit test header for a test answer, in particular Cache-Control, in determining if caching is disabled or not

Scenario:

I have a basic controller that disables caching inside the OnActionExecuting override.

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 
}

How can I create Unit Test to test this behavior?

+3
source share
1 answer

Trying to do the same. The best I have so far is ...

    [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);
    }

There must be a better way!

Update: How to check an MVC controller event

0
source

Source: https://habr.com/ru/post/1733227/


All Articles