-
Notifications
You must be signed in to change notification settings - Fork 188
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add unit tests for ConfigureFunctionsWebApplication
- Loading branch information
1 parent
a8ed46b
commit 7f4e1a1
Showing
5 changed files
with
122 additions
and
1 deletion.
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
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
80 changes: 80 additions & 0 deletions
80
test/Worker.Extensions.Http.AspNetCore.Tests/FunctionsHostBuilderExtensionsTests.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,80 @@ | ||
using Microsoft.Azure.Functions.Worker; | ||
using Microsoft.Azure.Functions.Worker.Extensions.Http.AspNetCore; | ||
using Microsoft.Azure.Functions.Worker.Extensions.Http.AspNetCore.AspNetMiddleware; | ||
using Microsoft.Azure.Functions.Worker.Middleware; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.Configuration.Memory; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Hosting; | ||
|
||
namespace Worker.Extensions.Http.AspNetCore.Tests | ||
{ | ||
public class FunctionsHostBuilderExtensionsTests | ||
{ | ||
[Fact] | ||
public void ConfigureFunctionsWebApplication_ShouldConfigureFunctionsWebApplicationWithoutAction() | ||
{ | ||
var underTest = new HostBuilder(); | ||
underTest.ConfigureFunctionsWebApplication(); | ||
var host = underTest.Build(); | ||
VerifyRegistrationOfAspNetCoreIntegrationServices(host); | ||
} | ||
|
||
[Fact] | ||
public void ConfigureFunctionsWebApplication_ShouldConfigureFunctionsWebApplicationWithActionForBuilder() | ||
{ | ||
var underTest = new HostBuilder(); | ||
underTest.ConfigureFunctionsWebApplication(builder => builder.UseMiddleware<TestMiddleware>()); | ||
var host = underTest.Build(); | ||
VerifyRegistrationOfCustomMiddleware(host); | ||
VerifyRegistrationOfAspNetCoreIntegrationServices(host); | ||
} | ||
|
||
[Fact] | ||
public void ConfigureFunctionsWebApplication_ShouldConfigureFunctionsWebApplicationWithActionForContext() | ||
{ | ||
const string expectedConfigValue = "test_config_value"; | ||
const string configKey = "test_config_key"; | ||
|
||
var underTest = new HostBuilder(); | ||
underTest.ConfigureHostConfiguration(builder => | ||
{ | ||
builder.Add(new MemoryConfigurationSource { InitialData = new Dictionary<string, string?> { { configKey, expectedConfigValue } } }); | ||
}); | ||
string? actualConfigValue = null; | ||
|
||
underTest.ConfigureFunctionsWebApplication((context, builder) => | ||
{ | ||
actualConfigValue = context.Configuration.GetValue<string>(configKey); | ||
builder.UseMiddleware<TestMiddleware>(); | ||
}); | ||
var host = underTest.Build(); | ||
|
||
Assert.Equal(expectedConfigValue, actualConfigValue); | ||
VerifyRegistrationOfCustomMiddleware(host); | ||
VerifyRegistrationOfAspNetCoreIntegrationServices(host); | ||
} | ||
|
||
private static void VerifyRegistrationOfCustomMiddleware(IHost host) | ||
{ | ||
Assert.NotNull(host.Services.GetService<TestMiddleware>()); | ||
} | ||
|
||
private static void VerifyRegistrationOfAspNetCoreIntegrationServices(IHost host) | ||
{ | ||
Assert.NotNull(host.Services.GetService<FunctionsEndpointDataSource>()); | ||
Assert.NotNull(host.Services.GetService<FunctionsHttpProxyingMiddleware>()); | ||
var httpCoordinator = host.Services.GetService<IHttpCoordinator>(); | ||
Assert.NotNull(httpCoordinator); | ||
Assert.IsType<DefaultHttpCoordinator>(httpCoordinator); | ||
} | ||
|
||
private class TestMiddleware : IFunctionsWorkerMiddleware | ||
{ | ||
public Task Invoke(FunctionContext context, FunctionExecutionDelegate next) | ||
{ | ||
return Task.CompletedTask; | ||
} | ||
} | ||
} | ||
} |
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 @@ | ||
global using Xunit; |
32 changes: 32 additions & 0 deletions
32
test/Worker.Extensions.Http.AspNetCore.Tests/Worker.Extensions.Http.AspNetCore.Tests.csproj
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,32 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net7.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
|
||
<IsPackable>false</IsPackable> | ||
<IsTestProject>true</IsTestProject> | ||
<SignAssembly>True</SignAssembly> | ||
<AssemblyOriginatorKeyFile>..\..\key.snk</AssemblyOriginatorKeyFile> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.1" /> | ||
<PackageReference Include="Moq" Version="4.18.2" /> | ||
<PackageReference Include="xunit" Version="2.4.2" /> | ||
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5"> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
<PrivateAssets>all</PrivateAssets> | ||
</PackageReference> | ||
<PackageReference Include="coverlet.collector" Version="3.2.0"> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
<PrivateAssets>all</PrivateAssets> | ||
</PackageReference> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\extensions\Worker.Extensions.Http.AspNetCore\src\Worker.Extensions.Http.AspNetCore.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |