Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion Microsoft.FeatureManagement.sln
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ConsoleApp", "examples\Cons
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TargetingConsoleApp", "examples\TargetingConsoleApp\TargetingConsoleApp.csproj", "{6558C21E-CF20-4278-AA08-EB9D1DF29D66}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RazorPages", "examples\RazorPages\RazorPages.csproj", "{BA29A1BB-81D5-4EB1-AF37-6ECF64AF27E2}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "RazorPages", "examples\RazorPages\RazorPages.csproj", "{BA29A1BB-81D5-4EB1-AF37-6ECF64AF27E2}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tests.FeatureManagement.AspNetCore", "tests\Tests.FeatureManagement.AspNetCore\Tests.FeatureManagement.AspNetCore.csproj", "{FC0DC3E2-5646-4AEC-A7DB-2D6167BC3BB4}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down Expand Up @@ -55,6 +57,10 @@ Global
{BA29A1BB-81D5-4EB1-AF37-6ECF64AF27E2}.Debug|Any CPU.Build.0 = Debug|Any CPU
{BA29A1BB-81D5-4EB1-AF37-6ECF64AF27E2}.Release|Any CPU.ActiveCfg = Release|Any CPU
{BA29A1BB-81D5-4EB1-AF37-6ECF64AF27E2}.Release|Any CPU.Build.0 = Release|Any CPU
{FC0DC3E2-5646-4AEC-A7DB-2D6167BC3BB4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{FC0DC3E2-5646-4AEC-A7DB-2D6167BC3BB4}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FC0DC3E2-5646-4AEC-A7DB-2D6167BC3BB4}.Release|Any CPU.ActiveCfg = Release|Any CPU
{FC0DC3E2-5646-4AEC-A7DB-2D6167BC3BB4}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -65,6 +71,7 @@ Global
{E50FB931-7A42-440E-AC47-B8DFE5E15394} = {FB5C34DF-695C-4DF9-8AED-B3EA2516EA72}
{6558C21E-CF20-4278-AA08-EB9D1DF29D66} = {FB5C34DF-695C-4DF9-8AED-B3EA2516EA72}
{BA29A1BB-81D5-4EB1-AF37-6ECF64AF27E2} = {FB5C34DF-695C-4DF9-8AED-B3EA2516EA72}
{FC0DC3E2-5646-4AEC-A7DB-2D6167BC3BB4} = {8ED6FFEE-4037-49A2-9709-BC519C104A90}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {84DA6C54-F140-4518-A1B4-E4CF42117FBD}
Expand Down
8 changes: 3 additions & 5 deletions build/install-dotnet.ps1
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
# Installs .NET Core 2.1, .NET 5 and .NET 6 for CI/CD environment
# Installs .NET 6 and .NET 7 for CI/CD environment
# see: https://docs.microsoft.com/en-us/dotnet/core/tools/dotnet-install-script#examples

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12;

&([scriptblock]::Create((Invoke-WebRequest -UseBasicParsing 'https://dot.net/v1/dotnet-install.ps1'))) -Version 2.1.816
&([scriptblock]::Create((Invoke-WebRequest -UseBasicParsing 'https://dot.net/v1/dotnet-install.ps1'))) -Channel 6.0

&([scriptblock]::Create((Invoke-WebRequest -UseBasicParsing 'https://dot.net/v1/dotnet-install.ps1'))) -Version 5.0.408

&([scriptblock]::Create((Invoke-WebRequest -UseBasicParsing 'https://dot.net/v1/dotnet-install.ps1')))
&([scriptblock]::Create((Invoke-WebRequest -UseBasicParsing 'https://dot.net/v1/dotnet-install.ps1'))) -Channel 7.0
2 changes: 1 addition & 1 deletion examples/RazorPages/RazorPages.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
//
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.FeatureManagement.FeatureFilters;
using Microsoft.FeatureManagement.Mvc;
using System;
using System.Collections.Generic;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<Import Project="..\..\build\Versioning.props" />

<PropertyGroup>
<TargetFrameworks>netstandard2.0;netcoreapp3.1;net5.0;net6.0</TargetFrameworks>
<TargetFrameworks>net6.0;net7.0</TargetFrameworks>
<SignAssembly>true</SignAssembly>
<DelaySign>false</DelaySign>
<AssemblyOriginatorKeyFile>..\..\build\Microsoft.FeatureManagement.snk</AssemblyOriginatorKeyFile>
Expand All @@ -28,17 +28,9 @@
<PackageIconUrl>https://aka.ms/AzureAppConfigurationPackageIcon</PackageIconUrl>
<Copyright>© Microsoft Corporation. All rights reserved.</Copyright>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\Microsoft.FeatureManagement\Microsoft.FeatureManagement.csproj" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
<PackageReference Include="Microsoft.AspNetCore.Mvc.RazorPages" Version="2.1.11" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.TagHelpers" Version="2.1.3" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' != 'netstandard2.0'">
<FrameworkReference Include="Microsoft.AspNetCore.App" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<Import Project="..\..\build\Versioning.props" />

<PropertyGroup>
<TargetFrameworks>netstandard2.0;netcoreapp3.1;net5.0;net6.0</TargetFrameworks>
<TargetFrameworks>netstandard2.0;netstandard2.1</TargetFrameworks>
<SignAssembly>true</SignAssembly>
<DelaySign>false</DelaySign>
<AssemblyOriginatorKeyFile>..\..\build\Microsoft.FeatureManagement.snk</AssemblyOriginatorKeyFile>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
//
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.TestHost;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.FeatureManagement;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using Xunit;

namespace Tests.FeatureManagement.AspNetCore
{
public class FeatureManagementAspNetCore
{
[Fact]
public async Task Integrates()
{
IConfiguration config = new ConfigurationBuilder().AddJsonFile("appsettings.json").Build();

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

services.AddMvcCore(o =>
{
DisableEndpointRouting(o);
o.Filters.AddForFeature<MvcFilter>(Enum.GetName(typeof(Features), Features.ConditionalFeature));
});
})
.Configure(app =>
{
app.UseForFeature(Enum.GetName(typeof(Features), Features.ConditionalFeature), a => a.Use(async (ctx, next) =>
{
ctx.Response.Headers[nameof(RouterMiddleware)] = bool.TrueString;

await next();
}));

app.UseMvc();
}));

IEnumerable<IFeatureFilterMetadata> featureFilters = testServer.Host.Services.GetRequiredService<IEnumerable<IFeatureFilterMetadata>>();

TestFilter testFeatureFilter = (TestFilter)featureFilters.First(f => f is TestFilter);

testFeatureFilter.Callback = _ => Task.FromResult(true);

HttpResponseMessage res = await testServer.CreateClient().GetAsync("");

Assert.True(res.Headers.Contains(nameof(MvcFilter)));
Assert.True(res.Headers.Contains(nameof(RouterMiddleware)));

testFeatureFilter.Callback = _ => Task.FromResult(false);

res = await testServer.CreateClient().GetAsync("");

Assert.False(res.Headers.Contains(nameof(MvcFilter)));
Assert.False(res.Headers.Contains(nameof(RouterMiddleware)));
}

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

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

services.AddMvcCore(o => DisableEndpointRouting(o));
})
.Configure(app => app.UseMvc()));

IEnumerable<IFeatureFilterMetadata> featureFilters = testServer.Host.Services.GetRequiredService<IEnumerable<IFeatureFilterMetadata>>();

TestFilter testFeatureFilter = (TestFilter)featureFilters.First(f => f is TestFilter);

//
// Enable all features
testFeatureFilter.Callback = ctx => Task.FromResult(true);

HttpResponseMessage gateAllResponse = await testServer.CreateClient().GetAsync("gateAll");
HttpResponseMessage gateAnyResponse = await testServer.CreateClient().GetAsync("gateAny");

Assert.Equal(HttpStatusCode.OK, gateAllResponse.StatusCode);
Assert.Equal(HttpStatusCode.OK, gateAnyResponse.StatusCode);

//
// Enable 1/2 features
testFeatureFilter.Callback = ctx => Task.FromResult(ctx.FeatureName == Enum.GetName(typeof(Features), Features.ConditionalFeature));

gateAllResponse = await testServer.CreateClient().GetAsync("gateAll");
gateAnyResponse = await testServer.CreateClient().GetAsync("gateAny");

Assert.Equal(HttpStatusCode.NotFound, gateAllResponse.StatusCode);
Assert.Equal(HttpStatusCode.OK, gateAnyResponse.StatusCode);

//
// Enable no
testFeatureFilter.Callback = ctx => Task.FromResult(false);

gateAllResponse = await testServer.CreateClient().GetAsync("gateAll");
gateAnyResponse = await testServer.CreateClient().GetAsync("gateAny");

Assert.Equal(HttpStatusCode.NotFound, gateAllResponse.StatusCode);
Assert.Equal(HttpStatusCode.NotFound, gateAnyResponse.StatusCode);
}

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

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

services.AddRazorPages();

services.AddMvc(o => DisableEndpointRouting(o));
})
.Configure(app =>
{
app.UseMvc();
}));

IEnumerable<IFeatureFilterMetadata> featureFilters = testServer.Host.Services.GetRequiredService<IEnumerable<IFeatureFilterMetadata>>();

TestFilter testFeatureFilter = (TestFilter)featureFilters.First(f => f is TestFilter);

//
// Enable all features
testFeatureFilter.Callback = ctx => Task.FromResult(true);

HttpResponseMessage gateAllResponse = await testServer.CreateClient().GetAsync("RazorTestAll");
HttpResponseMessage gateAnyResponse = await testServer.CreateClient().GetAsync("RazorTestAny");

Assert.Equal(HttpStatusCode.OK, gateAllResponse.StatusCode);
Assert.Equal(HttpStatusCode.OK, gateAnyResponse.StatusCode);

//
// Enable 1/2 features
testFeatureFilter.Callback = ctx => Task.FromResult(ctx.FeatureName == Enum.GetName(typeof(Features), Features.ConditionalFeature));

gateAllResponse = await testServer.CreateClient().GetAsync("RazorTestAll");
gateAnyResponse = await testServer.CreateClient().GetAsync("RazorTestAny");

Assert.Equal(HttpStatusCode.NotFound, gateAllResponse.StatusCode);
Assert.Equal(HttpStatusCode.OK, gateAnyResponse.StatusCode);

//
// Enable no
testFeatureFilter.Callback = ctx => Task.FromResult(false);

gateAllResponse = await testServer.CreateClient().GetAsync("RazorTestAll");
gateAnyResponse = await testServer.CreateClient().GetAsync("RazorTestAny");

Assert.Equal(HttpStatusCode.NotFound, gateAllResponse.StatusCode);
Assert.Equal(HttpStatusCode.NotFound, gateAnyResponse.StatusCode);
}

private static void DisableEndpointRouting(MvcOptions options)
{
//
// Endpoint routing is disabled by default in .NET Core 2.1 since it didn't exist.
options.EnableEndpointRouting = false;
}
}
}
11 changes: 11 additions & 0 deletions tests/Tests.FeatureManagement.AspNetCore/Features.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
//
namespace Tests.FeatureManagement.AspNetCore
{
enum Features
{
ConditionalFeature,
ConditionalFeature2
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc.Filters;

namespace Tests.FeatureManagement
namespace Tests.FeatureManagement.AspNetCore
{
public class MvcFilter : IAsyncActionFilter
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.FeatureManagement.Mvc;

namespace Tests.FeatureManagement.Pages
namespace Tests.FeatureManagement.AspNetCore.Pages
{
[FeatureGate(Features.ConditionalFeature, Features.ConditionalFeature2)]
public class RazorTestAllModel : PageModel
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
using Microsoft.FeatureManagement;
using Microsoft.FeatureManagement.Mvc;

namespace Tests.FeatureManagement.Pages
namespace Tests.FeatureManagement.AspNetCore.Pages
{
[FeatureGate(RequirementType.Any, Features.ConditionalFeature, Features.ConditionalFeature2)]
public class RazorTestAnyModel : PageModel
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@using Tests.FeatureManagement.AspNetCore
@namespace Tests.FeatureManagement.AspNetCore.Pages
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
using Microsoft.FeatureManagement;
using Microsoft.FeatureManagement.Mvc;

namespace Tests.FeatureManagement
namespace Tests.FeatureManagement.AspNetCore
{
[Route("")]
public class TestController : Controller
Expand Down
32 changes: 32 additions & 0 deletions tests/Tests.FeatureManagement.AspNetCore/TestFilter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
//
using Microsoft.Extensions.Configuration;
using Microsoft.FeatureManagement;
using System;
using System.Threading.Tasks;

namespace Tests.FeatureManagement.AspNetCore
{
class TestFilter : IFeatureFilter, IFilterParametersBinder
{
public Func<IConfiguration, object> ParametersBinderCallback { get; set; }

public Func<FeatureFilterEvaluationContext, Task<bool>> Callback { get; set; }

public object BindParameters(IConfiguration parameters)
{
if (ParametersBinderCallback != null)
{
return ParametersBinderCallback(parameters);
}

return parameters;
}

public Task<bool> EvaluateAsync(FeatureFilterEvaluationContext context)
{
return Callback?.Invoke(context) ?? Task.FromResult(false);
}
}
}
Loading