|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +using System.Net; |
| 5 | +using Microsoft.AspNetCore.Hosting; |
| 6 | +using Microsoft.AspNetCore.Mvc.Testing; |
| 7 | +using Microsoft.AspNetCore.TestHost; |
| 8 | + |
| 9 | +namespace Microsoft.AspNetCore.Mvc.FunctionalTests; |
| 10 | + |
| 11 | +public class CustomWebApplicationFactory : WebApplicationFactory<SimpleWebSite.Startup> |
| 12 | +{ |
| 13 | + public Action<TestServerOptions> ConfigureOptions { get; set; } |
| 14 | + |
| 15 | + protected override void ConfigureWebHost(IWebHostBuilder builder) |
| 16 | + { |
| 17 | + builder.UseTestServer(ConfigureOptions); |
| 18 | + } |
| 19 | +} |
| 20 | + |
| 21 | +public class TestServerWithCustomConfigurationIntegrationTests : IClassFixture<CustomWebApplicationFactory> |
| 22 | +{ |
| 23 | + public CustomWebApplicationFactory Factory { get; } |
| 24 | + |
| 25 | + public TestServerWithCustomConfigurationIntegrationTests(CustomWebApplicationFactory factory) |
| 26 | + { |
| 27 | + Factory = factory; |
| 28 | + } |
| 29 | + |
| 30 | + [Fact] |
| 31 | + public async Task ServerConfigured() |
| 32 | + { |
| 33 | + // Arrange |
| 34 | + var optionsConfiguredCounter = 0; |
| 35 | + Factory.ConfigureOptions = options => |
| 36 | + { |
| 37 | + options.AllowSynchronousIO = true; |
| 38 | + options.PreserveExecutionContext = true; |
| 39 | + optionsConfiguredCounter++; |
| 40 | + }; |
| 41 | + |
| 42 | + // Act |
| 43 | + Assert.Equal(0, optionsConfiguredCounter); |
| 44 | + Factory.StartServer(); |
| 45 | + |
| 46 | + using var client = Factory.CreateClient(); |
| 47 | + |
| 48 | + using var response = await client.GetAsync("/"); |
| 49 | + |
| 50 | + // Assert |
| 51 | + Assert.Equal(HttpStatusCode.OK, response.StatusCode); |
| 52 | + Assert.Equal(1, optionsConfiguredCounter); |
| 53 | + Assert.True(Factory.Server.AllowSynchronousIO); |
| 54 | + Assert.True(Factory.Server.PreserveExecutionContext); |
| 55 | + } |
| 56 | +} |
0 commit comments