|
7 | 7 | using Microsoft.AspNetCore.Http;
|
8 | 8 | using Microsoft.AspNetCore.Http.Features;
|
9 | 9 | using Microsoft.AspNetCore.ResponseCaching.Internal;
|
| 10 | +using Microsoft.Extensions.Caching.Memory; |
10 | 11 | using Microsoft.Extensions.Logging.Testing;
|
11 | 12 | using Microsoft.Extensions.Primitives;
|
12 | 13 | using Microsoft.Net.Http.Headers;
|
@@ -823,6 +824,40 @@ public async Task FinalizeCacheBody_DoNotCache_IfBufferingDisabled()
|
823 | 824 | LoggedMessage.ResponseNotCached);
|
824 | 825 | }
|
825 | 826 |
|
| 827 | + [Fact] |
| 828 | + public async Task FinalizeCacheBody_DoNotCache_IfSizeTooBig() |
| 829 | + { |
| 830 | + var sink = new TestSink(); |
| 831 | + var middleware = TestUtils.CreateTestMiddleware( |
| 832 | + testSink: sink, |
| 833 | + keyProvider: new TestResponseCachingKeyProvider("BaseKey"), |
| 834 | + cache: new MemoryResponseCache(new MemoryCache(new MemoryCacheOptions |
| 835 | + { |
| 836 | + SizeLimit = 100 |
| 837 | + }))); |
| 838 | + var context = TestUtils.CreateTestContext(); |
| 839 | + |
| 840 | + context.ShouldCacheResponse = true; |
| 841 | + middleware.ShimResponseStream(context); |
| 842 | + |
| 843 | + await context.HttpContext.Response.WriteAsync(new string('0', 101)); |
| 844 | + |
| 845 | + context.CachedResponse = new CachedResponse() { Headers = new HeaderDictionary() }; |
| 846 | + context.CachedResponseValidFor = TimeSpan.FromSeconds(10); |
| 847 | + |
| 848 | + await middleware.FinalizeCacheBodyAsync(context); |
| 849 | + |
| 850 | + // The response cached message will be logged but the adding of the entry will no-op |
| 851 | + TestUtils.AssertLoggedMessages( |
| 852 | + sink.Writes, |
| 853 | + LoggedMessage.ResponseCached); |
| 854 | + |
| 855 | + // Reset the context |
| 856 | + context = TestUtils.CreateTestContext(); |
| 857 | + |
| 858 | + Assert.False(await middleware.TryServeFromCacheAsync(context)); |
| 859 | + } |
| 860 | + |
826 | 861 | [Fact]
|
827 | 862 | public void AddResponseCachingFeature_SecondInvocation_Throws()
|
828 | 863 | {
|
|
0 commit comments