-
Notifications
You must be signed in to change notification settings - Fork 905
Expand file tree
/
Copy pathOutputCacheConfigTests.cs
More file actions
93 lines (81 loc) · 3.5 KB
/
OutputCacheConfigTests.cs
File metadata and controls
93 lines (81 loc) · 3.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Xunit;
namespace Yarp.ReverseProxy.Configuration;
public class OutputCacheConfigTests
{
[Fact]
public async Task All_Options_Added()
{
var config = new OutputCacheConfig();
config.DefaultExpirationTimeSpan = TimeSpan.FromSeconds(1);
config.MaximumBodySize = 10;
config.SizeLimit = 20;
config.UseCaseSensitivePaths = true;
config.NamedPolicies.Add("test1", new NamedCacheConfig { ExpirationTimeSpan = TimeSpan.FromSeconds(5), ExcludeDefaultPolicy = true });
config.NamedPolicies.Add("test2", new NamedCacheConfig { ExpirationTimeSpan = TimeSpan.FromSeconds(15), ExcludeDefaultPolicy = false });
config.NamedPolicies.Add("test3", new NamedCacheConfig { ExpirationTimeSpan = TimeSpan.FromSeconds(3), ExcludeDefaultPolicy = true, VaryByHeaders = new[] { "X-SomeHeader" } });
var builder = WebApplication.CreateBuilder();
builder.Services.AddOutputCache(config)
.AddReverseProxy();
var app = builder.Build();
var policies = app.Services.GetRequiredService<IYarpOutputCachePolicyProvider>();
var test1 = await policies.GetPolicyAsync("test1");
var test2 = await policies.GetPolicyAsync("test2");
var test3 = await policies.GetPolicyAsync("test3");
Assert.NotNull(test1);
Assert.NotNull(test2);
Assert.NotNull(test3);
}
[Fact]
public async Task All_Options_Added_Json()
{
var json =
"""
{
"OutputCache": {
"DefaultExpirationTimeSpan": "00:05:00",
"MaximumBodySize": 10,
"SizeLimit": 20,
"UseCaseSensitivePaths": true,
"NamedPolicies": {
"test1": {
"ExpirationTimeSpan": "00:05:00",
"ExcludeDefaultPolicy": true
},
"test2": {
"ExpirationTimeSpan": "00:15:00",
"ExcludeDefaultPolicy": false
},
"test3": {
"ExpirationTimeSpan": "00:03:00",
"ExcludeDefaultPolicy": true,
"VaryByHeaders": [ "X-SomeHeader" ]
}
}
}
}
""";
var configBuilder = new ConfigurationBuilder();
var stream = new MemoryStream(Encoding.UTF8.GetBytes(json));
var config = configBuilder.AddJsonStream(stream).Build();
var builder = WebApplication.CreateBuilder();
builder.Services.AddOutputCache(config.GetSection("OutputCache"))
.AddReverseProxy();
var app = builder.Build();
var policies = app.Services.GetRequiredService<IYarpOutputCachePolicyProvider>();
var test1 = await policies.GetPolicyAsync("test1");
var test2 = await policies.GetPolicyAsync("test2");
var test3 = await policies.GetPolicyAsync("test3");
Assert.NotNull(test1);
Assert.NotNull(test2);
Assert.NotNull(test3);
}
}