示例#1
0
        public async Task TryServeFromCacheAsync_VaryByRuleFound_CachedResponseFound_Succeeds()
        {
            var cache      = new TestResponseCache();
            var sink       = new TestSink();
            var middleware = TestUtils.CreateTestMiddleware(testSink: sink, cache: cache, keyProvider: new TestResponseCachingKeyProvider("BaseKey", new[] { "VaryKey", "VaryKey2" }));
            var context    = TestUtils.CreateTestContext();

            cache.Set(
                "BaseKey",
                new CachedVaryByRules(),
                TimeSpan.Zero);
            cache.Set(
                "BaseKeyVaryKey2",
                new CachedResponse()
            {
                Headers = new HeaderDictionary(),
                Body    = new CachedResponseBody(new List <byte[]>(0), 0)
            },
                TimeSpan.Zero);

            Assert.True(await middleware.TryServeFromCacheAsync(context));
            Assert.Equal(3, cache.GetCount);
            TestUtils.AssertLoggedMessages(
                sink.Writes,
                LoggedMessage.CachedResponseServed);
        }
示例#2
0
        public async Task TryServeFromCacheAsync_CachedResponseFound_OverwritesExistingHeaders()
        {
            var cache      = new TestResponseCache();
            var sink       = new TestSink();
            var middleware = TestUtils.CreateTestMiddleware(testSink: sink, cache: cache, keyProvider: new TestResponseCachingKeyProvider("BaseKey"));
            var context    = TestUtils.CreateTestContext();

            context.HttpContext.Response.Headers["MyHeader"] = "OldValue";
            cache.Set(
                "BaseKey",
                new CachedResponse()
            {
                Headers = new HeaderDictionary()
                {
                    { "MyHeader", "NewValue" }
                },
                Body = new CachedResponseBody(new List <byte[]>(0), 0)
            },
                TimeSpan.Zero);

            Assert.True(await middleware.TryServeFromCacheAsync(context));
            Assert.Equal("NewValue", context.HttpContext.Response.Headers["MyHeader"]);
            Assert.Equal(1, cache.GetCount);
            TestUtils.AssertLoggedMessages(
                sink.Writes,
                LoggedMessage.CachedResponseServed);
        }
示例#3
0
        public async Task TryServeFromCacheAsync_VaryByRuleFound_CachedResponseNotFound_Fails()
        {
            var cache      = new TestResponseCache();
            var sink       = new TestSink();
            var middleware = TestUtils.CreateTestMiddleware(testSink: sink, cache: cache, keyProvider: new TestResponseCachingKeyProvider("BaseKey", "VaryKey"));
            var context    = TestUtils.CreateTestContext();

            cache.Set(
                "BaseKey",
                new CachedVaryByRules(),
                TimeSpan.Zero);

            Assert.False(await middleware.TryServeFromCacheAsync(context));
            Assert.Equal(2, cache.GetCount);
            TestUtils.AssertLoggedMessages(
                sink.Writes,
                LoggedMessage.NoResponseServed);
        }
示例#4
0
        public async Task TryServeFromCacheAsync_CachedResponseFound_Serves304IfPossible()
        {
            var cache      = new TestResponseCache();
            var sink       = new TestSink();
            var middleware = TestUtils.CreateTestMiddleware(testSink: sink, cache: cache, keyProvider: new TestResponseCachingKeyProvider("BaseKey"));
            var context    = TestUtils.CreateTestContext();

            context.HttpContext.Request.Headers.IfNoneMatch = "*";

            cache.Set(
                "BaseKey",
                new CachedResponse()
            {
                Body = new CachedResponseBody(new List <byte[]>(0), 0)
            },
                TimeSpan.Zero);

            Assert.True(await middleware.TryServeFromCacheAsync(context));
            Assert.Equal(1, cache.GetCount);
            TestUtils.AssertLoggedMessages(
                sink.Writes,
                LoggedMessage.NotModifiedServed);
        }