Drop IResponseCache async methods (#15393)

This commit is contained in:
Kahbazi 2019-11-04 22:36:19 +03:30 committed by John Luo
parent 774f8dbdbd
commit 93432bd417
6 changed files with 56 additions and 116 deletions

View File

@ -1,4 +1,4 @@
// Copyright (c) .NET Foundation. All rights reserved. // Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System; using System;
@ -16,14 +16,6 @@ namespace Microsoft.AspNetCore.ResponseCaching
/// <returns>The response cache entry if it exists; otherwise <c>null</c>.</returns> /// <returns>The response cache entry if it exists; otherwise <c>null</c>.</returns>
IResponseCacheEntry Get(string key); IResponseCacheEntry Get(string key);
/// <summary>
/// Gets the cached response for the given key, if it exists.
/// If no cached response exists for the given key, <c>null</c> is returned.
/// </summary>
/// <param name="key">The cache key to look up.</param>
/// <returns>The response cache entry if it exists; otherwise <c>null</c>.</returns>
Task<IResponseCacheEntry> GetAsync(string key);
/// <summary> /// <summary>
/// Stores the given response in the response cache. /// Stores the given response in the response cache.
/// </summary> /// </summary>
@ -31,14 +23,5 @@ namespace Microsoft.AspNetCore.ResponseCaching
/// <param name="entry">The response cache entry to store.</param> /// <param name="entry">The response cache entry to store.</param>
/// <param name="validFor">The amount of time the entry will be kept in the cache before expiring, relative to now.</param> /// <param name="validFor">The amount of time the entry will be kept in the cache before expiring, relative to now.</param>
void Set(string key, IResponseCacheEntry entry, TimeSpan validFor); void Set(string key, IResponseCacheEntry entry, TimeSpan validFor);
/// <summary>
/// Stores the given response in the response cache.
/// </summary>
/// <param name="key">The cache key to store the response under.</param>
/// <param name="entry">The response cache entry to store.</param>
/// <param name="validFor">The amount of time the entry will be kept in the cache before expiring, relative to now.</param>
/// <returns>No result is returned.</returns>
Task SetAsync(string key, IResponseCacheEntry entry, TimeSpan validFor);
} }
} }

View File

@ -36,11 +36,6 @@ namespace Microsoft.AspNetCore.ResponseCaching
} }
} }
public Task<IResponseCacheEntry> GetAsync(string key)
{
return Task.FromResult(Get(key));
}
public void Set(string key, IResponseCacheEntry entry, TimeSpan validFor) public void Set(string key, IResponseCacheEntry entry, TimeSpan validFor)
{ {
if (entry is CachedResponse cachedResponse) if (entry is CachedResponse cachedResponse)
@ -76,11 +71,5 @@ namespace Microsoft.AspNetCore.ResponseCaching
}); });
} }
} }
public Task SetAsync(string key, IResponseCacheEntry entry, TimeSpan validFor)
{
Set(key, entry, validFor);
return Task.CompletedTask;
}
} }
} }

View File

@ -113,10 +113,10 @@ namespace Microsoft.AspNetCore.ResponseCaching
await _next(httpContext); await _next(httpContext);
// If there was no response body, check the response headers now. We can cache things like redirects. // If there was no response body, check the response headers now. We can cache things like redirects.
await StartResponseAsync(context); StartResponse(context);
// Finalize the cache entry // Finalize the cache entry
await FinalizeCacheBodyAsync(context); FinalizeCacheBody(context);
} }
finally finally
{ {
@ -211,7 +211,7 @@ namespace Microsoft.AspNetCore.ResponseCaching
internal async Task<bool> TryServeFromCacheAsync(ResponseCachingContext context) internal async Task<bool> TryServeFromCacheAsync(ResponseCachingContext context)
{ {
context.BaseKey = _keyProvider.CreateBaseKey(context); context.BaseKey = _keyProvider.CreateBaseKey(context);
var cacheEntry = await _cache.GetAsync(context.BaseKey); var cacheEntry = _cache.Get(context.BaseKey);
if (cacheEntry is CachedVaryByRules cachedVaryByRules) if (cacheEntry is CachedVaryByRules cachedVaryByRules)
{ {
@ -220,7 +220,7 @@ namespace Microsoft.AspNetCore.ResponseCaching
foreach (var varyKey in _keyProvider.CreateLookupVaryByKeys(context)) foreach (var varyKey in _keyProvider.CreateLookupVaryByKeys(context))
{ {
if (await TryServeCachedResponseAsync(context, await _cache.GetAsync(varyKey))) if (await TryServeCachedResponseAsync(context, _cache.Get(varyKey)))
{ {
return true; return true;
} }
@ -339,16 +339,7 @@ namespace Microsoft.AspNetCore.ResponseCaching
} }
} }
internal Task FinalizeCacheHeadersAsync(ResponseCachingContext context) internal void FinalizeCacheBody(ResponseCachingContext context)
{
if (OnFinalizeCacheHeaders(context))
{
return _cache.SetAsync(context.BaseKey, context.CachedVaryByRules, context.CachedResponseValidFor);
}
return Task.CompletedTask;
}
internal async Task FinalizeCacheBodyAsync(ResponseCachingContext context)
{ {
if (context.ShouldCacheResponse && context.ResponseCachingStream.BufferingEnabled) if (context.ShouldCacheResponse && context.ResponseCachingStream.BufferingEnabled)
{ {
@ -365,7 +356,7 @@ namespace Microsoft.AspNetCore.ResponseCaching
context.CachedResponse.Body = bufferStream; context.CachedResponse.Body = bufferStream;
_logger.ResponseCached(); _logger.ResponseCached();
await _cache.SetAsync(context.StorageVaryKey ?? context.BaseKey, context.CachedResponse, context.CachedResponseValidFor); _cache.Set(context.StorageVaryKey ?? context.BaseKey, context.CachedResponse, context.CachedResponseValidFor);
} }
else else
{ {
@ -403,15 +394,6 @@ namespace Microsoft.AspNetCore.ResponseCaching
} }
} }
internal Task StartResponseAsync(ResponseCachingContext context)
{
if (OnStartResponse(context))
{
return FinalizeCacheHeadersAsync(context);
}
return Task.CompletedTask;
}
internal static void AddResponseCachingFeature(HttpContext context) internal static void AddResponseCachingFeature(HttpContext context)
{ {
if (context.Features.Get<IResponseCachingFeature>() != null) if (context.Features.Get<IResponseCachingFeature>() != null)
@ -429,8 +411,7 @@ namespace Microsoft.AspNetCore.ResponseCaching
context.OriginalResponseStream, context.OriginalResponseStream,
_options.MaximumBodySize, _options.MaximumBodySize,
StreamUtilities.BodySegmentSize, StreamUtilities.BodySegmentSize,
() => StartResponse(context), () => StartResponse(context));
() => StartResponseAsync(context));
context.HttpContext.Response.Body = context.ResponseCachingStream; context.HttpContext.Response.Body = context.ResponseCachingStream;
// Add IResponseCachingFeature // Add IResponseCachingFeature

View File

@ -1,4 +1,4 @@
// Copyright (c) .NET Foundation. All rights reserved. // Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System; using System;
@ -15,15 +15,13 @@ namespace Microsoft.AspNetCore.ResponseCaching
private readonly int _segmentSize; private readonly int _segmentSize;
private readonly SegmentWriteStream _segmentWriteStream; private readonly SegmentWriteStream _segmentWriteStream;
private readonly Action _startResponseCallback; private readonly Action _startResponseCallback;
private readonly Func<Task> _startResponseCallbackAsync;
internal ResponseCachingStream(Stream innerStream, long maxBufferSize, int segmentSize, Action startResponseCallback, Func<Task> startResponseCallbackAsync) internal ResponseCachingStream(Stream innerStream, long maxBufferSize, int segmentSize, Action startResponseCallback)
{ {
_innerStream = innerStream; _innerStream = innerStream;
_maxBufferSize = maxBufferSize; _maxBufferSize = maxBufferSize;
_segmentSize = segmentSize; _segmentSize = segmentSize;
_startResponseCallback = startResponseCallback; _startResponseCallback = startResponseCallback;
_startResponseCallbackAsync = startResponseCallbackAsync;
_segmentWriteStream = new SegmentWriteStream(_segmentSize); _segmentWriteStream = new SegmentWriteStream(_segmentSize);
} }
@ -92,7 +90,7 @@ namespace Microsoft.AspNetCore.ResponseCaching
{ {
try try
{ {
await _startResponseCallbackAsync(); _startResponseCallback();
await _innerStream.FlushAsync(); await _innerStream.FlushAsync();
} }
catch catch
@ -136,7 +134,7 @@ namespace Microsoft.AspNetCore.ResponseCaching
{ {
try try
{ {
await _startResponseCallbackAsync(); _startResponseCallback();
await _innerStream.WriteAsync(buffer, offset, count, cancellationToken); await _innerStream.WriteAsync(buffer, offset, count, cancellationToken);
} }
catch catch

View File

@ -1,4 +1,4 @@
// Copyright (c) .NET Foundation. All rights reserved. // Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System; using System;
@ -58,7 +58,7 @@ namespace Microsoft.AspNetCore.ResponseCaching.Tests
var middleware = TestUtils.CreateTestMiddleware(testSink: sink, cache: cache, keyProvider: new TestResponseCachingKeyProvider("BaseKey")); var middleware = TestUtils.CreateTestMiddleware(testSink: sink, cache: cache, keyProvider: new TestResponseCachingKeyProvider("BaseKey"));
var context = TestUtils.CreateTestContext(); var context = TestUtils.CreateTestContext();
await cache.SetAsync( cache.Set(
"BaseKey", "BaseKey",
new CachedResponse() new CachedResponse()
{ {
@ -83,7 +83,7 @@ namespace Microsoft.AspNetCore.ResponseCaching.Tests
var context = TestUtils.CreateTestContext(); var context = TestUtils.CreateTestContext();
context.HttpContext.Response.Headers["MyHeader"] = "OldValue"; context.HttpContext.Response.Headers["MyHeader"] = "OldValue";
await cache.SetAsync( cache.Set(
"BaseKey", "BaseKey",
new CachedResponse() new CachedResponse()
{ {
@ -111,7 +111,7 @@ namespace Microsoft.AspNetCore.ResponseCaching.Tests
var middleware = TestUtils.CreateTestMiddleware(testSink: sink, cache: cache, keyProvider: new TestResponseCachingKeyProvider("BaseKey", "VaryKey")); var middleware = TestUtils.CreateTestMiddleware(testSink: sink, cache: cache, keyProvider: new TestResponseCachingKeyProvider("BaseKey", "VaryKey"));
var context = TestUtils.CreateTestContext(); var context = TestUtils.CreateTestContext();
await cache.SetAsync( cache.Set(
"BaseKey", "BaseKey",
new CachedVaryByRules(), new CachedVaryByRules(),
TimeSpan.Zero); TimeSpan.Zero);
@ -131,11 +131,11 @@ namespace Microsoft.AspNetCore.ResponseCaching.Tests
var middleware = TestUtils.CreateTestMiddleware(testSink: sink, cache: cache, keyProvider: new TestResponseCachingKeyProvider("BaseKey", new[] { "VaryKey", "VaryKey2" })); var middleware = TestUtils.CreateTestMiddleware(testSink: sink, cache: cache, keyProvider: new TestResponseCachingKeyProvider("BaseKey", new[] { "VaryKey", "VaryKey2" }));
var context = TestUtils.CreateTestContext(); var context = TestUtils.CreateTestContext();
await cache.SetAsync( cache.Set(
"BaseKey", "BaseKey",
new CachedVaryByRules(), new CachedVaryByRules(),
TimeSpan.Zero); TimeSpan.Zero);
await cache.SetAsync( cache.Set(
"BaseKeyVaryKey2", "BaseKeyVaryKey2",
new CachedResponse() new CachedResponse()
{ {
@ -160,7 +160,7 @@ namespace Microsoft.AspNetCore.ResponseCaching.Tests
var context = TestUtils.CreateTestContext(); var context = TestUtils.CreateTestContext();
context.HttpContext.Request.Headers[HeaderNames.IfNoneMatch] = "*"; context.HttpContext.Request.Headers[HeaderNames.IfNoneMatch] = "*";
await cache.SetAsync( cache.Set(
"BaseKey", "BaseKey",
new CachedResponse() new CachedResponse()
{ {
@ -358,7 +358,7 @@ namespace Microsoft.AspNetCore.ResponseCaching.Tests
} }
[Fact] [Fact]
public async Task StartResponsegAsync_IfAllowResponseCaptureIsTrue_SetsResponseTime() public void StartResponsegAsync_IfAllowResponseCaptureIsTrue_SetsResponseTime()
{ {
var clock = new TestClock var clock = new TestClock
{ {
@ -371,13 +371,13 @@ namespace Microsoft.AspNetCore.ResponseCaching.Tests
var context = TestUtils.CreateTestContext(); var context = TestUtils.CreateTestContext();
context.ResponseTime = null; context.ResponseTime = null;
await middleware.StartResponseAsync(context); middleware.StartResponse(context);
Assert.Equal(clock.UtcNow, context.ResponseTime); Assert.Equal(clock.UtcNow, context.ResponseTime);
} }
[Fact] [Fact]
public async Task StartResponseAsync_IfAllowResponseCaptureIsTrue_SetsResponseTimeOnlyOnce() public void StartResponseAsync_IfAllowResponseCaptureIsTrue_SetsResponseTimeOnlyOnce()
{ {
var clock = new TestClock var clock = new TestClock
{ {
@ -391,18 +391,18 @@ namespace Microsoft.AspNetCore.ResponseCaching.Tests
var initialTime = clock.UtcNow; var initialTime = clock.UtcNow;
context.ResponseTime = null; context.ResponseTime = null;
await middleware.StartResponseAsync(context); middleware.StartResponse(context);
Assert.Equal(initialTime, context.ResponseTime); Assert.Equal(initialTime, context.ResponseTime);
clock.UtcNow += TimeSpan.FromSeconds(10); clock.UtcNow += TimeSpan.FromSeconds(10);
await middleware.StartResponseAsync(context); middleware.StartResponse(context);
Assert.NotEqual(clock.UtcNow, context.ResponseTime); Assert.NotEqual(clock.UtcNow, context.ResponseTime);
Assert.Equal(initialTime, context.ResponseTime); Assert.Equal(initialTime, context.ResponseTime);
} }
[Fact] [Fact]
public async Task FinalizeCacheHeadersAsync_UpdateShouldCacheResponse_IfResponseCacheable() public void FinalizeCacheHeadersAsync_UpdateShouldCacheResponse_IfResponseCacheable()
{ {
var sink = new TestSink(); var sink = new TestSink();
var middleware = TestUtils.CreateTestMiddleware(testSink: sink, policyProvider: new ResponseCachingPolicyProvider()); var middleware = TestUtils.CreateTestMiddleware(testSink: sink, policyProvider: new ResponseCachingPolicyProvider());
@ -415,14 +415,14 @@ namespace Microsoft.AspNetCore.ResponseCaching.Tests
Assert.False(context.ShouldCacheResponse); Assert.False(context.ShouldCacheResponse);
await middleware.FinalizeCacheHeadersAsync(context); middleware.FinalizeCacheHeaders(context);
Assert.True(context.ShouldCacheResponse); Assert.True(context.ShouldCacheResponse);
Assert.Empty(sink.Writes); Assert.Empty(sink.Writes);
} }
[Fact] [Fact]
public async Task FinalizeCacheHeadersAsync_DoNotUpdateShouldCacheResponse_IfResponseIsNotCacheable() public void FinalizeCacheHeadersAsync_DoNotUpdateShouldCacheResponse_IfResponseIsNotCacheable()
{ {
var sink = new TestSink(); var sink = new TestSink();
var middleware = TestUtils.CreateTestMiddleware(testSink: sink, policyProvider: new ResponseCachingPolicyProvider()); var middleware = TestUtils.CreateTestMiddleware(testSink: sink, policyProvider: new ResponseCachingPolicyProvider());
@ -430,27 +430,27 @@ namespace Microsoft.AspNetCore.ResponseCaching.Tests
middleware.ShimResponseStream(context); middleware.ShimResponseStream(context);
await middleware.FinalizeCacheHeadersAsync(context); middleware.FinalizeCacheHeaders(context);
Assert.False(context.ShouldCacheResponse); Assert.False(context.ShouldCacheResponse);
Assert.Empty(sink.Writes); Assert.Empty(sink.Writes);
} }
[Fact] [Fact]
public async Task FinalizeCacheHeadersAsync_DefaultResponseValidity_Is10Seconds() public void FinalizeCacheHeadersAsync_DefaultResponseValidity_Is10Seconds()
{ {
var sink = new TestSink(); var sink = new TestSink();
var middleware = TestUtils.CreateTestMiddleware(testSink: sink); var middleware = TestUtils.CreateTestMiddleware(testSink: sink);
var context = TestUtils.CreateTestContext(); var context = TestUtils.CreateTestContext();
await middleware.FinalizeCacheHeadersAsync(context); middleware.FinalizeCacheHeaders(context);
Assert.Equal(TimeSpan.FromSeconds(10), context.CachedResponseValidFor); Assert.Equal(TimeSpan.FromSeconds(10), context.CachedResponseValidFor);
Assert.Empty(sink.Writes); Assert.Empty(sink.Writes);
} }
[Fact] [Fact]
public async Task FinalizeCacheHeadersAsync_ResponseValidity_UseExpiryIfAvailable() public void FinalizeCacheHeadersAsync_ResponseValidity_UseExpiryIfAvailable()
{ {
var clock = new TestClock var clock = new TestClock
{ {
@ -466,14 +466,14 @@ namespace Microsoft.AspNetCore.ResponseCaching.Tests
context.ResponseTime = clock.UtcNow; context.ResponseTime = clock.UtcNow;
context.HttpContext.Response.Headers[HeaderNames.Expires] = HeaderUtilities.FormatDate(clock.UtcNow + TimeSpan.FromSeconds(11)); context.HttpContext.Response.Headers[HeaderNames.Expires] = HeaderUtilities.FormatDate(clock.UtcNow + TimeSpan.FromSeconds(11));
await middleware.FinalizeCacheHeadersAsync(context); middleware.FinalizeCacheHeaders(context);
Assert.Equal(TimeSpan.FromSeconds(11), context.CachedResponseValidFor); Assert.Equal(TimeSpan.FromSeconds(11), context.CachedResponseValidFor);
Assert.Empty(sink.Writes); Assert.Empty(sink.Writes);
} }
[Fact] [Fact]
public async Task FinalizeCacheHeadersAsync_ResponseValidity_UseMaxAgeIfAvailable() public void FinalizeCacheHeadersAsync_ResponseValidity_UseMaxAgeIfAvailable()
{ {
var clock = new TestClock var clock = new TestClock
{ {
@ -494,14 +494,14 @@ namespace Microsoft.AspNetCore.ResponseCaching.Tests
context.HttpContext.Response.Headers[HeaderNames.Expires] = HeaderUtilities.FormatDate(clock.UtcNow + TimeSpan.FromSeconds(11)); context.HttpContext.Response.Headers[HeaderNames.Expires] = HeaderUtilities.FormatDate(clock.UtcNow + TimeSpan.FromSeconds(11));
await middleware.FinalizeCacheHeadersAsync(context); middleware.FinalizeCacheHeaders(context);
Assert.Equal(TimeSpan.FromSeconds(12), context.CachedResponseValidFor); Assert.Equal(TimeSpan.FromSeconds(12), context.CachedResponseValidFor);
Assert.Empty(sink.Writes); Assert.Empty(sink.Writes);
} }
[Fact] [Fact]
public async Task FinalizeCacheHeadersAsync_ResponseValidity_UseSharedMaxAgeIfAvailable() public void FinalizeCacheHeadersAsync_ResponseValidity_UseSharedMaxAgeIfAvailable()
{ {
var clock = new TestClock var clock = new TestClock
{ {
@ -522,14 +522,14 @@ namespace Microsoft.AspNetCore.ResponseCaching.Tests
}.ToString(); }.ToString();
context.HttpContext.Response.Headers[HeaderNames.Expires] = HeaderUtilities.FormatDate(clock.UtcNow + TimeSpan.FromSeconds(11)); context.HttpContext.Response.Headers[HeaderNames.Expires] = HeaderUtilities.FormatDate(clock.UtcNow + TimeSpan.FromSeconds(11));
await middleware.FinalizeCacheHeadersAsync(context); middleware.FinalizeCacheHeaders(context);
Assert.Equal(TimeSpan.FromSeconds(13), context.CachedResponseValidFor); Assert.Equal(TimeSpan.FromSeconds(13), context.CachedResponseValidFor);
Assert.Empty(sink.Writes); Assert.Empty(sink.Writes);
} }
[Fact] [Fact]
public async Task FinalizeCacheHeadersAsync_UpdateCachedVaryByRules_IfNotEquivalentToPrevious() public void FinalizeCacheHeadersAsync_UpdateCachedVaryByRules_IfNotEquivalentToPrevious()
{ {
var cache = new TestResponseCache(); var cache = new TestResponseCache();
var sink = new TestSink(); var sink = new TestSink();
@ -548,7 +548,7 @@ namespace Microsoft.AspNetCore.ResponseCaching.Tests
}; };
context.CachedVaryByRules = cachedVaryByRules; context.CachedVaryByRules = cachedVaryByRules;
await middleware.FinalizeCacheHeadersAsync(context); middleware.FinalizeCacheHeaders(context);
Assert.Equal(1, cache.SetCount); Assert.Equal(1, cache.SetCount);
Assert.NotSame(cachedVaryByRules, context.CachedVaryByRules); Assert.NotSame(cachedVaryByRules, context.CachedVaryByRules);
@ -558,7 +558,7 @@ namespace Microsoft.AspNetCore.ResponseCaching.Tests
} }
[Fact] [Fact]
public async Task FinalizeCacheHeadersAsync_UpdateCachedVaryByRules_IfEquivalentToPrevious() public void FinalizeCacheHeadersAsync_UpdateCachedVaryByRules_IfEquivalentToPrevious()
{ {
var cache = new TestResponseCache(); var cache = new TestResponseCache();
var sink = new TestSink(); var sink = new TestSink();
@ -578,7 +578,7 @@ namespace Microsoft.AspNetCore.ResponseCaching.Tests
}; };
context.CachedVaryByRules = cachedVaryByRules; context.CachedVaryByRules = cachedVaryByRules;
await middleware.FinalizeCacheHeadersAsync(context); middleware.FinalizeCacheHeaders(context);
// An update to the cache is always made but the entry should be the same // An update to the cache is always made but the entry should be the same
Assert.Equal(1, cache.SetCount); Assert.Equal(1, cache.SetCount);
@ -608,7 +608,7 @@ namespace Microsoft.AspNetCore.ResponseCaching.Tests
[Theory] [Theory]
[MemberData(nameof(NullOrEmptyVaryRules))] [MemberData(nameof(NullOrEmptyVaryRules))]
public async Task FinalizeCacheHeadersAsync_UpdateCachedVaryByRules_NullOrEmptyRules(StringValues vary) public void FinalizeCacheHeadersAsync_UpdateCachedVaryByRules_NullOrEmptyRules(StringValues vary)
{ {
var cache = new TestResponseCache(); var cache = new TestResponseCache();
var sink = new TestSink(); var sink = new TestSink();
@ -621,7 +621,7 @@ namespace Microsoft.AspNetCore.ResponseCaching.Tests
VaryByQueryKeys = vary VaryByQueryKeys = vary
}); });
await middleware.FinalizeCacheHeadersAsync(context); middleware.FinalizeCacheHeaders(context);
// Vary rules should not be updated // Vary rules should not be updated
Assert.Equal(0, cache.SetCount); Assert.Equal(0, cache.SetCount);
@ -629,7 +629,7 @@ namespace Microsoft.AspNetCore.ResponseCaching.Tests
} }
[Fact] [Fact]
public async Task FinalizeCacheHeadersAsync_AddsDate_IfNoneSpecified() public void FinalizeCacheHeadersAsync_AddsDate_IfNoneSpecified()
{ {
var utcNow = DateTimeOffset.UtcNow; var utcNow = DateTimeOffset.UtcNow;
var sink = new TestSink(); var sink = new TestSink();
@ -640,14 +640,14 @@ namespace Microsoft.AspNetCore.ResponseCaching.Tests
Assert.True(StringValues.IsNullOrEmpty(context.HttpContext.Response.Headers[HeaderNames.Date])); Assert.True(StringValues.IsNullOrEmpty(context.HttpContext.Response.Headers[HeaderNames.Date]));
await middleware.FinalizeCacheHeadersAsync(context); middleware.FinalizeCacheHeaders(context);
Assert.Equal(HeaderUtilities.FormatDate(utcNow), context.HttpContext.Response.Headers[HeaderNames.Date]); Assert.Equal(HeaderUtilities.FormatDate(utcNow), context.HttpContext.Response.Headers[HeaderNames.Date]);
Assert.Empty(sink.Writes); Assert.Empty(sink.Writes);
} }
[Fact] [Fact]
public async Task FinalizeCacheHeadersAsync_DoNotAddDate_IfSpecified() public void FinalizeCacheHeadersAsync_DoNotAddDate_IfSpecified()
{ {
var utcNow = DateTimeOffset.MinValue; var utcNow = DateTimeOffset.MinValue;
var sink = new TestSink(); var sink = new TestSink();
@ -659,14 +659,14 @@ namespace Microsoft.AspNetCore.ResponseCaching.Tests
Assert.Equal(HeaderUtilities.FormatDate(utcNow), context.HttpContext.Response.Headers[HeaderNames.Date]); Assert.Equal(HeaderUtilities.FormatDate(utcNow), context.HttpContext.Response.Headers[HeaderNames.Date]);
await middleware.FinalizeCacheHeadersAsync(context); middleware.FinalizeCacheHeaders(context);
Assert.Equal(HeaderUtilities.FormatDate(utcNow), context.HttpContext.Response.Headers[HeaderNames.Date]); Assert.Equal(HeaderUtilities.FormatDate(utcNow), context.HttpContext.Response.Headers[HeaderNames.Date]);
Assert.Empty(sink.Writes); Assert.Empty(sink.Writes);
} }
[Fact] [Fact]
public async Task FinalizeCacheHeadersAsync_StoresCachedResponse_InState() public void FinalizeCacheHeadersAsync_StoresCachedResponse_InState()
{ {
var sink = new TestSink(); var sink = new TestSink();
var middleware = TestUtils.CreateTestMiddleware(testSink: sink); var middleware = TestUtils.CreateTestMiddleware(testSink: sink);
@ -674,14 +674,14 @@ namespace Microsoft.AspNetCore.ResponseCaching.Tests
Assert.Null(context.CachedResponse); Assert.Null(context.CachedResponse);
await middleware.FinalizeCacheHeadersAsync(context); middleware.FinalizeCacheHeaders(context);
Assert.NotNull(context.CachedResponse); Assert.NotNull(context.CachedResponse);
Assert.Empty(sink.Writes); Assert.Empty(sink.Writes);
} }
[Fact] [Fact]
public async Task FinalizeCacheHeadersAsync_SplitsVaryHeaderByCommas() public void FinalizeCacheHeadersAsync_SplitsVaryHeaderByCommas()
{ {
var sink = new TestSink(); var sink = new TestSink();
var middleware = TestUtils.CreateTestMiddleware(testSink: sink); var middleware = TestUtils.CreateTestMiddleware(testSink: sink);
@ -689,7 +689,7 @@ namespace Microsoft.AspNetCore.ResponseCaching.Tests
context.HttpContext.Response.Headers[HeaderNames.Vary] = "HeaderB, heaDera"; context.HttpContext.Response.Headers[HeaderNames.Vary] = "HeaderB, heaDera";
await middleware.FinalizeCacheHeadersAsync(context); middleware.FinalizeCacheHeaders(context);
Assert.Equal(new StringValues(new[] { "HEADERA", "HEADERB" }), context.CachedVaryByRules.Headers); Assert.Equal(new StringValues(new[] { "HEADERA", "HEADERB" }), context.CachedVaryByRules.Headers);
TestUtils.AssertLoggedMessages( TestUtils.AssertLoggedMessages(
@ -715,7 +715,7 @@ namespace Microsoft.AspNetCore.ResponseCaching.Tests
context.BaseKey = "BaseKey"; context.BaseKey = "BaseKey";
context.CachedResponseValidFor = TimeSpan.FromSeconds(10); context.CachedResponseValidFor = TimeSpan.FromSeconds(10);
await middleware.FinalizeCacheBodyAsync(context); middleware.FinalizeCacheBody(context);
Assert.Equal(1, cache.SetCount); Assert.Equal(1, cache.SetCount);
TestUtils.AssertLoggedMessages( TestUtils.AssertLoggedMessages(
@ -741,7 +741,7 @@ namespace Microsoft.AspNetCore.ResponseCaching.Tests
context.BaseKey = "BaseKey"; context.BaseKey = "BaseKey";
context.CachedResponseValidFor = TimeSpan.FromSeconds(10); context.CachedResponseValidFor = TimeSpan.FromSeconds(10);
await middleware.FinalizeCacheBodyAsync(context); middleware.FinalizeCacheBody(context);
Assert.Equal(0, cache.SetCount); Assert.Equal(0, cache.SetCount);
TestUtils.AssertLoggedMessages( TestUtils.AssertLoggedMessages(
@ -769,7 +769,7 @@ namespace Microsoft.AspNetCore.ResponseCaching.Tests
context.BaseKey = "BaseKey"; context.BaseKey = "BaseKey";
context.CachedResponseValidFor = TimeSpan.FromSeconds(10); context.CachedResponseValidFor = TimeSpan.FromSeconds(10);
await middleware.FinalizeCacheBodyAsync(context); middleware.FinalizeCacheBody(context);
Assert.Equal(1, cache.SetCount); Assert.Equal(1, cache.SetCount);
TestUtils.AssertLoggedMessages( TestUtils.AssertLoggedMessages(
@ -789,7 +789,7 @@ namespace Microsoft.AspNetCore.ResponseCaching.Tests
await context.HttpContext.Response.WriteAsync(new string('0', 10)); await context.HttpContext.Response.WriteAsync(new string('0', 10));
context.ShouldCacheResponse = false; context.ShouldCacheResponse = false;
await middleware.FinalizeCacheBodyAsync(context); middleware.FinalizeCacheBody(context);
Assert.Equal(0, cache.SetCount); Assert.Equal(0, cache.SetCount);
TestUtils.AssertLoggedMessages( TestUtils.AssertLoggedMessages(
@ -811,7 +811,7 @@ namespace Microsoft.AspNetCore.ResponseCaching.Tests
context.ResponseCachingStream.DisableBuffering(); context.ResponseCachingStream.DisableBuffering();
await middleware.FinalizeCacheBodyAsync(context); middleware.FinalizeCacheBody(context);
Assert.Equal(0, cache.SetCount); Assert.Equal(0, cache.SetCount);
TestUtils.AssertLoggedMessages( TestUtils.AssertLoggedMessages(
@ -840,7 +840,7 @@ namespace Microsoft.AspNetCore.ResponseCaching.Tests
context.CachedResponse = new CachedResponse() { Headers = new HeaderDictionary() }; context.CachedResponse = new CachedResponse() { Headers = new HeaderDictionary() };
context.CachedResponseValidFor = TimeSpan.FromSeconds(10); context.CachedResponseValidFor = TimeSpan.FromSeconds(10);
await middleware.FinalizeCacheBodyAsync(context); middleware.FinalizeCacheBody(context);
// The response cached message will be logged but the adding of the entry will no-op // The response cached message will be logged but the adding of the entry will no-op
TestUtils.AssertLoggedMessages( TestUtils.AssertLoggedMessages(

View File

@ -385,22 +385,11 @@ namespace Microsoft.AspNetCore.ResponseCaching.Tests
} }
} }
public Task<IResponseCacheEntry> GetAsync(string key)
{
return Task.FromResult(Get(key));
}
public void Set(string key, IResponseCacheEntry entry, TimeSpan validFor) public void Set(string key, IResponseCacheEntry entry, TimeSpan validFor)
{ {
SetCount++; SetCount++;
_storage[key] = entry; _storage[key] = entry;
} }
public Task SetAsync(string key, IResponseCacheEntry entry, TimeSpan validFor)
{
Set(key, entry, validFor);
return Task.CompletedTask;
}
} }
internal class TestClock : ISystemClock internal class TestClock : ISystemClock