public async Task FinalizeCacheHeadersAsync_UpdateCachedVaryByRules_IfEquivalentToPrevious() { var cache = new TestResponseCache(); var sink = new TestSink(); var middleware = TestUtils.CreateTestMiddleware(testSink: sink, cache: cache); var context = TestUtils.CreateTestContext(); context.HttpContext.Response.Headers[HeaderNames.Vary] = new StringValues(new[] { "headerA", "HEADERB" }); context.HttpContext.Features.Set <IResponseCachingFeature>(new ResponseCachingFeature() { VaryByQueryKeys = new StringValues(new[] { "queryB", "QUERYA" }) }); var cachedVaryByRules = new CachedVaryByRules() { VaryByKeyPrefix = FastGuid.NewGuid().IdString, Headers = new StringValues(new[] { "HEADERA", "HEADERB" }), QueryKeys = new StringValues(new[] { "QUERYA", "QUERYB" }) }; context.CachedVaryByRules = cachedVaryByRules; await middleware.FinalizeCacheHeadersAsync(context); // An update to the cache is always made but the entry should be the same Assert.Equal(1, cache.SetCount); Assert.Same(cachedVaryByRules, context.CachedVaryByRules); TestUtils.AssertLoggedMessages( sink.Writes, LoggedMessage.VaryByRulesUpdated); }
public async Task FinalizeCacheHeadersAsync_UpdateCachedVaryByRules_IfNotEquivalentToPrevious() { var cache = new TestResponseCache(); var sink = new TestSink(); var middleware = TestUtils.CreateTestMiddleware(testSink: sink, cache: cache); var context = TestUtils.CreateTestContext(); context.HttpContext.Response.Headers[HeaderNames.Vary] = new StringValues(new[] { "headerA", "HEADERB", "HEADERc" }); context.HttpContext.Features.Set <IResponseCachingFeature>(new ResponseCachingFeature() { VaryByQueryKeys = new StringValues(new[] { "queryB", "QUERYA" }) }); var cachedVaryByRules = new CachedVaryByRules() { Headers = new StringValues(new[] { "HeaderA", "HeaderB" }), QueryKeys = new StringValues(new[] { "QueryA", "QueryB" }) }; context.CachedVaryByRules = cachedVaryByRules; await middleware.FinalizeCacheHeadersAsync(context); Assert.Equal(1, cache.SetCount); Assert.NotSame(cachedVaryByRules, context.CachedVaryByRules); TestUtils.AssertLoggedMessages( sink.Writes, LoggedMessage.VaryByRulesUpdated); }
public static SerializableCacheVaryByRules From(CachedVaryByRules varyByRules) { if (varyByRules == null) { return(null); } return(new SerializableCacheVaryByRules { VaryByKeyPrefix = varyByRules.VaryByKeyPrefix, Headers = varyByRules.Headers.ToList(), QueryKeys = varyByRules.QueryKeys.ToList() }); }
private CacheEntry ConvertToCacheEntry(IResponseCacheEntry entry, TimeSpan validFor) { if (entry is CachedResponse) { CachedResponse cachedResponse = (CachedResponse)entry; int responseLengthInBytes = (int)cachedResponse.Body.Length; if (responseLengthInBytes > _maxResponseBodyBytes) { return(null); } long oldPosition = cachedResponse.Body.Position; cachedResponse.Body.Position = 0; byte[] body = new byte[responseLengthInBytes]; cachedResponse.Body.Read(body, 0, responseLengthInBytes); cachedResponse.Body.Position = oldPosition; return(new ResponseCacheEntry() { ServerVersion = _versionIdentifier, Body = body, HeaderDictionary = cachedResponse.Headers.ToDictionary(x => x.Key, x => x.Value.ToArray()), StatusCode = cachedResponse.StatusCode, Created = cachedResponse.Created, ValidFor = validFor }); } if (entry is CachedVaryByRules) { CachedVaryByRules cachedResponse = (CachedVaryByRules)entry; return(new VaryByKeysCacheEntry() { ServerVersion = _versionIdentifier, Headers = cachedResponse.Headers, QueryKeys = cachedResponse.QueryKeys, VaryByKeyPrefix = cachedResponse.VaryByKeyPrefix, Created = DateTime.UtcNow, ValidFor = validFor }); } return(null); }