Skip to content

Commit e87e383

Browse files
Complete conversion of RequestBuilderTests, ClientHandlerTests, and WebSocketClientTests to HostBuilder
Co-authored-by: BrennanConroy <[email protected]>
1 parent 194d366 commit e87e383

File tree

2 files changed

+46
-17
lines changed

2 files changed

+46
-17
lines changed

src/Hosting/TestHost/test/ClientHandlerTests.cs

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
using Microsoft.AspNetCore.Http.Features;
1212
using Microsoft.AspNetCore.InternalTesting;
1313
using Microsoft.Extensions.DependencyInjection;
14+
using Microsoft.Extensions.Hosting;
1415
using Microsoft.Extensions.Logging;
1516
using Microsoft.Net.Http.Headers;
1617

@@ -691,19 +692,25 @@ public async Task ClientHandlerCreateContextWithDefaultRequestParameters()
691692
{
692693
// This logger will attempt to access information from HttpRequest once the HttpContext is created
693694
var logger = new VerifierLogger();
694-
var builder = new WebHostBuilder()
695-
.ConfigureServices(services =>
695+
using var host = new HostBuilder()
696+
.ConfigureWebHost(webHostBuilder =>
696697
{
697-
services.AddSingleton<ILogger<IWebHost>>(logger);
698+
webHostBuilder
699+
.UseTestServer()
700+
.ConfigureServices(services =>
701+
{
702+
services.AddSingleton<ILogger<IWebHost>>(logger);
703+
})
704+
.Configure(app =>
705+
{
706+
app.Run(context =>
707+
{
708+
return Task.FromResult(0);
709+
});
710+
});
698711
})
699-
.Configure(app =>
700-
{
701-
app.Run(context =>
702-
{
703-
return Task.FromResult(0);
704-
});
705-
});
706-
var server = new TestServer(builder);
712+
.Build();
713+
var server = host.GetTestServer();
707714

708715
// The HttpContext will be created and the logger will make sure that the HttpRequest exists and contains reasonable values
709716
var result = await server.CreateClient().GetStringAsync("/");

src/Hosting/TestHost/test/RequestBuilderTests.cs

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using Microsoft.AspNetCore.Hosting;
5+
using Microsoft.Extensions.Hosting;
56

67
namespace Microsoft.AspNetCore.TestHost;
78

@@ -10,8 +11,15 @@ public class RequestBuilderTests
1011
[Fact]
1112
public void AddRequestHeader()
1213
{
13-
var builder = new WebHostBuilder().Configure(app => { });
14-
var server = new TestServer(builder);
14+
using var host = new HostBuilder()
15+
.ConfigureWebHost(webHostBuilder =>
16+
{
17+
webHostBuilder
18+
.UseTestServer()
19+
.Configure(app => { });
20+
})
21+
.Build();
22+
var server = host.GetTestServer();
1523
server.CreateRequest("/")
1624
.AddHeader("Host", "MyHost:90")
1725
.And(request =>
@@ -23,8 +31,15 @@ public void AddRequestHeader()
2331
[Fact]
2432
public void AddContentHeaders()
2533
{
26-
var builder = new WebHostBuilder().Configure(app => { });
27-
var server = new TestServer(builder);
34+
using var host = new HostBuilder()
35+
.ConfigureWebHost(webHostBuilder =>
36+
{
37+
webHostBuilder
38+
.UseTestServer()
39+
.Configure(app => { });
40+
})
41+
.Build();
42+
var server = host.GetTestServer();
2843
server.CreateRequest("/")
2944
.AddHeader("Content-Type", "Test/Value")
3045
.And(request =>
@@ -38,8 +53,15 @@ public void AddContentHeaders()
3853
public void TestServer_PropertyShouldHoldTestServerInstance()
3954
{
4055
// Arrange
41-
var builder = new WebHostBuilder().Configure(app => { });
42-
var server = new TestServer(builder);
56+
using var host = new HostBuilder()
57+
.ConfigureWebHost(webHostBuilder =>
58+
{
59+
webHostBuilder
60+
.UseTestServer()
61+
.Configure(app => { });
62+
})
63+
.Build();
64+
var server = host.GetTestServer();
4365

4466
// Act
4567
var requestBuilder = server.CreateRequest("/");

0 commit comments

Comments
 (0)