|
| 1 | +using AspireTunnelProxy.Cors; |
| 2 | +using Microsoft.AspNetCore.Cors.Infrastructure; |
| 3 | +using Microsoft.Extensions.Configuration; |
| 4 | + |
| 5 | +namespace Proxy.Tests.Cors; |
| 6 | + |
| 7 | +public class CorsPolicyConfiguratorTests |
| 8 | +{ |
| 9 | + [Fact] |
| 10 | + public void Wildcard_origins_methods_headers_map_to_allow_any() |
| 11 | + { |
| 12 | + var options = Configure(new Dictionary<string, string?> |
| 13 | + { |
| 14 | + ["p:AllowedOrigins:0"] = "*", |
| 15 | + ["p:AllowedMethods:0"] = "*", |
| 16 | + ["p:AllowedHeaders:0"] = "*", |
| 17 | + }); |
| 18 | + |
| 19 | + var policy = options.GetPolicy("p")!; |
| 20 | + Assert.True(policy.AllowAnyOrigin); |
| 21 | + Assert.True(policy.AllowAnyMethod); |
| 22 | + Assert.True(policy.AllowAnyHeader); |
| 23 | + } |
| 24 | + |
| 25 | + [Fact] |
| 26 | + public void Explicit_values_map_to_with_lists() |
| 27 | + { |
| 28 | + var options = Configure(new Dictionary<string, string?> |
| 29 | + { |
| 30 | + ["p:AllowedOrigins:0"] = "https://a.example", |
| 31 | + ["p:AllowedOrigins:1"] = "https://b.example", |
| 32 | + ["p:AllowedMethods:0"] = "GET", |
| 33 | + ["p:AllowedMethods:1"] = "POST", |
| 34 | + ["p:AllowedHeaders:0"] = "X-Custom", |
| 35 | + ["p:ExposedHeaders:0"] = "X-Trace-Id", |
| 36 | + }); |
| 37 | + |
| 38 | + var policy = options.GetPolicy("p")!; |
| 39 | + Assert.False(policy.AllowAnyOrigin); |
| 40 | + Assert.Contains("https://a.example", policy.Origins); |
| 41 | + Assert.Contains("https://b.example", policy.Origins); |
| 42 | + Assert.Contains("GET", policy.Methods); |
| 43 | + Assert.Contains("POST", policy.Methods); |
| 44 | + Assert.Contains("X-Custom", policy.Headers); |
| 45 | + Assert.Contains("X-Trace-Id", policy.ExposedHeaders); |
| 46 | + } |
| 47 | + |
| 48 | + [Fact] |
| 49 | + public void AllowCredentials_true_enables_credentials() |
| 50 | + { |
| 51 | + var options = Configure(new Dictionary<string, string?> |
| 52 | + { |
| 53 | + ["p:AllowedOrigins:0"] = "https://a.example", |
| 54 | + ["p:AllowCredentials"] = "true", |
| 55 | + }); |
| 56 | + |
| 57 | + var policy = options.GetPolicy("p")!; |
| 58 | + Assert.True(policy.SupportsCredentials); |
| 59 | + } |
| 60 | + |
| 61 | + [Fact] |
| 62 | + public void Preflight_max_age_seconds_populates_policy() |
| 63 | + { |
| 64 | + var options = Configure(new Dictionary<string, string?> |
| 65 | + { |
| 66 | + ["p:AllowedOrigins:0"] = "*", |
| 67 | + ["p:PreflightMaxAgeSeconds"] = "600", |
| 68 | + }); |
| 69 | + |
| 70 | + var policy = options.GetPolicy("p")!; |
| 71 | + Assert.Equal(TimeSpan.FromSeconds(600), policy.PreflightMaxAge); |
| 72 | + } |
| 73 | + |
| 74 | + [Fact] |
| 75 | + public void Throws_when_credentials_with_wildcard_origin() |
| 76 | + { |
| 77 | + var ex = Assert.Throws<InvalidOperationException>(() => Configure(new Dictionary<string, string?> |
| 78 | + { |
| 79 | + ["bad:AllowedOrigins:0"] = "*", |
| 80 | + ["bad:AllowCredentials"] = "true", |
| 81 | + })); |
| 82 | + |
| 83 | + Assert.Contains("bad", ex.Message); |
| 84 | + } |
| 85 | + |
| 86 | + [Fact] |
| 87 | + public void Multiple_policies_all_get_registered() |
| 88 | + { |
| 89 | + var options = Configure(new Dictionary<string, string?> |
| 90 | + { |
| 91 | + ["p1:AllowedOrigins:0"] = "*", |
| 92 | + ["p2:AllowedOrigins:0"] = "https://a.example", |
| 93 | + }); |
| 94 | + |
| 95 | + Assert.NotNull(options.GetPolicy("p1")); |
| 96 | + Assert.NotNull(options.GetPolicy("p2")); |
| 97 | + } |
| 98 | + |
| 99 | + private static CorsOptions Configure(IDictionary<string, string?> values) |
| 100 | + { |
| 101 | + var config = new ConfigurationBuilder() |
| 102 | + .AddInMemoryCollection(values) |
| 103 | + .Build(); |
| 104 | + |
| 105 | + var options = new CorsOptions(); |
| 106 | + CorsPolicyConfigurator.Configure(options, config); |
| 107 | + return options; |
| 108 | + } |
| 109 | +} |
0 commit comments