public void CreateInstance_ThrowsIfThereAreNoMatchingCacheProfiles()
        {
            // Arrange
            var responseCache = new ResponseCacheAttribute()
            {
                CacheProfileName = "HelloWorld"
            };
            var cacheProfiles = new Dictionary <string, CacheProfile>();

            cacheProfiles.Add("Cache20Sec", new CacheProfile {
                NoStore = true
            });
            cacheProfiles.Add("Test", new CacheProfile {
                Duration = 20
            });

            // Act
            var ex = Assert.Throws <InvalidOperationException>(
                () => responseCache.CreateInstance(GetServiceProvider(cacheProfiles)));

            Assert.Equal("The 'HelloWorld' cache profile is not defined.", ex.Message);
        }
        public void SetsCacheControlPublicByDefault()
        {
            // Arrange
            var responseCacheAttribute = new ResponseCacheAttribute()
            {
                Duration = 40
            };
            var filter = (ResponseCacheFilter)responseCacheAttribute.CreateInstance(
                GetServiceProvider(cacheProfiles: null));
            var context = GetActionExecutingContext(filter);

            // Act
            filter.OnActionExecuting(context);

            // Assert
            StringValues values;

            Assert.True(context.HttpContext.Response.Headers.TryGetValue("Cache-Control", out values));
            var data = Assert.Single(values);

            AssertHeaderEquals("public, max-age=40", data);
        }
Ejemplo n.º 3
0
        public void CreateInstance_SelectsTheAppropriateCacheProfile(string profileName)
        {
            // Arrange
            var responseCache = new ResponseCacheAttribute()
            {
                CacheProfileName = profileName
            };
            var cacheProfiles = new Dictionary <string, CacheProfile>();

            cacheProfiles.Add("Cache20Sec", new CacheProfile {
                NoStore = true
            });
            cacheProfiles.Add("Test", new CacheProfile {
                Duration = 20
            });

            // Act
            var createdFilter = responseCache.CreateInstance(GetServiceProvider(cacheProfiles));

            // Assert
            var responseCacheFilter = Assert.IsType <ResponseCacheFilter>(createdFilter);

            Assert.True(responseCacheFilter.NoStore);
        }
Ejemplo n.º 4
0
        public void CreateInstance_HonorsOverrides(
            ResponseCacheAttribute responseCache,
            Dictionary <string, CacheProfile> cacheProfiles,
            CacheProfile expectedProfile)
        {
            // Arrange & Act
            var createdFilter = responseCache.CreateInstance(GetServiceProvider(cacheProfiles));

            // Assert
            var responseCacheFilter = Assert.IsType <ResponseCacheFilter>(createdFilter);

            Assert.Equal(expectedProfile.Duration, responseCacheFilter.Duration);
            Assert.Equal(expectedProfile.Location, responseCacheFilter.Location);
            Assert.Equal(expectedProfile.NoStore, responseCacheFilter.NoStore);
            Assert.Equal(expectedProfile.VaryByHeader, responseCacheFilter.VaryByHeader);
            if (expectedProfile.VaryByQueryKeys == null)
            {
                Assert.Null(responseCacheFilter.VaryByQueryKeys);
            }
            else
            {
                Assert.Equal(expectedProfile.VaryByQueryKeys, responseCacheFilter.VaryByQueryKeys);
            }
        }
Ejemplo n.º 5
0
        public void ThrowsWhenDurationIsNotSet()
        {
            // Arrange
            var responseCacheAttribute = new ResponseCacheAttribute()
            {
                VaryByHeader = "Accept"
            };
            var filter = (ResponseCacheFilter)responseCacheAttribute.CreateInstance(
                GetServiceProvider(cacheProfiles: null));
            var context = GetActionExecutingContext(filter);

            // Act & Assert
            var exception = Assert.Throws<InvalidOperationException>(() =>
            {
                filter.OnActionExecuting(context);
            });
            Assert.Equal(
                "If the 'NoStore' property is not set to true, 'Duration' property must be specified.",
                exception.Message);
        }
Ejemplo n.º 6
0
        public void ResponseCache_SetsDifferentCacheControlHeaders(
            ResponseCacheAttribute responseCacheAttribute,
            string expected)
        {
            // Arrange
            var filter = (ResponseCacheFilter)responseCacheAttribute.CreateInstance(
                GetServiceProvider(cacheProfiles: null));
            var context = GetActionExecutingContext(filter);

            // Act
            filter.OnActionExecuting(context);

            // Assert
            StringValues values;
            Assert.True(context.HttpContext.Response.Headers.TryGetValue("Cache-Control", out values));
            var data = Assert.Single(values);
            AssertHeaderEquals(expected, data);
        }
Ejemplo n.º 7
0
        public void SetsCacheControlPublicByDefault()
        {
            // Arrange
            var responseCacheAttribute = new ResponseCacheAttribute() { Duration = 40 };
            var filter = (ResponseCacheFilter)responseCacheAttribute.CreateInstance(
                GetServiceProvider(cacheProfiles: null));
            var context = GetActionExecutingContext(filter);

            // Act
            filter.OnActionExecuting(context);

            // Assert
            StringValues values;
            Assert.True(context.HttpContext.Response.Headers.TryGetValue("Cache-Control", out values));
            var data = Assert.Single(values);
            AssertHeaderEquals("public, max-age=40", data);
        }
Ejemplo n.º 8
0
        public void ResponseCache_SetsAllHeaders()
        {
            // Arrange
            var responseCache = new ResponseCacheAttribute()
            {
                Duration = 100,
                Location = ResponseCacheLocation.Any,
                VaryByHeader = "Accept"
            };
            var filter = (ResponseCacheFilter)responseCache.CreateInstance(GetServiceProvider(cacheProfiles: null));
            var context = GetActionExecutingContext(filter);

            // Act
            filter.OnActionExecuting(context);

            // Assert
            var response = context.HttpContext.Response;
            StringValues values;
            Assert.True(response.Headers.TryGetValue("Cache-Control", out values));
            var data = Assert.Single(values);
            AssertHeaderEquals("public, max-age=100", data);
            Assert.True(response.Headers.TryGetValue("Vary", out values));
            data = Assert.Single(values);
            Assert.Equal("Accept", data);
        }
Ejemplo n.º 9
0
        public void CreateInstance_ThrowsIfThereAreNoMatchingCacheProfiles()
        {
            // Arrange
            var responseCache = new ResponseCacheAttribute()
            {
                CacheProfileName = "HelloWorld"
            };
            var cacheProfiles = new Dictionary<string, CacheProfile>();
            cacheProfiles.Add("Cache20Sec", new CacheProfile { NoStore = true });
            cacheProfiles.Add("Test", new CacheProfile { Duration = 20 });

            // Act
            var ex = Assert.Throws<InvalidOperationException>(
                () => responseCache.CreateInstance(GetServiceProvider(cacheProfiles)));
            Assert.Equal("The 'HelloWorld' cache profile is not defined.", ex.Message);
        }
Ejemplo n.º 10
0
        public void CreateInstance_SelectsTheAppropriateCacheProfile(string profileName)
        {
            // Arrange
            var responseCache = new ResponseCacheAttribute()
            {
                CacheProfileName = profileName
            };
            var cacheProfiles = new Dictionary<string, CacheProfile>();
            cacheProfiles.Add("Cache20Sec", new CacheProfile { NoStore = true });
            cacheProfiles.Add("Test", new CacheProfile { Duration = 20 });

            // Act
            var createdFilter = responseCache.CreateInstance(GetServiceProvider(cacheProfiles));

            // Assert
            var responseCacheFilter = Assert.IsType<ResponseCacheFilter>(createdFilter);
            Assert.True(responseCacheFilter.NoStore);
        }
Ejemplo n.º 11
0
        public void CreateInstance_HonorsOverrides(
            ResponseCacheAttribute responseCache,
            Dictionary<string, CacheProfile> cacheProfiles,
            CacheProfile expectedProfile)
        {
            // Arrange & Act
            var createdFilter = responseCache.CreateInstance(GetServiceProvider(cacheProfiles));

            // Assert
            var responseCacheFilter = Assert.IsType<ResponseCacheFilter>(createdFilter);
            Assert.Equal(expectedProfile.Duration, responseCacheFilter.Duration);
            Assert.Equal(expectedProfile.Location, responseCacheFilter.Location);
            Assert.Equal(expectedProfile.NoStore, responseCacheFilter.NoStore);
            Assert.Equal(expectedProfile.VaryByHeader, responseCacheFilter.VaryByHeader);
        }
Ejemplo n.º 12
0
        public void CreateInstance_DoesNotThrowWhenTheDurationIsNotSet_WithNoStoreFalse()
        {
            // Arrange
            var responseCache = new ResponseCacheAttribute()
            {
                CacheProfileName = "Test"
            };
            var cacheProfiles = new Dictionary<string, CacheProfile>();
            cacheProfiles.Add("Test", new CacheProfile { NoStore = false });

            // Act
            var filter = responseCache.CreateInstance(GetServiceProvider(cacheProfiles));

            // Assert
            Assert.NotNull(filter);
        }