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);
    }
    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()
        },
Example #3
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);
    }