Skip to content

Commit

Permalink
[release/7.0] Fix predicate evaluation in OutputCacheAttribute (#45027)
Browse files Browse the repository at this point in the history
* Fix predicate evaluation in OutputCacheAttribute

Fixes #44739

* Fix typo

* Update src/Middleware/OutputCaching/test/OutputCacheAttributeTests.cs

Co-authored-by: Brennan <brecon@microsoft.com>

Co-authored-by: Sebastien Ros <sebastienros@gmail.com>
Co-authored-by: Brennan <brecon@microsoft.com>
  • Loading branch information
3 people committed Nov 14, 2022
1 parent 462405a commit 0384a80
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
8 changes: 7 additions & 1 deletion src/Middleware/OutputCaching/src/OutputCacheAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,18 @@ internal IOutputCachePolicy BuildPolicy()
return _builtPolicy;
}

var builder = new OutputCachePolicyBuilder();
OutputCachePolicyBuilder builder;

if (PolicyName != null)
{
// Don't add the default policy if a named one is used as it could already contain it
builder = new OutputCachePolicyBuilder(excludeDefaultPolicy: true);
builder.AddPolicy(new NamedPolicy(PolicyName));
}
else
{
builder = new();
}

if (_noCache != null && _noCache.Value)
{
Expand Down
16 changes: 15 additions & 1 deletion src/Middleware/OutputCaching/test/OutputCacheAttributeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public async Task Attribute_CreatesNamedPolicy()
var options = new OutputCacheOptions();
options.AddPolicy("MyPolicy", b => b.Expire(TimeSpan.FromSeconds(42)));

var context = TestUtils.CreateTestContext(options: options);
var context = TestUtils.CreateUninitializedContext(options: options);

var attribute = OutputCacheMethods.GetAttribute(nameof(OutputCacheMethods.PolicyName));
await attribute.BuildPolicy().CacheRequestAsync(context, cancellation: default);
Expand All @@ -57,6 +57,20 @@ public async Task Attribute_CreatesNamedPolicy()
Assert.Equal(42, context.ResponseExpirationTimeSpan?.TotalSeconds);
}

[Fact]
public async Task Attribute_NamedPolicyDoesNotInjectDefaultPolicy()
{
var options = new OutputCacheOptions();
options.AddPolicy("MyPolicy", b => b.With(x => false).Cache());

var context = TestUtils.CreateUninitializedContext(options: options);

var attribute = OutputCacheMethods.GetAttribute(nameof(OutputCacheMethods.PolicyName));
await attribute.BuildPolicy().CacheRequestAsync(context, cancellation: default);

Assert.False(context.EnableOutputCaching);
}

[Fact]
public async Task Attribute_CreatesVaryByHeaderPolicy()
{
Expand Down

0 comments on commit 0384a80

Please sign in to comment.