generated from codebeltnet/dotnet-new-classlib
-
Notifications
You must be signed in to change notification settings - Fork 1
V10.0.0/support for hostapplicationbuilder #30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 12 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
7c86695
:truck: rename to IEnvironmentTest
gimlichael 3de046c
:truck: renamed to HostTest and HostTestFactory
gimlichael c400c75
:boom: removed due to redudant property on IHost (Services)
gimlichael def4c42
:boom: IGenericHostTest is now IHostTest
gimlichael 50666d8
:building_construction: refactored to more confined responsibilities
gimlichael 8db8a90
:sparkles: added support for IHostApplicationBuilder (to complement I…
gimlichael 88e7f4a
:memo: rewording but connected to 50666d880
gimlichael c06647a
:arrow_up: bump dependencies
gimlichael 9bd5009
:building_construction: refactored for consistency (WebHostTest* and …
gimlichael 72f39e6
:sparkles: added support for IHostApplicationBuilder (to complement I…
gimlichael fa324b6
:memo: added additional seealso doc to connect with 8db8a90
gimlichael cdd4d42
:white_check_mark: updated test to reflect changes
gimlichael 6107445
docs
gimlichael c0aa874
:package: updated NuGet package definition
gimlichael c95fb3c
:speech_balloon: updated community health pages
gimlichael File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
24 changes: 0 additions & 24 deletions
24
src/Codebelt.Extensions.Xunit.Hosting.AspNetCore/AspNetCoreHostFixtureExtensions.cs
This file was deleted.
Oops, something went wrong.
20 changes: 0 additions & 20 deletions
20
src/Codebelt.Extensions.Xunit.Hosting.AspNetCore/BlockingAspNetCoreHostFixture.cs
This file was deleted.
Oops, something went wrong.
19 changes: 19 additions & 0 deletions
19
src/Codebelt.Extensions.Xunit.Hosting.AspNetCore/BlockingWebHostFixture.cs
This file contains hidden or 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,19 @@ | ||
| using Microsoft.Extensions.Hosting; | ||
|
|
||
| namespace Codebelt.Extensions.Xunit.Hosting.AspNetCore | ||
| { | ||
| /// <summary> | ||
| /// Extends the default implementation of the <see cref="IWebHostFixture"/> interface to be synchronous e.g., blocking where exceptions can be captured. | ||
| /// </summary> | ||
| /// <seealso cref="WebHostFixture" /> | ||
| public class BlockingWebHostFixture : WebHostFixture | ||
| { | ||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="BlockingWebHostFixture"/> class. | ||
| /// </summary> | ||
| public BlockingWebHostFixture() | ||
| { | ||
| HostRunnerCallback = host => host.Run(); | ||
| } | ||
| } | ||
| } |
26 changes: 26 additions & 0 deletions
26
src/Codebelt.Extensions.Xunit.Hosting.AspNetCore/HostBuilderApplicationExtensions.cs
This file contains hidden or 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,26 @@ | ||
| using System; | ||
| using Microsoft.AspNetCore.Builder; | ||
| using Microsoft.Extensions.Hosting; | ||
|
|
||
| namespace Codebelt.Extensions.Xunit.Hosting.AspNetCore | ||
| { | ||
| /// <summary> | ||
| /// Provides extension methods for <see cref="IHostApplicationBuilder"/>. | ||
| /// </summary> | ||
| public static class HostBuilderApplicationExtensions | ||
| { | ||
| /// <summary> | ||
| /// Converts an <see cref="IHostApplicationBuilder"/> to an <see cref="IHostBuilder"/>. | ||
| /// </summary> | ||
| /// <param name="builder">The <see cref="IHostApplicationBuilder"/> to convert.</param> | ||
| /// <returns>The <see cref="IHostBuilder"/> instance.</returns> | ||
| /// <exception cref="ArgumentException"> | ||
| /// <paramref name="builder"/> is not a <see cref="WebApplicationBuilder"/>. | ||
| /// </exception> | ||
| public static IHostBuilder ToHostBuilder(this IHostApplicationBuilder builder) | ||
| { | ||
| if (builder is WebApplicationBuilder webAppBuilder) { return webAppBuilder.Host; } | ||
| throw new ArgumentException($"The provided IHostApplicationBuilder is not a {nameof(WebApplicationBuilder)}.", nameof(builder)); | ||
|
Check warning on line 23 in src/Codebelt.Extensions.Xunit.Hosting.AspNetCore/HostBuilderApplicationExtensions.cs
|
||
| } | ||
| } | ||
| } | ||
This file contains hidden or 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
19 changes: 19 additions & 0 deletions
19
src/Codebelt.Extensions.Xunit.Hosting.AspNetCore/IMinimalWebHostFixture.cs
This file contains hidden or 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,19 @@ | ||
| using System; | ||
| using Microsoft.AspNetCore.Builder; | ||
|
|
||
| namespace Codebelt.Extensions.Xunit.Hosting.AspNetCore | ||
| { | ||
| /// <summary> | ||
| /// Provides a way to use Microsoft Dependency Injection in unit tests (minimal style). | ||
| /// </summary> | ||
| /// <seealso cref="IMinimalHostFixture" /> | ||
| /// <seealso cref="IPipelineTest" /> | ||
| public interface IMinimalWebHostFixture : IMinimalHostFixture, IPipelineTest | ||
| { | ||
| /// <summary> | ||
| /// Gets or sets the delegate that configures the HTTP request pipeline. | ||
| /// </summary> | ||
| /// <value>The delegate that configures the HTTP request pipeline.</value> | ||
| Action<IApplicationBuilder> ConfigureApplicationCallback { get; set; } | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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
72 changes: 72 additions & 0 deletions
72
src/Codebelt.Extensions.Xunit.Hosting.AspNetCore/Internal/MinimalWebHostTest.cs
This file contains hidden or 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,72 @@ | ||
| using System; | ||
| using Microsoft.AspNetCore.Builder; | ||
| using Microsoft.Extensions.DependencyInjection; | ||
| using Microsoft.Extensions.Hosting; | ||
|
|
||
| namespace Codebelt.Extensions.Xunit.Hosting.AspNetCore.Internal | ||
| { | ||
| internal sealed class MinimalWebHostTest : MinimalWebHostTest<IMinimalWebHostFixture> | ||
| { | ||
| private readonly Action<IApplicationBuilder> _pipelineConfigurator; | ||
| private readonly Action<IServiceCollection> _serviceConfigurator; | ||
| private readonly Action<HostBuilderContext, IApplicationBuilder> _pipelineConfiguratorWithContext; | ||
| private readonly Action<HostBuilderContext, IServiceCollection> _serviceConfiguratorWithContext; | ||
| private readonly Action<IHostApplicationBuilder> _hostConfigurator; | ||
| private HostBuilderContext _hostBuilderContext; | ||
|
|
||
| internal MinimalWebHostTest(Action<IServiceCollection> serviceConfigurator, Action<IApplicationBuilder> pipelineConfigurator, Action<IHostApplicationBuilder> hostConfigurator, IMinimalWebHostFixture hostFixture) : base(true, hostFixture, callerType: pipelineConfigurator?.Target?.GetType() ?? serviceConfigurator?.Target?.GetType() ?? hostConfigurator?.Target?.GetType()) | ||
| { | ||
| _serviceConfigurator = serviceConfigurator; | ||
| _pipelineConfigurator = pipelineConfigurator; | ||
| _hostConfigurator = hostConfigurator; | ||
| InitializeHostFixture(hostFixture); | ||
| } | ||
|
|
||
| internal MinimalWebHostTest(Action<HostBuilderContext, IServiceCollection> serviceConfigurator, Action<HostBuilderContext, IApplicationBuilder> pipelineConfigurator, Action<IHostApplicationBuilder> hostConfigurator, IMinimalWebHostFixture hostFixture) : base(true, hostFixture, callerType: pipelineConfigurator?.Target?.GetType() ?? serviceConfigurator?.Target?.GetType() ?? hostConfigurator?.Target?.GetType()) | ||
| { | ||
| _serviceConfiguratorWithContext = serviceConfigurator; | ||
| _pipelineConfiguratorWithContext = pipelineConfigurator; | ||
| _hostConfigurator = hostConfigurator; | ||
| InitializeHostFixture(hostFixture); | ||
| } | ||
|
|
||
| private void InitializeHostFixture(IMinimalWebHostFixture hostFixture) | ||
| { | ||
| if (!hostFixture.HasValidState()) | ||
| { | ||
| hostFixture.ConfigureHostCallback = ConfigureHost; | ||
| hostFixture.ConfigureCallback = Configure; | ||
| hostFixture.ConfigureApplicationCallback = ConfigureApplication; | ||
| hostFixture.ConfigureHost(this); | ||
| } | ||
| Host = hostFixture.Host; | ||
| Application = hostFixture.Application; | ||
| Configure(hostFixture.Configuration, hostFixture.Environment); | ||
| } | ||
|
|
||
| public override void ConfigureApplication(IApplicationBuilder app) | ||
| { | ||
| _pipelineConfigurator?.Invoke(app); | ||
| _pipelineConfiguratorWithContext?.Invoke(Tweaker.Adjust(_hostBuilderContext, hbc => | ||
| { | ||
| hbc.Configuration = Configuration; | ||
| hbc.HostingEnvironment = Environment; | ||
| return hbc; | ||
| }), app); | ||
| } | ||
|
|
||
| protected override void ConfigureHost(IHostApplicationBuilder hb) | ||
| { | ||
| _hostBuilderContext = new HostBuilderContext(hb.Properties); | ||
| _hostConfigurator?.Invoke(hb); | ||
| _serviceConfigurator?.Invoke(hb.Services); | ||
| _serviceConfiguratorWithContext?.Invoke(Tweaker.Adjust(_hostBuilderContext, hbc => | ||
| { | ||
| hbc.Configuration = hb.Configuration; | ||
| hbc.HostingEnvironment = hb.Environment; | ||
| return hbc; | ||
| }), hb.Services); | ||
| hb.Services.AddFakeHttpContextAccessor(); | ||
| } | ||
| } | ||
| } | ||
77 changes: 77 additions & 0 deletions
77
src/Codebelt.Extensions.Xunit.Hosting.AspNetCore/Internal/WebHostTest.cs
This file contains hidden or 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 Microsoft.AspNetCore.Builder; | ||
| using Microsoft.Extensions.DependencyInjection; | ||
| using Microsoft.Extensions.Hosting; | ||
|
|
||
| namespace Codebelt.Extensions.Xunit.Hosting.AspNetCore.Internal | ||
| { | ||
| internal sealed class WebHostTest : WebHostTest<IWebHostFixture> | ||
| { | ||
| private readonly Action<IApplicationBuilder> _pipelineConfigurator; | ||
| private readonly Action<IServiceCollection> _serviceConfigurator; | ||
| private readonly Action<HostBuilderContext, IApplicationBuilder> _pipelineConfiguratorWithContext; | ||
| private readonly Action<HostBuilderContext, IServiceCollection> _serviceConfiguratorWithContext; | ||
| private readonly Action<IHostBuilder> _hostConfigurator; | ||
| private HostBuilderContext _hostBuilderContext; | ||
|
|
||
| internal WebHostTest(Action<IServiceCollection> serviceConfigurator, Action<IApplicationBuilder> pipelineConfigurator, Action<IHostBuilder> hostConfigurator, IWebHostFixture hostFixture) : base(true, hostFixture, callerType: pipelineConfigurator?.Target?.GetType() ?? serviceConfigurator?.Target?.GetType() ?? hostConfigurator?.Target?.GetType()) | ||
| { | ||
| _serviceConfigurator = serviceConfigurator; | ||
| _pipelineConfigurator = pipelineConfigurator; | ||
| _hostConfigurator = hostConfigurator; | ||
| InitializeHostFixture(hostFixture); | ||
| } | ||
|
|
||
| internal WebHostTest(Action<HostBuilderContext, IServiceCollection> serviceConfigurator, Action<HostBuilderContext, IApplicationBuilder> pipelineConfigurator, Action<IHostBuilder> hostConfigurator, IWebHostFixture hostFixture) : base(true, hostFixture, callerType: pipelineConfigurator?.Target?.GetType() ?? serviceConfigurator?.Target?.GetType() ?? hostConfigurator?.Target?.GetType()) | ||
| { | ||
| _serviceConfiguratorWithContext = serviceConfigurator; | ||
| _pipelineConfiguratorWithContext = pipelineConfigurator; | ||
| _hostConfigurator = hostConfigurator; | ||
| InitializeHostFixture(hostFixture); | ||
| } | ||
|
|
||
| private void InitializeHostFixture(IWebHostFixture hostFixture) | ||
| { | ||
| if (!hostFixture.HasValidState()) | ||
| { | ||
| hostFixture.ConfigureHostCallback = ConfigureHost; | ||
| hostFixture.ConfigureCallback = Configure; | ||
| hostFixture.ConfigureServicesCallback = ConfigureServices; | ||
| hostFixture.ConfigureApplicationCallback = ConfigureApplication; | ||
| hostFixture.ConfigureHost(this); | ||
| } | ||
| Host = hostFixture.Host; | ||
| Application = hostFixture.Application; | ||
| Configure(hostFixture.Configuration, hostFixture.Environment); | ||
| } | ||
|
|
||
| public override void ConfigureApplication(IApplicationBuilder app) | ||
| { | ||
| _pipelineConfigurator?.Invoke(app); | ||
| _pipelineConfiguratorWithContext?.Invoke(Tweaker.Adjust(_hostBuilderContext, hbc => | ||
| { | ||
| hbc.Configuration = Configuration; | ||
| hbc.HostingEnvironment = Environment; | ||
| return hbc; | ||
| }), app); | ||
| } | ||
|
|
||
| protected override void ConfigureHost(IHostBuilder hb) | ||
| { | ||
| _hostBuilderContext = new HostBuilderContext(hb.Properties); | ||
| _hostConfigurator?.Invoke(hb); | ||
| } | ||
|
|
||
| public override void ConfigureServices(IServiceCollection services) | ||
| { | ||
| _serviceConfigurator?.Invoke(services); | ||
| _serviceConfiguratorWithContext?.Invoke(Tweaker.Adjust(_hostBuilderContext, hbc => | ||
| { | ||
| hbc.Configuration = Configuration; | ||
| hbc.HostingEnvironment = Environment; | ||
| return hbc; | ||
| }), services); | ||
| services.AddFakeHttpContextAccessor(); | ||
| } | ||
| } | ||
| } |
76 changes: 76 additions & 0 deletions
76
src/Codebelt.Extensions.Xunit.Hosting.AspNetCore/MinimalWebHostFixture.cs
This file contains hidden or 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,76 @@ | ||
| using System; | ||
| using Microsoft.AspNetCore.Builder; | ||
| using Microsoft.AspNetCore.TestHost; | ||
| using Microsoft.Extensions.Hosting; | ||
|
|
||
| namespace Codebelt.Extensions.Xunit.Hosting.AspNetCore | ||
| { | ||
| /// <summary> | ||
| /// Provides a default implementation of the <see cref="IMinimalWebHostFixture"/> interface. | ||
| /// </summary> | ||
| /// <seealso cref="MinimalHostFixture"/> | ||
| /// <seealso cref="IMinimalWebHostFixture" /> | ||
| /// <remarks>This is the "modern" minimal style implementation of <see cref="WebHostFixture"/>.</remarks> | ||
| public class MinimalWebHostFixture : MinimalHostFixture, IMinimalWebHostFixture | ||
| { | ||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="MinimalWebHostFixture"/> class. | ||
| /// </summary> | ||
| public MinimalWebHostFixture() | ||
| { | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Creates and configures the <see cref="IHost" /> of this instance. | ||
| /// </summary> | ||
| /// <param name="hostTest">The object that inherits from <see cref="MinimalWebHostTest{T}"/>.</param> | ||
| /// <remarks><paramref name="hostTest"/> was added to support those cases where the caller is required in the host configuration.</remarks> | ||
| /// <exception cref="ArgumentNullException"> | ||
| /// <paramref name="hostTest"/> is null. | ||
| /// </exception> | ||
| /// <exception cref="ArgumentOutOfRangeException"> | ||
| /// <paramref name="hostTest"/> is not assignable from <see cref="MinimalWebHostTest{T}"/>. | ||
| /// </exception> | ||
| public override void ConfigureHost(Test hostTest) | ||
| { | ||
| if (hostTest == null) { throw new ArgumentNullException(nameof(hostTest)); } | ||
| if (!HasTypes(hostTest.GetType(), typeof(MinimalWebHostTest<>))) { throw new ArgumentOutOfRangeException(nameof(hostTest), typeof(MinimalWebHostTest<>), $"{nameof(hostTest)} is not assignable from MinimalWebHostTest<T>."); } | ||
|
|
||
| var hb = WebApplication.CreateBuilder(new WebApplicationOptions() | ||
| { | ||
| EnvironmentName = "Development", | ||
| ApplicationName = hostTest.CallerType.Assembly.GetName().Name | ||
| }); | ||
|
|
||
| hb.WebHost.UseTestServer(o => o.PreserveExecutionContext = true); | ||
|
|
||
| Configuration = hb.Configuration; | ||
| Environment = hb.Environment; | ||
|
|
||
| ConfigureCallback(Configuration, Environment); | ||
|
|
||
| ConfigureHostCallback(hb); | ||
|
|
||
| var webApplication = hb.Build(); | ||
|
|
||
| ConfigureApplicationCallback(webApplication); | ||
| Application = webApplication; | ||
|
|
||
| Host = webApplication; | ||
|
|
||
| HostRunnerCallback(Host); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Gets or sets the delegate that configures the HTTP request pipeline. | ||
| /// </summary> | ||
| /// <value>The delegate that configures the HTTP request pipeline.</value> | ||
| public Action<IApplicationBuilder> ConfigureApplicationCallback { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Gets the <see cref="IApplicationBuilder" /> initialized by the <see cref="IHost" />. | ||
| /// </summary> | ||
| /// <value>The <see cref="IApplicationBuilder" /> initialized by the <see cref="IHost" />.</value> | ||
| public IApplicationBuilder Application { get; protected set; } | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Clarify naming to avoid confusion.
Having both this class and its generic base class named
MinimalWebHostTestcan be confusing. Consider renaming one for clarity, for example:• Keep the generic base class as
MinimalWebHostTest<TFixture>• Rename this sealed class to
ConcreteMinimalWebHostTestorMinimalWebHostTestImpl.