Skip to content
This repository was archived by the owner on Nov 22, 2018. It is now read-only.

Commit bb3867c

Browse files
committed
Add a test
1 parent e70356a commit bb3867c

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

src/Microsoft.AspNetCore.ResponseCaching/ResponseCachingMiddleware.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,6 @@ internal async Task FinalizeCacheBodyAsync(ResponseCachingContext context)
352352

353353
context.CachedResponse.Body = bufferStream;
354354
_logger.LogResponseCached();
355-
356355
await _cache.SetAsync(context.StorageVaryKey ?? context.BaseKey, context.CachedResponse, context.CachedResponseValidFor);
357356
}
358357
else

test/Microsoft.AspNetCore.ResponseCaching.Tests/ResponseCachingMiddlewareTests.cs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using Microsoft.AspNetCore.Http;
88
using Microsoft.AspNetCore.Http.Features;
99
using Microsoft.AspNetCore.ResponseCaching.Internal;
10+
using Microsoft.Extensions.Caching.Memory;
1011
using Microsoft.Extensions.Logging.Testing;
1112
using Microsoft.Extensions.Primitives;
1213
using Microsoft.Net.Http.Headers;
@@ -823,6 +824,40 @@ public async Task FinalizeCacheBody_DoNotCache_IfBufferingDisabled()
823824
LoggedMessage.ResponseNotCached);
824825
}
825826

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+
826861
[Fact]
827862
public void AddResponseCachingFeature_SecondInvocation_Throws()
828863
{

0 commit comments

Comments
 (0)