コード例 #1
0
        public void TestControllerLevel()
        {
            var configuration = new HttpConfiguration(new HttpRouteCollection("/"));
            configuration.Routes.MapHttpRoute("main", "api/{controller}/{id}");
            var request = new HttpRequestMessage(HttpMethod.Get, new Uri("http://aliostad/api/CachePolicy/1"));
            var routeData = configuration.Routes.GetRouteData(request);
            request.Properties.Add(HttpPropertyKeys.HttpRouteDataKey, (object)routeData);

            var attributeBasedCachePolicy = new AttributeBasedCacheControlPolicy(new CacheControlHeaderValue());
            var cchv = attributeBasedCachePolicy.GetCacheControl(request, configuration);

            Assert.AreEqual(TimeSpan.FromSeconds(110), cchv.MaxAge);
            Assert.AreEqual(false, cchv.Private, "Private");
        }
コード例 #2
0
        public void TestRefreshPolicyFor404()
        {
            var configuration = new HttpConfiguration(new HttpRouteCollection("/"));
            configuration.Services.Replace(typeof (IHttpControllerSelector), new NotFoundControllerSelector());
            configuration.Routes.MapHttpRoute("main", "api/{controller}/{id}");
            var request = new HttpRequestMessage(HttpMethod.Get, new Uri("http://aliostad/api/CachePolicyAction/1"));
            var routeData = configuration.Routes.GetRouteData(request);
            request.Properties.Add(HttpPropertyKeys.HttpRouteDataKey, (object)routeData);
            var headerValue = new CacheControlHeaderValue();
            var attributeBasedCachePolicy = new AttributeBasedCacheControlPolicy(headerValue);

            // act
            var cchv = attributeBasedCachePolicy.GetCacheControl(request, configuration);

            // assert
            Assert.AreEqual(headerValue, cchv);

        }
コード例 #3
0
        public void TestDefaultControllerOrActionLevelPolicy()
        {
            // arrange
            var configuration = new HttpConfiguration(new HttpRouteCollection("/"));
            configuration.Routes.MapHttpRoute("main", "api/{controller}/{id}");
            var request = new HttpRequestMessage(HttpMethod.Get, new Uri("http://aliostad/api/NoCachePolicy/1"));
            var routeData = configuration.Routes.GetRouteData(request);
            request.Properties.Add(HttpPropertyKeys.HttpRouteDataKey, (object)routeData);
            var attributeBasedCachePolicy = new AttributeBasedCacheControlPolicy(new CacheControlHeaderValue()
                {NoStore = true, NoCache = true});

            // action
            var cchv = attributeBasedCachePolicy.GetCacheControl(request, configuration);

            // assert
            Assert.AreEqual(true, cchv.NoCache, "NoCache");
            Assert.AreEqual(true, cchv.NoStore, "NoStore");
        }