Skip to content
Prev Previous commit
Next Next commit
add test
  • Loading branch information
zhiyuanliang-ms committed Oct 20, 2025
commit ff52bbef9564f1a4eabff2c3d621c405f5422f7e
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,47 @@ public async Task GatesRazorPageFeatures()
Assert.Equal(HttpStatusCode.OK, gateAnyNegateResponse.StatusCode);
}

[Fact]
public async Task GatesActionFilterFeatures()
{
IConfiguration config = new ConfigurationBuilder().AddJsonFile("appsettings.json").Build();

TestServer server = new TestServer(WebHost.CreateDefaultBuilder().ConfigureServices(services =>
{
services
.AddSingleton(config)
.AddFeatureManagement()
.AddFeatureFilter<TestFilter>();

services.AddMvcCore(o =>
{
DisableEndpointRouting(o);
o.Filters.AddForFeature<MvcFilter>(RequirementType.All, Features.ConditionalFeature, Features.ConditionalFeature2);
});
}).Configure(app => app.UseMvc()));

TestFilter filter = (TestFilter)server.Host.Services.GetRequiredService<IEnumerable<IFeatureFilterMetadata>>().First(f => f is TestFilter);
HttpClient client = server.CreateClient();

//
// Enable all features
filter.Callback = _ => Task.FromResult(true);
HttpResponseMessage res = await client.GetAsync("");
Assert.True(res.Headers.Contains(nameof(MvcFilter)));

//
// Enable 1/2 features
filter.Callback = ctx => Task.FromResult(ctx.FeatureName == Features.ConditionalFeature);
res = await client.GetAsync("");
Assert.False(res.Headers.Contains(nameof(MvcFilter)));

//
// Enable no
filter.Callback = _ => Task.FromResult(false);
res = await client.GetAsync("");
Assert.False(res.Headers.Contains(nameof(MvcFilter)));
}

private static void DisableEndpointRouting(MvcOptions options)
{
options.EnableEndpointRouting = false;
Expand Down