From fcb2ce8ee0cf0cbe0c2174dc8af944992258a739 Mon Sep 17 00:00:00 2001 From: Kahbazi Date: Fri, 24 Jul 2020 19:11:07 +0430 Subject: [PATCH 1/5] [Routing] Move to GenericHost --- .../EndpointRoutingIntegrationTest.cs | 303 +++++++++++------- .../FunctionalTests/RoutingTestFixture.cs | 14 +- 2 files changed, 189 insertions(+), 128 deletions(-) diff --git a/src/Http/Routing/test/FunctionalTests/EndpointRoutingIntegrationTest.cs b/src/Http/Routing/test/FunctionalTests/EndpointRoutingIntegrationTest.cs index f0fa4a4f5963..5afb5521c01f 100644 --- a/src/Http/Routing/test/FunctionalTests/EndpointRoutingIntegrationTest.cs +++ b/src/Http/Routing/test/FunctionalTests/EndpointRoutingIntegrationTest.cs @@ -10,6 +10,7 @@ using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.TestHost; using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; using Xunit; namespace Microsoft.AspNetCore.Routing.FunctionalTests @@ -31,20 +32,26 @@ public class EndpointRoutingIntegrationTest public async Task AuthorizationMiddleware_WhenNoAuthMetadataIsConfigured() { // Arrange - var builder = new WebHostBuilder(); - builder.Configure(app => - { - app.UseRouting(); - app.UseAuthorization(); - app.UseEndpoints(b => b.Map("/", TestDelegate)); - }) - .ConfigureServices(services => - { - services.AddAuthorization(); - services.AddRouting(); - }); - - using var server = new TestServer(builder); + var host = new HostBuilder() + .ConfigureWebHost(webHostBuilder => + { + webHostBuilder + .Configure(app => + { + app.UseRouting(); + app.UseAuthorization(); + app.UseEndpoints(b => b.Map("/", TestDelegate)); + }) + .UseTestServer(); + }) + .ConfigureServices(services => + { + services.AddAuthorization(); + services.AddRouting(); + }) + .Build(); + + using var server = host.GetTestServer(); var response = await server.CreateRequest("/").SendAsync("GET"); @@ -55,20 +62,26 @@ public async Task AuthorizationMiddleware_WhenNoAuthMetadataIsConfigured() public async Task AuthorizationMiddleware_WhenEndpointIsNotFound() { // Arrange - var builder = new WebHostBuilder(); - builder.Configure(app => - { - app.UseRouting(); - app.UseAuthorization(); - app.UseEndpoints(b => b.Map("/", TestDelegate)); - }) - .ConfigureServices(services => - { - services.AddAuthorization(); - services.AddRouting(); - }); - - using var server = new TestServer(builder); + var host = new HostBuilder() + .ConfigureWebHost(webHostBuilder => + { + webHostBuilder + .Configure(app => + { + app.UseRouting(); + app.UseAuthorization(); + app.UseEndpoints(b => b.Map("/", TestDelegate)); + }) + .UseTestServer(); + }) + .ConfigureServices(services => + { + services.AddAuthorization(); + services.AddRouting(); + }) + .Build(); + + using var server = host.GetTestServer(); var response = await server.CreateRequest("/not-found").SendAsync("GET"); @@ -79,20 +92,26 @@ public async Task AuthorizationMiddleware_WhenEndpointIsNotFound() public async Task AuthorizationMiddleware_WithAuthorizedEndpoint() { // Arrange - var builder = new WebHostBuilder(); - builder.Configure(app => - { - app.UseRouting(); - app.UseAuthorization(); - app.UseEndpoints(b => b.Map("/", TestDelegate).RequireAuthorization()); - }) - .ConfigureServices(services => - { - services.AddAuthorization(options => options.DefaultPolicy = new AuthorizationPolicyBuilder().RequireAssertion(_ => true).Build()); - services.AddRouting(); - }); - - using var server = new TestServer(builder); + var host = new HostBuilder() + .ConfigureWebHost(webHostBuilder => + { + webHostBuilder + .Configure(app => + { + app.UseRouting(); + app.UseAuthorization(); + app.UseEndpoints(b => b.Map("/", TestDelegate).RequireAuthorization()); + }) + .UseTestServer(); + }) + .ConfigureServices(services => + { + services.AddAuthorization(options => options.DefaultPolicy = new AuthorizationPolicyBuilder().RequireAssertion(_ => true).Build()); + services.AddRouting(); + }) + .Build(); + + using var server = host.GetTestServer(); var response = await server.CreateRequest("/").SendAsync("GET"); @@ -103,20 +122,26 @@ public async Task AuthorizationMiddleware_WithAuthorizedEndpoint() public async Task AuthorizationMiddleware_NotConfigured_Throws() { // Arrange - var builder = new WebHostBuilder(); - builder.Configure(app => - { - app.UseRouting(); - app.UseEndpoints(b => b.Map("/", TestDelegate).RequireAuthorization()); - - }) - .ConfigureServices(services => - { - services.AddAuthorization(options => options.DefaultPolicy = new AuthorizationPolicyBuilder().RequireAssertion(_ => true).Build()); - services.AddRouting(); - }); - - using var server = new TestServer(builder); + var host = new HostBuilder() + .ConfigureWebHost(webHostBuilder => + { + webHostBuilder + .Configure(app => + { + app.UseRouting(); + app.UseEndpoints(b => b.Map("/", TestDelegate).RequireAuthorization()); + + }) + .UseTestServer(); + }) + .ConfigureServices(services => + { + services.AddAuthorization(options => options.DefaultPolicy = new AuthorizationPolicyBuilder().RequireAssertion(_ => true).Build()); + services.AddRouting(); + }) + .Build(); + + using var server = host.GetTestServer(); var ex = await Assert.ThrowsAsync(() => server.CreateRequest("/").SendAsync("GET")); Assert.Equal(AuthErrorMessage, ex.Message); @@ -126,18 +151,24 @@ public async Task AuthorizationMiddleware_NotConfigured_Throws() public async Task AuthorizationMiddleware_NotConfigured_WhenEndpointIsNotFound() { // Arrange - var builder = new WebHostBuilder(); - builder.Configure(app => - { - app.UseRouting(); - app.UseEndpoints(b => b.Map("/", TestDelegate).RequireAuthorization()); - }) - .ConfigureServices(services => - { - services.AddRouting(); - }); - - using var server = new TestServer(builder); + var host = new HostBuilder() + .ConfigureWebHost(webHostBuilder => + { + webHostBuilder + .Configure(app => + { + app.UseRouting(); + app.UseEndpoints(b => b.Map("/", TestDelegate).RequireAuthorization()); + }) + .UseTestServer(); + }) + .ConfigureServices(services => + { + services.AddRouting(); + }) + .Build(); + + using var server = host.GetTestServer(); var response = await server.CreateRequest("/not-found").SendAsync("GET"); @@ -148,20 +179,26 @@ public async Task AuthorizationMiddleware_NotConfigured_WhenEndpointIsNotFound() public async Task AuthorizationMiddleware_ConfiguredBeforeRouting_Throws() { // Arrange - var builder = new WebHostBuilder(); - builder.Configure(app => - { - app.UseAuthorization(); - app.UseRouting(); - app.UseEndpoints(b => b.Map("/", TestDelegate).RequireAuthorization()); - }) - .ConfigureServices(services => - { - services.AddAuthorization(options => options.DefaultPolicy = new AuthorizationPolicyBuilder().RequireAssertion(_ => true).Build()); - services.AddRouting(); - }); - - using var server = new TestServer(builder); + var host = new HostBuilder() + .ConfigureWebHost(webHostBuilder => + { + webHostBuilder + .Configure(app => + { + app.UseAuthorization(); + app.UseRouting(); + app.UseEndpoints(b => b.Map("/", TestDelegate).RequireAuthorization()); + }) + .UseTestServer(); + }) + .ConfigureServices(services => + { + services.AddAuthorization(options => options.DefaultPolicy = new AuthorizationPolicyBuilder().RequireAssertion(_ => true).Build()); + services.AddRouting(); + }) + .Build(); + + using var server = host.GetTestServer(); var ex = await Assert.ThrowsAsync(() => server.CreateRequest("/").SendAsync("GET")); Assert.Equal(AuthErrorMessage, ex.Message); @@ -171,20 +208,26 @@ public async Task AuthorizationMiddleware_ConfiguredBeforeRouting_Throws() public async Task AuthorizationMiddleware_ConfiguredAfterRouting_Throws() { // Arrange - var builder = new WebHostBuilder(); - builder.Configure(app => - { - app.UseRouting(); - app.UseEndpoints(b => b.Map("/", TestDelegate).RequireAuthorization()); - app.UseAuthorization(); - }) - .ConfigureServices(services => - { - services.AddAuthorization(options => options.DefaultPolicy = new AuthorizationPolicyBuilder().RequireAssertion(_ => true).Build()); - services.AddRouting(); - }); - - using var server = new TestServer(builder); + var host = new HostBuilder() + .ConfigureWebHost(webHostBuilder => + { + webHostBuilder + .Configure(app => + { + app.UseRouting(); + app.UseEndpoints(b => b.Map("/", TestDelegate).RequireAuthorization()); + app.UseAuthorization(); + }) + .UseTestServer(); + }) + .ConfigureServices(services => + { + services.AddAuthorization(options => options.DefaultPolicy = new AuthorizationPolicyBuilder().RequireAssertion(_ => true).Build()); + services.AddRouting(); + }) + .Build(); + + using var server = host.GetTestServer(); var ex = await Assert.ThrowsAsync(() => server.CreateRequest("/").SendAsync("GET")); Assert.Equal(AuthErrorMessage, ex.Message); @@ -194,20 +237,26 @@ public async Task AuthorizationMiddleware_ConfiguredAfterRouting_Throws() public async Task CorsMiddleware_WithCorsEndpoint() { // Arrange - var builder = new WebHostBuilder(); - builder.Configure(app => - { - app.UseRouting(); - app.UseCors(); - app.UseEndpoints(b => b.Map("/", TestDelegate).RequireCors(policy => policy.AllowAnyOrigin())); - }) - .ConfigureServices(services => - { - services.AddCors(); - services.AddRouting(); - }); - - using var server = new TestServer(builder); + var host = new HostBuilder() + .ConfigureWebHost(webHostBuilder => + { + webHostBuilder + .Configure(app => + { + app.UseRouting(); + app.UseCors(); + app.UseEndpoints(b => b.Map("/", TestDelegate).RequireCors(policy => policy.AllowAnyOrigin())); + }) + .UseTestServer(); + }) + .ConfigureServices(services => + { + services.AddCors(); + services.AddRouting(); + }) + .Build(); + + using var server = host.GetTestServer(); var response = await server.CreateRequest("/").SendAsync("PUT"); @@ -218,20 +267,26 @@ public async Task CorsMiddleware_WithCorsEndpoint() public async Task CorsMiddleware_ConfiguredBeforeRouting_Throws() { // Arrange - var builder = new WebHostBuilder(); - builder.Configure(app => - { - app.UseCors(); - app.UseRouting(); - app.UseEndpoints(b => b.Map("/", TestDelegate).RequireCors(policy => policy.AllowAnyOrigin())); - }) - .ConfigureServices(services => - { - services.AddCors(); - services.AddRouting(); - }); - - using var server = new TestServer(builder); + var host = new HostBuilder() + .ConfigureWebHost(webHostBuilder => + { + webHostBuilder + .Configure(app => + { + app.UseCors(); + app.UseRouting(); + app.UseEndpoints(b => b.Map("/", TestDelegate).RequireCors(policy => policy.AllowAnyOrigin())); + }) + .UseTestServer(); + }) + .ConfigureServices(services => + { + services.AddCors(); + services.AddRouting(); + }) + .Build(); + + using var server = host.GetTestServer(); var ex = await Assert.ThrowsAsync(() => server.CreateRequest("/").SendAsync("GET")); Assert.Equal(CORSErrorMessage, ex.Message); diff --git a/src/Http/Routing/test/FunctionalTests/RoutingTestFixture.cs b/src/Http/Routing/test/FunctionalTests/RoutingTestFixture.cs index 51c9bc8c5ac7..eefbdb074856 100644 --- a/src/Http/Routing/test/FunctionalTests/RoutingTestFixture.cs +++ b/src/Http/Routing/test/FunctionalTests/RoutingTestFixture.cs @@ -5,6 +5,7 @@ using System.Net.Http; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.TestHost; +using Microsoft.Extensions.Hosting; namespace Microsoft.AspNetCore.Routing.FunctionalTests { @@ -14,10 +15,15 @@ public class RoutingTestFixture : IDisposable public RoutingTestFixture() { - var builder = new WebHostBuilder() - .UseStartup(typeof(TStartup)); - - _server = new TestServer(builder); + var host = new HostBuilder() + .ConfigureWebHost(webHostBuilder => + { + webHostBuilder + .UseStartup(typeof(TStartup)); + }) + .Build(); + + _server = host.GetTestServer(); Client = _server.CreateClient(); Client.BaseAddress = new Uri("http://localhost"); From 527096a3415b1b3f7d1f00098255d4cede9160b4 Mon Sep 17 00:00:00 2001 From: Kahbazi Date: Fri, 24 Jul 2020 20:50:20 +0430 Subject: [PATCH 2/5] Add missing UseTestServer --- src/Http/Routing/test/FunctionalTests/RoutingTestFixture.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Http/Routing/test/FunctionalTests/RoutingTestFixture.cs b/src/Http/Routing/test/FunctionalTests/RoutingTestFixture.cs index eefbdb074856..f8e4fc867de3 100644 --- a/src/Http/Routing/test/FunctionalTests/RoutingTestFixture.cs +++ b/src/Http/Routing/test/FunctionalTests/RoutingTestFixture.cs @@ -19,7 +19,8 @@ public RoutingTestFixture() .ConfigureWebHost(webHostBuilder => { webHostBuilder - .UseStartup(typeof(TStartup)); + .UseStartup(typeof(TStartup)) + .UseTestServer(); }) .Build(); From 3c29387b6beed0bc81448d9eeb82659843a01fef Mon Sep 17 00:00:00 2001 From: Kahbazi Date: Fri, 24 Jul 2020 23:55:10 +0430 Subject: [PATCH 3/5] Start host, change missing files --- .../EndpointRoutingBenchmarkTest.cs | 20 ++++++--- .../Benchmarks/RouterBenchmarkTest.cs | 23 +++++++--- .../EndpointRoutingIntegrationTest.cs | 36 +++++++++++---- .../EndpointRoutingSampleTest.cs | 11 ++++- .../test/FunctionalTests/RouterSampleTest.cs | 9 +++- .../WebHostBuilderExtensionsTest.cs | 24 ++++++---- .../testassets/Benchmarks/Benchmarks.csproj | 1 + .../test/testassets/Benchmarks/Program.cs | 44 +++++++++++++------ .../test/testassets/RoutingSandbox/Program.cs | 27 +++++++----- .../RoutingSandbox/RoutingSandbox.csproj | 1 + .../test/testassets/RoutingWebSite/Program.cs | 30 ++++++++----- .../RoutingWebSite/RoutingWebSite.csproj | 1 + 12 files changed, 158 insertions(+), 69 deletions(-) diff --git a/src/Http/Routing/test/FunctionalTests/Benchmarks/EndpointRoutingBenchmarkTest.cs b/src/Http/Routing/test/FunctionalTests/Benchmarks/EndpointRoutingBenchmarkTest.cs index b40da83fc914..2a5f1fe06be5 100644 --- a/src/Http/Routing/test/FunctionalTests/Benchmarks/EndpointRoutingBenchmarkTest.cs +++ b/src/Http/Routing/test/FunctionalTests/Benchmarks/EndpointRoutingBenchmarkTest.cs @@ -1,4 +1,4 @@ -// Copyright (c) .NET Foundation. All rights reserved. +// Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; @@ -6,6 +6,7 @@ using System.Net.Http; using System.Threading.Tasks; using Microsoft.AspNetCore.TestHost; +using Microsoft.Extensions.Hosting; using Xunit; namespace Microsoft.AspNetCore.Routing.FunctionalTests @@ -13,19 +14,25 @@ namespace Microsoft.AspNetCore.Routing.FunctionalTests public class EndpointRoutingBenchmarkTest : IDisposable { private readonly HttpClient _client; + private readonly IHost _host; private readonly TestServer _testServer; public EndpointRoutingBenchmarkTest() { // This switch and value are set by benchmark server when running the app for profiling. var args = new[] { "--scenarios", "PlaintextEndpointRouting" }; - var webHostBuilder = Benchmarks.Program.GetWebHostBuilder(args); + var hostBuilder = Benchmarks.Program.GetHostBuilder(args); - // Make sure we are using the right startup - var startupName = webHostBuilder.GetSetting("Startup"); - Assert.Equal(nameof(Benchmarks.StartupUsingEndpointRouting), startupName); + hostBuilder.ConfigureWebHost(webHostBuilder => + { + // Make sure we are using the right startup + var startupName = webHostBuilder.GetSetting("Startup"); + Assert.Equal(nameof(Benchmarks.StartupUsingEndpointRouting), startupName); + }); - _testServer = new TestServer(webHostBuilder); + _host = hostBuilder.Build(); + _testServer = _host.GetTestServer(); + _host.Start(); _client = _testServer.CreateClient(); _client.BaseAddress = new Uri("http://localhost"); } @@ -53,6 +60,7 @@ public void Dispose() { _testServer.Dispose(); _client.Dispose(); + _host.Dispose(); } } } diff --git a/src/Http/Routing/test/FunctionalTests/Benchmarks/RouterBenchmarkTest.cs b/src/Http/Routing/test/FunctionalTests/Benchmarks/RouterBenchmarkTest.cs index aecbaaab8c47..c3bddf1e1050 100644 --- a/src/Http/Routing/test/FunctionalTests/Benchmarks/RouterBenchmarkTest.cs +++ b/src/Http/Routing/test/FunctionalTests/Benchmarks/RouterBenchmarkTest.cs @@ -1,4 +1,4 @@ -// Copyright (c) .NET Foundation. All rights reserved. +// Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; @@ -6,6 +6,8 @@ using System.Net.Http; using System.Threading.Tasks; using Microsoft.AspNetCore.TestHost; +using Microsoft.AspNetCore.Hosting; +using Microsoft.Extensions.Hosting; using Xunit; namespace Microsoft.AspNetCore.Routing.FunctionalTests @@ -13,19 +15,25 @@ namespace Microsoft.AspNetCore.Routing.FunctionalTests public class RouterBenchmarkTest : IDisposable { private readonly HttpClient _client; + private readonly IHost _host; private readonly TestServer _testServer; public RouterBenchmarkTest() { // This switch and value are set by benchmark server when running the app for profiling. var args = new[] { "--scenarios", "PlaintextRouting" }; - var webHostBuilder = Benchmarks.Program.GetWebHostBuilder(args); + var hostBuilder = Benchmarks.Program.GetHostBuilder(args); - // Make sure we are using the right startup - var startupName = webHostBuilder.GetSetting("Startup"); - Assert.Equal(nameof(Benchmarks.StartupUsingRouter), startupName); + hostBuilder.ConfigureWebHost(webHostBuilder => + { + // Make sure we are using the right startup + var startupName = webHostBuilder.GetSetting("Startup"); + Assert.Equal(nameof(Benchmarks.StartupUsingRouter), startupName); + }); - _testServer = new TestServer(webHostBuilder); + _host = hostBuilder.Build(); + _testServer = _host.GetTestServer(); + _host.Start(); _client = _testServer.CreateClient(); _client.BaseAddress = new Uri("http://localhost"); } @@ -53,6 +61,7 @@ public void Dispose() { _testServer.Dispose(); _client.Dispose(); + _host.Dispose(); } } -} \ No newline at end of file +} diff --git a/src/Http/Routing/test/FunctionalTests/EndpointRoutingIntegrationTest.cs b/src/Http/Routing/test/FunctionalTests/EndpointRoutingIntegrationTest.cs index 5afb5521c01f..860ebac13359 100644 --- a/src/Http/Routing/test/FunctionalTests/EndpointRoutingIntegrationTest.cs +++ b/src/Http/Routing/test/FunctionalTests/EndpointRoutingIntegrationTest.cs @@ -32,7 +32,7 @@ public class EndpointRoutingIntegrationTest public async Task AuthorizationMiddleware_WhenNoAuthMetadataIsConfigured() { // Arrange - var host = new HostBuilder() + using var host = new HostBuilder() .ConfigureWebHost(webHostBuilder => { webHostBuilder @@ -53,6 +53,8 @@ public async Task AuthorizationMiddleware_WhenNoAuthMetadataIsConfigured() using var server = host.GetTestServer(); + await host.StartAsync(); + var response = await server.CreateRequest("/").SendAsync("GET"); response.EnsureSuccessStatusCode(); @@ -62,7 +64,7 @@ public async Task AuthorizationMiddleware_WhenNoAuthMetadataIsConfigured() public async Task AuthorizationMiddleware_WhenEndpointIsNotFound() { // Arrange - var host = new HostBuilder() + using var host = new HostBuilder() .ConfigureWebHost(webHostBuilder => { webHostBuilder @@ -83,6 +85,8 @@ public async Task AuthorizationMiddleware_WhenEndpointIsNotFound() using var server = host.GetTestServer(); + await host.StartAsync(); + var response = await server.CreateRequest("/not-found").SendAsync("GET"); Assert.Equal(HttpStatusCode.NotFound, response.StatusCode); @@ -92,7 +96,7 @@ public async Task AuthorizationMiddleware_WhenEndpointIsNotFound() public async Task AuthorizationMiddleware_WithAuthorizedEndpoint() { // Arrange - var host = new HostBuilder() + using var host = new HostBuilder() .ConfigureWebHost(webHostBuilder => { webHostBuilder @@ -113,6 +117,8 @@ public async Task AuthorizationMiddleware_WithAuthorizedEndpoint() using var server = host.GetTestServer(); + await host.StartAsync(); + var response = await server.CreateRequest("/").SendAsync("GET"); response.EnsureSuccessStatusCode(); @@ -122,7 +128,7 @@ public async Task AuthorizationMiddleware_WithAuthorizedEndpoint() public async Task AuthorizationMiddleware_NotConfigured_Throws() { // Arrange - var host = new HostBuilder() + using var host = new HostBuilder() .ConfigureWebHost(webHostBuilder => { webHostBuilder @@ -143,6 +149,8 @@ public async Task AuthorizationMiddleware_NotConfigured_Throws() using var server = host.GetTestServer(); + await host.StartAsync(); + var ex = await Assert.ThrowsAsync(() => server.CreateRequest("/").SendAsync("GET")); Assert.Equal(AuthErrorMessage, ex.Message); } @@ -151,7 +159,7 @@ public async Task AuthorizationMiddleware_NotConfigured_Throws() public async Task AuthorizationMiddleware_NotConfigured_WhenEndpointIsNotFound() { // Arrange - var host = new HostBuilder() + using var host = new HostBuilder() .ConfigureWebHost(webHostBuilder => { webHostBuilder @@ -170,6 +178,8 @@ public async Task AuthorizationMiddleware_NotConfigured_WhenEndpointIsNotFound() using var server = host.GetTestServer(); + await host.StartAsync(); + var response = await server.CreateRequest("/not-found").SendAsync("GET"); Assert.Equal(HttpStatusCode.NotFound, response.StatusCode); @@ -179,7 +189,7 @@ public async Task AuthorizationMiddleware_NotConfigured_WhenEndpointIsNotFound() public async Task AuthorizationMiddleware_ConfiguredBeforeRouting_Throws() { // Arrange - var host = new HostBuilder() + using var host = new HostBuilder() .ConfigureWebHost(webHostBuilder => { webHostBuilder @@ -200,6 +210,8 @@ public async Task AuthorizationMiddleware_ConfiguredBeforeRouting_Throws() using var server = host.GetTestServer(); + await host.StartAsync(); + var ex = await Assert.ThrowsAsync(() => server.CreateRequest("/").SendAsync("GET")); Assert.Equal(AuthErrorMessage, ex.Message); } @@ -208,7 +220,7 @@ public async Task AuthorizationMiddleware_ConfiguredBeforeRouting_Throws() public async Task AuthorizationMiddleware_ConfiguredAfterRouting_Throws() { // Arrange - var host = new HostBuilder() + using var host = new HostBuilder() .ConfigureWebHost(webHostBuilder => { webHostBuilder @@ -229,6 +241,8 @@ public async Task AuthorizationMiddleware_ConfiguredAfterRouting_Throws() using var server = host.GetTestServer(); + await host.StartAsync(); + var ex = await Assert.ThrowsAsync(() => server.CreateRequest("/").SendAsync("GET")); Assert.Equal(AuthErrorMessage, ex.Message); } @@ -237,7 +251,7 @@ public async Task AuthorizationMiddleware_ConfiguredAfterRouting_Throws() public async Task CorsMiddleware_WithCorsEndpoint() { // Arrange - var host = new HostBuilder() + using var host = new HostBuilder() .ConfigureWebHost(webHostBuilder => { webHostBuilder @@ -258,6 +272,8 @@ public async Task CorsMiddleware_WithCorsEndpoint() using var server = host.GetTestServer(); + await host.StartAsync(); + var response = await server.CreateRequest("/").SendAsync("PUT"); response.EnsureSuccessStatusCode(); @@ -267,7 +283,7 @@ public async Task CorsMiddleware_WithCorsEndpoint() public async Task CorsMiddleware_ConfiguredBeforeRouting_Throws() { // Arrange - var host = new HostBuilder() + using var host = new HostBuilder() .ConfigureWebHost(webHostBuilder => { webHostBuilder @@ -288,6 +304,8 @@ public async Task CorsMiddleware_ConfiguredBeforeRouting_Throws() using var server = host.GetTestServer(); + await host.StartAsync(); + var ex = await Assert.ThrowsAsync(() => server.CreateRequest("/").SendAsync("GET")); Assert.Equal(CORSErrorMessage, ex.Message); } diff --git a/src/Http/Routing/test/FunctionalTests/EndpointRoutingSampleTest.cs b/src/Http/Routing/test/FunctionalTests/EndpointRoutingSampleTest.cs index e5ad2bd10a5a..aa575ab08483 100644 --- a/src/Http/Routing/test/FunctionalTests/EndpointRoutingSampleTest.cs +++ b/src/Http/Routing/test/FunctionalTests/EndpointRoutingSampleTest.cs @@ -6,6 +6,7 @@ using System.Net.Http; using System.Threading.Tasks; using Microsoft.AspNetCore.TestHost; +using Microsoft.Extensions.Hosting; using RoutingWebSite; using Xunit; @@ -14,12 +15,17 @@ namespace Microsoft.AspNetCore.Routing.FunctionalTests public class EndpointRoutingSampleTest : IDisposable { private readonly HttpClient _client; + private readonly IHost _host; private readonly TestServer _testServer; public EndpointRoutingSampleTest() { - var webHostBuilder = Program.GetWebHostBuilder(new[] { Program.EndpointRoutingScenario, }); - _testServer = new TestServer(webHostBuilder); + var hostBuilder = Program.GetHostBuilder(new[] { Program.EndpointRoutingScenario, }); + _host = hostBuilder.Build(); + + _testServer = _host.GetTestServer(); + _host.Start(); + _client = _testServer.CreateClient(); _client.BaseAddress = new Uri("http://localhost"); } @@ -227,6 +233,7 @@ public void Dispose() { _testServer.Dispose(); _client.Dispose(); + _host.Dispose(); } } } diff --git a/src/Http/Routing/test/FunctionalTests/RouterSampleTest.cs b/src/Http/Routing/test/FunctionalTests/RouterSampleTest.cs index 9388a037815c..d0b93a896518 100644 --- a/src/Http/Routing/test/FunctionalTests/RouterSampleTest.cs +++ b/src/Http/Routing/test/FunctionalTests/RouterSampleTest.cs @@ -6,6 +6,7 @@ using System.Net.Http; using System.Threading.Tasks; using Microsoft.AspNetCore.TestHost; +using Microsoft.Extensions.Hosting; using RoutingWebSite; using Xunit; @@ -14,12 +15,15 @@ namespace Microsoft.AspNetCore.Routing.FunctionalTests public class RouterSampleTest : IDisposable { private readonly HttpClient _client; + private readonly IHost _host; private readonly TestServer _testServer; public RouterSampleTest() { - var webHostBuilder = Program.GetWebHostBuilder(new[] { Program.RouterScenario, }); - _testServer = new TestServer(webHostBuilder); + var hostBuilder = Program.GetHostBuilder(new[] { Program.RouterScenario, }); + _host = hostBuilder.Build(); + _testServer = _host.GetTestServer(); + _host.Start(); _client = _testServer.CreateClient(); _client.BaseAddress = new Uri("http://localhost"); } @@ -96,6 +100,7 @@ public void Dispose() { _testServer.Dispose(); _client.Dispose(); + _host.Dispose(); } } } diff --git a/src/Http/Routing/test/FunctionalTests/WebHostBuilderExtensionsTest.cs b/src/Http/Routing/test/FunctionalTests/WebHostBuilderExtensionsTest.cs index 7466006026bf..59373e259478 100644 --- a/src/Http/Routing/test/FunctionalTests/WebHostBuilderExtensionsTest.cs +++ b/src/Http/Routing/test/FunctionalTests/WebHostBuilderExtensionsTest.cs @@ -1,4 +1,4 @@ -// Copyright (c) .NET Foundation. All rights reserved. +// Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; @@ -12,6 +12,7 @@ using Microsoft.AspNetCore.TestHost; using Microsoft.Extensions.DependencyInjection; using Xunit; +using Microsoft.Extensions.Hosting; namespace Microsoft.AspNetCore.Routing.FunctionalTests { @@ -79,14 +80,21 @@ public static TheoryData, HttpRequestMessage, string> Matc public async Task UseRouter_MapGet_MatchesRequest(Action routeBuilder, HttpRequestMessage request, string expected) { // Arrange - var webhostbuilder = new WebHostBuilder(); - webhostbuilder - .ConfigureServices(services => services.AddRouting()) - .Configure(app => + using var host = new HostBuilder() + .ConfigureWebHost(webhostbuilder => { - app.UseRouter(routeBuilder); - }); - var testServer = new TestServer(webhostbuilder); + webhostbuilder + .Configure(app => + { + app.UseRouter(routeBuilder); + }) + .UseTestServer(); + }) + .ConfigureServices(services => services.AddRouting()) + .Build(); + + var testServer = host.GetTestServer(); + await host.StartAsync(); var client = testServer.CreateClient(); // Act diff --git a/src/Http/Routing/test/testassets/Benchmarks/Benchmarks.csproj b/src/Http/Routing/test/testassets/Benchmarks/Benchmarks.csproj index 8bab94164fa6..6f70f9b391ff 100644 --- a/src/Http/Routing/test/testassets/Benchmarks/Benchmarks.csproj +++ b/src/Http/Routing/test/testassets/Benchmarks/Benchmarks.csproj @@ -8,6 +8,7 @@ + diff --git a/src/Http/Routing/test/testassets/Benchmarks/Program.cs b/src/Http/Routing/test/testassets/Benchmarks/Program.cs index 4e832df4a2fd..b4b29acb0304 100644 --- a/src/Http/Routing/test/testassets/Benchmarks/Program.cs +++ b/src/Http/Routing/test/testassets/Benchmarks/Program.cs @@ -1,20 +1,23 @@ -// Copyright (c) .NET Foundation. All rights reserved. +// Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; +using System.Threading.Tasks; using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore.TestHost; using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.Hosting; namespace Benchmarks { public class Program { - public static void Main(string[] args) + public static Task Main(string[] args) { - GetWebHostBuilder(args).Build().Run(); + return GetHostBuilder(args).Build().RunAsync(); } - public static IWebHostBuilder GetWebHostBuilder(string[] args) + public static IHostBuilder GetHostBuilder(string[] args) { var config = new ConfigurationBuilder() .AddCommandLine(args) @@ -24,22 +27,35 @@ public static IWebHostBuilder GetWebHostBuilder(string[] args) // Consoler logger has a major impact on perf results, so do not use // default builder. - var webHostBuilder = new WebHostBuilder() - .UseConfiguration(config) - .UseKestrel(); + var hostBuilder = new HostBuilder() + .ConfigureWebHost(webHostBuilder => + { + webHostBuilder + .UseKestrel() + .UseTestServer() + .UseConfiguration(config); + }); var scenario = config["scenarios"]?.ToLower(); if (scenario == "plaintextdispatcher" || scenario == "plaintextendpointrouting") { - webHostBuilder.UseStartup(); - // for testing - webHostBuilder.UseSetting("Startup", nameof(StartupUsingEndpointRouting)); + hostBuilder.ConfigureWebHost(webHostBuilder => + { + webHostBuilder + .UseStartup() + // for testing + .UseSetting("Startup", nameof(StartupUsingEndpointRouting)); + }); } else if (scenario == "plaintextrouting" || scenario == "plaintextrouter") { - webHostBuilder.UseStartup(); - // for testing - webHostBuilder.UseSetting("Startup", nameof(StartupUsingRouter)); + hostBuilder.ConfigureWebHost(webHostBuilder => + { + webHostBuilder + .UseStartup() + // for testing + .UseSetting("Startup", nameof(StartupUsingRouter)); + }); } else { @@ -47,7 +63,7 @@ public static IWebHostBuilder GetWebHostBuilder(string[] args) $"Invalid scenario '{scenario}'. Allowed scenarios are PlaintextEndpointRouting and PlaintextRouter"); } - return webHostBuilder; + return hostBuilder; } } } diff --git a/src/Http/Routing/test/testassets/RoutingSandbox/Program.cs b/src/Http/Routing/test/testassets/RoutingSandbox/Program.cs index 4f2d60bc7688..edcc06274554 100644 --- a/src/Http/Routing/test/testassets/RoutingSandbox/Program.cs +++ b/src/Http/Routing/test/testassets/RoutingSandbox/Program.cs @@ -2,8 +2,11 @@ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; +using System.Threading.Tasks; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore.TestHost; +using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; namespace RoutingSandbox @@ -13,14 +16,14 @@ public class Program public const string EndpointRoutingScenario = "endpointrouting"; public const string RouterScenario = "router"; - public static void Main(string[] args) + public static Task Main(string[] args) { - var webHost = GetWebHostBuilder(args).Build(); - webHost.Run(); + var host = GetHostBuilder(args).Build(); + return host.RunAsync(); } // For unit testing - public static IWebHostBuilder GetWebHostBuilder(string[] args) + public static IHostBuilder GetHostBuilder(string[] args) { string scenario; if (args.Length == 0) @@ -57,16 +60,20 @@ public static IWebHostBuilder GetWebHostBuilder(string[] args) } - return new WebHostBuilder() - .UseKestrel() - .UseIISIntegration() + return new HostBuilder() + .ConfigureWebHost(webHostBuilder => + { + webHostBuilder + .UseKestrel() + .UseIISIntegration() + .UseContentRoot(Environment.CurrentDirectory) + .UseStartup(startupType); + }) .ConfigureLogging(b => { b.AddConsole(); b.SetMinimumLevel(LogLevel.Critical); - }) - .UseContentRoot(Environment.CurrentDirectory) - .UseStartup(startupType); + }); } } } diff --git a/src/Http/Routing/test/testassets/RoutingSandbox/RoutingSandbox.csproj b/src/Http/Routing/test/testassets/RoutingSandbox/RoutingSandbox.csproj index 873236404fd4..554514e46f57 100644 --- a/src/Http/Routing/test/testassets/RoutingSandbox/RoutingSandbox.csproj +++ b/src/Http/Routing/test/testassets/RoutingSandbox/RoutingSandbox.csproj @@ -5,6 +5,7 @@ + diff --git a/src/Http/Routing/test/testassets/RoutingWebSite/Program.cs b/src/Http/Routing/test/testassets/RoutingWebSite/Program.cs index d96af1814f6a..3727c9a9a82b 100644 --- a/src/Http/Routing/test/testassets/RoutingWebSite/Program.cs +++ b/src/Http/Routing/test/testassets/RoutingWebSite/Program.cs @@ -1,9 +1,12 @@ -// Copyright (c) .NET Foundation. All rights reserved. +// Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; +using System.Threading.Tasks; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore.TestHost; +using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; namespace RoutingWebSite @@ -13,14 +16,14 @@ public class Program public const string EndpointRoutingScenario = "endpointrouting"; public const string RouterScenario = "router"; - public static void Main(string[] args) + public static Task Main(string[] args) { - var webHost = GetWebHostBuilder(args).Build(); - webHost.Run(); + var host = GetHostBuilder(args).Build(); + return host.RunAsync(); } // For unit testing - public static IWebHostBuilder GetWebHostBuilder(string[] args) + public static IHostBuilder GetHostBuilder(string[] args) { string scenario; if (args.Length == 0) @@ -57,16 +60,21 @@ public static IWebHostBuilder GetWebHostBuilder(string[] args) } - return new WebHostBuilder() - .UseKestrel() - .UseIISIntegration() + return new HostBuilder() + .ConfigureWebHost(webHostBuilder => + { + webHostBuilder + .UseKestrel() + .UseIISIntegration() + .UseContentRoot(Environment.CurrentDirectory) + .UseStartup(startupType) + .UseTestServer(); + }) .ConfigureLogging(b => { b.AddConsole(); b.SetMinimumLevel(LogLevel.Critical); - }) - .UseContentRoot(Environment.CurrentDirectory) - .UseStartup(startupType); + }); } } } diff --git a/src/Http/Routing/test/testassets/RoutingWebSite/RoutingWebSite.csproj b/src/Http/Routing/test/testassets/RoutingWebSite/RoutingWebSite.csproj index 873236404fd4..554514e46f57 100644 --- a/src/Http/Routing/test/testassets/RoutingWebSite/RoutingWebSite.csproj +++ b/src/Http/Routing/test/testassets/RoutingWebSite/RoutingWebSite.csproj @@ -5,6 +5,7 @@ + From 5c981a88953fbf68f5f772875693eb219f7ec27b Mon Sep 17 00:00:00 2001 From: Kahbazi Date: Sat, 25 Jul 2020 15:32:11 +0430 Subject: [PATCH 4/5] Start the host --- src/Http/Routing/test/FunctionalTests/RoutingTestFixture.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Http/Routing/test/FunctionalTests/RoutingTestFixture.cs b/src/Http/Routing/test/FunctionalTests/RoutingTestFixture.cs index f8e4fc867de3..c0c97347f354 100644 --- a/src/Http/Routing/test/FunctionalTests/RoutingTestFixture.cs +++ b/src/Http/Routing/test/FunctionalTests/RoutingTestFixture.cs @@ -26,6 +26,8 @@ public RoutingTestFixture() _server = host.GetTestServer(); + host.Start(); + Client = _server.CreateClient(); Client.BaseAddress = new Uri("http://localhost"); } From 055bcd95e4dbb5fe2f3de74778dea320203b3b15 Mon Sep 17 00:00:00 2001 From: Kahbazi Date: Sat, 25 Jul 2020 19:16:26 +0430 Subject: [PATCH 5/5] Fix tests --- .../Benchmarks/EndpointRoutingBenchmarkTest.cs | 15 ++++++++------- .../Benchmarks/RouterBenchmarkTest.cs | 15 ++++++++------- 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/src/Http/Routing/test/FunctionalTests/Benchmarks/EndpointRoutingBenchmarkTest.cs b/src/Http/Routing/test/FunctionalTests/Benchmarks/EndpointRoutingBenchmarkTest.cs index 2a5f1fe06be5..003db4f95f7d 100644 --- a/src/Http/Routing/test/FunctionalTests/Benchmarks/EndpointRoutingBenchmarkTest.cs +++ b/src/Http/Routing/test/FunctionalTests/Benchmarks/EndpointRoutingBenchmarkTest.cs @@ -6,6 +6,8 @@ using System.Net.Http; using System.Threading.Tasks; using Microsoft.AspNetCore.TestHost; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Xunit; @@ -23,14 +25,13 @@ public EndpointRoutingBenchmarkTest() var args = new[] { "--scenarios", "PlaintextEndpointRouting" }; var hostBuilder = Benchmarks.Program.GetHostBuilder(args); - hostBuilder.ConfigureWebHost(webHostBuilder => - { - // Make sure we are using the right startup - var startupName = webHostBuilder.GetSetting("Startup"); - Assert.Equal(nameof(Benchmarks.StartupUsingEndpointRouting), startupName); - }); - _host = hostBuilder.Build(); + + // Make sure we are using the right startup + var configuration = _host.Services.GetService(); + var startupName = configuration["Startup"]; + Assert.Equal(nameof(Benchmarks.StartupUsingEndpointRouting), startupName); + _testServer = _host.GetTestServer(); _host.Start(); _client = _testServer.CreateClient(); diff --git a/src/Http/Routing/test/FunctionalTests/Benchmarks/RouterBenchmarkTest.cs b/src/Http/Routing/test/FunctionalTests/Benchmarks/RouterBenchmarkTest.cs index c3bddf1e1050..ccd11a551a91 100644 --- a/src/Http/Routing/test/FunctionalTests/Benchmarks/RouterBenchmarkTest.cs +++ b/src/Http/Routing/test/FunctionalTests/Benchmarks/RouterBenchmarkTest.cs @@ -7,6 +7,8 @@ using System.Threading.Tasks; using Microsoft.AspNetCore.TestHost; using Microsoft.AspNetCore.Hosting; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Xunit; @@ -24,14 +26,13 @@ public RouterBenchmarkTest() var args = new[] { "--scenarios", "PlaintextRouting" }; var hostBuilder = Benchmarks.Program.GetHostBuilder(args); - hostBuilder.ConfigureWebHost(webHostBuilder => - { - // Make sure we are using the right startup - var startupName = webHostBuilder.GetSetting("Startup"); - Assert.Equal(nameof(Benchmarks.StartupUsingRouter), startupName); - }); - _host = hostBuilder.Build(); + + // Make sure we are using the right startup + var configuration = _host.Services.GetService(); + var startupName = configuration["Startup"]; + Assert.Equal(nameof(Benchmarks.StartupUsingRouter), startupName); + _testServer = _host.GetTestServer(); _host.Start(); _client = _testServer.CreateClient();