-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add MotorHostApplicationFactory to make IMotorStartup testable
Fixes #61 Signed-off-by: jan.jansen <jan.jansen@gdata.de>
- Loading branch information
Showing
9 changed files
with
202 additions
and
60 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,30 @@ | ||
<Project> | ||
|
||
<PropertyGroup> | ||
<Version>0.4.0</Version> | ||
<LangVersion>9</LangVersion> | ||
<Nullable>enable</Nullable> | ||
<WarningsAsErrors>CS8600;CS8602;CS8625;CS8618;CS8604;CS8601</WarningsAsErrors> | ||
</PropertyGroup> | ||
<PropertyGroup> | ||
<Version>0.4.0</Version> | ||
<LangVersion>9</LangVersion> | ||
<Nullable>enable</Nullable> | ||
<WarningsAsErrors>CS8600;CS8602;CS8625;CS8618;CS8604;CS8601</WarningsAsErrors> | ||
</PropertyGroup> | ||
|
||
<PropertyGroup> | ||
<IncludeSymbols>true</IncludeSymbols> | ||
<PublishRepositoryUrl>true</PublishRepositoryUrl> | ||
</PropertyGroup> | ||
<PropertyGroup> | ||
<IncludeSymbols>true</IncludeSymbols> | ||
<PublishRepositoryUrl>true</PublishRepositoryUrl> | ||
</PropertyGroup> | ||
|
||
<PropertyGroup> | ||
<PackageLicenseExpression>MIT</PackageLicenseExpression> | ||
<Description>Motor.NET is a microservice framework based on Microsoft.Extensions.Hosting.</Description> | ||
<Copyright>Copyright (C) 2020 G DATA CyberDefense AG</Copyright> | ||
<Company>G DATA CyberDefense AG</Company> | ||
<RepositoryUrl>https://github.com/GDATASoftwareAG/motornet</RepositoryUrl> | ||
<PackageProjectUrl>https://github.com/GDATASoftwareAG/motornet</PackageProjectUrl> | ||
<PackageTags>microservice hosting rabbitmq kafka http logging tracing</PackageTags> | ||
<PackageIcon>Icon_Motor_NET_128.png</PackageIcon> | ||
</PropertyGroup> | ||
<PropertyGroup> | ||
<PackageLicenseExpression>MIT</PackageLicenseExpression> | ||
<Description>Motor.NET is a microservice framework based on Microsoft.Extensions.Hosting.</Description> | ||
<Copyright>Copyright (C) 2020 G DATA CyberDefense AG</Copyright> | ||
<Company>G DATA CyberDefense AG</Company> | ||
<RepositoryUrl>https://github.com/GDATASoftwareAG/motornet</RepositoryUrl> | ||
<PackageProjectUrl>https://github.com/GDATASoftwareAG/motornet</PackageProjectUrl> | ||
<PackageTags>microservice hosting rabbitmq kafka http logging tracing</PackageTags> | ||
<PackageIcon>Icon_Motor_NET_128.png</PackageIcon> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<None Include="../../rsc/Icon_Motor_NET_128.png" Pack="true" PackagePath="/" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<None Include="../../rsc/Icon_Motor_NET_128.png" Pack="true" PackagePath="/" /> | ||
</ItemGroup> | ||
|
||
</Project> |
10 changes: 9 additions & 1 deletion
10
src/Motor.Extensions.TestUtilities/Motor.Extensions.TestUtilities.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
43 changes: 43 additions & 0 deletions
43
src/Motor.Extensions.TestUtilities/MotorHostApplicationFactory.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,43 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Net.Http; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Hosting; | ||
using Microsoft.AspNetCore.TestHost; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Hosting; | ||
using Motor.Extensions.Utilities; | ||
using Motor.Extensions.Utilities.Abstractions; | ||
using Xunit; | ||
|
||
namespace Motor.Extensions.TestUtilities | ||
{ | ||
public class MotorHostApplicationFactory<TStartup> : IAsyncLifetime where TStartup : IMotorStartup | ||
{ | ||
private TestServer? server; | ||
|
||
public async Task InitializeAsync() | ||
{ | ||
var useSetting = new Dictionary<string, string> | ||
{ | ||
{MotorHostDefaults.EnablePrometheusEndpointKey, false.ToString()} | ||
}; | ||
|
||
var webHostBuilder = new WebHostBuilder(); | ||
MotorHostBuilderHelper.ConfigureWebHost(webHostBuilder, s => useSetting.GetValueOrDefault(s), typeof(TStartup)); | ||
webHostBuilder.ConfigureServices(collection => | ||
{ | ||
collection.AddHealthChecks(); | ||
}); | ||
|
||
server = new TestServer(webHostBuilder); | ||
} | ||
|
||
public HttpClient CreateClient() => server?.CreateClient()!; | ||
|
||
public async Task DisposeAsync() | ||
{ | ||
server?.Dispose(); | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
using System; | ||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.AspNetCore.Hosting; | ||
using Motor.Extensions.Diagnostics.Metrics; | ||
using Motor.Extensions.Utilities.Abstractions; | ||
|
||
namespace Motor.Extensions.Utilities | ||
{ | ||
public static class MotorHostBuilderHelper | ||
{ | ||
public static void ConfigureWebHost(IWebHostBuilder builder, Func<string, string?> getSetting, Type? motorStartup) | ||
{ | ||
IMotorStartup? startup = null; | ||
if (motorStartup != null) startup = Activator.CreateInstance(motorStartup) as IMotorStartup; | ||
|
||
var urls = builder.GetSetting(WebHostDefaults.ServerUrlsKey); | ||
const string defaultUrl = "http://0.0.0.0:9110"; | ||
if (string.IsNullOrEmpty(urls)) | ||
builder.UseUrls(defaultUrl); | ||
else if (!urls.Contains(defaultUrl)) builder.UseUrls($"{urls};{defaultUrl}"); | ||
|
||
builder.Configure((context, applicationBuilder) => | ||
{ | ||
applicationBuilder.UseRouting(); | ||
var enablePrometheusSetting = getSetting(MotorHostDefaults.EnablePrometheusEndpointKey); | ||
if (string.IsNullOrEmpty(enablePrometheusSetting) || bool.Parse(enablePrometheusSetting)) | ||
applicationBuilder.UsePrometheusServer(); | ||
startup?.Configure(context, applicationBuilder); | ||
applicationBuilder.UseEndpoints(endpoints => { endpoints.MapHealthChecks("/health"); }); | ||
}); | ||
if (motorStartup != null) | ||
builder.UseSetting(WebHostDefaults.ApplicationKey, motorStartup.Assembly.GetName().Name); | ||
|
||
builder.ConfigureServices((context, collection) => | ||
{ | ||
startup?.ConfigureServices(context, collection); | ||
}); | ||
} | ||
} | ||
} |
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
77 changes: 77 additions & 0 deletions
77
test/Motor.Extensions.Utilities_IntegrationTest/DefaultHostingTest.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,77 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.AspNetCore.Hosting; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Motor.Extensions.TestUtilities; | ||
using Motor.Extensions.Utilities.Abstractions; | ||
using Xunit; | ||
|
||
namespace Motor.Extensions.Utilities_IntegrationTest | ||
{ | ||
public class TestService | ||
{ | ||
} | ||
|
||
public class HomeController : Controller | ||
{ | ||
private TestService service; | ||
|
||
public HomeController(TestService testService) | ||
{ | ||
service = testService ?? throw new ArgumentNullException(); | ||
} | ||
|
||
// | ||
// GET: / | ||
public string Index() | ||
{ | ||
return "This is my default action..."; | ||
} | ||
} | ||
|
||
public class TestStartup : IMotorStartup | ||
{ | ||
public void ConfigureServices(WebHostBuilderContext context, IServiceCollection services) | ||
{ | ||
services.AddControllers(); | ||
services.AddTransient<TestService>(); | ||
} | ||
|
||
public void Configure(WebHostBuilderContext context, IApplicationBuilder app) | ||
{ | ||
app.UseEndpoints(endpoints => | ||
{ | ||
endpoints.MapControllerRoute( | ||
name: "default", | ||
pattern: "{controller=Home}/{action=Index}/{id?}"); | ||
}); | ||
} | ||
} | ||
|
||
|
||
public class DefaultHostingTest : IClassFixture<MotorHostApplicationFactory<TestStartup>> | ||
{ | ||
private readonly MotorHostApplicationFactory<TestStartup> _factory; | ||
|
||
public DefaultHostingTest(MotorHostApplicationFactory<TestStartup> factory) | ||
{ | ||
_factory = factory; | ||
} | ||
|
||
[Theory] | ||
[InlineData("/")] | ||
public async Task Get_EndpointsReturnSuccessAndCorrectContentType(string url) | ||
{ | ||
// Arrange | ||
var client = _factory.CreateClient(); | ||
|
||
// Act | ||
var response = await client.GetAsync(url); | ||
|
||
// Assert | ||
response.EnsureSuccessStatusCode(); // Status Code 200-299 | ||
} | ||
} | ||
} |
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