Skip to content

Commit

Permalink
add unit tests for ConfigureFunctionsWebApplication
Browse files Browse the repository at this point in the history
  • Loading branch information
vmcbaptista authored and fabiocav committed Oct 17, 2023
1 parent a8ed46b commit 7f4e1a1
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 1 deletion.
9 changes: 8 additions & 1 deletion DotNetWorker.sln
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Worker.Extensions.Tests", "
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AspNetIntegration", "samples\AspNetIntegration\AspNetIntegration.csproj", "{D2F67410-9933-42E8-B04A-E17634D83A30}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DependentAssemblyWithFunctions", "test\DependentAssemblyWithFunctions\DependentAssemblyWithFunctions.csproj", "{AB6E1E7A-0D2C-4086-9487-566887C1E753}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DependentAssemblyWithFunctions", "test\DependentAssemblyWithFunctions\DependentAssemblyWithFunctions.csproj", "{AB6E1E7A-0D2C-4086-9487-566887C1E753}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Worker.Extensions.Http.AspNetCore.Tests", "test\Worker.Extensions.Http.AspNetCore.Tests\Worker.Extensions.Http.AspNetCore.Tests.csproj", "{8970EB64-E6B1-465C-BBBC-CE3D9F257BBF}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down Expand Up @@ -332,6 +334,10 @@ Global
{AB6E1E7A-0D2C-4086-9487-566887C1E753}.Debug|Any CPU.Build.0 = Debug|Any CPU
{AB6E1E7A-0D2C-4086-9487-566887C1E753}.Release|Any CPU.ActiveCfg = Release|Any CPU
{AB6E1E7A-0D2C-4086-9487-566887C1E753}.Release|Any CPU.Build.0 = Release|Any CPU
{8970EB64-E6B1-465C-BBBC-CE3D9F257BBF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8970EB64-E6B1-465C-BBBC-CE3D9F257BBF}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8970EB64-E6B1-465C-BBBC-CE3D9F257BBF}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8970EB64-E6B1-465C-BBBC-CE3D9F257BBF}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -390,6 +396,7 @@ Global
{17BDCE12-6964-4B87-B2AC-68CE270A3E9A} = {FD7243E4-BF18-43F8-8744-BA1D17ACF378}
{D2F67410-9933-42E8-B04A-E17634D83A30} = {9D6603BD-7EA2-4D11-A69C-0D9E01317FD6}
{AB6E1E7A-0D2C-4086-9487-566887C1E753} = {B5821230-6E0A-4535-88A9-ED31B6F07596}
{8970EB64-E6B1-465C-BBBC-CE3D9F257BBF} = {AA4D318D-101B-49E7-A4EC-B34165AEDB33}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {497D2ED4-A13E-4BCA-8D29-F30CA7D0EA4A}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
using System.Runtime.CompilerServices;

[assembly: InternalsVisibleTo("Microsoft.Azure.Functions.Worker.Tests, PublicKey=00240000048000009400000006020000002400005253413100040000010001005148be37ac1d9f58bd40a2e472c9d380d635b6048278f7d47480b08c928858f0f7fe17a6e4ce98da0e7a7f0b8c308aecd9e9b02d7e9680a5b5b75ac7773cec096fbbc64aebd429e77cb5f89a569a79b28e9c76426783f624b6b70327eb37341eb498a2c3918af97c4860db6cdca4732787150841e395a29cfacb959c1fd971c1")]
[assembly: InternalsVisibleTo("Worker.Extensions.Http.AspNetCore.Tests, PublicKey=00240000048000009400000006020000002400005253413100040000010001005148be37ac1d9f58bd40a2e472c9d380d635b6048278f7d47480b08c928858f0f7fe17a6e4ce98da0e7a7f0b8c308aecd9e9b02d7e9680a5b5b75ac7773cec096fbbc64aebd429e77cb5f89a569a79b28e9c76426783f624b6b70327eb37341eb498a2c3918af97c4860db6cdca4732787150841e395a29cfacb959c1fd971c1")]
[assembly: InternalsVisibleTo("DynamicProxyGenAssembly2, PublicKey=0024000004800000940000000602000000240000525341310004000001000100c547cac37abd99c8db225ef2f6c8a3602f3b3606cc9891605d02baa56104f4cfc0734aa39b93bf7852f7d9266654753cc297e7d2edfe0bac1cdcf9f717241550e0a7b191195b7667bb4f64bcb8e2121380fd1d9d46ad2d92d2d15605093924cceaf74c4861eff62abf69b9291ed0a340e113be11e6a7d3113e92484cf7045cc7")]
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;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
global using Xunit;
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>

0 comments on commit 7f4e1a1

Please sign in to comment.