-
Notifications
You must be signed in to change notification settings - Fork 114
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
update documentation for endpoint feature flags support and add tests…
… for FeatureFlagsEndpointFilterExtensions
- Loading branch information
1 parent
3339e7a
commit a25a92f
Showing
2 changed files
with
148 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
111 changes: 111 additions & 0 deletions
111
tests/Tests.FeatureManagement.AspNetCore/FeatureFlagsEndpoint.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
#if NET7_0_OR_GREATER | ||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.AspNetCore.Hosting; | ||
using Microsoft.AspNetCore.TestHost; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Hosting; | ||
using Microsoft.FeatureManagement; | ||
using Microsoft.FeatureManagement.AspNetCore; | ||
using Microsoft.FeatureManagement.FeatureFilters; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Net.Http; | ||
using System.Threading.Tasks; | ||
using Xunit; | ||
|
||
namespace Tests.FeatureManagement.AspNetCore | ||
{ | ||
public class TestTargetingContextAccessor : ITargetingContextAccessor | ||
{ | ||
public ValueTask<TargetingContext> GetContextAsync() | ||
{ | ||
return new ValueTask<TargetingContext>(new TargetingContext | ||
{ | ||
UserId = "testUser", | ||
Groups = new[] { "testGroup" } | ||
}); | ||
} | ||
} | ||
|
||
public class FeatureTestServer : IDisposable | ||
{ | ||
private readonly IHost _host; | ||
private readonly HttpClient _client; | ||
private readonly bool _featureEnabled; | ||
|
||
public FeatureTestServer(bool featureEnabled = true) | ||
{ | ||
_featureEnabled = featureEnabled; | ||
_host = CreateHostBuilder().Build(); | ||
_host.Start(); | ||
_client = _host.GetTestServer().CreateClient(); | ||
} | ||
|
||
private IHostBuilder CreateHostBuilder() | ||
{ | ||
var configuration = new ConfigurationBuilder() | ||
.AddInMemoryCollection(new Dictionary<string, string> | ||
{ | ||
["FeatureManagement:TestFeature"] = _featureEnabled.ToString().ToLower() | ||
}) | ||
.Build(); | ||
|
||
return Host.CreateDefaultBuilder() | ||
.ConfigureWebHost(webBuilder => | ||
{ | ||
webBuilder | ||
.UseTestServer() | ||
.ConfigureServices(services => | ||
{ | ||
services.AddSingleton<IConfiguration>(configuration); | ||
services.AddSingleton<ITargetingContextAccessor, TestTargetingContextAccessor>(); | ||
services.AddFeatureManagement(); | ||
services.AddRouting(); | ||
}) | ||
.Configure(app => | ||
{ | ||
app.UseRouting(); | ||
app.UseEndpoints(endpoints => | ||
{ | ||
endpoints.MapGet("/test", new Func<string>(() => "Feature Enabled")) | ||
.WithFeatureFlag("TestFeature", () => | ||
new TargetingContext | ||
{ | ||
UserId = "testUser", | ||
Groups = new[] { "testGroup" } | ||
}); | ||
}); | ||
}); | ||
}); | ||
} | ||
|
||
public HttpClient Client => _client; | ||
|
||
public void Dispose() | ||
{ | ||
_host?.Dispose(); | ||
_client?.Dispose(); | ||
} | ||
} | ||
|
||
public class FeatureFlagsEndpointFilterTests | ||
{ | ||
[Fact] | ||
public async Task WhenFeatureEnabled_ReturnsSuccess() | ||
{ | ||
using var server = new FeatureTestServer(featureEnabled: true); | ||
var response = await server.Client.GetAsync("/test"); | ||
Assert.Equal(System.Net.HttpStatusCode.OK, response.StatusCode); | ||
} | ||
|
||
[Fact] | ||
public async Task WhenFeatureDisabled_ReturnsNotFound() | ||
{ | ||
using var server = new FeatureTestServer(featureEnabled: false); | ||
var response = await server.Client.GetAsync("/test"); | ||
Assert.Equal(System.Net.HttpStatusCode.NotFound, response.StatusCode); | ||
} | ||
} | ||
} | ||
#endif |