Esempio n. 1
0
    public void Execute_DoesNotThrowIfDurationIsNotSet_WhenNoStoreIsFalse()
    {
        // Arrange, Act
        var executor = new ResponseCacheFilterExecutor(
            new CacheProfile
        {
            Duration = null
        });

        // Assert
        Assert.NotNull(executor);
    }
Esempio n. 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"]);
    }
Esempio n. 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"]);
    }
Esempio n. 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"]);
    }
Esempio n. 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);
    }
Esempio n. 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);
    }
Esempio n. 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"]);
    }
Esempio n. 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"]);
    }
Esempio n. 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"]);
    }
Esempio n. 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"]);
    }
Esempio n. 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);
    }
Esempio n. 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"]);
    }
Esempio n. 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());
 }
Esempio n. 14
0
 /// <summary>
 /// Creates a new instance of <see cref="ResponseCacheFilter"/>
 /// </summary>
 /// <param name="cacheProfile">The profile which contains the settings for
 /// <see cref="ResponseCacheFilter"/>.</param>
 public ResponseCacheFilter(CacheProfile cacheProfile)
 {
     _executor = new ResponseCacheFilterExecutor(cacheProfile);
 }