Ejemplo n.º 1
0
        public void ContentIsNotModified_IfNoneMatch_Overrides_IfModifiedSince_ToFalse()
        {
            var utcNow  = DateTimeOffset.UtcNow;
            var sink    = new TestSink();
            var context = TestUtils.CreateTestContext(sink);

            context.CachedResponseHeaders = new HeaderDictionary();

            // This would pass the IfModifiedSince checks
            context.HttpContext.Request.Headers.IfModifiedSince     = HeaderUtilities.FormatDate(utcNow);
            context.CachedResponseHeaders[HeaderNames.LastModified] = HeaderUtilities.FormatDate(utcNow - TimeSpan.FromSeconds(10));

            context.HttpContext.Request.Headers.IfNoneMatch = "\"E1\"";
            Assert.False(ResponseCachingMiddleware.ContentIsNotModified(context));
            Assert.Empty(sink.Writes);
        }
Ejemplo n.º 2
0
        public static void ClearResponseCache()
        {
            if (Instance != null)
            {
                ResponseCachingOptions options = (ResponseCachingOptions)(typeof(ResponseCachingMiddleware).GetField("_options", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(Instance));

                long sizeLimit = options.SizeLimit;

                var newResponseCachingMiddleware = new ResponseCachingMiddleware((context) => Task.CompletedTask, Options, LoggerFactory, PoolProvider);

                var fieldInfo = typeof(ResponseCachingMiddleware).GetField("_cache", BindingFlags.Instance | BindingFlags.NonPublic);

                var newCache = fieldInfo.GetValue(newResponseCachingMiddleware);

                fieldInfo.SetValue(Instance, newCache);
            }
        }
        public void ContentIsNotModified_IfNoneMatch_Overrides_IfModifiedSince_ToTrue()
        {
            var utcNow  = DateTimeOffset.UtcNow;
            var sink    = new TestSink();
            var context = TestUtils.CreateTestContext(sink);

            context.CachedResponseHeaders = new HeaderDictionary();

            // This would fail the IfModifiedSince checks
            context.HttpContext.Request.Headers[HeaderNames.IfModifiedSince] = HeaderUtilities.FormatDate(utcNow);
            context.CachedResponseHeaders[HeaderNames.LastModified]          = HeaderUtilities.FormatDate(utcNow + TimeSpan.FromSeconds(10));

            context.HttpContext.Request.Headers[HeaderNames.IfNoneMatch] = EntityTagHeaderValue.Any.ToString();
            Assert.True(ResponseCachingMiddleware.ContentIsNotModified(context));
            TestUtils.AssertLoggedMessages(
                sink.Writes,
                LoggedMessage.NotModifiedIfNoneMatchStar);
        }
Ejemplo n.º 4
0
        public void Setup()
        {
            _middleware = new ResponseCachingMiddleware(
                async context => {
                context.Response.Headers.CacheControl = _cacheControl;
                await context.Response.BodyWriter.WriteAsync(new ReadOnlyMemory <byte>(_data, 0, Size));
            },
                Options.Create(new ResponseCachingOptions
            {
                SizeLimit       = int.MaxValue,   // ~2GB
                MaximumBodySize = 1 * 1024 * 1024,
            }),
                NullLoggerFactory.Instance,
                new DefaultObjectPoolProvider()
                );

            // no need to actually cache as there is a warm-up fase
        }