public async Task TryServeFromCacheAsync_CachedResponseFound_OverwritesExistingHeaders()
    {
        var cache      = new TestOutputCache();
        var sink       = new TestSink();
        var middleware = TestUtils.CreateTestMiddleware(testSink: sink, cache: cache, keyProvider: new TestResponseCachingKeyProvider("BaseKey"));
        var context    = TestUtils.CreateTestContext(cache: cache);

        middleware.TryGetRequestPolicies(context.HttpContext, out var policies);
        context.CacheKey = "BaseKey";

        context.HttpContext.Response.Headers["MyHeader"] = "OldValue";
        await OutputCacheEntryFormatter.StoreAsync(context.CacheKey,
                                                   new OutputCacheEntry()
        {
            Headers = new HeaderDictionary()
            {
                { "MyHeader", "NewValue" }
            },
            Body = new CachedResponseBody(new List <byte[]>(0), 0)
        },
                                                   TimeSpan.Zero,
                                                   cache,
                                                   default);

        Assert.True(await middleware.TryServeFromCacheAsync(context, policies));
        Assert.Equal("NewValue", context.HttpContext.Response.Headers["MyHeader"]);
        Assert.Equal(1, cache.GetCount);
        TestUtils.AssertLoggedMessages(
            sink.Writes,
            LoggedMessage.CachedResponseServed);
    }
Exemple #2
0
    internal static OutputCacheMiddleware CreateTestMiddleware(
        RequestDelegate?next                = null,
        IOutputCacheStore?cache             = null,
        OutputCacheOptions?options          = null,
        TestSink?testSink                   = null,
        IOutputCacheKeyProvider?keyProvider = null
        )
    {
        if (next == null)
        {
            next = httpContext => Task.CompletedTask;
        }
        if (cache == null)
        {
            cache = new TestOutputCache();
        }
        if (options == null)
        {
            options = new OutputCacheOptions();
        }
        if (keyProvider == null)
        {
            keyProvider = new OutputCacheKeyProvider(new DefaultObjectPoolProvider(), Options.Create(options));
        }

        return(new OutputCacheMiddleware(
                   next,
                   Options.Create(options),
                   testSink == null ? NullLoggerFactory.Instance : new TestLoggerFactory(testSink, true),
                   cache,
                   keyProvider));
    }
Exemple #3
0
 public async Task StoreAndGet_StoresAllValues()
 {
     var store = new TestOutputCache();
     var key   = "abc";
     var entry = new OutputCacheEntry()
     {
         Body = new CachedResponseBody(new List <byte[]>()
         {
             "lorem" u8.ToArray(), "ipsum" u8.ToArray()
         }, 10),
    public async Task TryServeFromCacheAsync_CachedResponseNotFound_Fails()
    {
        var cache      = new TestOutputCache();
        var sink       = new TestSink();
        var middleware = TestUtils.CreateTestMiddleware(testSink: sink, cache: cache, keyProvider: new TestResponseCachingKeyProvider("BaseKey"));
        var context    = TestUtils.CreateTestContext(cache: cache);

        middleware.TryGetRequestPolicies(context.HttpContext, out var policies);

        Assert.False(await middleware.TryServeFromCacheAsync(context, policies));
        Assert.Equal(1, cache.GetCount);
        TestUtils.AssertLoggedMessages(
            sink.Writes,
            LoggedMessage.NoResponseServed);
    }
    public async Task TryServeFromCacheAsync_CachedResponseFound_Serves304IfPossible()
    {
        var cache      = new TestOutputCache();
        var sink       = new TestSink();
        var middleware = TestUtils.CreateTestMiddleware(testSink: sink, cache: cache, keyProvider: new TestResponseCachingKeyProvider("BaseKey"));
        var context    = TestUtils.CreateTestContext(cache: cache);

        context.HttpContext.Request.Headers.IfNoneMatch = "*";
        middleware.TryGetRequestPolicies(context.HttpContext, out var policies);

        await OutputCacheEntryFormatter.StoreAsync("BaseKey",
                                                   new OutputCacheEntry()
        {
            Body    = new CachedResponseBody(new List <byte[]>(0), 0),
            Headers = new()
        },
Exemple #6
0
    public async Task StoreAndGet_StoresEmptyValues()
    {
        var store = new TestOutputCache();
        var key   = "abc";
        var entry = new OutputCacheEntry()
        {
            Body    = new CachedResponseBody(new List <byte[]>(), 0),
            Headers = new HeaderDictionary(),
            Tags    = Array.Empty <string>()
        };

        await OutputCacheEntryFormatter.StoreAsync(key, entry, TimeSpan.Zero, store, default);

        var result = await OutputCacheEntryFormatter.GetAsync(key, store, default);

        AssertEntriesAreSame(entry, result);
    }
    public async Task TryServeFromCacheAsync_OnlyIfCached_Serves504()
    {
        var cache      = new TestOutputCache();
        var sink       = new TestSink();
        var middleware = TestUtils.CreateTestMiddleware(testSink: sink, cache: cache, keyProvider: new TestResponseCachingKeyProvider("BaseKey"));
        var context    = TestUtils.CreateTestContext(cache: cache);

        context.HttpContext.Request.Headers.CacheControl = new CacheControlHeaderValue()
        {
            OnlyIfCached = true
        }.ToString();
        middleware.TryGetRequestPolicies(context.HttpContext, out var policies);

        Assert.True(await middleware.TryServeFromCacheAsync(context, policies));
        Assert.Equal(StatusCodes.Status504GatewayTimeout, context.HttpContext.Response.StatusCode);
        TestUtils.AssertLoggedMessages(
            sink.Writes,
            LoggedMessage.GatewayTimeoutServed);
    }