Skip to content

Commit 3f16853

Browse files
Update TestServerTests.cs and HttpsConfigurationTests.cs to use HostBuilder pattern
Co-authored-by: BrennanConroy <[email protected]>
1 parent dd80d9c commit 3f16853

File tree

2 files changed

+171
-121
lines changed

2 files changed

+171
-121
lines changed

src/Servers/Kestrel/Kestrel/test/HttpsConfigurationTests.cs

Lines changed: 124 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@
44
using System.Security.Cryptography.X509Certificates;
55
using Microsoft.AspNetCore.Connections;
66
using Microsoft.AspNetCore.Hosting;
7+
using Microsoft.AspNetCore.Hosting.Server;
78
using Microsoft.AspNetCore.Hosting.Server.Features;
89
using Microsoft.AspNetCore.Server.Kestrel.Core;
910
using Microsoft.AspNetCore.Server.Kestrel.Https;
1011
using Microsoft.Extensions.Configuration;
1112
using Microsoft.Extensions.DependencyInjection;
13+
using Microsoft.Extensions.Hosting;
1214

1315
namespace Microsoft.AspNetCore.Server.Kestrel.Tests;
1416

@@ -21,34 +23,37 @@ public class HttpsConfigurationTests
2123
[InlineData("https://127.0.0.1:0", false)]
2224
public async Task BindAddressFromSetting(string address, bool useKestrelHttpsConfiguration)
2325
{
24-
var hostBuilder = new WebHostBuilder()
25-
.UseKestrelCore()
26-
.ConfigureKestrel(serverOptions =>
27-
{
28-
serverOptions.TestOverrideDefaultCertificate = new X509Certificate2(Path.Combine("shared", "TestCertificates", "aspnetdevcert.pfx"), "testPassword");
29-
})
30-
.Configure(app => { });
31-
32-
// This is what ASPNETCORE_URLS would populate
33-
hostBuilder.UseSetting(WebHostDefaults.ServerUrlsKey, address);
26+
var hostBuilder = new HostBuilder()
27+
.ConfigureWebHost(webHostBuilder =>
28+
{
29+
webHostBuilder
30+
.UseKestrelCore()
31+
.ConfigureKestrel(serverOptions =>
32+
{
33+
serverOptions.TestOverrideDefaultCertificate = new X509Certificate2(Path.Combine("shared", "TestCertificates", "aspnetdevcert.pfx"), "testPassword");
34+
})
35+
.Configure(app => { })
36+
// This is what ASPNETCORE_URLS would populate
37+
.UseSetting(WebHostDefaults.ServerUrlsKey, address);
3438

35-
if (useKestrelHttpsConfiguration)
36-
{
37-
hostBuilder.UseKestrelHttpsConfiguration();
38-
}
39+
if (useKestrelHttpsConfiguration)
40+
{
41+
webHostBuilder.UseKestrelHttpsConfiguration();
42+
}
43+
});
3944

4045
var host = hostBuilder.Build();
46+
await host.StartAsync();
4147

42-
Assert.Single(host.ServerFeatures.Get<IServerAddressesFeature>().Addresses, address);
48+
Assert.Single(host.Services.GetRequiredService<IServer>().Features.Get<IServerAddressesFeature>().Addresses, address);
4349

4450
if (address.StartsWith("https", StringComparison.OrdinalIgnoreCase) && !useKestrelHttpsConfiguration)
4551
{
4652
Assert.Throws<InvalidOperationException>(host.Run);
4753
}
4854
else
4955
{
50-
// Binding succeeds
51-
await host.StartAsync();
56+
// Binding succeeds - server is already started, so we just stop it
5257
await host.StopAsync();
5358
}
5459
}
@@ -59,16 +64,20 @@ public void NoFallbackToHttpAddress()
5964
const string httpAddress = "http://127.0.0.1:0";
6065
const string httpsAddress = "https://localhost:5001";
6166

62-
var hostBuilder = new WebHostBuilder()
63-
.UseKestrelCore()
64-
.Configure(app => { });
65-
66-
// This is what ASPNETCORE_URLS would populate
67-
hostBuilder.UseSetting(WebHostDefaults.ServerUrlsKey, $"{httpAddress};{httpsAddress}");
67+
var hostBuilder = new HostBuilder()
68+
.ConfigureWebHost(webHostBuilder =>
69+
{
70+
webHostBuilder
71+
.UseKestrelCore()
72+
.Configure(app => { })
73+
// This is what ASPNETCORE_URLS would populate
74+
.UseSetting(WebHostDefaults.ServerUrlsKey, $"{httpAddress};{httpsAddress}");
75+
});
6876

6977
var host = hostBuilder.Build();
78+
await host.StartAsync();
7079

71-
Assert.Equal(new[] { httpAddress, httpsAddress }, host.ServerFeatures.Get<IServerAddressesFeature>().Addresses);
80+
Assert.Equal(new[] { httpAddress, httpsAddress }, host.Services.GetRequiredService<IServer>().Features.Get<IServerAddressesFeature>().Addresses);
7281

7382
Assert.Throws<InvalidOperationException>(host.Run);
7483
}
@@ -80,24 +89,28 @@ public void NoFallbackToHttpAddress()
8089
[InlineData("https://127.0.0.1:0", false)]
8190
public async Task BindAddressFromEndpoint(string address, bool useKestrelHttpsConfiguration)
8291
{
83-
var hostBuilder = new WebHostBuilder()
84-
.UseKestrelCore()
85-
.ConfigureKestrel(serverOptions =>
86-
{
87-
var config = new ConfigurationBuilder().AddInMemoryCollection(new[]
92+
var hostBuilder = new HostBuilder()
93+
.ConfigureWebHost(webHostBuilder =>
94+
{
95+
webHostBuilder
96+
.UseKestrelCore()
97+
.ConfigureKestrel(serverOptions =>
8898
{
89-
new KeyValuePair<string, string>("Endpoints:end1:Url", address),
90-
new KeyValuePair<string, string>("Certificates:Default:Path", Path.Combine("shared", "TestCertificates", "aspnetdevcert.pfx")),
91-
new KeyValuePair<string, string>("Certificates:Default:Password", "testPassword"),
92-
}).Build();
93-
serverOptions.Configure(config);
94-
})
95-
.Configure(app => { });
96-
97-
if (useKestrelHttpsConfiguration)
98-
{
99-
hostBuilder.UseKestrelHttpsConfiguration();
100-
}
99+
var config = new ConfigurationBuilder().AddInMemoryCollection(new[]
100+
{
101+
new KeyValuePair<string, string>("Endpoints:end1:Url", address),
102+
new KeyValuePair<string, string>("Certificates:Default:Path", Path.Combine("shared", "TestCertificates", "aspnetdevcert.pfx")),
103+
new KeyValuePair<string, string>("Certificates:Default:Password", "testPassword"),
104+
}).Build();
105+
serverOptions.Configure(config);
106+
})
107+
.Configure(app => { });
108+
109+
if (useKestrelHttpsConfiguration)
110+
{
111+
webHostBuilder.UseKestrelHttpsConfiguration();
112+
}
113+
});
101114

102115
var host = hostBuilder.Build();
103116

@@ -118,23 +131,27 @@ public async Task BindAddressFromEndpoint(string address, bool useKestrelHttpsCo
118131
[InlineData(false)]
119132
public async Task LoadDefaultCertificate(bool useKestrelHttpsConfiguration)
120133
{
121-
var hostBuilder = new WebHostBuilder()
122-
.UseKestrelCore()
123-
.ConfigureKestrel(serverOptions =>
124-
{
125-
var config = new ConfigurationBuilder().AddInMemoryCollection(new[]
134+
var hostBuilder = new HostBuilder()
135+
.ConfigureWebHost(webHostBuilder =>
136+
{
137+
webHostBuilder
138+
.UseKestrelCore()
139+
.ConfigureKestrel(serverOptions =>
126140
{
127-
new KeyValuePair<string, string>("Certificates:Default:Path", Path.Combine("shared", "TestCertificates", "aspnetdevcert.pfx")),
128-
new KeyValuePair<string, string>("Certificates:Default:Password", "testPassword"),
129-
}).Build();
130-
serverOptions.Configure(config);
131-
})
132-
.Configure(app => { });
133-
134-
if (useKestrelHttpsConfiguration)
135-
{
136-
hostBuilder.UseKestrelHttpsConfiguration();
137-
}
141+
var config = new ConfigurationBuilder().AddInMemoryCollection(new[]
142+
{
143+
new KeyValuePair<string, string>("Certificates:Default:Path", Path.Combine("shared", "TestCertificates", "aspnetdevcert.pfx")),
144+
new KeyValuePair<string, string>("Certificates:Default:Password", "testPassword"),
145+
}).Build();
146+
serverOptions.Configure(config);
147+
})
148+
.Configure(app => { });
149+
150+
if (useKestrelHttpsConfiguration)
151+
{
152+
webHostBuilder.UseKestrelHttpsConfiguration();
153+
}
154+
});
138155

139156
var host = hostBuilder.Build();
140157

@@ -150,24 +167,28 @@ public async Task LoadDefaultCertificate(bool useKestrelHttpsConfiguration)
150167
[InlineData("https://127.0.0.1:0", false)]
151168
public async Task LoadEndpointCertificate(string address, bool useKestrelHttpsConfiguration)
152169
{
153-
var hostBuilder = new WebHostBuilder()
154-
.UseKestrelCore()
155-
.ConfigureKestrel(serverOptions =>
156-
{
157-
var config = new ConfigurationBuilder().AddInMemoryCollection(new[]
170+
var hostBuilder = new HostBuilder()
171+
.ConfigureWebHost(webHostBuilder =>
172+
{
173+
webHostBuilder
174+
.UseKestrelCore()
175+
.ConfigureKestrel(serverOptions =>
158176
{
159-
new KeyValuePair<string, string>("Endpoints:end1:Url", address),
160-
new KeyValuePair<string, string>("Certificates:Default:Path", Path.Combine("shared", "TestCertificates", "aspnetdevcert.pfx")),
161-
new KeyValuePair<string, string>("Certificates:Default:Password", "testPassword"),
162-
}).Build();
163-
serverOptions.Configure(config);
164-
})
165-
.Configure(app => { });
166-
167-
if (useKestrelHttpsConfiguration)
168-
{
169-
hostBuilder.UseKestrelHttpsConfiguration();
170-
}
177+
var config = new ConfigurationBuilder().AddInMemoryCollection(new[]
178+
{
179+
new KeyValuePair<string, string>("Endpoints:end1:Url", address),
180+
new KeyValuePair<string, string>("Certificates:Default:Path", Path.Combine("shared", "TestCertificates", "aspnetdevcert.pfx")),
181+
new KeyValuePair<string, string>("Certificates:Default:Password", "testPassword"),
182+
}).Build();
183+
serverOptions.Configure(config);
184+
})
185+
.Configure(app => { });
186+
187+
if (useKestrelHttpsConfiguration)
188+
{
189+
webHostBuilder.UseKestrelHttpsConfiguration();
190+
}
191+
});
171192

172193
var host = hostBuilder.Build();
173194

@@ -186,18 +207,22 @@ public async Task LoadEndpointCertificate(string address, bool useKestrelHttpsCo
186207
[Fact]
187208
public async Task UseHttpsJustWorks()
188209
{
189-
var hostBuilder = new WebHostBuilder()
190-
.UseKestrelCore()
191-
.ConfigureKestrel(serverOptions =>
210+
var hostBuilder = new HostBuilder()
211+
.ConfigureWebHost(webHostBuilder =>
192212
{
193-
serverOptions.TestOverrideDefaultCertificate = new X509Certificate2(Path.Combine("shared", "TestCertificates", "aspnetdevcert.pfx"), "testPassword");
213+
webHostBuilder
214+
.UseKestrelCore()
215+
.ConfigureKestrel(serverOptions =>
216+
{
217+
serverOptions.TestOverrideDefaultCertificate = new X509Certificate2(Path.Combine("shared", "TestCertificates", "aspnetdevcert.pfx"), "testPassword");
194218

195-
serverOptions.ListenAnyIP(0, listenOptions =>
196-
{
197-
listenOptions.UseHttps();
198-
});
199-
})
200-
.Configure(app => { });
219+
serverOptions.ListenAnyIP(0, listenOptions =>
220+
{
221+
listenOptions.UseHttps();
222+
});
223+
})
224+
.Configure(app => { });
225+
});
201226

202227
var host = hostBuilder.Build();
203228

@@ -211,19 +236,23 @@ public async Task UseHttpsJustWorks()
211236
[Fact]
212237
public async Task UseHttpsMayNotImplyUseKestrelHttpsConfiguration()
213238
{
214-
var hostBuilder = new WebHostBuilder()
215-
.UseKestrelCore()
216-
.ConfigureKestrel(serverOptions =>
239+
var hostBuilder = new HostBuilder()
240+
.ConfigureWebHost(webHostBuilder =>
217241
{
218-
serverOptions.ListenAnyIP(0, listenOptions =>
219-
{
220-
listenOptions.UseHttps(new HttpsConnectionAdapterOptions()
242+
webHostBuilder
243+
.UseKestrelCore()
244+
.ConfigureKestrel(serverOptions =>
221245
{
222-
ServerCertificate = new X509Certificate2(Path.Combine("shared", "TestCertificates", "aspnetdevcert.pfx"), "testPassword"),
223-
});
224-
});
225-
})
226-
.Configure(app => { });
246+
serverOptions.ListenAnyIP(0, listenOptions =>
247+
{
248+
listenOptions.UseHttps(new HttpsConnectionAdapterOptions()
249+
{
250+
ServerCertificate = new X509Certificate2(Path.Combine("shared", "TestCertificates", "aspnetdevcert.pfx"), "testPassword"),
251+
});
252+
});
253+
})
254+
.Configure(app => { });
255+
});
227256

228257
var host = hostBuilder.Build();
229258

src/SignalR/clients/csharp/Client/test/UnitTests/TestServerTests.cs

Lines changed: 47 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using Microsoft.AspNetCore.SignalR.Tests;
88
using Microsoft.AspNetCore.TestHost;
99
using Microsoft.Extensions.DependencyInjection;
10+
using Microsoft.Extensions.Hosting;
1011
using Microsoft.Extensions.Logging;
1112
using Xunit;
1213

@@ -19,20 +20,30 @@ public async Task WebSocketsWorks()
1920
{
2021
using (StartVerifiableLog())
2122
{
22-
var builder = new WebHostBuilder().ConfigureServices(s =>
23-
{
24-
s.AddLogging();
25-
s.AddSingleton(LoggerFactory);
26-
s.AddSignalR();
27-
}).Configure(app =>
28-
{
29-
app.UseRouting();
30-
app.UseEndpoints(endpoints =>
23+
using var host = new HostBuilder()
24+
.ConfigureWebHost(webHostBuilder =>
3125
{
32-
endpoints.MapHub<EchoHub>("/echo");
33-
});
34-
});
35-
var server = new TestServer(builder);
26+
webHostBuilder
27+
.UseTestServer()
28+
.ConfigureServices(s =>
29+
{
30+
s.AddLogging();
31+
s.AddSingleton(LoggerFactory);
32+
s.AddSignalR();
33+
})
34+
.Configure(app =>
35+
{
36+
app.UseRouting();
37+
app.UseEndpoints(endpoints =>
38+
{
39+
endpoints.MapHub<EchoHub>("/echo");
40+
});
41+
});
42+
})
43+
.Build();
44+
45+
await host.StartAsync();
46+
var server = host.GetTestServer();
3647

3748
var webSocketFactoryCalled = false;
3849
var connectionBuilder = new HubConnectionBuilder()
@@ -71,20 +82,30 @@ public async Task LongPollingWorks()
7182
{
7283
using (StartVerifiableLog())
7384
{
74-
var builder = new WebHostBuilder().ConfigureServices(s =>
75-
{
76-
s.AddLogging();
77-
s.AddSingleton(LoggerFactory);
78-
s.AddSignalR();
79-
}).Configure(app =>
80-
{
81-
app.UseRouting();
82-
app.UseEndpoints(endpoints =>
85+
using var host = new HostBuilder()
86+
.ConfigureWebHost(webHostBuilder =>
8387
{
84-
endpoints.MapHub<EchoHub>("/echo");
85-
});
86-
});
87-
var server = new TestServer(builder);
88+
webHostBuilder
89+
.UseTestServer()
90+
.ConfigureServices(s =>
91+
{
92+
s.AddLogging();
93+
s.AddSingleton(LoggerFactory);
94+
s.AddSignalR();
95+
})
96+
.Configure(app =>
97+
{
98+
app.UseRouting();
99+
app.UseEndpoints(endpoints =>
100+
{
101+
endpoints.MapHub<EchoHub>("/echo");
102+
});
103+
});
104+
})
105+
.Build();
106+
107+
await host.StartAsync();
108+
var server = host.GetTestServer();
88109

89110
var connectionBuilder = new HubConnectionBuilder()
90111
.WithUrl(server.BaseAddress + "echo", options =>

0 commit comments

Comments
 (0)