示例#1
0
        public void Execute_DoesNotThrowIfDurationIsNotSet_WhenNoStoreIsFalse()
        {
            // Arrange, Act
            var executor = new ResponseCacheFilterExecutor(
                new CacheProfile
            {
                Duration = null
            });

            // Assert
            Assert.NotNull(executor);
        }
示例#2
0
        public void Execute_DoesNotSetLocationOrDuration_IfNoStoreIsSet(CacheProfile cacheProfile, string output)
        {
            // Arrange
            var executor = new ResponseCacheFilterExecutor(cacheProfile);
            var context  = GetActionExecutingContext();

            // Act
            executor.Execute(context);

            // Assert
            Assert.Equal(output, context.HttpContext.Response.Headers["Cache-control"]);
        }
示例#3
0
        public void Execute_CanSetCacheControlHeaders(CacheProfile cacheProfile, string output)
        {
            // Arrange
            var executor = new ResponseCacheFilterExecutor(cacheProfile);
            var context  = GetActionExecutingContext();

            // Act
            executor.Execute(context);

            // Assert
            Assert.Equal(output, context.HttpContext.Response.Headers["Cache-control"]);
        }
示例#4
0
        public void ResponseCacheCanSetVaryByHeader(CacheProfile cacheProfile, string varyOutput, string cacheControlOutput)
        {
            // Arrange
            var executor = new ResponseCacheFilterExecutor(cacheProfile);
            var context  = GetActionExecutingContext();

            // Act
            executor.Execute(context);

            // Assert
            Assert.Equal(varyOutput, context.HttpContext.Response.Headers["Vary"]);
            Assert.Equal(cacheControlOutput, context.HttpContext.Response.Headers["Cache-control"]);
        }
示例#5
0
        public void ResponseCacheCanSetVaryByQueryKeys(CacheProfile cacheProfile, string[] varyOutput, string cacheControlOutput)
        {
            // Arrange
            var executor = new ResponseCacheFilterExecutor(cacheProfile);
            var context  = GetActionExecutingContext();

            context.HttpContext.Features.Set <IResponseCachingFeature>(new ResponseCachingFeature());

            // Acts
            executor.Execute(context);

            // Assert
            Assert.Equal(varyOutput, context.HttpContext.Features.Get <IResponseCachingFeature>().VaryByQueryKeys);
            Assert.Equal(cacheControlOutput, context.HttpContext.Response.Headers.CacheControl);
        }
示例#6
0
        public void Execute_ThrowsIfDurationIsNotSet_WhenNoStoreIsFalse()
        {
            // Arrange
            var executor = new ResponseCacheFilterExecutor(
                new CacheProfile()
            {
                Duration = null
            });

            var context = GetActionExecutingContext();

            // Act & Assert
            var ex = Assert.Throws <InvalidOperationException>(() => executor.Execute(context));

            Assert.Equal("If the 'NoStore' property is not set to true, 'Duration' property must be specified.",
                         ex.Message);
        }
示例#7
0
        public void Execute_DoesNotThrow_WhenNoStoreIsTrue()
        {
            // Arrange
            var executor = new ResponseCacheFilterExecutor(
                new CacheProfile
            {
                NoStore  = true,
                Duration = null
            });
            var context = GetActionExecutingContext();

            // Act
            executor.Execute(context);

            // Assert
            Assert.Equal("no-store", context.HttpContext.Response.Headers["Cache-control"]);
        }
示例#8
0
        public void FilterDurationProperty_OverridesCachePolicySetting()
        {
            // Arrange
            var executor = new ResponseCacheFilterExecutor(
                new CacheProfile
            {
                Duration = 10
            });

            executor.Duration = 20;
            var context = GetActionExecutingContext();

            // Act
            executor.Execute(context);

            // Assert
            Assert.Equal("public,max-age=20", context.HttpContext.Response.Headers["Cache-control"]);
        }
示例#9
0
        public void FilterVaryByProperty_OverridesCachePolicySetting()
        {
            // Arrange
            var executor = new ResponseCacheFilterExecutor(
                new CacheProfile
            {
                NoStore      = true,
                VaryByHeader = "Accept"
            });

            executor.VaryByHeader = "Test";
            var context = GetActionExecutingContext();

            // Act
            executor.Execute(context);

            // Assert
            Assert.Equal("Test", context.HttpContext.Response.Headers["Vary"]);
        }
示例#10
0
        public void FilterLocationProperty_OverridesCachePolicySetting()
        {
            // Arrange
            var executor = new ResponseCacheFilterExecutor(
                new CacheProfile
            {
                Duration = 10,
                Location = ResponseCacheLocation.None
            });

            executor.Location = ResponseCacheLocation.Client;
            var context = GetActionExecutingContext();

            // Act
            executor.Execute(context);

            // Assert
            Assert.Equal("private,max-age=10", context.HttpContext.Response.Headers["Cache-control"]);
        }
示例#11
0
        public void NonEmptyVaryByQueryKeys_WithoutConfiguringMiddleware_Throws()
        {
            // Arrange
            var executor = new ResponseCacheFilterExecutor(
                new CacheProfile
            {
                Duration        = 0,
                Location        = ResponseCacheLocation.None,
                NoStore         = true,
                VaryByHeader    = null,
                VaryByQueryKeys = new[] { "Test" }
            });
            var context = GetActionExecutingContext();

            // Act & Assert
            var exception = Assert.Throws <InvalidOperationException>(() => executor.Execute(context));

            Assert.Equal("'VaryByQueryKeys' requires the response cache middleware.", exception.Message);
        }
示例#12
0
        public void SetsPragmaOnNoCache()
        {
            // Arrange
            var executor = new ResponseCacheFilterExecutor(
                new CacheProfile
            {
                Duration     = 0,
                Location     = ResponseCacheLocation.None,
                NoStore      = true,
                VaryByHeader = null
            });
            var context = GetActionExecutingContext();

            // Act
            executor.Execute(context);

            // Assert
            Assert.Equal("no-store,no-cache", context.HttpContext.Response.Headers["Cache-control"]);
            Assert.Equal("no-cache", context.HttpContext.Response.Headers["Pragma"]);
        }
示例#13
0
 /// <summary>
 /// Creates a new instance of <see cref="PageResponseCacheFilter"/>
 /// </summary>
 /// <param name="cacheProfile">The profile which contains the settings for
 /// <see cref="PageResponseCacheFilter"/>.</param>
 /// <param name="loggerFactory">The <see cref="ILoggerFactory"/>.</param>
 public PageResponseCacheFilter(CacheProfile cacheProfile, ILoggerFactory loggerFactory)
 {
     _executor = new ResponseCacheFilterExecutor(cacheProfile);
     _logger   = loggerFactory.CreateLogger(GetType());
 }