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

Commit b979709

Browse files
committed
test response caching feature always added
1 parent 86c344d commit b979709

File tree

2 files changed

+28
-27
lines changed

2 files changed

+28
-27
lines changed

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

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -851,38 +851,33 @@ private class FakeResponseFeature : HttpResponseFeature
851851
public override void OnStarting(Func<object, Task> callback, object state) { }
852852
}
853853

854-
[Fact]
855-
public async Task Invoke_CacheableRequest_AddsResponseCachingFeature()
854+
[Theory]
855+
// If allowResponseCaching is false, other settings will not matter but are included for completeness
856+
[InlineData(false, false, false)]
857+
[InlineData(false, false, true)]
858+
[InlineData(false, true, false)]
859+
[InlineData(false, true, true)]
860+
[InlineData(true, false, false)]
861+
[InlineData(true, false, true)]
862+
[InlineData(true, true, false)]
863+
[InlineData(true, true, true)]
864+
public async Task Invoke_AddsResponseCachingFeature_Always(bool allowResponseCaching, bool allowCacheLookup, bool allowCacheStorage)
856865
{
857866
var responseCachingFeatureAdded = false;
858867
var middleware = TestUtils.CreateTestMiddleware(next: httpContext =>
859868
{
860869
responseCachingFeatureAdded = httpContext.Features.Get<IResponseCachingFeature>() != null;
861870
return TaskCache.CompletedTask;
862871
},
863-
policyProvider: new ResponseCachingPolicyProvider());
864-
865-
var context = new DefaultHttpContext();
866-
context.Request.Method = HttpMethods.Get;
867-
context.Features.Set<IHttpResponseFeature>(new FakeResponseFeature());
868-
await middleware.Invoke(context);
869-
870-
Assert.True(responseCachingFeatureAdded);
871-
}
872-
873-
[Fact]
874-
public async Task Invoke_NonCacheableRequest_AddsResponseCachingFeature()
875-
{
876-
var responseCachingFeatureAdded = false;
877-
var middleware = TestUtils.CreateTestMiddleware(next: httpContext =>
872+
policyProvider: new TestResponseCachingPolicyProvider
878873
{
879-
responseCachingFeatureAdded = httpContext.Features.Get<IResponseCachingFeature>() != null;
880-
return TaskCache.CompletedTask;
881-
},
882-
policyProvider: new ResponseCachingPolicyProvider());
874+
AllowResponseCachingValue = allowResponseCaching,
875+
AllowCacheLookupValue = allowCacheLookup,
876+
AllowCacheStorageValue = allowCacheStorage
877+
});
883878

884879
var context = new DefaultHttpContext();
885-
context.Request.Method = HttpMethods.Post;
880+
context.Features.Set<IHttpResponseFeature>(new FakeResponseFeature());
886881
await middleware.Invoke(context);
887882

888883
Assert.True(responseCachingFeatureAdded);

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

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -233,15 +233,21 @@ public Task SendFileAsync(string path, long offset, long? count, CancellationTok
233233

234234
internal class TestResponseCachingPolicyProvider : IResponseCachingPolicyProvider
235235
{
236-
public bool AllowCacheLookup(ResponseCachingContext context) => false;
236+
public bool AllowCacheLookupValue { get; set; } = false;
237+
public bool AllowCacheStorageValue { get; set; } = false;
238+
public bool AllowResponseCachingValue { get; set; } = false;
239+
public bool IsCachedEntryFreshValue { get; set; } = true;
240+
public bool IsResponseCacheableValue { get; set; } = true;
237241

238-
public bool AllowCacheStorage(ResponseCachingContext context) => false;
242+
public bool AllowCacheLookup(ResponseCachingContext context) => AllowCacheLookupValue;
239243

240-
public bool AllowResponseCaching(ResponseCachingContext context) => false;
244+
public bool AllowCacheStorage(ResponseCachingContext context) => AllowCacheStorageValue;
241245

242-
public bool IsCachedEntryFresh(ResponseCachingContext context) => true;
246+
public bool AllowResponseCaching(ResponseCachingContext context) => AllowResponseCachingValue;
243247

244-
public bool IsResponseCacheable(ResponseCachingContext context) => true;
248+
public bool IsCachedEntryFresh(ResponseCachingContext context) => IsCachedEntryFreshValue;
249+
250+
public bool IsResponseCacheable(ResponseCachingContext context) => IsResponseCacheableValue;
245251
}
246252

247253
internal class TestResponseCachingKeyProvider : IResponseCachingKeyProvider

0 commit comments

Comments
 (0)