Skip to content

Commit

Permalink
Merge in 'release/7.0' changes
Browse files Browse the repository at this point in the history
  • Loading branch information
dotnet-bot committed Nov 14, 2022
2 parents 744a5d2 + 0384a80 commit 2164e82
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 2164e82

Please sign in to comment.