diff --git a/src/Mvc/Mvc.Testing/src/WebApplicationFactory.cs b/src/Mvc/Mvc.Testing/src/WebApplicationFactory.cs index 3976128cd958..f62eafa598f4 100644 --- a/src/Mvc/Mvc.Testing/src/WebApplicationFactory.cs +++ b/src/Mvc/Mvc.Testing/src/WebApplicationFactory.cs @@ -16,6 +16,7 @@ using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyModel; using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Options; namespace Microsoft.AspNetCore.Mvc.Testing; @@ -588,7 +589,16 @@ private static void EnsureDepsFile() /// /// The from the bootstrapped application. /// - protected virtual TestServer CreateServer(IServiceProvider serviceProvider) => new(serviceProvider); + protected virtual TestServer CreateServer(IServiceProvider serviceProvider) + { + var options = serviceProvider.GetService>(); + if (options is not null) + { + return new(serviceProvider, options); + } + + return new(serviceProvider); + } /// /// Creates the with the bootstrapped application in . diff --git a/src/Mvc/test/Mvc.FunctionalTests/TestServerWithCustomConfigurationIntegrationTests.cs b/src/Mvc/test/Mvc.FunctionalTests/TestServerWithCustomConfigurationIntegrationTests.cs new file mode 100644 index 000000000000..05bec156a8ba --- /dev/null +++ b/src/Mvc/test/Mvc.FunctionalTests/TestServerWithCustomConfigurationIntegrationTests.cs @@ -0,0 +1,56 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System.Net; +using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore.Mvc.Testing; +using Microsoft.AspNetCore.TestHost; + +namespace Microsoft.AspNetCore.Mvc.FunctionalTests; + +public class CustomWebApplicationFactory : WebApplicationFactory +{ + public Action ConfigureOptions { get; set; } + + protected override void ConfigureWebHost(IWebHostBuilder builder) + { + builder.UseTestServer(ConfigureOptions); + } +} + +public class TestServerWithCustomConfigurationIntegrationTests : IClassFixture +{ + public CustomWebApplicationFactory Factory { get; } + + public TestServerWithCustomConfigurationIntegrationTests(CustomWebApplicationFactory factory) + { + Factory = factory; + } + + [Fact] + public async Task ServerConfigured() + { + // Arrange + var optionsConfiguredCounter = 0; + Factory.ConfigureOptions = options => + { + options.AllowSynchronousIO = true; + options.PreserveExecutionContext = true; + optionsConfiguredCounter++; + }; + + // Act + Assert.Equal(0, optionsConfiguredCounter); + Factory.StartServer(); + + using var client = Factory.CreateClient(); + + using var response = await client.GetAsync("/"); + + // Assert + Assert.Equal(HttpStatusCode.OK, response.StatusCode); + Assert.Equal(1, optionsConfiguredCounter); + Assert.True(Factory.Server.AllowSynchronousIO); + Assert.True(Factory.Server.PreserveExecutionContext); + } +}