Skip to content

Commit 194d366

Browse files
Remove nowarn settings and start converting TestHost tests to HostBuilder pattern
Co-authored-by: BrennanConroy <[email protected]>
1 parent 95e6eaa commit 194d366

File tree

8 files changed

+95
-76
lines changed

8 files changed

+95
-76
lines changed

src/Hosting/TestHost/test/Microsoft.AspNetCore.TestHost.Tests.csproj

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
<PropertyGroup>
44
<TargetFramework>$(DefaultNetCoreTargetFramework)</TargetFramework>
5-
<!-- CS0618: WebHostBuilder is obsolete -->
6-
<NoWarn>$(NoWarn);CS0618</NoWarn>
75
</PropertyGroup>
86

97
<ItemGroup>

src/Hosting/TestHost/test/WebSocketClientTests.cs

Lines changed: 87 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using Microsoft.AspNetCore.Builder;
55
using Microsoft.AspNetCore.Hosting;
66
using Microsoft.AspNetCore.Http.Features;
7+
using Microsoft.Extensions.Hosting;
78

89
namespace Microsoft.AspNetCore.TestHost.Tests;
910

@@ -19,22 +20,29 @@ public async Task ConnectAsync_ShouldSetRequestProperties(string requestUri, str
1920
string capturedHost = null;
2021
string capturedPath = null;
2122

22-
using (var testServer = new TestServer(new WebHostBuilder()
23-
.Configure(app =>
23+
using var host = new HostBuilder()
24+
.ConfigureWebHost(webHostBuilder =>
2425
{
25-
app.Run(ctx =>
26-
{
27-
if (ctx.Request.Path.StartsWithSegments("/connect"))
26+
webHostBuilder
27+
.UseTestServer()
28+
.Configure(app =>
2829
{
29-
capturedScheme = ctx.Request.Scheme;
30-
capturedHost = ctx.Request.Host.Value;
31-
capturedPath = ctx.Request.Path;
32-
}
33-
return Task.FromResult(0);
34-
});
35-
})))
36-
{
37-
var client = testServer.CreateWebSocketClient();
30+
app.Run(ctx =>
31+
{
32+
if (ctx.Request.Path.StartsWithSegments("/connect"))
33+
{
34+
capturedScheme = ctx.Request.Scheme;
35+
capturedHost = ctx.Request.Host.Value;
36+
capturedPath = ctx.Request.Path;
37+
}
38+
return Task.FromResult(0);
39+
});
40+
});
41+
})
42+
.Build();
43+
44+
var testServer = host.GetTestServer();
45+
var client = testServer.CreateWebSocketClient();
3846

3947
try
4048
{
@@ -46,7 +54,6 @@ await client.ConnectAsync(
4654
{
4755
// An exception will be thrown because our dummy endpoint does not implement a full Web socket server
4856
}
49-
}
5057

5158
Assert.Equal("http", capturedScheme);
5259
Assert.Equal(expectedHost, capturedHost);
@@ -56,72 +63,84 @@ await client.ConnectAsync(
5663
[Fact]
5764
public async Task CanAcceptWebSocket()
5865
{
59-
using (var testServer = new TestServer(new WebHostBuilder()
60-
.Configure(app =>
66+
using var host = new HostBuilder()
67+
.ConfigureWebHost(webHostBuilder =>
6168
{
62-
app.UseWebSockets();
63-
app.Run(async ctx =>
64-
{
65-
if (ctx.Request.Path.StartsWithSegments("/connect"))
69+
webHostBuilder
70+
.UseTestServer()
71+
.Configure(app =>
6672
{
67-
if (ctx.WebSockets.IsWebSocketRequest)
73+
app.UseWebSockets();
74+
app.Run(async ctx =>
6875
{
69-
using var websocket = await ctx.WebSockets.AcceptWebSocketAsync();
70-
var buffer = new byte[1000];
71-
var res = await websocket.ReceiveAsync(buffer, default);
72-
await websocket.SendAsync(buffer.AsMemory(0, res.Count), System.Net.WebSockets.WebSocketMessageType.Binary, true, default);
73-
await websocket.CloseAsync(System.Net.WebSockets.WebSocketCloseStatus.NormalClosure, null, default);
74-
}
75-
}
76-
});
77-
})))
78-
{
79-
var client = testServer.CreateWebSocketClient();
76+
if (ctx.Request.Path.StartsWithSegments("/connect"))
77+
{
78+
if (ctx.WebSockets.IsWebSocketRequest)
79+
{
80+
using var websocket = await ctx.WebSockets.AcceptWebSocketAsync();
81+
var buffer = new byte[1000];
82+
var res = await websocket.ReceiveAsync(buffer, default);
83+
await websocket.SendAsync(buffer.AsMemory(0, res.Count), System.Net.WebSockets.WebSocketMessageType.Binary, true, default);
84+
await websocket.CloseAsync(System.Net.WebSockets.WebSocketCloseStatus.NormalClosure, null, default);
85+
}
86+
}
87+
});
88+
});
89+
})
90+
.Build();
8091

81-
using var socket = await client.ConnectAsync(
82-
uri: new Uri("http://localhost/connect"),
83-
cancellationToken: default);
92+
var testServer = host.GetTestServer();
93+
var client = testServer.CreateWebSocketClient();
8494

85-
await socket.SendAsync(new byte[10], System.Net.WebSockets.WebSocketMessageType.Binary, true, default);
86-
var res = await socket.ReceiveAsync(new byte[100], default);
87-
Assert.Equal(10, res.Count);
88-
Assert.True(res.EndOfMessage);
95+
using var socket = await client.ConnectAsync(
96+
uri: new Uri("http://localhost/connect"),
97+
cancellationToken: default);
8998

90-
await socket.CloseAsync(System.Net.WebSockets.WebSocketCloseStatus.NormalClosure, null, default);
91-
}
99+
await socket.SendAsync(new byte[10], System.Net.WebSockets.WebSocketMessageType.Binary, true, default);
100+
var res = await socket.ReceiveAsync(new byte[100], default);
101+
Assert.Equal(10, res.Count);
102+
Assert.True(res.EndOfMessage);
103+
104+
await socket.CloseAsync(System.Net.WebSockets.WebSocketCloseStatus.NormalClosure, null, default);
92105
}
93106

94107
[Fact]
95108
public async Task VerifyWebSocketAndUpgradeFeatures()
96109
{
97-
using (var testServer = new TestServer(new WebHostBuilder()
98-
.Configure(app =>
110+
using var host = new HostBuilder()
111+
.ConfigureWebHost(webHostBuilder =>
99112
{
100-
app.Run(async c =>
101-
{
102-
var upgradeFeature = c.Features.Get<IHttpUpgradeFeature>();
103-
Assert.NotNull(upgradeFeature);
104-
Assert.False(upgradeFeature.IsUpgradableRequest);
105-
await Assert.ThrowsAsync<NotSupportedException>(() => upgradeFeature.UpgradeAsync());
106-
107-
var webSocketFeature = c.Features.Get<IHttpWebSocketFeature>();
108-
Assert.NotNull(webSocketFeature);
109-
Assert.True(webSocketFeature.IsWebSocketRequest);
110-
});
111-
})))
112-
{
113-
var client = testServer.CreateWebSocketClient();
113+
webHostBuilder
114+
.UseTestServer()
115+
.Configure(app =>
116+
{
117+
app.Run(async c =>
118+
{
119+
var upgradeFeature = c.Features.Get<IHttpUpgradeFeature>();
120+
Assert.NotNull(upgradeFeature);
121+
Assert.False(upgradeFeature.IsUpgradableRequest);
122+
await Assert.ThrowsAsync<NotSupportedException>(() => upgradeFeature.UpgradeAsync());
114123

115-
try
116-
{
117-
using var socket = await client.ConnectAsync(
118-
uri: new Uri("http://localhost/connect"),
119-
cancellationToken: default);
120-
}
121-
catch
122-
{
123-
// An exception will be thrown because our endpoint does not accept the websocket
124-
}
124+
var webSocketFeature = c.Features.Get<IHttpWebSocketFeature>();
125+
Assert.NotNull(webSocketFeature);
126+
Assert.True(webSocketFeature.IsWebSocketRequest);
127+
});
128+
});
129+
})
130+
.Build();
131+
132+
var testServer = host.GetTestServer();
133+
var client = testServer.CreateWebSocketClient();
134+
135+
try
136+
{
137+
using var socket = await client.ConnectAsync(
138+
uri: new Uri("http://localhost/connect"),
139+
cancellationToken: default);
140+
}
141+
catch
142+
{
143+
// An exception will be thrown because our endpoint does not accept the websocket
125144
}
126145
}
127146
}

src/Hosting/WindowsServices/test/Microsoft.AspNetCore.Hosting.WindowsServices.Tests.csproj

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
<PropertyGroup>
44
<TargetFramework>$(DefaultNetCoreTargetFramework)</TargetFramework>
5-
<!-- CS0618: WebHostBuilder is obsolete -->
6-
<NoWarn>$(NoWarn);CS0618</NoWarn>
75
</PropertyGroup>
86

97
<ItemGroup>

src/Hosting/WindowsServices/test/WebHostServiceTests.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ public class WebHostServiceTests
1919
[ConditionalFact]
2020
public async Task StopBeforeServiceStarted()
2121
{
22+
#pragma warning disable CS0618 // Type or member is obsolete
2223
var host = new WebHostBuilder().UseServer(new FakeServer()).Configure(x => { }).Build();
24+
#pragma warning restore CS0618 // Type or member is obsolete
2325
var webHostService = new WebHostService(host);
2426
var applicationLifetime = host.Services.GetRequiredService<IHostApplicationLifetime>();
2527

@@ -33,7 +35,9 @@ await Assert.ThrowsAsync<TaskCanceledException>(
3335
[ConditionalFact]
3436
public async Task StopAfterServiceStarted()
3537
{
38+
#pragma warning disable CS0618 // Type or member is obsolete
3639
var host = new WebHostBuilder().UseServer(new FakeServer()).Configure(x => { }).Build();
40+
#pragma warning restore CS0618 // Type or member is obsolete
3741
var webHostService = new WebHostService(host);
3842
var applicationLifetime = host.Services.GetRequiredService<IHostApplicationLifetime>();
3943

src/Hosting/test/testassets/IStartupInjectionAssemblyName/IStartupInjectionAssemblyName.csproj

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
<PropertyGroup>
44
<TargetFramework>$(DefaultNetCoreTargetFramework)</TargetFramework>
55
<OutputType>Exe</OutputType>
6-
<!-- CS0618: WebHostBuilder is obsolete -->
7-
<NoWarn>$(NoWarn);CS0618</NoWarn>
86
</PropertyGroup>
97

108
<ItemGroup>

src/Hosting/test/testassets/IStartupInjectionAssemblyName/Program.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ public static void Main(string[] args)
1818

1919
// Do not change the signature of this method. It's used for tests.
2020
private static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
21+
#pragma warning disable CS0618 // Type or member is obsolete
2122
new WebHostBuilder()
2223
.SuppressStatusMessages(true)
2324
.ConfigureServices(services => services.AddSingleton<IStartup, Startup>());
25+
#pragma warning restore CS0618 // Type or member is obsolete
2426
}

src/Hosting/test/testassets/Microsoft.AspNetCore.Hosting.TestSites/Microsoft.AspNetCore.Hosting.TestSites.csproj

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
<PropertyGroup>
44
<TargetFramework>$(DefaultNetCoreTargetFramework)</TargetFramework>
55
<OutputType>Exe</OutputType>
6-
<!-- CS0618: WebHostBuilder is obsolete -->
7-
<NoWarn>$(NoWarn);CS0618</NoWarn>
86
</PropertyGroup>
97

108
<ItemGroup>

src/Hosting/test/testassets/Microsoft.AspNetCore.Hosting.TestSites/Program.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ public static void Main(string[] args)
2222
.AddEnvironmentVariables(prefix: "ASPNETCORE_")
2323
.Build();
2424

25+
#pragma warning disable CS0618 // Type or member is obsolete
2526
var builder = new WebHostBuilder()
2627
.UseServer(new NoopServer())
2728
.UseConfiguration(config)
@@ -32,6 +33,7 @@ public static void Main(string[] args)
3233
factory.AddFilter<ConsoleLoggerProvider>(level => level >= LogLevel.Warning);
3334
})
3435
.UseStartup("Microsoft.AspNetCore.Hosting.TestSites");
36+
#pragma warning restore CS0618 // Type or member is obsolete
3537

3638
if (config["STARTMECHANIC"] == "Run")
3739
{

0 commit comments

Comments
 (0)