Skip to content

Commit 1b7269e

Browse files
authored
CreateServer with TestServerOptions in WAF (#64803)
1 parent b51307a commit 1b7269e

File tree

2 files changed

+67
-1
lines changed

2 files changed

+67
-1
lines changed

src/Mvc/Mvc.Testing/src/WebApplicationFactory.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
using Microsoft.Extensions.DependencyInjection;
1717
using Microsoft.Extensions.DependencyModel;
1818
using Microsoft.Extensions.Hosting;
19+
using Microsoft.Extensions.Options;
1920

2021
namespace Microsoft.AspNetCore.Mvc.Testing;
2122

@@ -588,7 +589,16 @@ private static void EnsureDepsFile()
588589
/// </summary>
589590
/// <param name="serviceProvider">The <see cref="IServiceProvider"/> from the bootstrapped application.</param>
590591
/// <returns></returns>
591-
protected virtual TestServer CreateServer(IServiceProvider serviceProvider) => new(serviceProvider);
592+
protected virtual TestServer CreateServer(IServiceProvider serviceProvider)
593+
{
594+
var options = serviceProvider.GetService<IOptions<TestServerOptions>>();
595+
if (options is not null)
596+
{
597+
return new(serviceProvider, options);
598+
}
599+
600+
return new(serviceProvider);
601+
}
592602

593603
/// <summary>
594604
/// Creates the <see cref="IHost"/> with the bootstrapped application in <paramref name="builder"/>.
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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

Comments
 (0)