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
8 changes: 4 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ jobs:
8.0.x
9.0.x
10.0.x
- name: Build
run: dotnet build src --configuration Release
- name: Tests
run: dotnet test src --configuration Release --no-build
- name: Build
run: dotnet build src/ServiceComposer.AspNetCore.Testing.sln --configuration Release
- name: Tests
run: dotnet test src/ServiceComposer.AspNetCore.Testing.sln --configuration Release --no-build
- name: Upload packages
if: matrix.name == 'Linux'
uses: actions/upload-artifact@v6.0.0
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
[assembly: System.Reflection.AssemblyMetadata("RepositoryUrl", "https://github.com/ServiceComposer/ServiceComposer.AspNetCore.Testing")]
[assembly: System.Runtime.Versioning.TargetFramework(".NETCoreApp,Version=v10.0", FrameworkDisplayName=".NET 10.0")]
namespace ServiceComposer.AspNetCore.Testing
{
public class SelfContainedWebApplicationFactoryWithHost<TEntryPoint> : Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory<TEntryPoint>
where TEntryPoint : class
{
public SelfContainedWebApplicationFactoryWithHost(System.Action<Microsoft.Extensions.DependencyInjection.IServiceCollection> configureServices, System.Action<Microsoft.AspNetCore.Builder.IApplicationBuilder> configure, string[] args = null) { }
public System.Action<Microsoft.Extensions.Hosting.IHostBuilder> HostBuilderCustomization { get; set; }
public System.Action<Microsoft.AspNetCore.Hosting.IWebHostBuilder> WebHostBuilderCustomization { get; set; }
protected override Microsoft.Extensions.Hosting.IHostBuilder CreateHostBuilder() { }
}
public class SelfContainedWebApplicationFactoryWithWebHost<TEntryPoint> : Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory<TEntryPoint>
where TEntryPoint : class
{
public SelfContainedWebApplicationFactoryWithWebHost(System.Action<Microsoft.Extensions.DependencyInjection.IServiceCollection> configureServices, System.Action<Microsoft.AspNetCore.Builder.IApplicationBuilder> configure) { }
public SelfContainedWebApplicationFactoryWithWebHost(System.Action<Microsoft.AspNetCore.Hosting.WebHostBuilderContext, Microsoft.Extensions.DependencyInjection.IServiceCollection> configureServices, System.Action<Microsoft.AspNetCore.Hosting.WebHostBuilderContext, Microsoft.AspNetCore.Builder.IApplicationBuilder> configure) { }
public System.Action<Microsoft.AspNetCore.Hosting.IWebHostBuilder> BuilderCustomization { get; set; }
protected override void ConfigureWebHost(Microsoft.AspNetCore.Hosting.IWebHostBuilder builder) { }
protected override Microsoft.AspNetCore.Hosting.IWebHostBuilder CreateWebHostBuilder() { }
}
public class WebApplicationFactoryWithWebHost<TStartup> : Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory<TStartup>
where TStartup : class
{
public WebApplicationFactoryWithWebHost() { }
public System.Action<Microsoft.AspNetCore.Hosting.IWebHostBuilder> BuilderCustomization { get; set; }
protected override void ConfigureWebHost(Microsoft.AspNetCore.Hosting.IWebHostBuilder builder) { }
protected override Microsoft.AspNetCore.Hosting.IWebHostBuilder CreateWebHostBuilder() { }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using PublicApiGenerator;
using VerifyXunit;

namespace ServiceComposer.AspNetCore.Testing.Tests;

public class PublicApiApprovalTests
{
[Fact]
public Task Public_api_is_approved()
{
var publicApi = ApiGenerator.GeneratePublicApi(typeof(WebApplicationFactoryWithWebHost<>).Assembly);
return Verifier.Verify(publicApi);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

namespace ServiceComposer.AspNetCore.Testing.Tests;

public class SelfContainedWebApplicationFactoryTests
{
[Fact]
public async Task SelfContainedFactoryWithHost_serves_request()
{
await using var factory = new SelfContainedWebApplicationFactoryWithHost<TestEntryPoint>(
services => services.AddSingleton(new Message("host")),
app => app.Run(async context =>
{
var message = context.RequestServices.GetRequiredService<Message>();
await context.Response.WriteAsync(message.Value);
}));

var client = factory.CreateClient();
var response = await client.GetStringAsync("/");

Assert.Equal("host", response);
}

[Fact]
public async Task SelfContainedFactoryWithWebHost_invokes_builder_customization()
{
var builderCustomizationCalled = false;
await using var factory = new SelfContainedWebApplicationFactoryWithWebHost<TestEntryPoint>(
services => services.AddSingleton(new Message("webhost")),
app => app.Run(async context =>
{
var message = context.RequestServices.GetRequiredService<Message>();
await context.Response.WriteAsync(message.Value);
}))
{
BuilderCustomization = _ => builderCustomizationCalled = true
};

var client = factory.CreateClient();
var response = await client.GetStringAsync("/");

Assert.Equal("webhost", response);
Assert.True(builderCustomizationCalled);
}

[Fact]
public async Task WebApplicationFactoryWithWebHost_invokes_builder_customization()
{
var builderCustomizationCalled = false;
await using var factory = new WebApplicationFactoryWithWebHost<Startup>
{
BuilderCustomization = _ => builderCustomizationCalled = true
};

var client = factory.CreateClient();
var response = await client.GetStringAsync("/");

Assert.Equal("startup", response);
Assert.True(builderCustomizationCalled);
}

class TestEntryPoint;

class Startup
{
public void Configure(IApplicationBuilder app)
{
app.Run(context => context.Response.WriteAsync("startup"));
}
}

record Message(string Value);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.4" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.14.1" />
<PackageReference Include="PublicApiGenerator" Version="11.5.4" />
<PackageReference Include="Verify.Xunit" Version="31.12.5" />
<PackageReference Include="xunit" Version="2.9.3" />
<PackageReference Include="xunit.runner.visualstudio" Version="3.1.4" />
</ItemGroup>

<ItemGroup>
<Using Include="Xunit" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\ServiceComposer.AspNetCore.Testing\ServiceComposer.AspNetCore.Testing.csproj" />
</ItemGroup>

</Project>
6 changes: 6 additions & 0 deletions src/ServiceComposer.AspNetCore.Testing.sln
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ VisualStudioVersion = 15.0.26730.16
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ServiceComposer.AspNetCore.Testing", "ServiceComposer.AspNetCore.Testing\ServiceComposer.AspNetCore.Testing.csproj", "{37843543-9921-4A2E-A62E-EC64E08A8BA6}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ServiceComposer.AspNetCore.Testing.Tests", "ServiceComposer.AspNetCore.Testing.Tests\ServiceComposer.AspNetCore.Testing.Tests.csproj", "{83B393CD-C479-4306-B2B5-56EB3B75170E}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -14,6 +16,10 @@ Global
{37843543-9921-4A2E-A62E-EC64E08A8BA6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{37843543-9921-4A2E-A62E-EC64E08A8BA6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{37843543-9921-4A2E-A62E-EC64E08A8BA6}.Release|Any CPU.Build.0 = Release|Any CPU
{83B393CD-C479-4306-B2B5-56EB3B75170E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{83B393CD-C479-4306-B2B5-56EB3B75170E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{83B393CD-C479-4306-B2B5-56EB3B75170E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{83B393CD-C479-4306-B2B5-56EB3B75170E}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
Loading