From 7f4e1a1925cb11ec341d02da094dd25908a49e89 Mon Sep 17 00:00:00 2001 From: vmcbaptista Date: Wed, 11 Oct 2023 00:01:12 +0100 Subject: [PATCH] add unit tests for ConfigureFunctionsWebApplication --- DotNetWorker.sln | 9 ++- .../src/Properties/AssemblyInfo.cs | 1 + .../FunctionsHostBuilderExtensionsTests.cs | 80 +++++++++++++++++++ .../GlobalUsings.cs | 1 + ...er.Extensions.Http.AspNetCore.Tests.csproj | 32 ++++++++ 5 files changed, 122 insertions(+), 1 deletion(-) create mode 100644 test/Worker.Extensions.Http.AspNetCore.Tests/FunctionsHostBuilderExtensionsTests.cs create mode 100644 test/Worker.Extensions.Http.AspNetCore.Tests/GlobalUsings.cs create mode 100644 test/Worker.Extensions.Http.AspNetCore.Tests/Worker.Extensions.Http.AspNetCore.Tests.csproj diff --git a/DotNetWorker.sln b/DotNetWorker.sln index 2fe0c13d0..5a38a5775 100644 --- a/DotNetWorker.sln +++ b/DotNetWorker.sln @@ -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 @@ -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 @@ -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} diff --git a/extensions/Worker.Extensions.Http.AspNetCore/src/Properties/AssemblyInfo.cs b/extensions/Worker.Extensions.Http.AspNetCore/src/Properties/AssemblyInfo.cs index 982103208..edaa12791 100644 --- a/extensions/Worker.Extensions.Http.AspNetCore/src/Properties/AssemblyInfo.cs +++ b/extensions/Worker.Extensions.Http.AspNetCore/src/Properties/AssemblyInfo.cs @@ -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")] diff --git a/test/Worker.Extensions.Http.AspNetCore.Tests/FunctionsHostBuilderExtensionsTests.cs b/test/Worker.Extensions.Http.AspNetCore.Tests/FunctionsHostBuilderExtensionsTests.cs new file mode 100644 index 000000000..e3e3eac2a --- /dev/null +++ b/test/Worker.Extensions.Http.AspNetCore.Tests/FunctionsHostBuilderExtensionsTests.cs @@ -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()); + 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 { { configKey, expectedConfigValue } } }); + }); + string? actualConfigValue = null; + + underTest.ConfigureFunctionsWebApplication((context, builder) => + { + actualConfigValue = context.Configuration.GetValue(configKey); + builder.UseMiddleware(); + }); + var host = underTest.Build(); + + Assert.Equal(expectedConfigValue, actualConfigValue); + VerifyRegistrationOfCustomMiddleware(host); + VerifyRegistrationOfAspNetCoreIntegrationServices(host); + } + + private static void VerifyRegistrationOfCustomMiddleware(IHost host) + { + Assert.NotNull(host.Services.GetService()); + } + + private static void VerifyRegistrationOfAspNetCoreIntegrationServices(IHost host) + { + Assert.NotNull(host.Services.GetService()); + Assert.NotNull(host.Services.GetService()); + var httpCoordinator = host.Services.GetService(); + Assert.NotNull(httpCoordinator); + Assert.IsType(httpCoordinator); + } + + private class TestMiddleware : IFunctionsWorkerMiddleware + { + public Task Invoke(FunctionContext context, FunctionExecutionDelegate next) + { + return Task.CompletedTask; + } + } + } +} diff --git a/test/Worker.Extensions.Http.AspNetCore.Tests/GlobalUsings.cs b/test/Worker.Extensions.Http.AspNetCore.Tests/GlobalUsings.cs new file mode 100644 index 000000000..8c927eb74 --- /dev/null +++ b/test/Worker.Extensions.Http.AspNetCore.Tests/GlobalUsings.cs @@ -0,0 +1 @@ +global using Xunit; \ No newline at end of file diff --git a/test/Worker.Extensions.Http.AspNetCore.Tests/Worker.Extensions.Http.AspNetCore.Tests.csproj b/test/Worker.Extensions.Http.AspNetCore.Tests/Worker.Extensions.Http.AspNetCore.Tests.csproj new file mode 100644 index 000000000..3d2d0392a --- /dev/null +++ b/test/Worker.Extensions.Http.AspNetCore.Tests/Worker.Extensions.Http.AspNetCore.Tests.csproj @@ -0,0 +1,32 @@ + + + + net7.0 + enable + enable + + false + true + True + ..\..\key.snk + + + + + + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + + + + + +