[Routing] Move to GenericHost (#24281)
This commit is contained in:
parent
f26942805d
commit
e893ef59e8
|
|
@ -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.
|
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||||
|
|
||||||
using System;
|
using System;
|
||||||
|
|
@ -6,6 +6,9 @@ using System.Net;
|
||||||
using System.Net.Http;
|
using System.Net.Http;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Microsoft.AspNetCore.TestHost;
|
using Microsoft.AspNetCore.TestHost;
|
||||||
|
using Microsoft.Extensions.Configuration;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
using Microsoft.Extensions.Hosting;
|
||||||
using Xunit;
|
using Xunit;
|
||||||
|
|
||||||
namespace Microsoft.AspNetCore.Routing.FunctionalTests
|
namespace Microsoft.AspNetCore.Routing.FunctionalTests
|
||||||
|
|
@ -13,19 +16,24 @@ namespace Microsoft.AspNetCore.Routing.FunctionalTests
|
||||||
public class EndpointRoutingBenchmarkTest : IDisposable
|
public class EndpointRoutingBenchmarkTest : IDisposable
|
||||||
{
|
{
|
||||||
private readonly HttpClient _client;
|
private readonly HttpClient _client;
|
||||||
|
private readonly IHost _host;
|
||||||
private readonly TestServer _testServer;
|
private readonly TestServer _testServer;
|
||||||
|
|
||||||
public EndpointRoutingBenchmarkTest()
|
public EndpointRoutingBenchmarkTest()
|
||||||
{
|
{
|
||||||
// This switch and value are set by benchmark server when running the app for profiling.
|
// This switch and value are set by benchmark server when running the app for profiling.
|
||||||
var args = new[] { "--scenarios", "PlaintextEndpointRouting" };
|
var args = new[] { "--scenarios", "PlaintextEndpointRouting" };
|
||||||
var webHostBuilder = Benchmarks.Program.GetWebHostBuilder(args);
|
var hostBuilder = Benchmarks.Program.GetHostBuilder(args);
|
||||||
|
|
||||||
|
_host = hostBuilder.Build();
|
||||||
|
|
||||||
// Make sure we are using the right startup
|
// Make sure we are using the right startup
|
||||||
var startupName = webHostBuilder.GetSetting("Startup");
|
var configuration = _host.Services.GetService<IConfiguration>();
|
||||||
Assert.Equal(nameof(Benchmarks.StartupUsingEndpointRouting), startupName);
|
var startupName = configuration["Startup"];
|
||||||
|
Assert.Equal(nameof(Benchmarks.StartupUsingEndpointRouting), startupName);
|
||||||
|
|
||||||
_testServer = new TestServer(webHostBuilder);
|
_testServer = _host.GetTestServer();
|
||||||
|
_host.Start();
|
||||||
_client = _testServer.CreateClient();
|
_client = _testServer.CreateClient();
|
||||||
_client.BaseAddress = new Uri("http://localhost");
|
_client.BaseAddress = new Uri("http://localhost");
|
||||||
}
|
}
|
||||||
|
|
@ -53,6 +61,7 @@ namespace Microsoft.AspNetCore.Routing.FunctionalTests
|
||||||
{
|
{
|
||||||
_testServer.Dispose();
|
_testServer.Dispose();
|
||||||
_client.Dispose();
|
_client.Dispose();
|
||||||
|
_host.Dispose();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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.
|
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||||
|
|
||||||
using System;
|
using System;
|
||||||
|
|
@ -6,6 +6,10 @@ using System.Net;
|
||||||
using System.Net.Http;
|
using System.Net.Http;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Microsoft.AspNetCore.TestHost;
|
using Microsoft.AspNetCore.TestHost;
|
||||||
|
using Microsoft.AspNetCore.Hosting;
|
||||||
|
using Microsoft.Extensions.Configuration;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
using Microsoft.Extensions.Hosting;
|
||||||
using Xunit;
|
using Xunit;
|
||||||
|
|
||||||
namespace Microsoft.AspNetCore.Routing.FunctionalTests
|
namespace Microsoft.AspNetCore.Routing.FunctionalTests
|
||||||
|
|
@ -13,19 +17,24 @@ namespace Microsoft.AspNetCore.Routing.FunctionalTests
|
||||||
public class RouterBenchmarkTest : IDisposable
|
public class RouterBenchmarkTest : IDisposable
|
||||||
{
|
{
|
||||||
private readonly HttpClient _client;
|
private readonly HttpClient _client;
|
||||||
|
private readonly IHost _host;
|
||||||
private readonly TestServer _testServer;
|
private readonly TestServer _testServer;
|
||||||
|
|
||||||
public RouterBenchmarkTest()
|
public RouterBenchmarkTest()
|
||||||
{
|
{
|
||||||
// This switch and value are set by benchmark server when running the app for profiling.
|
// This switch and value are set by benchmark server when running the app for profiling.
|
||||||
var args = new[] { "--scenarios", "PlaintextRouting" };
|
var args = new[] { "--scenarios", "PlaintextRouting" };
|
||||||
var webHostBuilder = Benchmarks.Program.GetWebHostBuilder(args);
|
var hostBuilder = Benchmarks.Program.GetHostBuilder(args);
|
||||||
|
|
||||||
|
_host = hostBuilder.Build();
|
||||||
|
|
||||||
// Make sure we are using the right startup
|
// Make sure we are using the right startup
|
||||||
var startupName = webHostBuilder.GetSetting("Startup");
|
var configuration = _host.Services.GetService<IConfiguration>();
|
||||||
|
var startupName = configuration["Startup"];
|
||||||
Assert.Equal(nameof(Benchmarks.StartupUsingRouter), startupName);
|
Assert.Equal(nameof(Benchmarks.StartupUsingRouter), startupName);
|
||||||
|
|
||||||
_testServer = new TestServer(webHostBuilder);
|
_testServer = _host.GetTestServer();
|
||||||
|
_host.Start();
|
||||||
_client = _testServer.CreateClient();
|
_client = _testServer.CreateClient();
|
||||||
_client.BaseAddress = new Uri("http://localhost");
|
_client.BaseAddress = new Uri("http://localhost");
|
||||||
}
|
}
|
||||||
|
|
@ -53,6 +62,7 @@ namespace Microsoft.AspNetCore.Routing.FunctionalTests
|
||||||
{
|
{
|
||||||
_testServer.Dispose();
|
_testServer.Dispose();
|
||||||
_client.Dispose();
|
_client.Dispose();
|
||||||
|
_host.Dispose();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -10,6 +10,7 @@ using Microsoft.AspNetCore.Hosting;
|
||||||
using Microsoft.AspNetCore.Http;
|
using Microsoft.AspNetCore.Http;
|
||||||
using Microsoft.AspNetCore.TestHost;
|
using Microsoft.AspNetCore.TestHost;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
using Microsoft.Extensions.Hosting;
|
||||||
using Xunit;
|
using Xunit;
|
||||||
|
|
||||||
namespace Microsoft.AspNetCore.Routing.FunctionalTests
|
namespace Microsoft.AspNetCore.Routing.FunctionalTests
|
||||||
|
|
@ -31,20 +32,28 @@ namespace Microsoft.AspNetCore.Routing.FunctionalTests
|
||||||
public async Task AuthorizationMiddleware_WhenNoAuthMetadataIsConfigured()
|
public async Task AuthorizationMiddleware_WhenNoAuthMetadataIsConfigured()
|
||||||
{
|
{
|
||||||
// Arrange
|
// Arrange
|
||||||
var builder = new WebHostBuilder();
|
using var host = new HostBuilder()
|
||||||
builder.Configure(app =>
|
.ConfigureWebHost(webHostBuilder =>
|
||||||
{
|
{
|
||||||
app.UseRouting();
|
webHostBuilder
|
||||||
app.UseAuthorization();
|
.Configure(app =>
|
||||||
app.UseEndpoints(b => b.Map("/", TestDelegate));
|
{
|
||||||
})
|
app.UseRouting();
|
||||||
.ConfigureServices(services =>
|
app.UseAuthorization();
|
||||||
{
|
app.UseEndpoints(b => b.Map("/", TestDelegate));
|
||||||
services.AddAuthorization();
|
})
|
||||||
services.AddRouting();
|
.UseTestServer();
|
||||||
});
|
})
|
||||||
|
.ConfigureServices(services =>
|
||||||
|
{
|
||||||
|
services.AddAuthorization();
|
||||||
|
services.AddRouting();
|
||||||
|
})
|
||||||
|
.Build();
|
||||||
|
|
||||||
using var server = new TestServer(builder);
|
using var server = host.GetTestServer();
|
||||||
|
|
||||||
|
await host.StartAsync();
|
||||||
|
|
||||||
var response = await server.CreateRequest("/").SendAsync("GET");
|
var response = await server.CreateRequest("/").SendAsync("GET");
|
||||||
|
|
||||||
|
|
@ -55,20 +64,28 @@ namespace Microsoft.AspNetCore.Routing.FunctionalTests
|
||||||
public async Task AuthorizationMiddleware_WhenEndpointIsNotFound()
|
public async Task AuthorizationMiddleware_WhenEndpointIsNotFound()
|
||||||
{
|
{
|
||||||
// Arrange
|
// Arrange
|
||||||
var builder = new WebHostBuilder();
|
using var host = new HostBuilder()
|
||||||
builder.Configure(app =>
|
.ConfigureWebHost(webHostBuilder =>
|
||||||
{
|
{
|
||||||
app.UseRouting();
|
webHostBuilder
|
||||||
app.UseAuthorization();
|
.Configure(app =>
|
||||||
app.UseEndpoints(b => b.Map("/", TestDelegate));
|
{
|
||||||
})
|
app.UseRouting();
|
||||||
.ConfigureServices(services =>
|
app.UseAuthorization();
|
||||||
{
|
app.UseEndpoints(b => b.Map("/", TestDelegate));
|
||||||
services.AddAuthorization();
|
})
|
||||||
services.AddRouting();
|
.UseTestServer();
|
||||||
});
|
})
|
||||||
|
.ConfigureServices(services =>
|
||||||
|
{
|
||||||
|
services.AddAuthorization();
|
||||||
|
services.AddRouting();
|
||||||
|
})
|
||||||
|
.Build();
|
||||||
|
|
||||||
using var server = new TestServer(builder);
|
using var server = host.GetTestServer();
|
||||||
|
|
||||||
|
await host.StartAsync();
|
||||||
|
|
||||||
var response = await server.CreateRequest("/not-found").SendAsync("GET");
|
var response = await server.CreateRequest("/not-found").SendAsync("GET");
|
||||||
|
|
||||||
|
|
@ -79,20 +96,28 @@ namespace Microsoft.AspNetCore.Routing.FunctionalTests
|
||||||
public async Task AuthorizationMiddleware_WithAuthorizedEndpoint()
|
public async Task AuthorizationMiddleware_WithAuthorizedEndpoint()
|
||||||
{
|
{
|
||||||
// Arrange
|
// Arrange
|
||||||
var builder = new WebHostBuilder();
|
using var host = new HostBuilder()
|
||||||
builder.Configure(app =>
|
.ConfigureWebHost(webHostBuilder =>
|
||||||
{
|
{
|
||||||
app.UseRouting();
|
webHostBuilder
|
||||||
app.UseAuthorization();
|
.Configure(app =>
|
||||||
app.UseEndpoints(b => b.Map("/", TestDelegate).RequireAuthorization());
|
{
|
||||||
})
|
app.UseRouting();
|
||||||
.ConfigureServices(services =>
|
app.UseAuthorization();
|
||||||
{
|
app.UseEndpoints(b => b.Map("/", TestDelegate).RequireAuthorization());
|
||||||
services.AddAuthorization(options => options.DefaultPolicy = new AuthorizationPolicyBuilder().RequireAssertion(_ => true).Build());
|
})
|
||||||
services.AddRouting();
|
.UseTestServer();
|
||||||
});
|
})
|
||||||
|
.ConfigureServices(services =>
|
||||||
|
{
|
||||||
|
services.AddAuthorization(options => options.DefaultPolicy = new AuthorizationPolicyBuilder().RequireAssertion(_ => true).Build());
|
||||||
|
services.AddRouting();
|
||||||
|
})
|
||||||
|
.Build();
|
||||||
|
|
||||||
using var server = new TestServer(builder);
|
using var server = host.GetTestServer();
|
||||||
|
|
||||||
|
await host.StartAsync();
|
||||||
|
|
||||||
var response = await server.CreateRequest("/").SendAsync("GET");
|
var response = await server.CreateRequest("/").SendAsync("GET");
|
||||||
|
|
||||||
|
|
@ -103,20 +128,28 @@ namespace Microsoft.AspNetCore.Routing.FunctionalTests
|
||||||
public async Task AuthorizationMiddleware_NotConfigured_Throws()
|
public async Task AuthorizationMiddleware_NotConfigured_Throws()
|
||||||
{
|
{
|
||||||
// Arrange
|
// Arrange
|
||||||
var builder = new WebHostBuilder();
|
using var host = new HostBuilder()
|
||||||
builder.Configure(app =>
|
.ConfigureWebHost(webHostBuilder =>
|
||||||
{
|
{
|
||||||
app.UseRouting();
|
webHostBuilder
|
||||||
app.UseEndpoints(b => b.Map("/", TestDelegate).RequireAuthorization());
|
.Configure(app =>
|
||||||
|
{
|
||||||
|
app.UseRouting();
|
||||||
|
app.UseEndpoints(b => b.Map("/", TestDelegate).RequireAuthorization());
|
||||||
|
|
||||||
})
|
})
|
||||||
.ConfigureServices(services =>
|
.UseTestServer();
|
||||||
{
|
})
|
||||||
services.AddAuthorization(options => options.DefaultPolicy = new AuthorizationPolicyBuilder().RequireAssertion(_ => true).Build());
|
.ConfigureServices(services =>
|
||||||
services.AddRouting();
|
{
|
||||||
});
|
services.AddAuthorization(options => options.DefaultPolicy = new AuthorizationPolicyBuilder().RequireAssertion(_ => true).Build());
|
||||||
|
services.AddRouting();
|
||||||
|
})
|
||||||
|
.Build();
|
||||||
|
|
||||||
using var server = new TestServer(builder);
|
using var server = host.GetTestServer();
|
||||||
|
|
||||||
|
await host.StartAsync();
|
||||||
|
|
||||||
var ex = await Assert.ThrowsAsync<InvalidOperationException>(() => server.CreateRequest("/").SendAsync("GET"));
|
var ex = await Assert.ThrowsAsync<InvalidOperationException>(() => server.CreateRequest("/").SendAsync("GET"));
|
||||||
Assert.Equal(AuthErrorMessage, ex.Message);
|
Assert.Equal(AuthErrorMessage, ex.Message);
|
||||||
|
|
@ -126,18 +159,26 @@ namespace Microsoft.AspNetCore.Routing.FunctionalTests
|
||||||
public async Task AuthorizationMiddleware_NotConfigured_WhenEndpointIsNotFound()
|
public async Task AuthorizationMiddleware_NotConfigured_WhenEndpointIsNotFound()
|
||||||
{
|
{
|
||||||
// Arrange
|
// Arrange
|
||||||
var builder = new WebHostBuilder();
|
using var host = new HostBuilder()
|
||||||
builder.Configure(app =>
|
.ConfigureWebHost(webHostBuilder =>
|
||||||
{
|
{
|
||||||
app.UseRouting();
|
webHostBuilder
|
||||||
app.UseEndpoints(b => b.Map("/", TestDelegate).RequireAuthorization());
|
.Configure(app =>
|
||||||
})
|
{
|
||||||
.ConfigureServices(services =>
|
app.UseRouting();
|
||||||
{
|
app.UseEndpoints(b => b.Map("/", TestDelegate).RequireAuthorization());
|
||||||
services.AddRouting();
|
})
|
||||||
});
|
.UseTestServer();
|
||||||
|
})
|
||||||
|
.ConfigureServices(services =>
|
||||||
|
{
|
||||||
|
services.AddRouting();
|
||||||
|
})
|
||||||
|
.Build();
|
||||||
|
|
||||||
using var server = new TestServer(builder);
|
using var server = host.GetTestServer();
|
||||||
|
|
||||||
|
await host.StartAsync();
|
||||||
|
|
||||||
var response = await server.CreateRequest("/not-found").SendAsync("GET");
|
var response = await server.CreateRequest("/not-found").SendAsync("GET");
|
||||||
|
|
||||||
|
|
@ -148,20 +189,28 @@ namespace Microsoft.AspNetCore.Routing.FunctionalTests
|
||||||
public async Task AuthorizationMiddleware_ConfiguredBeforeRouting_Throws()
|
public async Task AuthorizationMiddleware_ConfiguredBeforeRouting_Throws()
|
||||||
{
|
{
|
||||||
// Arrange
|
// Arrange
|
||||||
var builder = new WebHostBuilder();
|
using var host = new HostBuilder()
|
||||||
builder.Configure(app =>
|
.ConfigureWebHost(webHostBuilder =>
|
||||||
{
|
{
|
||||||
app.UseAuthorization();
|
webHostBuilder
|
||||||
app.UseRouting();
|
.Configure(app =>
|
||||||
app.UseEndpoints(b => b.Map("/", TestDelegate).RequireAuthorization());
|
{
|
||||||
})
|
app.UseAuthorization();
|
||||||
.ConfigureServices(services =>
|
app.UseRouting();
|
||||||
{
|
app.UseEndpoints(b => b.Map("/", TestDelegate).RequireAuthorization());
|
||||||
services.AddAuthorization(options => options.DefaultPolicy = new AuthorizationPolicyBuilder().RequireAssertion(_ => true).Build());
|
})
|
||||||
services.AddRouting();
|
.UseTestServer();
|
||||||
});
|
})
|
||||||
|
.ConfigureServices(services =>
|
||||||
|
{
|
||||||
|
services.AddAuthorization(options => options.DefaultPolicy = new AuthorizationPolicyBuilder().RequireAssertion(_ => true).Build());
|
||||||
|
services.AddRouting();
|
||||||
|
})
|
||||||
|
.Build();
|
||||||
|
|
||||||
using var server = new TestServer(builder);
|
using var server = host.GetTestServer();
|
||||||
|
|
||||||
|
await host.StartAsync();
|
||||||
|
|
||||||
var ex = await Assert.ThrowsAsync<InvalidOperationException>(() => server.CreateRequest("/").SendAsync("GET"));
|
var ex = await Assert.ThrowsAsync<InvalidOperationException>(() => server.CreateRequest("/").SendAsync("GET"));
|
||||||
Assert.Equal(AuthErrorMessage, ex.Message);
|
Assert.Equal(AuthErrorMessage, ex.Message);
|
||||||
|
|
@ -171,20 +220,28 @@ namespace Microsoft.AspNetCore.Routing.FunctionalTests
|
||||||
public async Task AuthorizationMiddleware_ConfiguredAfterRouting_Throws()
|
public async Task AuthorizationMiddleware_ConfiguredAfterRouting_Throws()
|
||||||
{
|
{
|
||||||
// Arrange
|
// Arrange
|
||||||
var builder = new WebHostBuilder();
|
using var host = new HostBuilder()
|
||||||
builder.Configure(app =>
|
.ConfigureWebHost(webHostBuilder =>
|
||||||
{
|
{
|
||||||
app.UseRouting();
|
webHostBuilder
|
||||||
app.UseEndpoints(b => b.Map("/", TestDelegate).RequireAuthorization());
|
.Configure(app =>
|
||||||
app.UseAuthorization();
|
{
|
||||||
})
|
app.UseRouting();
|
||||||
.ConfigureServices(services =>
|
app.UseEndpoints(b => b.Map("/", TestDelegate).RequireAuthorization());
|
||||||
{
|
app.UseAuthorization();
|
||||||
services.AddAuthorization(options => options.DefaultPolicy = new AuthorizationPolicyBuilder().RequireAssertion(_ => true).Build());
|
})
|
||||||
services.AddRouting();
|
.UseTestServer();
|
||||||
});
|
})
|
||||||
|
.ConfigureServices(services =>
|
||||||
|
{
|
||||||
|
services.AddAuthorization(options => options.DefaultPolicy = new AuthorizationPolicyBuilder().RequireAssertion(_ => true).Build());
|
||||||
|
services.AddRouting();
|
||||||
|
})
|
||||||
|
.Build();
|
||||||
|
|
||||||
using var server = new TestServer(builder);
|
using var server = host.GetTestServer();
|
||||||
|
|
||||||
|
await host.StartAsync();
|
||||||
|
|
||||||
var ex = await Assert.ThrowsAsync<InvalidOperationException>(() => server.CreateRequest("/").SendAsync("GET"));
|
var ex = await Assert.ThrowsAsync<InvalidOperationException>(() => server.CreateRequest("/").SendAsync("GET"));
|
||||||
Assert.Equal(AuthErrorMessage, ex.Message);
|
Assert.Equal(AuthErrorMessage, ex.Message);
|
||||||
|
|
@ -194,20 +251,28 @@ namespace Microsoft.AspNetCore.Routing.FunctionalTests
|
||||||
public async Task CorsMiddleware_WithCorsEndpoint()
|
public async Task CorsMiddleware_WithCorsEndpoint()
|
||||||
{
|
{
|
||||||
// Arrange
|
// Arrange
|
||||||
var builder = new WebHostBuilder();
|
using var host = new HostBuilder()
|
||||||
builder.Configure(app =>
|
.ConfigureWebHost(webHostBuilder =>
|
||||||
{
|
{
|
||||||
app.UseRouting();
|
webHostBuilder
|
||||||
app.UseCors();
|
.Configure(app =>
|
||||||
app.UseEndpoints(b => b.Map("/", TestDelegate).RequireCors(policy => policy.AllowAnyOrigin()));
|
{
|
||||||
})
|
app.UseRouting();
|
||||||
.ConfigureServices(services =>
|
app.UseCors();
|
||||||
{
|
app.UseEndpoints(b => b.Map("/", TestDelegate).RequireCors(policy => policy.AllowAnyOrigin()));
|
||||||
services.AddCors();
|
})
|
||||||
services.AddRouting();
|
.UseTestServer();
|
||||||
});
|
})
|
||||||
|
.ConfigureServices(services =>
|
||||||
|
{
|
||||||
|
services.AddCors();
|
||||||
|
services.AddRouting();
|
||||||
|
})
|
||||||
|
.Build();
|
||||||
|
|
||||||
using var server = new TestServer(builder);
|
using var server = host.GetTestServer();
|
||||||
|
|
||||||
|
await host.StartAsync();
|
||||||
|
|
||||||
var response = await server.CreateRequest("/").SendAsync("PUT");
|
var response = await server.CreateRequest("/").SendAsync("PUT");
|
||||||
|
|
||||||
|
|
@ -218,20 +283,28 @@ namespace Microsoft.AspNetCore.Routing.FunctionalTests
|
||||||
public async Task CorsMiddleware_ConfiguredBeforeRouting_Throws()
|
public async Task CorsMiddleware_ConfiguredBeforeRouting_Throws()
|
||||||
{
|
{
|
||||||
// Arrange
|
// Arrange
|
||||||
var builder = new WebHostBuilder();
|
using var host = new HostBuilder()
|
||||||
builder.Configure(app =>
|
.ConfigureWebHost(webHostBuilder =>
|
||||||
{
|
{
|
||||||
app.UseCors();
|
webHostBuilder
|
||||||
app.UseRouting();
|
.Configure(app =>
|
||||||
app.UseEndpoints(b => b.Map("/", TestDelegate).RequireCors(policy => policy.AllowAnyOrigin()));
|
{
|
||||||
})
|
app.UseCors();
|
||||||
.ConfigureServices(services =>
|
app.UseRouting();
|
||||||
{
|
app.UseEndpoints(b => b.Map("/", TestDelegate).RequireCors(policy => policy.AllowAnyOrigin()));
|
||||||
services.AddCors();
|
})
|
||||||
services.AddRouting();
|
.UseTestServer();
|
||||||
});
|
})
|
||||||
|
.ConfigureServices(services =>
|
||||||
|
{
|
||||||
|
services.AddCors();
|
||||||
|
services.AddRouting();
|
||||||
|
})
|
||||||
|
.Build();
|
||||||
|
|
||||||
using var server = new TestServer(builder);
|
using var server = host.GetTestServer();
|
||||||
|
|
||||||
|
await host.StartAsync();
|
||||||
|
|
||||||
var ex = await Assert.ThrowsAsync<InvalidOperationException>(() => server.CreateRequest("/").SendAsync("GET"));
|
var ex = await Assert.ThrowsAsync<InvalidOperationException>(() => server.CreateRequest("/").SendAsync("GET"));
|
||||||
Assert.Equal(CORSErrorMessage, ex.Message);
|
Assert.Equal(CORSErrorMessage, ex.Message);
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,7 @@ using System.Net;
|
||||||
using System.Net.Http;
|
using System.Net.Http;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Microsoft.AspNetCore.TestHost;
|
using Microsoft.AspNetCore.TestHost;
|
||||||
|
using Microsoft.Extensions.Hosting;
|
||||||
using RoutingWebSite;
|
using RoutingWebSite;
|
||||||
using Xunit;
|
using Xunit;
|
||||||
|
|
||||||
|
|
@ -14,12 +15,17 @@ namespace Microsoft.AspNetCore.Routing.FunctionalTests
|
||||||
public class EndpointRoutingSampleTest : IDisposable
|
public class EndpointRoutingSampleTest : IDisposable
|
||||||
{
|
{
|
||||||
private readonly HttpClient _client;
|
private readonly HttpClient _client;
|
||||||
|
private readonly IHost _host;
|
||||||
private readonly TestServer _testServer;
|
private readonly TestServer _testServer;
|
||||||
|
|
||||||
public EndpointRoutingSampleTest()
|
public EndpointRoutingSampleTest()
|
||||||
{
|
{
|
||||||
var webHostBuilder = Program.GetWebHostBuilder(new[] { Program.EndpointRoutingScenario, });
|
var hostBuilder = Program.GetHostBuilder(new[] { Program.EndpointRoutingScenario, });
|
||||||
_testServer = new TestServer(webHostBuilder);
|
_host = hostBuilder.Build();
|
||||||
|
|
||||||
|
_testServer = _host.GetTestServer();
|
||||||
|
_host.Start();
|
||||||
|
|
||||||
_client = _testServer.CreateClient();
|
_client = _testServer.CreateClient();
|
||||||
_client.BaseAddress = new Uri("http://localhost");
|
_client.BaseAddress = new Uri("http://localhost");
|
||||||
}
|
}
|
||||||
|
|
@ -227,6 +233,7 @@ namespace Microsoft.AspNetCore.Routing.FunctionalTests
|
||||||
{
|
{
|
||||||
_testServer.Dispose();
|
_testServer.Dispose();
|
||||||
_client.Dispose();
|
_client.Dispose();
|
||||||
|
_host.Dispose();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,7 @@ using System.Net;
|
||||||
using System.Net.Http;
|
using System.Net.Http;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Microsoft.AspNetCore.TestHost;
|
using Microsoft.AspNetCore.TestHost;
|
||||||
|
using Microsoft.Extensions.Hosting;
|
||||||
using RoutingWebSite;
|
using RoutingWebSite;
|
||||||
using Xunit;
|
using Xunit;
|
||||||
|
|
||||||
|
|
@ -14,12 +15,15 @@ namespace Microsoft.AspNetCore.Routing.FunctionalTests
|
||||||
public class RouterSampleTest : IDisposable
|
public class RouterSampleTest : IDisposable
|
||||||
{
|
{
|
||||||
private readonly HttpClient _client;
|
private readonly HttpClient _client;
|
||||||
|
private readonly IHost _host;
|
||||||
private readonly TestServer _testServer;
|
private readonly TestServer _testServer;
|
||||||
|
|
||||||
public RouterSampleTest()
|
public RouterSampleTest()
|
||||||
{
|
{
|
||||||
var webHostBuilder = Program.GetWebHostBuilder(new[] { Program.RouterScenario, });
|
var hostBuilder = Program.GetHostBuilder(new[] { Program.RouterScenario, });
|
||||||
_testServer = new TestServer(webHostBuilder);
|
_host = hostBuilder.Build();
|
||||||
|
_testServer = _host.GetTestServer();
|
||||||
|
_host.Start();
|
||||||
_client = _testServer.CreateClient();
|
_client = _testServer.CreateClient();
|
||||||
_client.BaseAddress = new Uri("http://localhost");
|
_client.BaseAddress = new Uri("http://localhost");
|
||||||
}
|
}
|
||||||
|
|
@ -96,6 +100,7 @@ namespace Microsoft.AspNetCore.Routing.FunctionalTests
|
||||||
{
|
{
|
||||||
_testServer.Dispose();
|
_testServer.Dispose();
|
||||||
_client.Dispose();
|
_client.Dispose();
|
||||||
|
_host.Dispose();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,7 @@ using System;
|
||||||
using System.Net.Http;
|
using System.Net.Http;
|
||||||
using Microsoft.AspNetCore.Hosting;
|
using Microsoft.AspNetCore.Hosting;
|
||||||
using Microsoft.AspNetCore.TestHost;
|
using Microsoft.AspNetCore.TestHost;
|
||||||
|
using Microsoft.Extensions.Hosting;
|
||||||
|
|
||||||
namespace Microsoft.AspNetCore.Routing.FunctionalTests
|
namespace Microsoft.AspNetCore.Routing.FunctionalTests
|
||||||
{
|
{
|
||||||
|
|
@ -14,10 +15,18 @@ namespace Microsoft.AspNetCore.Routing.FunctionalTests
|
||||||
|
|
||||||
public RoutingTestFixture()
|
public RoutingTestFixture()
|
||||||
{
|
{
|
||||||
var builder = new WebHostBuilder()
|
var host = new HostBuilder()
|
||||||
.UseStartup(typeof(TStartup));
|
.ConfigureWebHost(webHostBuilder =>
|
||||||
|
{
|
||||||
|
webHostBuilder
|
||||||
|
.UseStartup(typeof(TStartup))
|
||||||
|
.UseTestServer();
|
||||||
|
})
|
||||||
|
.Build();
|
||||||
|
|
||||||
_server = new TestServer(builder);
|
_server = host.GetTestServer();
|
||||||
|
|
||||||
|
host.Start();
|
||||||
|
|
||||||
Client = _server.CreateClient();
|
Client = _server.CreateClient();
|
||||||
Client.BaseAddress = new Uri("http://localhost");
|
Client.BaseAddress = new Uri("http://localhost");
|
||||||
|
|
|
||||||
|
|
@ -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.
|
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||||
|
|
||||||
using System;
|
using System;
|
||||||
|
|
@ -12,6 +12,7 @@ using Microsoft.AspNetCore.Builder;
|
||||||
using Microsoft.AspNetCore.TestHost;
|
using Microsoft.AspNetCore.TestHost;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using Xunit;
|
using Xunit;
|
||||||
|
using Microsoft.Extensions.Hosting;
|
||||||
|
|
||||||
namespace Microsoft.AspNetCore.Routing.FunctionalTests
|
namespace Microsoft.AspNetCore.Routing.FunctionalTests
|
||||||
{
|
{
|
||||||
|
|
@ -79,14 +80,21 @@ namespace Microsoft.AspNetCore.Routing.FunctionalTests
|
||||||
public async Task UseRouter_MapGet_MatchesRequest(Action<IRouteBuilder> routeBuilder, HttpRequestMessage request, string expected)
|
public async Task UseRouter_MapGet_MatchesRequest(Action<IRouteBuilder> routeBuilder, HttpRequestMessage request, string expected)
|
||||||
{
|
{
|
||||||
// Arrange
|
// Arrange
|
||||||
var webhostbuilder = new WebHostBuilder();
|
using var host = new HostBuilder()
|
||||||
webhostbuilder
|
.ConfigureWebHost(webhostbuilder =>
|
||||||
.ConfigureServices(services => services.AddRouting())
|
|
||||||
.Configure(app =>
|
|
||||||
{
|
{
|
||||||
app.UseRouter(routeBuilder);
|
webhostbuilder
|
||||||
});
|
.Configure(app =>
|
||||||
var testServer = new TestServer(webhostbuilder);
|
{
|
||||||
|
app.UseRouter(routeBuilder);
|
||||||
|
})
|
||||||
|
.UseTestServer();
|
||||||
|
})
|
||||||
|
.ConfigureServices(services => services.AddRouting())
|
||||||
|
.Build();
|
||||||
|
|
||||||
|
var testServer = host.GetTestServer();
|
||||||
|
await host.StartAsync();
|
||||||
var client = testServer.CreateClient();
|
var client = testServer.CreateClient();
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,7 @@
|
||||||
|
|
||||||
<!--These references are used when running locally-->
|
<!--These references are used when running locally-->
|
||||||
<ItemGroup Condition="'$(UseP2PReferences)'=='true'">
|
<ItemGroup Condition="'$(UseP2PReferences)'=='true'">
|
||||||
|
<Reference Include="Microsoft.AspNetCore.TestHost" />
|
||||||
<Reference Include="Microsoft.AspNetCore.Routing" />
|
<Reference Include="Microsoft.AspNetCore.Routing" />
|
||||||
<Reference Include="Microsoft.AspNetCore.Server.Kestrel" />
|
<Reference Include="Microsoft.AspNetCore.Server.Kestrel" />
|
||||||
<Reference Include="Microsoft.Extensions.Configuration.CommandLine" />
|
<Reference Include="Microsoft.Extensions.Configuration.CommandLine" />
|
||||||
|
|
|
||||||
|
|
@ -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.
|
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||||
|
|
||||||
using System;
|
using System;
|
||||||
|
using System.Threading.Tasks;
|
||||||
using Microsoft.AspNetCore.Hosting;
|
using Microsoft.AspNetCore.Hosting;
|
||||||
|
using Microsoft.AspNetCore.TestHost;
|
||||||
using Microsoft.Extensions.Configuration;
|
using Microsoft.Extensions.Configuration;
|
||||||
|
using Microsoft.Extensions.Hosting;
|
||||||
|
|
||||||
namespace Benchmarks
|
namespace Benchmarks
|
||||||
{
|
{
|
||||||
public class Program
|
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()
|
var config = new ConfigurationBuilder()
|
||||||
.AddCommandLine(args)
|
.AddCommandLine(args)
|
||||||
|
|
@ -24,22 +27,35 @@ namespace Benchmarks
|
||||||
// Consoler logger has a major impact on perf results, so do not use
|
// Consoler logger has a major impact on perf results, so do not use
|
||||||
// default builder.
|
// default builder.
|
||||||
|
|
||||||
var webHostBuilder = new WebHostBuilder()
|
var hostBuilder = new HostBuilder()
|
||||||
.UseConfiguration(config)
|
.ConfigureWebHost(webHostBuilder =>
|
||||||
.UseKestrel();
|
{
|
||||||
|
webHostBuilder
|
||||||
|
.UseKestrel()
|
||||||
|
.UseTestServer()
|
||||||
|
.UseConfiguration(config);
|
||||||
|
});
|
||||||
|
|
||||||
var scenario = config["scenarios"]?.ToLower();
|
var scenario = config["scenarios"]?.ToLower();
|
||||||
if (scenario == "plaintextdispatcher" || scenario == "plaintextendpointrouting")
|
if (scenario == "plaintextdispatcher" || scenario == "plaintextendpointrouting")
|
||||||
{
|
{
|
||||||
webHostBuilder.UseStartup<StartupUsingEndpointRouting>();
|
hostBuilder.ConfigureWebHost(webHostBuilder =>
|
||||||
// for testing
|
{
|
||||||
webHostBuilder.UseSetting("Startup", nameof(StartupUsingEndpointRouting));
|
webHostBuilder
|
||||||
|
.UseStartup<StartupUsingEndpointRouting>()
|
||||||
|
// for testing
|
||||||
|
.UseSetting("Startup", nameof(StartupUsingEndpointRouting));
|
||||||
|
});
|
||||||
}
|
}
|
||||||
else if (scenario == "plaintextrouting" || scenario == "plaintextrouter")
|
else if (scenario == "plaintextrouting" || scenario == "plaintextrouter")
|
||||||
{
|
{
|
||||||
webHostBuilder.UseStartup<StartupUsingRouter>();
|
hostBuilder.ConfigureWebHost(webHostBuilder =>
|
||||||
// for testing
|
{
|
||||||
webHostBuilder.UseSetting("Startup", nameof(StartupUsingRouter));
|
webHostBuilder
|
||||||
|
.UseStartup<StartupUsingRouter>()
|
||||||
|
// for testing
|
||||||
|
.UseSetting("Startup", nameof(StartupUsingRouter));
|
||||||
|
});
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
|
@ -47,7 +63,7 @@ namespace Benchmarks
|
||||||
$"Invalid scenario '{scenario}'. Allowed scenarios are PlaintextEndpointRouting and PlaintextRouter");
|
$"Invalid scenario '{scenario}'. Allowed scenarios are PlaintextEndpointRouting and PlaintextRouter");
|
||||||
}
|
}
|
||||||
|
|
||||||
return webHostBuilder;
|
return hostBuilder;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,8 +2,11 @@
|
||||||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||||
|
|
||||||
using System;
|
using System;
|
||||||
|
using System.Threading.Tasks;
|
||||||
using Microsoft.AspNetCore.Builder;
|
using Microsoft.AspNetCore.Builder;
|
||||||
using Microsoft.AspNetCore.Hosting;
|
using Microsoft.AspNetCore.Hosting;
|
||||||
|
using Microsoft.AspNetCore.TestHost;
|
||||||
|
using Microsoft.Extensions.Hosting;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
|
|
||||||
namespace RoutingSandbox
|
namespace RoutingSandbox
|
||||||
|
|
@ -13,14 +16,14 @@ namespace RoutingSandbox
|
||||||
public const string EndpointRoutingScenario = "endpointrouting";
|
public const string EndpointRoutingScenario = "endpointrouting";
|
||||||
public const string RouterScenario = "router";
|
public const string RouterScenario = "router";
|
||||||
|
|
||||||
public static void Main(string[] args)
|
public static Task Main(string[] args)
|
||||||
{
|
{
|
||||||
var webHost = GetWebHostBuilder(args).Build();
|
var host = GetHostBuilder(args).Build();
|
||||||
webHost.Run();
|
return host.RunAsync();
|
||||||
}
|
}
|
||||||
|
|
||||||
// For unit testing
|
// For unit testing
|
||||||
public static IWebHostBuilder GetWebHostBuilder(string[] args)
|
public static IHostBuilder GetHostBuilder(string[] args)
|
||||||
{
|
{
|
||||||
string scenario;
|
string scenario;
|
||||||
if (args.Length == 0)
|
if (args.Length == 0)
|
||||||
|
|
@ -57,16 +60,20 @@ namespace RoutingSandbox
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return new WebHostBuilder()
|
return new HostBuilder()
|
||||||
.UseKestrel()
|
.ConfigureWebHost(webHostBuilder =>
|
||||||
.UseIISIntegration()
|
{
|
||||||
|
webHostBuilder
|
||||||
|
.UseKestrel()
|
||||||
|
.UseIISIntegration()
|
||||||
|
.UseContentRoot(Environment.CurrentDirectory)
|
||||||
|
.UseStartup(startupType);
|
||||||
|
})
|
||||||
.ConfigureLogging(b =>
|
.ConfigureLogging(b =>
|
||||||
{
|
{
|
||||||
b.AddConsole();
|
b.AddConsole();
|
||||||
b.SetMinimumLevel(LogLevel.Critical);
|
b.SetMinimumLevel(LogLevel.Critical);
|
||||||
})
|
});
|
||||||
.UseContentRoot(Environment.CurrentDirectory)
|
|
||||||
.UseStartup(startupType);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,7 @@
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<Reference Include="Microsoft.AspNetCore.TestHost" />
|
||||||
<Reference Include="Microsoft.AspNetCore.Routing" />
|
<Reference Include="Microsoft.AspNetCore.Routing" />
|
||||||
<Reference Include="Microsoft.AspNetCore.Server.IISIntegration" />
|
<Reference Include="Microsoft.AspNetCore.Server.IISIntegration" />
|
||||||
<Reference Include="Microsoft.AspNetCore.Server.Kestrel" />
|
<Reference Include="Microsoft.AspNetCore.Server.Kestrel" />
|
||||||
|
|
|
||||||
|
|
@ -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.
|
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||||
|
|
||||||
using System;
|
using System;
|
||||||
|
using System.Threading.Tasks;
|
||||||
using Microsoft.AspNetCore.Builder;
|
using Microsoft.AspNetCore.Builder;
|
||||||
using Microsoft.AspNetCore.Hosting;
|
using Microsoft.AspNetCore.Hosting;
|
||||||
|
using Microsoft.AspNetCore.TestHost;
|
||||||
|
using Microsoft.Extensions.Hosting;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
|
|
||||||
namespace RoutingWebSite
|
namespace RoutingWebSite
|
||||||
|
|
@ -13,14 +16,14 @@ namespace RoutingWebSite
|
||||||
public const string EndpointRoutingScenario = "endpointrouting";
|
public const string EndpointRoutingScenario = "endpointrouting";
|
||||||
public const string RouterScenario = "router";
|
public const string RouterScenario = "router";
|
||||||
|
|
||||||
public static void Main(string[] args)
|
public static Task Main(string[] args)
|
||||||
{
|
{
|
||||||
var webHost = GetWebHostBuilder(args).Build();
|
var host = GetHostBuilder(args).Build();
|
||||||
webHost.Run();
|
return host.RunAsync();
|
||||||
}
|
}
|
||||||
|
|
||||||
// For unit testing
|
// For unit testing
|
||||||
public static IWebHostBuilder GetWebHostBuilder(string[] args)
|
public static IHostBuilder GetHostBuilder(string[] args)
|
||||||
{
|
{
|
||||||
string scenario;
|
string scenario;
|
||||||
if (args.Length == 0)
|
if (args.Length == 0)
|
||||||
|
|
@ -57,16 +60,21 @@ namespace RoutingWebSite
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return new WebHostBuilder()
|
return new HostBuilder()
|
||||||
.UseKestrel()
|
.ConfigureWebHost(webHostBuilder =>
|
||||||
.UseIISIntegration()
|
{
|
||||||
|
webHostBuilder
|
||||||
|
.UseKestrel()
|
||||||
|
.UseIISIntegration()
|
||||||
|
.UseContentRoot(Environment.CurrentDirectory)
|
||||||
|
.UseStartup(startupType)
|
||||||
|
.UseTestServer();
|
||||||
|
})
|
||||||
.ConfigureLogging(b =>
|
.ConfigureLogging(b =>
|
||||||
{
|
{
|
||||||
b.AddConsole();
|
b.AddConsole();
|
||||||
b.SetMinimumLevel(LogLevel.Critical);
|
b.SetMinimumLevel(LogLevel.Critical);
|
||||||
})
|
});
|
||||||
.UseContentRoot(Environment.CurrentDirectory)
|
|
||||||
.UseStartup(startupType);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,7 @@
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<Reference Include="Microsoft.AspNetCore.TestHost" />
|
||||||
<Reference Include="Microsoft.AspNetCore.Routing" />
|
<Reference Include="Microsoft.AspNetCore.Routing" />
|
||||||
<Reference Include="Microsoft.AspNetCore.Server.IISIntegration" />
|
<Reference Include="Microsoft.AspNetCore.Server.IISIntegration" />
|
||||||
<Reference Include="Microsoft.AspNetCore.Server.Kestrel" />
|
<Reference Include="Microsoft.AspNetCore.Server.Kestrel" />
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue