[Kestrel] Move to GenericHost (#24279)

* [Kestrel] Move to GenericHost

* Change TransportSelector

* Add an empty app for TestServer

* Add an empty app for TestServer

* Set app

* Stop host

* Stop the TestSserver

* Stop more hosts!

* Stop host in dispose

* implement IAsyncDisposable for TestServer

* configure await

* catch OperationCanceledException

* Apply suggestions from code review

Co-authored-by: Brennan <brecon@microsoft.com>
This commit is contained in:
Kahbazi 2020-08-06 02:18:53 +04:30 committed by GitHub
parent 25948ab962
commit b2c7d514c9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
35 changed files with 915 additions and 735 deletions

View File

@ -16,6 +16,7 @@ using Microsoft.AspNetCore.Connections;
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
namespace Microsoft.AspNetCore.Server.Kestrel.Performance namespace Microsoft.AspNetCore.Server.Kestrel.Performance
{ {
@ -33,22 +34,26 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Performance
private static readonly string _plaintextPipelinedExpectedResponse = private static readonly string _plaintextPipelinedExpectedResponse =
string.Concat(Enumerable.Repeat(_plaintextExpectedResponse, RequestParsingData.Pipelining)); string.Concat(Enumerable.Repeat(_plaintextExpectedResponse, RequestParsingData.Pipelining));
private IWebHost _host; private IHost _host;
private InMemoryConnection _connection; private InMemoryConnection _connection;
[GlobalSetup(Target = nameof(Plaintext) + "," + nameof(PlaintextPipelined))] [GlobalSetup(Target = nameof(Plaintext) + "," + nameof(PlaintextPipelined))]
public void GlobalSetupPlaintext() public void GlobalSetupPlaintext()
{ {
var transportFactory = new InMemoryTransportFactory(connectionsPerEndPoint: 1); var transportFactory = new InMemoryTransportFactory(connectionsPerEndPoint: 1);
_host = new WebHostBuilder() _host = new HostBuilder()
// Prevent VS from attaching to hosting startup which could impact results .ConfigureWebHost(webHostBuilder =>
.UseSetting("preventHostingStartup", "true") {
.UseKestrel() webHostBuilder
// Bind to a single non-HTTPS endpoint // Prevent VS from attaching to hosting startup which could impact results
.UseUrls("http://127.0.0.1:5000") .UseSetting("preventHostingStartup", "true")
.UseKestrel()
// Bind to a single non-HTTPS endpoint
.UseUrls("http://127.0.0.1:5000")
.Configure(app => app.UseMiddleware<PlaintextMiddleware>());
})
.ConfigureServices(services => services.AddSingleton<IConnectionListenerFactory>(transportFactory)) .ConfigureServices(services => services.AddSingleton<IConnectionListenerFactory>(transportFactory))
.Configure(app => app.UseMiddleware<PlaintextMiddleware>())
.Build(); .Build();
_host.Start(); _host.Start();

View File

@ -1,12 +1,12 @@
using System; using System;
using System.IO; using System.IO;
using System.Net;
using System.Security.Authentication; using System.Security.Authentication;
using Microsoft.AspNetCore.Connections; using Microsoft.AspNetCore.Connections;
using Microsoft.AspNetCore.Connections.Features; using Microsoft.AspNetCore.Connections.Features;
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Server.Kestrel.Core; using Microsoft.AspNetCore.Server.Kestrel.Core;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
namespace Http2SampleApp namespace Http2SampleApp
@ -15,54 +15,58 @@ namespace Http2SampleApp
{ {
public static void Main(string[] args) public static void Main(string[] args)
{ {
var hostBuilder = new WebHostBuilder() var hostBuilder = new HostBuilder()
.ConfigureWebHost(webHostBuilder =>
{
webHostBuilder
.UseKestrel()
.ConfigureKestrel((context, options) =>
{
var basePort = context.Configuration.GetValue<int?>("BASE_PORT") ?? 5000;
// Http/1.1 endpoint for comparison
options.ListenAnyIP(basePort, listenOptions =>
{
listenOptions.Protocols = HttpProtocols.Http1;
});
// TLS Http/1.1 or HTTP/2 endpoint negotiated via ALPN
options.ListenAnyIP(basePort + 1, listenOptions =>
{
listenOptions.Protocols = HttpProtocols.Http1AndHttp2;
listenOptions.UseHttps();
listenOptions.Use((context, next) =>
{
// https://tools.ietf.org/html/rfc7540#appendix-A
// Allows filtering TLS handshakes on a per connection basis
var tlsFeature = context.Features.Get<ITlsHandshakeFeature>();
if (tlsFeature.CipherAlgorithm == CipherAlgorithmType.Null)
{
throw new NotSupportedException("Prohibited cipher: " + tlsFeature.CipherAlgorithm);
}
return next();
});
});
// Prior knowledge, no TLS handshake. WARNING: Not supported by browsers
// but useful for the h2spec tests
options.ListenAnyIP(basePort + 5, listenOptions =>
{
listenOptions.Protocols = HttpProtocols.Http2;
});
})
.UseContentRoot(Directory.GetCurrentDirectory())
.UseStartup<Startup>();
})
.ConfigureLogging((_, factory) => .ConfigureLogging((_, factory) =>
{ {
// Set logging to the MAX. // Set logging to the MAX.
factory.SetMinimumLevel(LogLevel.Trace); factory.SetMinimumLevel(LogLevel.Trace);
factory.AddConsole(); factory.AddConsole();
}) });
.UseKestrel()
.ConfigureKestrel((context, options) =>
{
var basePort = context.Configuration.GetValue<int?>("BASE_PORT") ?? 5000;
// Http/1.1 endpoint for comparison
options.ListenAnyIP(basePort, listenOptions =>
{
listenOptions.Protocols = HttpProtocols.Http1;
});
// TLS Http/1.1 or HTTP/2 endpoint negotiated via ALPN
options.ListenAnyIP(basePort + 1, listenOptions =>
{
listenOptions.Protocols = HttpProtocols.Http1AndHttp2;
listenOptions.UseHttps();
listenOptions.Use((context, next) =>
{
// https://tools.ietf.org/html/rfc7540#appendix-A
// Allows filtering TLS handshakes on a per connection basis
var tlsFeature = context.Features.Get<ITlsHandshakeFeature>();
if (tlsFeature.CipherAlgorithm == CipherAlgorithmType.Null)
{
throw new NotSupportedException("Prohibited cipher: " + tlsFeature.CipherAlgorithm);
}
return next();
});
});
// Prior knowledge, no TLS handshake. WARNING: Not supported by browsers
// but useful for the h2spec tests
options.ListenAnyIP(basePort + 5, listenOptions =>
{
listenOptions.Protocols = HttpProtocols.Http2;
});
})
.UseContentRoot(Directory.GetCurrentDirectory())
.UseStartup<Startup>();
hostBuilder.Build().Run(); hostBuilder.Build().Run();
} }

View File

@ -7,6 +7,7 @@ using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;
namespace LargeResponseApp namespace LargeResponseApp
{ {
@ -38,13 +39,17 @@ namespace LargeResponseApp
public static Task Main(string[] args) public static Task Main(string[] args)
{ {
var host = new WebHostBuilder() var host = new HostBuilder()
.UseKestrel(options => .ConfigureWebHost(webHostBuilder =>
{ {
options.Listen(IPAddress.Loopback, 5001); webHostBuilder
}) .UseKestrel(options =>
.UseContentRoot(Directory.GetCurrentDirectory()) {
.UseStartup<Startup>() options.Listen(IPAddress.Loopback, 5001);
})
.UseContentRoot(Directory.GetCurrentDirectory())
.UseStartup<Startup>();
})
.Build(); .Build();
return host.RunAsync(); return host.RunAsync();

View File

@ -10,6 +10,7 @@ using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Connections; using Microsoft.AspNetCore.Connections;
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;
namespace PlaintextApp namespace PlaintextApp
{ {
@ -34,13 +35,17 @@ namespace PlaintextApp
public static async Task Main(string[] args) public static async Task Main(string[] args)
{ {
var host = new WebHostBuilder() var host = new HostBuilder()
.UseKestrel(options => .ConfigureWebHost(webHostBuilder =>
{ {
options.Listen(IPAddress.Loopback, 5001); webHostBuilder
.UseKestrel(options =>
{
options.Listen(IPAddress.Loopback, 5001);
})
.UseContentRoot(Directory.GetCurrentDirectory())
.UseStartup<Startup>();
}) })
.UseContentRoot(Directory.GetCurrentDirectory())
.UseStartup<Startup>()
.Build(); .Build();
await host.RunAsync(); await host.RunAsync();

View File

@ -6,6 +6,7 @@ using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Connections; using Microsoft.AspNetCore.Connections;
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Server.Kestrel.Core; using Microsoft.AspNetCore.Server.Kestrel.Core;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
namespace QuicSampleApp namespace QuicSampleApp
@ -22,60 +23,64 @@ namespace QuicSampleApp
public static void Main(string[] args) public static void Main(string[] args)
{ {
var hostBuilder = new WebHostBuilder() var hostBuilder = new HostBuilder()
.ConfigureLogging((_, factory) => .ConfigureWebHost(webHostBuilder =>
{ {
factory.SetMinimumLevel(LogLevel.Debug); webHostBuilder
factory.AddConsole(); .UseKestrel()
}) .UseQuic(options =>
.UseKestrel() {
.UseQuic(options => options.Certificate = null;
{ options.Alpn = "QuicTest";
options.Certificate = null; options.IdleTimeout = TimeSpan.FromHours(1);
options.Alpn = "QuicTest"; })
options.IdleTimeout = TimeSpan.FromHours(1); .ConfigureKestrel((context, options) =>
}) {
.ConfigureKestrel((context, options) => var basePort = 5555;
{
var basePort = 5555;
options.Listen(IPAddress.Any, basePort, listenOptions => options.Listen(IPAddress.Any, basePort, listenOptions =>
{ {
listenOptions.Protocols = HttpProtocols.Http3; listenOptions.Protocols = HttpProtocols.Http3;
async Task EchoServer(MultiplexedConnectionContext connection) async Task EchoServer(MultiplexedConnectionContext connection)
{ {
// For graceful shutdown // For graceful shutdown
while (true) while (true)
{ {
var stream = await connection.AcceptAsync(); var stream = await connection.AcceptAsync();
while (true) while (true)
{ {
var result = await stream.Transport.Input.ReadAsync(); var result = await stream.Transport.Input.ReadAsync();
if (result.IsCompleted) if (result.IsCompleted)
{ {
break; break;
} }
await stream.Transport.Output.WriteAsync(result.Buffer.ToArray()); await stream.Transport.Output.WriteAsync(result.Buffer.ToArray());
stream.Transport.Input.AdvanceTo(result.Buffer.End); stream.Transport.Input.AdvanceTo(result.Buffer.End);
} }
} }
} }
((IMultiplexedConnectionBuilder)listenOptions).Use(next => ((IMultiplexedConnectionBuilder)listenOptions).Use(next =>
{ {
return context => return context =>
{ {
return EchoServer(context); return EchoServer(context);
}; };
}); });
}); });
}) })
.UseStartup<Startup>(); .UseStartup<Startup>();
})
.ConfigureLogging((_, factory) =>
{
factory.SetMinimumLevel(LogLevel.Debug);
factory.AddConsole();
});
hostBuilder.Build().Run(); hostBuilder.Build().Run();
} }

View File

@ -62,7 +62,102 @@ namespace SampleApp
Console.WriteLine("Unobserved exception: {0}", e.Exception); Console.WriteLine("Unobserved exception: {0}", e.Exception);
}; };
var hostBuilder = new WebHostBuilder() var hostBuilder = new HostBuilder()
.ConfigureWebHost(webHostBuilder =>
{
webHostBuilder
.UseKestrel((context, options) =>
{
if (context.HostingEnvironment.IsDevelopment())
{
ShowConfig(context.Configuration);
}
var basePort = context.Configuration.GetValue<int?>("BASE_PORT") ?? 5000;
options.ConfigureHttpsDefaults(httpsOptions =>
{
httpsOptions.SslProtocols = SslProtocols.Tls12;
});
options.Listen(IPAddress.Loopback, basePort, listenOptions =>
{
// Uncomment the following to enable Nagle's algorithm for this endpoint.
//listenOptions.NoDelay = false;
listenOptions.UseConnectionLogging();
});
options.Listen(IPAddress.Loopback, basePort + 1, listenOptions =>
{
listenOptions.UseHttps();
listenOptions.UseConnectionLogging();
});
options.ListenLocalhost(basePort + 2, listenOptions =>
{
// Use default dev cert
listenOptions.UseHttps();
});
options.ListenAnyIP(basePort + 3);
options.ListenAnyIP(basePort + 4, listenOptions =>
{
listenOptions.UseHttps(StoreName.My, "localhost", allowInvalid: true);
});
options.ListenAnyIP(basePort + 5, listenOptions =>
{
listenOptions.UseHttps(httpsOptions =>
{
var localhostCert = CertificateLoader.LoadFromStoreCert("localhost", "My", StoreLocation.CurrentUser, allowInvalid: true);
httpsOptions.ServerCertificateSelector = (features, name) =>
{
// Here you would check the name, select an appropriate cert, and provide a fallback or fail for null names.
return localhostCert;
};
});
});
options
.Configure()
.Endpoint(IPAddress.Loopback, basePort + 6)
.LocalhostEndpoint(basePort + 7)
.Load();
// reloadOnChange: true is the default
options
.Configure(context.Configuration.GetSection("Kestrel"), reloadOnChange: true)
.Endpoint("NamedEndpoint", opt =>
{
})
.Endpoint("NamedHttpsEndpoint", opt =>
{
opt.HttpsOptions.SslProtocols = SslProtocols.Tls12;
});
options.UseSystemd();
// The following section should be used to demo sockets
//options.ListenUnixSocket("/tmp/kestrel-test.sock");
})
.UseContentRoot(Directory.GetCurrentDirectory())
.UseStartup<Startup>();
if (string.Equals(Process.GetCurrentProcess().Id.ToString(), Environment.GetEnvironmentVariable("LISTEN_PID")))
{
// Use libuv if activated by systemd, since that's currently the only transport that supports being passed a socket handle.
#pragma warning disable CS0618
webHostBuilder.UseLibuv(options =>
{
// Uncomment the following line to change the default number of libuv threads for all endpoints.
// options.ThreadCount = 4;
});
#pragma warning restore CS0618
}
})
.ConfigureLogging((_, factory) => .ConfigureLogging((_, factory) =>
{ {
factory.SetMinimumLevel(LogLevel.Debug); factory.SetMinimumLevel(LogLevel.Debug);
@ -73,98 +168,7 @@ namespace SampleApp
var env = hostingContext.HostingEnvironment; var env = hostingContext.HostingEnvironment;
config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true); .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true);
}) });
.UseKestrel((context, options) =>
{
if (context.HostingEnvironment.IsDevelopment())
{
ShowConfig(context.Configuration);
}
var basePort = context.Configuration.GetValue<int?>("BASE_PORT") ?? 5000;
options.ConfigureHttpsDefaults(httpsOptions =>
{
httpsOptions.SslProtocols = SslProtocols.Tls12;
});
options.Listen(IPAddress.Loopback, basePort, listenOptions =>
{
// Uncomment the following to enable Nagle's algorithm for this endpoint.
//listenOptions.NoDelay = false;
listenOptions.UseConnectionLogging();
});
options.Listen(IPAddress.Loopback, basePort + 1, listenOptions =>
{
listenOptions.UseHttps();
listenOptions.UseConnectionLogging();
});
options.ListenLocalhost(basePort + 2, listenOptions =>
{
// Use default dev cert
listenOptions.UseHttps();
});
options.ListenAnyIP(basePort + 3);
options.ListenAnyIP(basePort + 4, listenOptions =>
{
listenOptions.UseHttps(StoreName.My, "localhost", allowInvalid: true);
});
options.ListenAnyIP(basePort + 5, listenOptions =>
{
listenOptions.UseHttps(httpsOptions =>
{
var localhostCert = CertificateLoader.LoadFromStoreCert("localhost", "My", StoreLocation.CurrentUser, allowInvalid: true);
httpsOptions.ServerCertificateSelector = (features, name) =>
{
// Here you would check the name, select an appropriate cert, and provide a fallback or fail for null names.
return localhostCert;
};
});
});
options
.Configure()
.Endpoint(IPAddress.Loopback, basePort + 6)
.LocalhostEndpoint(basePort + 7)
.Load();
// reloadOnChange: true is the default
options
.Configure(context.Configuration.GetSection("Kestrel"), reloadOnChange: true)
.Endpoint("NamedEndpoint", opt =>
{
})
.Endpoint("NamedHttpsEndpoint", opt =>
{
opt.HttpsOptions.SslProtocols = SslProtocols.Tls12;
});
options.UseSystemd();
// The following section should be used to demo sockets
//options.ListenUnixSocket("/tmp/kestrel-test.sock");
})
.UseContentRoot(Directory.GetCurrentDirectory())
.UseStartup<Startup>();
if (string.Equals(Process.GetCurrentProcess().Id.ToString(), Environment.GetEnvironmentVariable("LISTEN_PID")))
{
// Use libuv if activated by systemd, since that's currently the only transport that supports being passed a socket handle.
#pragma warning disable CS0618
hostBuilder.UseLibuv(options =>
{
// Uncomment the following line to change the default number of libuv threads for all endpoints.
// options.ThreadCount = 4;
});
#pragma warning restore CS0618
}
return hostBuilder.Build().RunAsync(); return hostBuilder.Build().RunAsync();
} }

View File

@ -10,6 +10,7 @@ using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
namespace SystemdTestApp namespace SystemdTestApp
@ -41,48 +42,52 @@ namespace SystemdTestApp
Console.WriteLine("Unobserved exception: {0}", e.Exception); Console.WriteLine("Unobserved exception: {0}", e.Exception);
}; };
var hostBuilder = new WebHostBuilder() var hostBuilder = new HostBuilder()
.ConfigureWebHost(webHostBuilder =>
{
webHostBuilder
.UseKestrel((context, options) =>
{
var basePort = context.Configuration.GetValue<int?>("BASE_PORT") ?? 5000;
options.Listen(IPAddress.Loopback, basePort, listenOptions =>
{
// Uncomment the following to enable Nagle's algorithm for this endpoint.
//listenOptions.NoDelay = false;
listenOptions.UseConnectionLogging();
});
options.Listen(IPAddress.Loopback, basePort + 1, listenOptions =>
{
listenOptions.UseHttps();
listenOptions.UseConnectionLogging();
});
options.UseSystemd();
// The following section should be used to demo sockets
//options.ListenUnixSocket("/tmp/kestrel-test.sock");
})
.UseContentRoot(Directory.GetCurrentDirectory())
.UseStartup<Startup>();
if (string.Equals(Process.GetCurrentProcess().Id.ToString(), Environment.GetEnvironmentVariable("LISTEN_PID")))
{
// Use libuv if activated by systemd, since that's currently the only transport that supports being passed a socket handle.
#pragma warning disable CS0618
webHostBuilder.UseLibuv(options =>
{
// Uncomment the following line to change the default number of libuv threads for all endpoints.
// options.ThreadCount = 4;
});
#pragma warning restore CS0618
}
})
.ConfigureLogging((_, factory) => .ConfigureLogging((_, factory) =>
{ {
factory.AddConsole(); factory.AddConsole();
}) });
.UseKestrel((context, options) =>
{
var basePort = context.Configuration.GetValue<int?>("BASE_PORT") ?? 5000;
options.Listen(IPAddress.Loopback, basePort, listenOptions =>
{
// Uncomment the following to enable Nagle's algorithm for this endpoint.
//listenOptions.NoDelay = false;
listenOptions.UseConnectionLogging();
});
options.Listen(IPAddress.Loopback, basePort + 1, listenOptions =>
{
listenOptions.UseHttps();
listenOptions.UseConnectionLogging();
});
options.UseSystemd();
// The following section should be used to demo sockets
//options.ListenUnixSocket("/tmp/kestrel-test.sock");
})
.UseContentRoot(Directory.GetCurrentDirectory())
.UseStartup<Startup>();
if (string.Equals(Process.GetCurrentProcess().Id.ToString(), Environment.GetEnvironmentVariable("LISTEN_PID")))
{
// Use libuv if activated by systemd, since that's currently the only transport that supports being passed a socket handle.
#pragma warning disable CS0618
hostBuilder.UseLibuv(options =>
{
// Uncomment the following line to change the default number of libuv threads for all endpoints.
// options.ThreadCount = 4;
});
#pragma warning restore CS0618
}
return hostBuilder.Build().RunAsync(); return hostBuilder.Build().RunAsync();
} }

View File

@ -26,9 +26,9 @@ namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests
/// <summary> /// <summary>
/// Summary description for TestServer /// Summary description for TestServer
/// </summary> /// </summary>
internal class TestServer : IDisposable, IStartup internal class TestServer : IAsyncDisposable, IStartup
{ {
private IWebHost _host; private IHost _host;
private ListenOptions _listenOptions; private ListenOptions _listenOptions;
private readonly RequestDelegate _app; private readonly RequestDelegate _app;
@ -70,32 +70,37 @@ namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests
_app = app; _app = app;
Context = context; Context = context;
_host = TransportSelector.GetWebHostBuilder(context.MemoryPoolFactory, context.ServerOptions.Limits.MaxRequestBufferSize) _host = TransportSelector.GetHostBuilder(context.MemoryPoolFactory, context.ServerOptions.Limits.MaxRequestBufferSize)
.UseKestrel(options => .ConfigureWebHost(webHostBuilder =>
{ {
configureKestrel(options); webHostBuilder
_listenOptions = options.ListenOptions.First(); .UseKestrel(options =>
})
.ConfigureServices(services =>
{
services.AddSingleton<IStartup>(this);
services.AddSingleton(context.LoggerFactory);
services.AddSingleton<IServer>(sp =>
{
// Manually configure options on the TestServiceContext.
// We're doing this so we can use the same instance that was passed in
var configureOptions = sp.GetServices<IConfigureOptions<KestrelServerOptions>>();
foreach (var c in configureOptions)
{ {
c.Configure(context.ServerOptions); configureKestrel(options);
} _listenOptions = options.ListenOptions.First();
})
.ConfigureServices(services =>
{
services.AddSingleton<IStartup>(this);
services.AddSingleton(context.LoggerFactory);
services.AddSingleton<IServer>(sp =>
{
// Manually configure options on the TestServiceContext.
// We're doing this so we can use the same instance that was passed in
var configureOptions = sp.GetServices<IConfigureOptions<KestrelServerOptions>>();
foreach (var c in configureOptions)
{
c.Configure(context.ServerOptions);
}
return new KestrelServer(new List<IConnectionListenerFactory>() { sp.GetRequiredService<IConnectionListenerFactory>() }, context); return new KestrelServer(new List<IConnectionListenerFactory>() { sp.GetRequiredService<IConnectionListenerFactory>() }, context);
}); });
configureServices(services); configureServices(services);
})
.UseSetting(WebHostDefaults.ApplicationKey, typeof(TestServer).GetTypeInfo().Assembly.FullName)
.UseSetting(WebHostDefaults.ShutdownTimeoutKey, TestConstants.DefaultTimeout.TotalSeconds.ToString())
.Configure(app => { app.Run(_app); });
}) })
.UseSetting(WebHostDefaults.ApplicationKey, typeof(TestServer).GetTypeInfo().Assembly.FullName)
.UseSetting(WebHostDefaults.ShutdownTimeoutKey, TestConstants.DefaultTimeout.TotalSeconds.ToString())
.Build(); .Build();
_host.Start(); _host.Start();
@ -130,9 +135,11 @@ namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests
return _host.StopAsync(token); return _host.StopAsync(token);
} }
public void Dispose() public async ValueTask DisposeAsync()
{ {
_host.Dispose(); await _host.StopAsync().ConfigureAwait(false);
// The concrete Host implements IAsyncDisposable
await ((IAsyncDisposable)_host).DisposeAsync().ConfigureAwait(false);
} }
} }
} }

View File

@ -21,8 +21,11 @@ using Microsoft.AspNetCore.Testing;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
using Microsoft.Extensions.Hosting;
using Xunit; using Xunit;
using Xunit.Sdk; using Xunit.Sdk;
using Microsoft.AspNetCore.Hosting.Server;
using Microsoft.Extensions.DependencyInjection;
namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests
{ {
@ -182,17 +185,21 @@ namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests
private async Task RegisterAddresses_Success(string addressInput, string[] testUrls, int testPort = 0) private async Task RegisterAddresses_Success(string addressInput, string[] testUrls, int testPort = 0)
{ {
var hostBuilder = TransportSelector.GetWebHostBuilder() var hostBuilder = TransportSelector.GetHostBuilder()
.UseKestrel(serverOptions => .ConfigureWebHost(webHostBuilder =>
{ {
serverOptions.ConfigureHttpsDefaults(httpsOptions => webHostBuilder
{ .UseKestrel(serverOptions =>
httpsOptions.ServerCertificate = TestResources.GetTestCertificate(); {
}); serverOptions.ConfigureHttpsDefaults(httpsOptions =>
{
httpsOptions.ServerCertificate = TestResources.GetTestCertificate();
});
})
.UseUrls(addressInput)
.Configure(ConfigureEchoAddress);
}) })
.ConfigureServices(AddTestLogging) .ConfigureServices(AddTestLogging);
.UseUrls(addressInput)
.Configure(ConfigureEchoAddress);
using (var host = hostBuilder.Build()) using (var host = hostBuilder.Build())
{ {
@ -214,6 +221,8 @@ namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests
} }
Assert.Equal(uri.ToString(), response); Assert.Equal(uri.ToString(), response);
} }
await host.StopAsync();
} }
} }
@ -226,25 +235,29 @@ namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests
[Fact] [Fact]
public async Task RegisterHttpAddress_UpgradedToHttpsByConfigureEndpointDefaults() public async Task RegisterHttpAddress_UpgradedToHttpsByConfigureEndpointDefaults()
{ {
var hostBuilder = TransportSelector.GetWebHostBuilder() var hostBuilder = TransportSelector.GetHostBuilder()
.UseKestrel(serverOptions => .ConfigureWebHost(webHostBuilder =>
{ {
serverOptions.ConfigureEndpointDefaults(listenOptions => webHostBuilder
{ .UseKestrel(serverOptions =>
listenOptions.UseHttps(TestResources.GetTestCertificate()); {
}); serverOptions.ConfigureEndpointDefaults(listenOptions =>
{
listenOptions.UseHttps(TestResources.GetTestCertificate());
});
})
.UseUrls("http://127.0.0.1:0")
.Configure(app =>
{
var serverAddresses = app.ServerFeatures.Get<IServerAddressesFeature>();
app.Run(context =>
{
Assert.Single(serverAddresses.Addresses);
return context.Response.WriteAsync(serverAddresses.Addresses.First());
});
});
}) })
.ConfigureServices(AddTestLogging) .ConfigureServices(AddTestLogging);
.UseUrls("http://127.0.0.1:0")
.Configure(app =>
{
var serverAddresses = app.ServerFeatures.Get<IServerAddressesFeature>();
app.Run(context =>
{
Assert.Single(serverAddresses.Addresses);
return context.Response.WriteAsync(serverAddresses.Addresses.First());
});
});
using (var host = hostBuilder.Build()) using (var host = hostBuilder.Build())
{ {
@ -254,6 +267,8 @@ namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests
var response = await HttpClientSlim.GetStringAsync(expectedUrl, validateCertificate: false); var response = await HttpClientSlim.GetStringAsync(expectedUrl, validateCertificate: false);
Assert.Equal(expectedUrl, response); Assert.Equal(expectedUrl, response);
await host.StopAsync();
} }
} }
@ -293,19 +308,23 @@ namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests
private async Task RegisterIPEndPoint_Success(IPEndPoint endPoint, string testUrl, int testPort = 0) private async Task RegisterIPEndPoint_Success(IPEndPoint endPoint, string testUrl, int testPort = 0)
{ {
var hostBuilder = TransportSelector.GetWebHostBuilder() var hostBuilder = TransportSelector.GetHostBuilder()
.ConfigureServices(AddTestLogging) .ConfigureWebHost(webHostBuilder =>
.UseKestrel(options =>
{ {
options.Listen(endPoint, listenOptions => webHostBuilder
{ .UseKestrel(options =>
if (testUrl.StartsWith("https"))
{ {
listenOptions.UseHttps(TestResources.GetTestCertificate()); options.Listen(endPoint, listenOptions =>
} {
}); if (testUrl.StartsWith("https"))
{
listenOptions.UseHttps(TestResources.GetTestCertificate());
}
});
})
.Configure(ConfigureEchoAddress);
}) })
.Configure(ConfigureEchoAddress); .ConfigureServices(AddTestLogging);
using (var host = hostBuilder.Build()) using (var host = hostBuilder.Build())
{ {
@ -353,13 +372,17 @@ namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests
private async Task ListenAnyIP_Success(string[] testUrls, int testPort = 0) private async Task ListenAnyIP_Success(string[] testUrls, int testPort = 0)
{ {
var hostBuilder = TransportSelector.GetWebHostBuilder() var hostBuilder = TransportSelector.GetHostBuilder()
.UseKestrel(options => .ConfigureWebHost(webHostBuilder =>
{ {
options.ListenAnyIP(testPort); webHostBuilder
.UseKestrel(options =>
{
options.ListenAnyIP(testPort);
})
.Configure(ConfigureEchoAddress);
}) })
.ConfigureServices(AddTestLogging) .ConfigureServices(AddTestLogging);
.Configure(ConfigureEchoAddress);
using (var host = hostBuilder.Build()) using (var host = hostBuilder.Build())
{ {
@ -396,13 +419,17 @@ namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests
private async Task ListenLocalhost_Success(string[] testUrls, int testPort = 0) private async Task ListenLocalhost_Success(string[] testUrls, int testPort = 0)
{ {
var hostBuilder = TransportSelector.GetWebHostBuilder() var hostBuilder = TransportSelector.GetHostBuilder()
.UseKestrel(options => .ConfigureWebHost(webHostBuilder =>
{ {
options.ListenLocalhost(testPort); webHostBuilder
.UseKestrel(options =>
{
options.ListenLocalhost(testPort);
})
.Configure(ConfigureEchoAddress);
}) })
.ConfigureServices(AddTestLogging) .ConfigureServices(AddTestLogging);
.Configure(ConfigureEchoAddress);
using (var host = hostBuilder.Build()) using (var host = hostBuilder.Build())
{ {
@ -478,16 +505,20 @@ namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests
private async Task RegisterDefaultServerAddresses_Success(IEnumerable<string> addresses, bool mockHttps = false) private async Task RegisterDefaultServerAddresses_Success(IEnumerable<string> addresses, bool mockHttps = false)
{ {
var hostBuilder = TransportSelector.GetWebHostBuilder() var hostBuilder = TransportSelector.GetHostBuilder()
.ConfigureServices(AddTestLogging) .ConfigureWebHost(webHostBuilder =>
.UseKestrel(options =>
{ {
if (mockHttps) webHostBuilder
{ .UseKestrel(options =>
options.DefaultCertificate = TestResources.GetTestCertificate(); {
} if (mockHttps)
{
options.DefaultCertificate = TestResources.GetTestCertificate();
}
})
.Configure(ConfigureEchoAddress);
}) })
.Configure(ConfigureEchoAddress); .ConfigureServices(AddTestLogging);
using (var host = hostBuilder.Build()) using (var host = hostBuilder.Build())
{ {
@ -514,7 +545,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests
} }
[Fact] [Fact]
public void ThrowsWhenBindingToIPv4AddressInUse() public async Task ThrowsWhenBindingToIPv4AddressInUse()
{ {
using (var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) using (var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
{ {
@ -522,22 +553,28 @@ namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests
socket.Listen(0); socket.Listen(0);
var port = ((IPEndPoint)socket.LocalEndPoint).Port; var port = ((IPEndPoint)socket.LocalEndPoint).Port;
var hostBuilder = TransportSelector.GetWebHostBuilder() var hostBuilder = TransportSelector.GetHostBuilder()
.UseKestrel() .ConfigureWebHost(webHostBuilder =>
.UseUrls($"http://127.0.0.1:{port}") {
.Configure(ConfigureEchoAddress); webHostBuilder
.UseKestrel()
.UseUrls($"http://127.0.0.1:{port}")
.Configure(ConfigureEchoAddress);
});
using (var host = hostBuilder.Build()) using (var host = hostBuilder.Build())
{ {
var exception = Assert.Throws<IOException>(() => host.Start()); var exception = Assert.Throws<IOException>(() => host.Start());
Assert.Equal(CoreStrings.FormatEndpointAlreadyInUse($"http://127.0.0.1:{port}"), exception.Message); Assert.Equal(CoreStrings.FormatEndpointAlreadyInUse($"http://127.0.0.1:{port}"), exception.Message);
await host.StopAsync();
} }
} }
} }
[ConditionalFact] [ConditionalFact]
[IPv6SupportedCondition] [IPv6SupportedCondition]
public void ThrowsWhenBindingToIPv6AddressInUse() public async Task ThrowsWhenBindingToIPv6AddressInUse()
{ {
TestApplicationErrorLogger.IgnoredExceptions.Add(typeof(IOException)); TestApplicationErrorLogger.IgnoredExceptions.Add(typeof(IOException));
@ -547,16 +584,22 @@ namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests
socket.Listen(0); socket.Listen(0);
var port = ((IPEndPoint)socket.LocalEndPoint).Port; var port = ((IPEndPoint)socket.LocalEndPoint).Port;
var hostBuilder = TransportSelector.GetWebHostBuilder() var hostBuilder = TransportSelector.GetHostBuilder()
.ConfigureServices(AddTestLogging) .ConfigureWebHost(webHostBuilder =>
.UseKestrel() {
.UseUrls($"http://[::1]:{port}") webHostBuilder
.Configure(ConfigureEchoAddress); .UseKestrel()
.UseUrls($"http://[::1]:{port}")
.Configure(ConfigureEchoAddress);
})
.ConfigureServices(AddTestLogging);
using (var host = hostBuilder.Build()) using (var host = hostBuilder.Build())
{ {
var exception = Assert.Throws<IOException>(() => host.Start()); var exception = Assert.Throws<IOException>(() => host.Start());
Assert.Equal(CoreStrings.FormatEndpointAlreadyInUse($"http://[::1]:{port}"), exception.Message); Assert.Equal(CoreStrings.FormatEndpointAlreadyInUse($"http://[::1]:{port}"), exception.Message);
await host.StopAsync();
} }
} }
} }
@ -565,18 +608,22 @@ namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests
public async Task OverrideDirectConfigurationWithIServerAddressesFeature_Succeeds() public async Task OverrideDirectConfigurationWithIServerAddressesFeature_Succeeds()
{ {
var useUrlsAddress = $"http://127.0.0.1:0"; var useUrlsAddress = $"http://127.0.0.1:0";
var hostBuilder = TransportSelector.GetWebHostBuilder() var hostBuilder = TransportSelector.GetHostBuilder()
.UseKestrel(options => .ConfigureWebHost(webHostBuilder =>
{ {
options.Listen(new IPEndPoint(IPAddress.Loopback, 0), listenOptions => webHostBuilder
{ .UseKestrel(options =>
listenOptions.UseHttps(TestResources.GetTestCertificate()); {
}); options.Listen(new IPEndPoint(IPAddress.Loopback, 0), listenOptions =>
{
listenOptions.UseHttps(TestResources.GetTestCertificate());
});
})
.UseUrls(useUrlsAddress)
.PreferHostingUrls(true)
.Configure(ConfigureEchoAddress);
}) })
.UseUrls(useUrlsAddress) .ConfigureServices(AddTestLogging);
.PreferHostingUrls(true)
.ConfigureServices(AddTestLogging)
.Configure(ConfigureEchoAddress);
using (var host = hostBuilder.Build()) using (var host = hostBuilder.Build())
{ {
@ -586,7 +633,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests
// If this isn't working properly, we'll get the HTTPS endpoint defined in UseKestrel // If this isn't working properly, we'll get the HTTPS endpoint defined in UseKestrel
// instead of the HTTP endpoint defined in UseUrls. // instead of the HTTP endpoint defined in UseUrls.
var serverAddresses = host.ServerFeatures.Get<IServerAddressesFeature>().Addresses; var serverAddresses = host.Services.GetRequiredService<IServer>().Features.Get<IServerAddressesFeature>().Addresses;
Assert.Equal(1, serverAddresses.Count); Assert.Equal(1, serverAddresses.Count);
var useUrlsAddressWithPort = $"http://127.0.0.1:{port}"; var useUrlsAddressWithPort = $"http://127.0.0.1:{port}";
Assert.Equal(serverAddresses.First(), useUrlsAddressWithPort); Assert.Equal(serverAddresses.First(), useUrlsAddressWithPort);
@ -606,18 +653,22 @@ namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests
{ {
var useUrlsAddress = $"http://127.0.0.1:0"; var useUrlsAddress = $"http://127.0.0.1:0";
var hostBuilder = TransportSelector.GetWebHostBuilder() var hostBuilder = TransportSelector.GetHostBuilder()
.ConfigureServices(AddTestLogging) .ConfigureWebHost(webHostBuilder =>
.UseKestrel(options =>
{ {
options.Listen(new IPEndPoint(IPAddress.Loopback, 0), listenOptions => webHostBuilder
{ .UseKestrel(options =>
listenOptions.UseHttps(TestResources.TestCertificatePath, "testPassword"); {
}); options.Listen(new IPEndPoint(IPAddress.Loopback, 0), listenOptions =>
{
listenOptions.UseHttps(TestResources.TestCertificatePath, "testPassword");
});
})
.UseUrls($"http://127.0.0.1:0")
.PreferHostingUrls(false)
.Configure(ConfigureEchoAddress);
}) })
.UseUrls($"http://127.0.0.1:0") .ConfigureServices(AddTestLogging);
.PreferHostingUrls(false)
.Configure(ConfigureEchoAddress);
using (var host = hostBuilder.Build()) using (var host = hostBuilder.Build())
{ {
@ -627,7 +678,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests
// If this isn't working properly, we'll get the HTTP endpoint defined in UseUrls // If this isn't working properly, we'll get the HTTP endpoint defined in UseUrls
// instead of the HTTPS endpoint defined in UseKestrel. // instead of the HTTPS endpoint defined in UseKestrel.
var serverAddresses = host.ServerFeatures.Get<IServerAddressesFeature>().Addresses; var serverAddresses = host.Services.GetRequiredService<IServer>().Features.Get<IServerAddressesFeature>().Addresses;
Assert.Equal(1, serverAddresses.Count); Assert.Equal(1, serverAddresses.Count);
var endPointAddress = $"https://127.0.0.1:{port}"; var endPointAddress = $"https://127.0.0.1:{port}";
Assert.Equal(serverAddresses.First(), endPointAddress); Assert.Equal(serverAddresses.First(), endPointAddress);
@ -644,17 +695,21 @@ namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests
[Fact] [Fact]
public async Task DoesNotOverrideDirectConfigurationWithIServerAddressesFeature_IfAddressesEmpty() public async Task DoesNotOverrideDirectConfigurationWithIServerAddressesFeature_IfAddressesEmpty()
{ {
var hostBuilder = TransportSelector.GetWebHostBuilder() var hostBuilder = TransportSelector.GetHostBuilder()
.ConfigureServices(AddTestLogging) .ConfigureWebHost(webHostBuilder =>
.UseKestrel(options =>
{ {
options.Listen(new IPEndPoint(IPAddress.Loopback, 0), listenOptions => webHostBuilder
{ .UseKestrel(options =>
listenOptions.UseHttps(TestResources.GetTestCertificate()); {
}); options.Listen(new IPEndPoint(IPAddress.Loopback, 0), listenOptions =>
{
listenOptions.UseHttps(TestResources.GetTestCertificate());
});
})
.PreferHostingUrls(true)
.Configure(ConfigureEchoAddress);
}) })
.PreferHostingUrls(true) .ConfigureServices(AddTestLogging);
.Configure(ConfigureEchoAddress);
using (var host = hostBuilder.Build()) using (var host = hostBuilder.Build())
{ {
@ -663,7 +718,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests
var port = host.GetPort(); var port = host.GetPort();
// If this isn't working properly, we'll not get the HTTPS endpoint defined in UseKestrel. // If this isn't working properly, we'll not get the HTTPS endpoint defined in UseKestrel.
var serverAddresses = host.ServerFeatures.Get<IServerAddressesFeature>().Addresses; var serverAddresses = host.Services.GetRequiredService<IServer>().Features.Get<IServerAddressesFeature>().Addresses;
Assert.Equal(1, serverAddresses.Count); Assert.Equal(1, serverAddresses.Count);
var endPointAddress = $"https://127.0.0.1:{port}"; var endPointAddress = $"https://127.0.0.1:{port}";
Assert.Equal(serverAddresses.First(), endPointAddress); Assert.Equal(serverAddresses.First(), endPointAddress);
@ -688,38 +743,50 @@ namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests
} }
[Fact] [Fact]
public void ThrowsWhenBindingLocalhostToDynamicPort() public async Task ThrowsWhenBindingLocalhostToDynamicPort()
{ {
TestApplicationErrorLogger.IgnoredExceptions.Add(typeof(InvalidOperationException)); TestApplicationErrorLogger.IgnoredExceptions.Add(typeof(InvalidOperationException));
var hostBuilder = TransportSelector.GetWebHostBuilder() var hostBuilder = TransportSelector.GetHostBuilder()
.ConfigureServices(AddTestLogging) .ConfigureWebHost(webHostBuilder =>
.UseKestrel() {
.UseUrls("http://localhost:0") webHostBuilder
.Configure(ConfigureEchoAddress); .UseKestrel()
.UseUrls("http://localhost:0")
.Configure(ConfigureEchoAddress);
})
.ConfigureServices(AddTestLogging);
using (var host = hostBuilder.Build()) using (var host = hostBuilder.Build())
{ {
Assert.Throws<InvalidOperationException>(() => host.Start()); Assert.Throws<InvalidOperationException>(() => host.Start());
await host.StopAsync();
} }
} }
[Theory] [Theory]
[InlineData("ftp://localhost")] [InlineData("ftp://localhost")]
[InlineData("ssh://localhost")] [InlineData("ssh://localhost")]
public void ThrowsForUnsupportedAddressFromHosting(string address) public async Task ThrowsForUnsupportedAddressFromHosting(string address)
{ {
TestApplicationErrorLogger.IgnoredExceptions.Add(typeof(InvalidOperationException)); TestApplicationErrorLogger.IgnoredExceptions.Add(typeof(InvalidOperationException));
var hostBuilder = TransportSelector.GetWebHostBuilder() var hostBuilder = TransportSelector.GetHostBuilder()
.ConfigureServices(AddTestLogging) .ConfigureWebHost(webHostBuilder =>
.UseKestrel() {
.UseUrls(address) webHostBuilder
.Configure(ConfigureEchoAddress); .UseKestrel()
.UseUrls(address)
.Configure(ConfigureEchoAddress);
})
.ConfigureServices(AddTestLogging);
using (var host = hostBuilder.Build()) using (var host = hostBuilder.Build())
{ {
Assert.Throws<InvalidOperationException>(() => host.Start()); Assert.Throws<InvalidOperationException>(() => host.Start());
await host.StopAsync();
} }
} }
@ -729,13 +796,17 @@ namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests
var port = GetNextPort(); var port = GetNextPort();
var endPointAddress = $"http://127.0.0.1:{port}/"; var endPointAddress = $"http://127.0.0.1:{port}/";
var hostBuilder = TransportSelector.GetWebHostBuilder() var hostBuilder = TransportSelector.GetHostBuilder()
.ConfigureServices(AddTestLogging) .ConfigureWebHost(webHostBuilder =>
.UseKestrel(options =>
{ {
options.Listen(IPAddress.Loopback, port); webHostBuilder
.UseKestrel(options =>
{
options.Listen(IPAddress.Loopback, port);
})
.Configure(ConfigureEchoAddress);
}) })
.Configure(ConfigureEchoAddress); .ConfigureServices(AddTestLogging);
using (var host = hostBuilder.Build()) using (var host = hostBuilder.Build())
{ {
@ -746,12 +817,16 @@ namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests
await host.StopAsync(); await host.StopAsync();
} }
hostBuilder = TransportSelector.GetWebHostBuilder() hostBuilder = TransportSelector.GetHostBuilder()
.UseKestrel(options => .ConfigureWebHost(webHostBuilder =>
{ {
options.Listen(IPAddress.Loopback, port); webHostBuilder
}) .UseKestrel(options =>
.Configure(ConfigureEchoAddress); {
options.Listen(IPAddress.Loopback, port);
})
.Configure(ConfigureEchoAddress);
});
using (var host = hostBuilder.Build()) using (var host = hostBuilder.Build())
{ {
@ -771,14 +846,18 @@ namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests
var ipv4endPointAddress = $"http://127.0.0.1:{port}/"; var ipv4endPointAddress = $"http://127.0.0.1:{port}/";
var ipv6endPointAddress = $"http://[::1]:{port}/"; var ipv6endPointAddress = $"http://[::1]:{port}/";
var hostBuilder = TransportSelector.GetWebHostBuilder() var hostBuilder = TransportSelector.GetHostBuilder()
.ConfigureServices(AddTestLogging) .ConfigureWebHost(webHostBuilder =>
.UseKestrel(options =>
{ {
options.Listen(IPAddress.Loopback, port); webHostBuilder
options.Listen(IPAddress.IPv6Loopback, port); .UseKestrel(options =>
{
options.Listen(IPAddress.Loopback, port);
options.Listen(IPAddress.IPv6Loopback, port);
})
.Configure(ConfigureEchoAddress);
}) })
.Configure(ConfigureEchoAddress); .ConfigureServices(AddTestLogging);
using (var host = hostBuilder.Build()) using (var host = hostBuilder.Build())
{ {
@ -790,13 +869,17 @@ namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests
await host.StopAsync(); await host.StopAsync();
} }
hostBuilder = TransportSelector.GetWebHostBuilder() hostBuilder = TransportSelector.GetHostBuilder()
.UseKestrel(options => .ConfigureWebHost(webHostBuilder =>
{ {
options.Listen(IPAddress.Loopback, port); webHostBuilder
options.Listen(IPAddress.IPv6Loopback, port); .UseKestrel(options =>
}) {
.Configure(ConfigureEchoAddress); options.Listen(IPAddress.Loopback, port);
options.Listen(IPAddress.IPv6Loopback, port);
})
.Configure(ConfigureEchoAddress);
});
using (var host = hostBuilder.Build()) using (var host = hostBuilder.Build())
{ {
@ -816,20 +899,24 @@ namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests
public async Task EndpointDefaultsConfig_CanSetProtocolForUrlsConfig(string input, HttpProtocols expected) public async Task EndpointDefaultsConfig_CanSetProtocolForUrlsConfig(string input, HttpProtocols expected)
{ {
KestrelServerOptions capturedOptions = null; KestrelServerOptions capturedOptions = null;
var hostBuilder = TransportSelector.GetWebHostBuilder() var hostBuilder = TransportSelector.GetHostBuilder()
.UseKestrel(options => .ConfigureWebHost(webHostBuilder =>
{ {
var config = new ConfigurationBuilder().AddInMemoryCollection(new[] webHostBuilder
{ .UseKestrel(options =>
new KeyValuePair<string, string>("EndpointDefaults:Protocols", input), {
}).Build(); var config = new ConfigurationBuilder().AddInMemoryCollection(new[]
options.Configure(config); {
new KeyValuePair<string, string>("EndpointDefaults:Protocols", input),
}).Build();
options.Configure(config);
capturedOptions = options; capturedOptions = options;
})
.UseUrls("http://127.0.0.1:0")
.Configure(ConfigureEchoAddress);
}) })
.ConfigureServices(AddTestLogging) .ConfigureServices(AddTestLogging);
.UseUrls("http://127.0.0.1:0")
.Configure(ConfigureEchoAddress);
using (var host = hostBuilder.Build()) using (var host = hostBuilder.Build())
{ {
@ -875,11 +962,15 @@ namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests
continue; continue;
} }
var hostBuilder = TransportSelector.GetWebHostBuilder() var hostBuilder = TransportSelector.GetHostBuilder()
.ConfigureServices(AddTestLogging) .ConfigureWebHost(webHostBuilder =>
.UseKestrel() {
.UseUrls($"http://localhost:{port}") webHostBuilder
.Configure(ConfigureEchoAddress); .UseKestrel()
.UseUrls($"http://localhost:{port}")
.Configure(ConfigureEchoAddress);
})
.ConfigureServices(AddTestLogging);
using (var host = hostBuilder.Build()) using (var host = hostBuilder.Build())
{ {

View File

@ -22,7 +22,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests
var serviceContext = new TestServiceContext(LoggerFactory); var serviceContext = new TestServiceContext(LoggerFactory);
using (var server = new TestServer(TestApp.EchoApp, serviceContext, listenOptions)) await using (var server = new TestServer(TestApp.EchoApp, serviceContext, listenOptions))
{ {
using (var connection = server.CreateConnection()) using (var connection = server.CreateConnection())
{ {
@ -41,7 +41,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests
} }
}); });
} }
await server.StopAsync();
} }
} }
} }

View File

@ -83,7 +83,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests.Http2
[MinimumOSVersion(OperatingSystems.Windows, WindowsVersions.Win10)] [MinimumOSVersion(OperatingSystems.Windows, WindowsVersions.Win10)]
public async Task TlsAlpnHandshakeSelectsHttp2From1and2() public async Task TlsAlpnHandshakeSelectsHttp2From1and2()
{ {
using (var server = new TestServer(context => await using (var server = new TestServer(context =>
{ {
var tlsFeature = context.Features.Get<ITlsApplicationProtocolFeature>(); var tlsFeature = context.Features.Get<ITlsApplicationProtocolFeature>();
Assert.NotNull(tlsFeature); Assert.NotNull(tlsFeature);
@ -103,8 +103,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests.Http2
{ {
var result = await Client.GetStringAsync($"https://localhost:{server.Port}/"); var result = await Client.GetStringAsync($"https://localhost:{server.Port}/");
Assert.Equal("hello world HTTP/2", result); Assert.Equal("hello world HTTP/2", result);
await server.StopAsync();
} }
} }
@ -113,7 +111,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests.Http2
[MinimumOSVersion(OperatingSystems.Windows, WindowsVersions.Win10)] [MinimumOSVersion(OperatingSystems.Windows, WindowsVersions.Win10)]
public async Task TlsAlpnHandshakeSelectsHttp2() public async Task TlsAlpnHandshakeSelectsHttp2()
{ {
using (var server = new TestServer(context => await using (var server = new TestServer(context =>
{ {
var tlsFeature = context.Features.Get<ITlsApplicationProtocolFeature>(); var tlsFeature = context.Features.Get<ITlsApplicationProtocolFeature>();
Assert.NotNull(tlsFeature); Assert.NotNull(tlsFeature);
@ -133,7 +131,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests.Http2
{ {
var result = await Client.GetStringAsync($"https://localhost:{server.Port}/"); var result = await Client.GetStringAsync($"https://localhost:{server.Port}/");
Assert.Equal("hello world HTTP/2", result); Assert.Equal("hello world HTTP/2", result);
await server.StopAsync();
} }
} }
} }

View File

@ -61,7 +61,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests.Http2
testContext.InitializeHeartbeat(); testContext.InitializeHeartbeat();
using (var server = new TestServer(async context => await using (var server = new TestServer(async context =>
{ {
requestStarted.SetResult(); requestStarted.SetResult();
await requestUnblocked.Task.DefaultTimeout(); await requestUnblocked.Task.DefaultTimeout();
@ -115,7 +115,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests.Http2
TestApplicationErrorLogger.ThrowOnUngracefulShutdown = false; TestApplicationErrorLogger.ThrowOnUngracefulShutdown = false;
// Abortive shutdown leaves one request hanging // Abortive shutdown leaves one request hanging
using (var server = new TestServer(async context => await using (var server = new TestServer(async context =>
{ {
requestStarted.SetResult(); requestStarted.SetResult();
await requestUnblocked.Task.DefaultTimeout(); await requestUnblocked.Task.DefaultTimeout();
@ -145,7 +145,15 @@ namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests.Http2
await closingMessageTask; await closingMessageTask;
cts.Cancel(); cts.Cancel();
await stopServerTask; try
{
await stopServerTask;
}
// Remove when https://github.com/dotnet/runtime/issues/40290 is fixed
catch (OperationCanceledException)
{
}
} }
Assert.Contains(TestApplicationErrorLogger.Messages, m => m.Message.Contains("is closing.")); Assert.Contains(TestApplicationErrorLogger.Messages, m => m.Message.Contains("is closing."));

View File

@ -28,7 +28,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests
{ {
_canListenToOpenTcpSocketHandleSocket.Bind(new IPEndPoint(IPAddress.Loopback, 0)); _canListenToOpenTcpSocketHandleSocket.Bind(new IPEndPoint(IPAddress.Loopback, 0));
using (var server = new TestServer(_ => Task.CompletedTask, new TestServiceContext(LoggerFactory), await using (var server = new TestServer(_ => Task.CompletedTask, new TestServiceContext(LoggerFactory),
new ListenOptions((ulong)_canListenToOpenTcpSocketHandleSocket.Handle))) new ListenOptions((ulong)_canListenToOpenTcpSocketHandleSocket.Handle)))
{ {
using (var connection = new TestConnection(((IPEndPoint)_canListenToOpenTcpSocketHandleSocket.LocalEndPoint).Port)) using (var connection = new TestConnection(((IPEndPoint)_canListenToOpenTcpSocketHandleSocket.LocalEndPoint).Port))
@ -42,7 +42,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests
"", "",
""); "");
} }
await server.StopAsync();
} }
} }
} }

View File

@ -14,7 +14,8 @@ using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Testing; using Microsoft.AspNetCore.Testing;
using Microsoft.Extensions.Logging.Testing; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Xunit; using Xunit;
namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests
@ -124,7 +125,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests
var memoryPoolFactory = new DiagnosticMemoryPoolFactory(allowLateReturn: true); var memoryPoolFactory = new DiagnosticMemoryPoolFactory(allowLateReturn: true);
using (var host = await StartWebHost(maxRequestBufferSize, data, connectionAdapter, startReadingRequestBody, clientFinishedSendingRequestBody, memoryPoolFactory.Create)) using (var host = await StartHost(maxRequestBufferSize, data, connectionAdapter, startReadingRequestBody, clientFinishedSendingRequestBody, memoryPoolFactory.Create))
{ {
var port = host.GetPort(); var port = host.GetPort();
using (var socket = CreateSocket(port)) using (var socket = CreateSocket(port))
@ -217,7 +218,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests
var memoryPoolFactory = new DiagnosticMemoryPoolFactory(allowLateReturn: true); var memoryPoolFactory = new DiagnosticMemoryPoolFactory(allowLateReturn: true);
using (var host = await StartWebHost(16 * 1024, data, false, startReadingRequestBody, clientFinishedSendingRequestBody, memoryPoolFactory.Create)) using (var host = await StartHost(16 * 1024, data, false, startReadingRequestBody, clientFinishedSendingRequestBody, memoryPoolFactory.Create))
{ {
var port = host.GetPort(); var port = host.GetPort();
using (var socket = CreateSocket(port)) using (var socket = CreateSocket(port))
@ -271,7 +272,15 @@ namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests
// Dispose host prior to closing connection to verify the server doesn't throw during shutdown // Dispose host prior to closing connection to verify the server doesn't throw during shutdown
// if a connection no longer has alloc and read callbacks configured. // if a connection no longer has alloc and read callbacks configured.
await host.StopAsync(); try
{
await host.StopAsync();
}
// Remove when https://github.com/dotnet/runtime/issues/40290 is fixed
catch (OperationCanceledException)
{
}
host.Dispose(); host.Dispose();
} }
} }
@ -281,67 +290,71 @@ namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests
await memoryPoolFactory.WhenAllBlocksReturned(TestConstants.DefaultTimeout); await memoryPoolFactory.WhenAllBlocksReturned(TestConstants.DefaultTimeout);
} }
private async Task<IWebHost> StartWebHost(long? maxRequestBufferSize, private async Task<IHost> StartHost(long? maxRequestBufferSize,
byte[] expectedBody, byte[] expectedBody,
bool useConnectionAdapter, bool useConnectionAdapter,
TaskCompletionSource startReadingRequestBody, TaskCompletionSource startReadingRequestBody,
TaskCompletionSource clientFinishedSendingRequestBody, TaskCompletionSource clientFinishedSendingRequestBody,
Func<MemoryPool<byte>> memoryPoolFactory = null) Func<MemoryPool<byte>> memoryPoolFactory = null)
{ {
var host = TransportSelector.GetWebHostBuilder(memoryPoolFactory, maxRequestBufferSize) var host = TransportSelector.GetHostBuilder(memoryPoolFactory, maxRequestBufferSize)
.ConfigureServices(AddTestLogging) .ConfigureWebHost(webHostBuilder =>
.UseKestrel(options =>
{ {
options.Listen(new IPEndPoint(IPAddress.Loopback, 0), listenOptions => webHostBuilder
{ .UseKestrel(options =>
if (useConnectionAdapter)
{ {
listenOptions.UsePassThrough(); options.Listen(new IPEndPoint(IPAddress.Loopback, 0), listenOptions =>
} {
}); if (useConnectionAdapter)
{
listenOptions.UsePassThrough();
}
});
options.Limits.MaxRequestBufferSize = maxRequestBufferSize; options.Limits.MaxRequestBufferSize = maxRequestBufferSize;
if (maxRequestBufferSize.HasValue && if (maxRequestBufferSize.HasValue &&
maxRequestBufferSize.Value < options.Limits.MaxRequestLineSize) maxRequestBufferSize.Value < options.Limits.MaxRequestLineSize)
{ {
options.Limits.MaxRequestLineSize = (int)maxRequestBufferSize; options.Limits.MaxRequestLineSize = (int)maxRequestBufferSize;
} }
if (maxRequestBufferSize.HasValue && if (maxRequestBufferSize.HasValue &&
maxRequestBufferSize.Value < options.Limits.MaxRequestHeadersTotalSize) maxRequestBufferSize.Value < options.Limits.MaxRequestHeadersTotalSize)
{ {
options.Limits.MaxRequestHeadersTotalSize = (int)maxRequestBufferSize; options.Limits.MaxRequestHeadersTotalSize = (int)maxRequestBufferSize;
} }
options.Limits.MinRequestBodyDataRate = null; options.Limits.MinRequestBodyDataRate = null;
options.Limits.MaxRequestBodySize = _dataLength; options.Limits.MaxRequestBodySize = _dataLength;
})
.UseContentRoot(Directory.GetCurrentDirectory())
.Configure(app => app.Run(async context =>
{
await startReadingRequestBody.Task.TimeoutAfter(TimeSpan.FromSeconds(120));
var buffer = new byte[expectedBody.Length];
var bytesRead = 0;
while (bytesRead < buffer.Length)
{
bytesRead += await context.Request.Body.ReadAsync(buffer, bytesRead, buffer.Length - bytesRead);
}
await clientFinishedSendingRequestBody.Task.TimeoutAfter(TimeSpan.FromSeconds(120));
// Verify client didn't send extra bytes
if (await context.Request.Body.ReadAsync(new byte[1], 0, 1) != 0)
{
context.Response.StatusCode = StatusCodes.Status500InternalServerError;
await context.Response.WriteAsync("Client sent more bytes than expectedBody.Length");
return;
}
await context.Response.WriteAsync($"bytesRead: {bytesRead.ToString()}");
}));
}) })
.UseContentRoot(Directory.GetCurrentDirectory()) .ConfigureServices(AddTestLogging)
.Configure(app => app.Run(async context =>
{
await startReadingRequestBody.Task.TimeoutAfter(TimeSpan.FromSeconds(120));
var buffer = new byte[expectedBody.Length];
var bytesRead = 0;
while (bytesRead < buffer.Length)
{
bytesRead += await context.Request.Body.ReadAsync(buffer, bytesRead, buffer.Length - bytesRead);
}
await clientFinishedSendingRequestBody.Task.TimeoutAfter(TimeSpan.FromSeconds(120));
// Verify client didn't send extra bytes
if (await context.Request.Body.ReadAsync(new byte[1], 0, 1) != 0)
{
context.Response.StatusCode = StatusCodes.Status500InternalServerError;
await context.Response.WriteAsync("Client sent more bytes than expectedBody.Length");
return;
}
await context.Response.WriteAsync($"bytesRead: {bytesRead.ToString()}");
}))
.Build(); .Build();
await host.StartAsync(); await host.StartAsync();

View File

@ -23,6 +23,7 @@ using Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Infrastructure;
using Microsoft.AspNetCore.Testing; using Microsoft.AspNetCore.Testing;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Testing; using Microsoft.Extensions.Logging.Testing;
using Microsoft.Extensions.Hosting;
using Moq; using Moq;
using Newtonsoft.Json; using Newtonsoft.Json;
using Newtonsoft.Json.Linq; using Newtonsoft.Json.Linq;
@ -55,39 +56,43 @@ namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests
Assert.True(contentLength % bufferLength == 0, $"{nameof(contentLength)} sent must be evenly divisible by {bufferLength}."); Assert.True(contentLength % bufferLength == 0, $"{nameof(contentLength)} sent must be evenly divisible by {bufferLength}.");
Assert.True(bufferLength % 256 == 0, $"{nameof(bufferLength)} must be evenly divisible by 256"); Assert.True(bufferLength % 256 == 0, $"{nameof(bufferLength)} must be evenly divisible by 256");
var builder = TransportSelector.GetWebHostBuilder() var builder = TransportSelector.GetHostBuilder()
.ConfigureServices(AddTestLogging) .ConfigureWebHost(webHostBuilder =>
.UseKestrel(options =>
{ {
options.Limits.MaxRequestBodySize = contentLength; webHostBuilder
options.Limits.MinRequestBodyDataRate = null; .UseKestrel(options =>
})
.UseUrls("http://127.0.0.1:0/")
.Configure(app =>
{
app.Run(async context =>
{
// Read the full request body
long total = 0;
var receivedBytes = new byte[bufferLength];
var received = 0;
while ((received = await context.Request.Body.ReadAsync(receivedBytes, 0, receivedBytes.Length)) > 0)
{ {
if (checkBytes) options.Limits.MaxRequestBodySize = contentLength;
options.Limits.MinRequestBodyDataRate = null;
})
.UseUrls("http://127.0.0.1:0/")
.Configure(app =>
{
app.Run(async context =>
{ {
for (var i = 0; i < received; i++) // Read the full request body
long total = 0;
var receivedBytes = new byte[bufferLength];
var received = 0;
while ((received = await context.Request.Body.ReadAsync(receivedBytes, 0, receivedBytes.Length)) > 0)
{ {
// Do not use Assert.Equal here, it is to slow for this hot path if (checkBytes)
Assert.True((byte)((total + i) % 256) == receivedBytes[i], "Data received is incorrect"); {
for (var i = 0; i < received; i++)
{
// Do not use Assert.Equal here, it is to slow for this hot path
Assert.True((byte)((total + i) % 256) == receivedBytes[i], "Data received is incorrect");
}
}
total += received;
} }
}
total += received; await context.Response.WriteAsync($"bytesRead: {total.ToString()}");
} });
});
await context.Response.WriteAsync($"bytesRead: {total.ToString()}"); })
}); .ConfigureServices(AddTestLogging);
});
using (var host = builder.Build()) using (var host = builder.Build())
{ {
@ -140,17 +145,21 @@ namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests
[Fact] [Fact]
public async Task DoesNotHangOnConnectionCloseRequest() public async Task DoesNotHangOnConnectionCloseRequest()
{ {
var builder = TransportSelector.GetWebHostBuilder() var builder = TransportSelector.GetHostBuilder()
.UseKestrel() .ConfigureWebHost(webHostBuilder =>
.UseUrls("http://127.0.0.1:0")
.ConfigureServices(AddTestLogging)
.Configure(app =>
{ {
app.Run(async context => webHostBuilder
{ .UseKestrel()
await context.Response.WriteAsync("hello, world"); .UseUrls("http://127.0.0.1:0")
}); .Configure(app =>
}); {
app.Run(async context =>
{
await context.Response.WriteAsync("hello, world");
});
});
})
.ConfigureServices(AddTestLogging);
using (var host = builder.Build()) using (var host = builder.Build())
using (var client = new HttpClient()) using (var client = new HttpClient())
@ -173,7 +182,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests
var requestNumber = 0; var requestNumber = 0;
var ensureConcurrentRequestTcs = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); var ensureConcurrentRequestTcs = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
using (var server = new TestServer(async context => await using (var server = new TestServer(async context =>
{ {
if (Interlocked.Increment(ref requestNumber) == 1) if (Interlocked.Increment(ref requestNumber) == 1)
{ {
@ -210,7 +219,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests
"", "",
""); "");
} }
await server.StopAsync();
} }
} }
@ -245,7 +253,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests
} }
}; };
using (var server = new TestServer(context => Task.CompletedTask, new TestServiceContext(LoggerFactory))) await using (var server = new TestServer(context => Task.CompletedTask, new TestServiceContext(LoggerFactory)))
{ {
using (var connection = server.CreateConnection()) using (var connection = server.CreateConnection())
{ {
@ -260,8 +268,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests
// is still in flight when the connection is aborted, leading to the reset never being received // is still in flight when the connection is aborted, leading to the reset never being received
// and therefore not logged. // and therefore not logged.
Assert.True(await connectionReset.WaitAsync(TestConstants.DefaultTimeout)); Assert.True(await connectionReset.WaitAsync(TestConstants.DefaultTimeout));
await server.StopAsync();
} }
Assert.False(loggedHigherThanDebug); Assert.False(loggedHigherThanDebug);
@ -293,7 +299,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests
} }
}; };
using (var server = new TestServer(context => Task.CompletedTask, new TestServiceContext(LoggerFactory))) await using (var server = new TestServer(context => Task.CompletedTask, new TestServiceContext(LoggerFactory)))
{ {
using (var connection = server.CreateConnection()) using (var connection = server.CreateConnection())
{ {
@ -321,7 +327,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests
// is still in flight when the connection is aborted, leading to the reset never being received // is still in flight when the connection is aborted, leading to the reset never being received
// and therefore not logged. // and therefore not logged.
Assert.True(await connectionReset.WaitAsync(TestConstants.DefaultTimeout)); Assert.True(await connectionReset.WaitAsync(TestConstants.DefaultTimeout));
await server.StopAsync();
} }
Assert.False(loggedHigherThanDebug); Assert.False(loggedHigherThanDebug);
@ -355,7 +360,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests
} }
}; };
using (var server = new TestServer(async context => await using (var server = new TestServer(async context =>
{ {
requestStarted.Release(); requestStarted.Release();
await connectionClosing.WaitAsync(); await connectionClosing.WaitAsync();
@ -378,7 +383,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests
// and therefore not logged. // and therefore not logged.
Assert.True(await connectionReset.WaitAsync(TestConstants.DefaultTimeout), "Connection reset event should have been logged"); Assert.True(await connectionReset.WaitAsync(TestConstants.DefaultTimeout), "Connection reset event should have been logged");
connectionClosing.Release(); connectionClosing.Release();
await server.StopAsync();
} }
Assert.False(loggedHigherThanDebug, "Logged event should not have been higher than debug."); Assert.False(loggedHigherThanDebug, "Logged event should not have been higher than debug.");
@ -392,26 +396,30 @@ namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests
var appDone = new SemaphoreSlim(0); var appDone = new SemaphoreSlim(0);
var expectedExceptionThrown = false; var expectedExceptionThrown = false;
var builder = TransportSelector.GetWebHostBuilder() var builder = TransportSelector.GetHostBuilder()
.ConfigureServices(AddTestLogging) .ConfigureWebHost(webHostBuilder =>
.UseKestrel()
.UseUrls("http://127.0.0.1:0")
.Configure(app => app.Run(async context =>
{ {
requestStarted.Release(); webHostBuilder
Assert.True(await connectionReset.WaitAsync(_semaphoreWaitTimeout)); .UseKestrel()
.UseUrls("http://127.0.0.1:0")
.Configure(app => app.Run(async context =>
{
requestStarted.Release();
Assert.True(await connectionReset.WaitAsync(_semaphoreWaitTimeout));
try try
{ {
await context.Request.Body.ReadAsync(new byte[1], 0, 1); await context.Request.Body.ReadAsync(new byte[1], 0, 1);
} }
catch (ConnectionResetException) catch (ConnectionResetException)
{ {
expectedExceptionThrown = true; expectedExceptionThrown = true;
} }
appDone.Release(); appDone.Release();
})); }));
})
.ConfigureServices(AddTestLogging);
using (var host = builder.Build()) using (var host = builder.Build())
{ {
@ -439,18 +447,22 @@ namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests
{ {
var appStarted = new SemaphoreSlim(0); var appStarted = new SemaphoreSlim(0);
var requestAborted = new SemaphoreSlim(0); var requestAborted = new SemaphoreSlim(0);
var builder = TransportSelector.GetWebHostBuilder() var builder = TransportSelector.GetHostBuilder()
.UseKestrel() .ConfigureWebHost(webHostBuilder =>
.UseUrls("http://127.0.0.1:0")
.ConfigureServices(AddTestLogging)
.Configure(app => app.Run(async context =>
{ {
appStarted.Release(); webHostBuilder
.UseKestrel()
.UseUrls("http://127.0.0.1:0")
.Configure(app => app.Run(async context =>
{
appStarted.Release();
var token = context.RequestAborted; var token = context.RequestAborted;
token.Register(() => requestAborted.Release(2)); token.Register(() => requestAborted.Release(2));
await requestAborted.WaitAsync().DefaultTimeout(); await requestAborted.WaitAsync().DefaultTimeout();
})); }));
})
.ConfigureServices(AddTestLogging);
using (var host = builder.Build()) using (var host = builder.Build())
{ {
@ -472,15 +484,19 @@ namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests
[Fact] [Fact]
public async Task AbortingTheConnectionSendsFIN() public async Task AbortingTheConnectionSendsFIN()
{ {
var builder = TransportSelector.GetWebHostBuilder() var builder = TransportSelector.GetHostBuilder()
.UseKestrel() .ConfigureWebHost(webHostBuilder =>
.UseUrls("http://127.0.0.1:0")
.ConfigureServices(AddTestLogging)
.Configure(app => app.Run(context =>
{ {
context.Abort(); webHostBuilder
return Task.CompletedTask; .UseKestrel()
})); .UseUrls("http://127.0.0.1:0")
.Configure(app => app.Run(context =>
{
context.Abort();
return Task.CompletedTask;
}));
})
.ConfigureServices(AddTestLogging);
using (var host = builder.Build()) using (var host = builder.Build())
{ {
@ -507,7 +523,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests
var appStartedTcs = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); var appStartedTcs = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
var connectionClosedTcs = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); var connectionClosedTcs = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
using (var server = new TestServer(context => await using (var server = new TestServer(context =>
{ {
appStartedTcs.SetResult(); appStartedTcs.SetResult();
@ -531,7 +547,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests
await connectionClosedTcs.Task.DefaultTimeout(); await connectionClosedTcs.Task.DefaultTimeout();
} }
await server.StopAsync();
} }
} }
@ -542,7 +557,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests
var testContext = new TestServiceContext(LoggerFactory); var testContext = new TestServiceContext(LoggerFactory);
var connectionClosedTcs = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); var connectionClosedTcs = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
using (var server = new TestServer(context => await using (var server = new TestServer(context =>
{ {
var connectionLifetimeFeature = context.Features.Get<IConnectionLifetimeFeature>(); var connectionLifetimeFeature = context.Features.Get<IConnectionLifetimeFeature>();
connectionLifetimeFeature.ConnectionClosed.Register(() => connectionClosedTcs.SetResult()); connectionLifetimeFeature.ConnectionClosed.Register(() => connectionClosedTcs.SetResult());
@ -568,7 +583,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests
"", "",
""); "");
} }
await server.StopAsync();
} }
} }
@ -579,7 +593,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests
var testContext = new TestServiceContext(LoggerFactory); var testContext = new TestServiceContext(LoggerFactory);
var connectionClosedTcs = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); var connectionClosedTcs = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
using (var server = new TestServer(context => await using (var server = new TestServer(context =>
{ {
var connectionLifetimeFeature = context.Features.Get<IConnectionLifetimeFeature>(); var connectionLifetimeFeature = context.Features.Get<IConnectionLifetimeFeature>();
connectionLifetimeFeature.ConnectionClosed.Register(() => connectionClosedTcs.SetResult()); connectionLifetimeFeature.ConnectionClosed.Register(() => connectionClosedTcs.SetResult());
@ -610,7 +624,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests
// isn't guaranteed but not unexpected. // isn't guaranteed but not unexpected.
} }
} }
await server.StopAsync();
} }
} }
@ -627,7 +640,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests
var registrationTcs = new TaskCompletionSource<int>(TaskCreationOptions.RunContinuationsAsynchronously); var registrationTcs = new TaskCompletionSource<int>(TaskCreationOptions.RunContinuationsAsynchronously);
var requestId = 0; var requestId = 0;
using (var server = new TestServer(async httpContext => await using (var server = new TestServer(async httpContext =>
{ {
requestId++; requestId++;
@ -692,7 +705,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests
""); "");
await connection.WaitForConnectionClose(); await connection.WaitForConnectionClose();
} }
await server.StopAsync();
} }
await Assert.ThrowsAsync<TaskCanceledException>(async () => await readTcs.Task); await Assert.ThrowsAsync<TaskCanceledException>(async () => await readTcs.Task);
@ -752,7 +764,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests
var scratchBuffer = new byte[maxRequestBufferSize * 8]; var scratchBuffer = new byte[maxRequestBufferSize * 8];
using (var server = new TestServer(async context => await using (var server = new TestServer(async context =>
{ {
await clientClosedConnection.Task; await clientClosedConnection.Task;
@ -781,7 +793,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests
clientClosedConnection.SetResult(); clientClosedConnection.SetResult();
await appFuncCompleted.Task.DefaultTimeout(); await appFuncCompleted.Task.DefaultTimeout();
await server.StopAsync();
} }
mockKestrelTrace.Verify(t => t.ConnectionStop(It.IsAny<string>()), Times.Once()); mockKestrelTrace.Verify(t => t.ConnectionStop(It.IsAny<string>()), Times.Once());
@ -799,7 +810,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests
var scratchBuffer = new byte[4096]; var scratchBuffer = new byte[4096];
using (var server = new TestServer(async context => await using (var server = new TestServer(async context =>
{ {
appStartedTcs.SetResult(); appStartedTcs.SetResult();
@ -834,7 +845,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests
} }
await Assert.ThrowsAnyAsync<IOException>(() => readTcs.Task).DefaultTimeout(); await Assert.ThrowsAnyAsync<IOException>(() => readTcs.Task).DefaultTimeout();
await server.StopAsync();
} }
mockKestrelTrace.Verify(t => t.ConnectionStop(It.IsAny<string>()), Times.Once()); mockKestrelTrace.Verify(t => t.ConnectionStop(It.IsAny<string>()), Times.Once());
@ -842,24 +852,28 @@ namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests
private async Task TestRemoteIPAddress(string registerAddress, string requestAddress, string expectAddress) private async Task TestRemoteIPAddress(string registerAddress, string requestAddress, string expectAddress)
{ {
var builder = TransportSelector.GetWebHostBuilder() var builder = TransportSelector.GetHostBuilder()
.UseKestrel() .ConfigureWebHost(webHostBuilder =>
.UseUrls($"http://{registerAddress}:0")
.ConfigureServices(AddTestLogging)
.Configure(app =>
{ {
app.Run(async context => webHostBuilder
{ .UseKestrel()
var connection = context.Connection; .UseUrls($"http://{registerAddress}:0")
await context.Response.WriteAsync(JsonConvert.SerializeObject(new .Configure(app =>
{ {
RemoteIPAddress = connection.RemoteIpAddress?.ToString(), app.Run(async context =>
RemotePort = connection.RemotePort, {
LocalIPAddress = connection.LocalIpAddress?.ToString(), var connection = context.Connection;
LocalPort = connection.LocalPort await context.Response.WriteAsync(JsonConvert.SerializeObject(new
})); {
}); RemoteIPAddress = connection.RemoteIpAddress?.ToString(),
}); RemotePort = connection.RemotePort,
LocalIPAddress = connection.LocalIpAddress?.ToString(),
LocalPort = connection.LocalPort
}));
});
});
})
.ConfigureServices(AddTestLogging);
using (var host = builder.Build()) using (var host = builder.Build())
using (var client = new HttpClient()) using (var client = new HttpClient())

View File

@ -23,6 +23,7 @@ using Microsoft.AspNetCore.Server.Kestrel.Https;
using Microsoft.AspNetCore.Server.Kestrel.Https.Internal; using Microsoft.AspNetCore.Server.Kestrel.Https.Internal;
using Microsoft.AspNetCore.Server.Kestrel.Tests; using Microsoft.AspNetCore.Server.Kestrel.Tests;
using Microsoft.AspNetCore.Testing; using Microsoft.AspNetCore.Testing;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Testing; using Microsoft.Extensions.Logging.Testing;
using Microsoft.Extensions.Primitives; using Microsoft.Extensions.Primitives;
@ -42,28 +43,32 @@ namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests
[Fact] [Fact]
public async Task LargeDownload() public async Task LargeDownload()
{ {
var hostBuilder = TransportSelector.GetWebHostBuilder() var hostBuilder = TransportSelector.GetHostBuilder()
.UseKestrel() .ConfigureWebHost(webHostBuilder =>
.UseUrls("http://127.0.0.1:0/")
.ConfigureServices(AddTestLogging)
.Configure(app =>
{ {
app.Run(async context => webHostBuilder
{ .UseKestrel()
var bytes = new byte[1024]; .UseUrls("http://127.0.0.1:0/")
for (int i = 0; i < bytes.Length; i++) .Configure(app =>
{ {
bytes[i] = (byte)i; app.Run(async context =>
} {
var bytes = new byte[1024];
for (int i = 0; i < bytes.Length; i++)
{
bytes[i] = (byte)i;
}
context.Response.ContentLength = bytes.Length * 1024; context.Response.ContentLength = bytes.Length * 1024;
for (int i = 0; i < 1024; i++) for (int i = 0; i < 1024; i++)
{ {
await context.Response.BodyWriter.WriteAsync(new Memory<byte>(bytes, 0, bytes.Length)); await context.Response.BodyWriter.WriteAsync(new Memory<byte>(bytes, 0, bytes.Length));
} }
}); });
}); });
})
.ConfigureServices(AddTestLogging);
using (var host = hostBuilder.Build()) using (var host = hostBuilder.Build())
{ {
@ -96,19 +101,23 @@ namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests
[Theory, MemberData(nameof(NullHeaderData))] [Theory, MemberData(nameof(NullHeaderData))]
public async Task IgnoreNullHeaderValues(string headerName, StringValues headerValue, string expectedValue) public async Task IgnoreNullHeaderValues(string headerName, StringValues headerValue, string expectedValue)
{ {
var hostBuilder = TransportSelector.GetWebHostBuilder() var hostBuilder = TransportSelector.GetHostBuilder()
.UseKestrel() .ConfigureWebHost(webHostBuilder =>
.UseUrls("http://127.0.0.1:0/")
.ConfigureServices(AddTestLogging)
.Configure(app =>
{ {
app.Run(async context => webHostBuilder
{ .UseKestrel()
context.Response.Headers.Add(headerName, headerValue); .UseUrls("http://127.0.0.1:0/")
.Configure(app =>
{
app.Run(async context =>
{
context.Response.Headers.Add(headerName, headerValue);
await context.Response.WriteAsync(""); await context.Response.WriteAsync("");
}); });
}); });
})
.ConfigureServices(AddTestLogging);
using (var host = hostBuilder.Build()) using (var host = hostBuilder.Build())
{ {
@ -143,7 +152,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests
var requestStarted = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); var requestStarted = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
var appCompleted = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); var appCompleted = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
using (var server = new TestServer(async httpContext => await using (var server = new TestServer(async httpContext =>
{ {
try try
{ {
@ -175,7 +184,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests
connectionClosed.SetResult(); connectionClosed.SetResult();
await appCompleted.Task.DefaultTimeout(); await appCompleted.Task.DefaultTimeout();
await server.StopAsync();
} }
} }
@ -193,7 +201,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests
var requestAbortedWh = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); var requestAbortedWh = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
var requestStartWh = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); var requestStartWh = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
using (var server = new TestServer(async httpContext => await using (var server = new TestServer(async httpContext =>
{ {
requestStartWh.SetResult(); requestStartWh.SetResult();
@ -239,7 +247,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests
// RequestAborted tripped // RequestAborted tripped
await requestAbortedWh.Task.DefaultTimeout(); await requestAbortedWh.Task.DefaultTimeout();
await server.StopAsync();
} }
} }
@ -286,7 +293,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests
var scratchBuffer = new byte[maxRequestBufferSize * 8]; var scratchBuffer = new byte[maxRequestBufferSize * 8];
using (var server = new TestServer(async context => await using (var server = new TestServer(async context =>
{ {
context.RequestAborted.Register(() => requestAborted.SetResult()); context.RequestAborted.Register(() => requestAborted.SetResult());
@ -331,7 +338,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests
clientClosedConnection.SetResult(); clientClosedConnection.SetResult();
await Assert.ThrowsAnyAsync<OperationCanceledException>(() => writeTcs.Task).DefaultTimeout(); await Assert.ThrowsAnyAsync<OperationCanceledException>(() => writeTcs.Task).DefaultTimeout();
await server.StopAsync();
} }
mockKestrelTrace.Verify(t => t.ConnectionStop(It.IsAny<string>()), Times.Once()); mockKestrelTrace.Verify(t => t.ConnectionStop(It.IsAny<string>()), Times.Once());
@ -354,7 +360,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests
var scratchBuffer = new byte[responseBodySegmentSize]; var scratchBuffer = new byte[responseBodySegmentSize];
using (var server = new TestServer(async context => await using (var server = new TestServer(async context =>
{ {
context.RequestAborted.Register(() => requestAborted.SetResult()); context.RequestAborted.Register(() => requestAborted.SetResult());
@ -398,7 +404,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests
// On macOS, the default 5 shutdown timeout is insufficient for the write loop to complete, so give it extra time. // On macOS, the default 5 shutdown timeout is insufficient for the write loop to complete, so give it extra time.
await appCompletedTcs.Task.DefaultTimeout(); await appCompletedTcs.Task.DefaultTimeout();
await server.StopAsync();
} }
var coreLogs = TestSink.Writes.Where(w => w.LoggerName == "Microsoft.AspNetCore.Server.Kestrel"); var coreLogs = TestSink.Writes.Where(w => w.LoggerName == "Microsoft.AspNetCore.Server.Kestrel");
@ -421,7 +426,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests
// There's not guarantee that the app even gets invoked in this test. The connection reset can be observed // There's not guarantee that the app even gets invoked in this test. The connection reset can be observed
// as early as accept. // as early as accept.
var testServiceContext = new TestServiceContext(LoggerFactory); var testServiceContext = new TestServiceContext(LoggerFactory);
using (var server = new TestServer(context => Task.CompletedTask, testServiceContext, listenOptions)) await using (var server = new TestServer(context => Task.CompletedTask, testServiceContext, listenOptions))
{ {
for (var i = 0; i < numConnections; i++) for (var i = 0; i < numConnections; i++)
{ {
@ -436,7 +441,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests
connection.Reset(); connection.Reset();
} }
} }
await server.StopAsync();
} }
var transportLogs = TestSink.Writes.Where(w => w.LoggerName == "Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv" || var transportLogs = TestSink.Writes.Where(w => w.LoggerName == "Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv" ||
@ -521,7 +525,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests
} }
} }
using (var server = new TestServer(App, testContext)) await using (var server = new TestServer(App, testContext))
{ {
using (var connection = server.CreateConnection()) using (var connection = server.CreateConnection())
{ {
@ -546,7 +550,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests
sw.Stop(); sw.Stop();
logger.LogInformation("Connection was aborted after {totalMilliseconds}ms.", sw.ElapsedMilliseconds); logger.LogInformation("Connection was aborted after {totalMilliseconds}ms.", sw.ElapsedMilliseconds);
} }
await server.StopAsync();
} }
} }
@ -590,7 +593,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests
listenOptions.UseHttps(new HttpsConnectionAdapterOptions { ServerCertificate = certificate }); listenOptions.UseHttps(new HttpsConnectionAdapterOptions { ServerCertificate = certificate });
} }
using (var server = new TestServer(async context => await using (var server = new TestServer(async context =>
{ {
context.RequestAborted.Register(() => context.RequestAborted.Register(() =>
{ {
@ -634,7 +637,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests
await AssertStreamAborted(connection.Stream, chunkSize * chunks); await AssertStreamAborted(connection.Stream, chunkSize * chunks);
} }
} }
await server.StopAsync();
} }
} }
@ -699,7 +701,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests
copyToAsyncCts.SetException(new Exception("This shouldn't be reached.")); copyToAsyncCts.SetException(new Exception("This shouldn't be reached."));
} }
using (var server = new TestServer(App, testContext, listenOptions)) await using (var server = new TestServer(App, testContext, listenOptions))
{ {
using (var connection = server.CreateConnection()) using (var connection = server.CreateConnection())
{ {
@ -728,7 +730,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests
await Assert.ThrowsAnyAsync<OperationCanceledException>(() => copyToAsyncCts.Task).DefaultTimeout(); await Assert.ThrowsAnyAsync<OperationCanceledException>(() => copyToAsyncCts.Task).DefaultTimeout();
await AssertStreamAborted(connection.Stream, responseSize); await AssertStreamAborted(connection.Stream, responseSize);
} }
await server.StopAsync();
} }
} }
@ -776,7 +777,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests
appFuncCompleted.SetResult(); appFuncCompleted.SetResult();
} }
using (var server = new TestServer(App, testContext, listenOptions)) await using (var server = new TestServer(App, testContext, listenOptions))
{ {
using (var connection = server.CreateConnection()) using (var connection = server.CreateConnection())
{ {
@ -802,7 +803,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests
connection.ShutdownSend(); connection.ShutdownSend();
await connection.WaitForConnectionClose(); await connection.WaitForConnectionClose();
} }
await server.StopAsync();
} }
mockKestrelTrace.Verify(t => t.ResponseMinimumDataRateNotSatisfied(It.IsAny<string>(), It.IsAny<string>()), Times.Never()); mockKestrelTrace.Verify(t => t.ResponseMinimumDataRateNotSatisfied(It.IsAny<string>(), It.IsAny<string>()), Times.Never());
@ -854,7 +854,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests
await context.Response.BodyWriter.FlushAsync(); await context.Response.BodyWriter.FlushAsync();
} }
using (var server = new TestServer(App, testContext, listenOptions)) await using (var server = new TestServer(App, testContext, listenOptions))
{ {
using (var connection = server.CreateConnection()) using (var connection = server.CreateConnection())
{ {
@ -889,8 +889,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests
connection.ShutdownSend(); connection.ShutdownSend();
await connection.WaitForConnectionClose(); await connection.WaitForConnectionClose();
} }
await server.StopAsync();
} }
mockKestrelTrace.Verify(t => t.ResponseMinimumDataRateNotSatisfied(It.IsAny<string>(), It.IsAny<string>()), Times.Never()); mockKestrelTrace.Verify(t => t.ResponseMinimumDataRateNotSatisfied(It.IsAny<string>(), It.IsAny<string>()), Times.Never());
@ -942,7 +940,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests
appFuncCompleted.SetResult(); appFuncCompleted.SetResult();
} }
using (var server = new TestServer(App, testContext, listenOptions)) await using (var server = new TestServer(App, testContext, listenOptions))
{ {
using (var connection = server.CreateConnection()) using (var connection = server.CreateConnection())
{ {
@ -967,7 +965,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests
await AssertStreamCompletedAtTargetRate(connection.Stream, expectedBytes: 33_553_556, targetBytesPerSecond); await AssertStreamCompletedAtTargetRate(connection.Stream, expectedBytes: 33_553_556, targetBytesPerSecond);
await appFuncCompleted.Task.DefaultTimeout(); await appFuncCompleted.Task.DefaultTimeout();
} }
await server.StopAsync();
} }
mockKestrelTrace.Verify(t => t.ResponseMinimumDataRateNotSatisfied(It.IsAny<string>(), It.IsAny<string>()), Times.Never()); mockKestrelTrace.Verify(t => t.ResponseMinimumDataRateNotSatisfied(It.IsAny<string>(), It.IsAny<string>()), Times.Never());

View File

@ -15,6 +15,7 @@ using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Features; using Microsoft.AspNetCore.Http.Features;
using Microsoft.AspNetCore.Testing; using Microsoft.AspNetCore.Testing;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Testing; using Microsoft.Extensions.Logging.Testing;
using Xunit; using Xunit;
@ -72,16 +73,20 @@ namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests
} }
} }
var hostBuilder = TransportSelector.GetWebHostBuilder() var hostBuilder = TransportSelector.GetHostBuilder()
.UseKestrel(o => .ConfigureWebHost(webHostBuilder =>
{ {
o.ListenUnixSocket(path, builder => webHostBuilder
{ .UseKestrel(o =>
builder.Run(EchoServer); {
}); o.ListenUnixSocket(path, builder =>
{
builder.Run(EchoServer);
});
})
.Configure(c => { });
}) })
.ConfigureServices(AddTestLogging) .ConfigureServices(AddTestLogging);
.Configure(c => { });
using (var host = hostBuilder.Build()) using (var host = hostBuilder.Build())
{ {
@ -100,7 +105,10 @@ namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests
{ {
var bytesReceived = await socket.ReceiveAsync(buffer.AsMemory(read, buffer.Length - read), SocketFlags.None).DefaultTimeout(); var bytesReceived = await socket.ReceiveAsync(buffer.AsMemory(read, buffer.Length - read), SocketFlags.None).DefaultTimeout();
read += bytesReceived; read += bytesReceived;
if (bytesReceived <= 0) break; if (bytesReceived <= 0)
{
break;
}
} }
Assert.Equal(data, buffer); Assert.Equal(data, buffer);
@ -134,17 +142,21 @@ namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests
try try
{ {
var hostBuilder = TransportSelector.GetWebHostBuilder() var hostBuilder = TransportSelector.GetHostBuilder()
.UseUrls(url) .ConfigureWebHost(webHostBuilder =>
.UseKestrel()
.ConfigureServices(AddTestLogging)
.Configure(app =>
{ {
app.Run(async context => webHostBuilder
{ .UseUrls(url)
await context.Response.WriteAsync("Hello World"); .UseKestrel()
}); .Configure(app =>
}); {
app.Run(async context =>
{
await context.Response.WriteAsync("Hello World");
});
});
})
.ConfigureServices(AddTestLogging);
using (var host = hostBuilder.Build()) using (var host = hostBuilder.Build())
{ {
@ -165,7 +177,10 @@ namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests
{ {
var bytesReceived = await socket.ReceiveAsync(readBuffer.AsMemory(read), SocketFlags.None).DefaultTimeout(); var bytesReceived = await socket.ReceiveAsync(readBuffer.AsMemory(read), SocketFlags.None).DefaultTimeout();
read += bytesReceived; read += bytesReceived;
if (bytesReceived <= 0) break; if (bytesReceived <= 0)
{
break;
}
} }
var httpResponse = Encoding.ASCII.GetString(readBuffer, 0, read); var httpResponse = Encoding.ASCII.GetString(readBuffer, 0, read);

View File

@ -169,7 +169,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests
} }
} }
Assert.All(TestSink.Writes, w => Assert.InRange(w.LogLevel, LogLevel.Trace, LogLevel.Debug)); Assert.All(TestSink.Writes.Where(w => w.LoggerName != "Microsoft.Hosting.Lifetime"), w => Assert.InRange(w.LogLevel, LogLevel.Trace, LogLevel.Debug));
Assert.Contains(TestSink.Writes, w => w.EventId.Id == 17); Assert.Contains(TestSink.Writes, w => w.EventId.Id == 17);
} }

View File

@ -9,12 +9,10 @@ using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Server.Kestrel.Core;
using Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http; using Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http;
using Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests.TestTransport; using Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests.TestTransport;
using Microsoft.AspNetCore.Testing; using Microsoft.AspNetCore.Testing;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Testing;
using Xunit; using Xunit;
using BadHttpRequestException = Microsoft.AspNetCore.Server.Kestrel.Core.BadHttpRequestException; using BadHttpRequestException = Microsoft.AspNetCore.Server.Kestrel.Core.BadHttpRequestException;
@ -686,7 +684,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests
"", "",
""); "");
} }
await server.StopAsync();
} }
} }
@ -993,7 +990,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests
"", "",
"Hello World"); "Hello World");
} }
await server.StopAsync();
} }
} }
@ -1065,7 +1061,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests
{ {
var testContext = new TestServiceContext(LoggerFactory); var testContext = new TestServiceContext(LoggerFactory);
using (var server = new TestServer(async httpContext => await using (var server = new TestServer(async httpContext =>
{ {
var response = httpContext.Response; var response = httpContext.Response;
var request = httpContext.Request; var request = httpContext.Request;

View File

@ -4,7 +4,6 @@
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests.TestTransport; using Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests.TestTransport;
using Microsoft.AspNetCore.Testing; using Microsoft.AspNetCore.Testing;
using Microsoft.Extensions.Logging.Testing;
using Xunit; using Xunit;
namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests

View File

@ -10,7 +10,6 @@ using Microsoft.AspNetCore.Http.Features;
using Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Infrastructure; using Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Infrastructure;
using Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests.TestTransport; using Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests.TestTransport;
using Microsoft.AspNetCore.Testing; using Microsoft.AspNetCore.Testing;
using Microsoft.Extensions.Logging.Testing;
using Xunit; using Xunit;
namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests

View File

@ -33,7 +33,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests.Http2
[SkipOnHelix("Ubuntu 20.04 disables TLS1.1 by default which SslStream requires in this scenario", Queues = "Ubuntu.2004.Amd64.Open")] [SkipOnHelix("Ubuntu 20.04 disables TLS1.1 by default which SslStream requires in this scenario", Queues = "Ubuntu.2004.Amd64.Open")]
public async Task TlsHandshakeRejectsTlsLessThan12() public async Task TlsHandshakeRejectsTlsLessThan12()
{ {
using (var server = new TestServer(context => await using (var server = new TestServer(context =>
{ {
var tlsFeature = context.Features.Get<ITlsApplicationProtocolFeature>(); var tlsFeature = context.Features.Get<ITlsApplicationProtocolFeature>();
Assert.NotNull(tlsFeature); Assert.NotNull(tlsFeature);
@ -66,7 +66,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests.Http2
await WaitForConnectionErrorAsync(reader, ignoreNonGoAwayFrames: false, expectedLastStreamId: 0, expectedErrorCode: Http2ErrorCode.INADEQUATE_SECURITY); await WaitForConnectionErrorAsync(reader, ignoreNonGoAwayFrames: false, expectedLastStreamId: 0, expectedErrorCode: Http2ErrorCode.INADEQUATE_SECURITY);
reader.Complete(); reader.Complete();
} }
await server.StopAsync();
} }
} }

View File

@ -11,7 +11,6 @@ using Microsoft.AspNetCore.Http.Features;
using Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Infrastructure; using Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Infrastructure;
using Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests.TestTransport; using Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests.TestTransport;
using Microsoft.AspNetCore.Testing; using Microsoft.AspNetCore.Testing;
using Microsoft.Extensions.Logging.Testing;
using Xunit; using Xunit;
namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests

View File

@ -10,7 +10,6 @@ using Microsoft.AspNetCore.Http.Features;
using Microsoft.AspNetCore.Server.Kestrel.Core; using Microsoft.AspNetCore.Server.Kestrel.Core;
using Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests.TestTransport; using Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests.TestTransport;
using Microsoft.AspNetCore.Testing; using Microsoft.AspNetCore.Testing;
using Microsoft.Extensions.Logging.Testing;
using Xunit; using Xunit;
using BadHttpRequestException = Microsoft.AspNetCore.Server.Kestrel.Core.BadHttpRequestException; using BadHttpRequestException = Microsoft.AspNetCore.Server.Kestrel.Core.BadHttpRequestException;

View File

@ -11,7 +11,6 @@ using Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Infrastructure;
using Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests.TestTransport; using Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests.TestTransport;
using Microsoft.AspNetCore.Testing; using Microsoft.AspNetCore.Testing;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Testing;
using Xunit; using Xunit;
namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests

View File

@ -7,7 +7,6 @@ using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Server.Kestrel.Core; using Microsoft.AspNetCore.Server.Kestrel.Core;
using Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests.TestTransport; using Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests.TestTransport;
using Microsoft.AspNetCore.Testing; using Microsoft.AspNetCore.Testing;
using Microsoft.Extensions.Logging.Testing;
using Xunit; using Xunit;
namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests

View File

@ -8,7 +8,6 @@ using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Infrastructure; using Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Infrastructure;
using Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests.TestTransport; using Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests.TestTransport;
using Microsoft.AspNetCore.Testing; using Microsoft.AspNetCore.Testing;
using Microsoft.Extensions.Logging.Testing;
using Xunit; using Xunit;
namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests

View File

@ -8,7 +8,6 @@ using Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http;
using Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Infrastructure; using Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Infrastructure;
using Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests.TestTransport; using Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests.TestTransport;
using Microsoft.AspNetCore.Testing; using Microsoft.AspNetCore.Testing;
using Microsoft.Extensions.Logging.Testing;
using Xunit; using Xunit;
namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests

View File

@ -18,8 +18,6 @@ using Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Infrastructure;
using Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests.TestTransport; using Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests.TestTransport;
using Microsoft.AspNetCore.Testing; using Microsoft.AspNetCore.Testing;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Testing;
using Serilog;
using Xunit; using Xunit;
namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests

View File

@ -3410,8 +3410,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests
"", "",
"Hello World!"); "Hello World!");
} }
await server.StopAsync();
} }
} }

View File

@ -16,6 +16,7 @@ using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Server.Kestrel.Core; using Microsoft.AspNetCore.Server.Kestrel.Core;
using Microsoft.AspNetCore.Testing; using Microsoft.AspNetCore.Testing;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Xunit; using Xunit;
namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests.TestTransport namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests.TestTransport
@ -23,12 +24,12 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests.TestTrans
/// <summary> /// <summary>
/// In-memory TestServer /// In-memory TestServer
/// </summary /// </summary
internal class TestServer : IAsyncDisposable, IDisposable, IStartup internal class TestServer : IAsyncDisposable, IStartup
{ {
private readonly MemoryPool<byte> _memoryPool; private readonly MemoryPool<byte> _memoryPool;
private readonly RequestDelegate _app; private readonly RequestDelegate _app;
private readonly InMemoryTransportFactory _transportFactory; private readonly InMemoryTransportFactory _transportFactory;
private readonly IWebHost _host; private readonly IHost _host;
public TestServer(RequestDelegate app) public TestServer(RequestDelegate app)
: this(app, new TestServiceContext()) : this(app, new TestServiceContext())
@ -69,8 +70,13 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests.TestTrans
_transportFactory = new InMemoryTransportFactory(); _transportFactory = new InMemoryTransportFactory();
HttpClientSlim = new InMemoryHttpClientSlim(this); HttpClientSlim = new InMemoryHttpClientSlim(this);
var hostBuilder = new WebHostBuilder() var hostBuilder = new HostBuilder()
.UseSetting(WebHostDefaults.ShutdownTimeoutKey, TestConstants.DefaultTimeout.TotalSeconds.ToString()) .ConfigureWebHost(webHostBuilder =>
{
webHostBuilder
.UseSetting(WebHostDefaults.ShutdownTimeoutKey, TestConstants.DefaultTimeout.TotalSeconds.ToString())
.Configure(app => { app.Run(_app); });
})
.ConfigureServices(services => .ConfigureServices(services =>
{ {
configureServices(services); configureServices(services);
@ -108,11 +114,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests.TestTrans
return _host.StopAsync(cancellationToken); return _host.StopAsync(cancellationToken);
} }
public void Dispose()
{
_host.Dispose();
_memoryPool.Dispose();
}
void IStartup.Configure(IApplicationBuilder app) void IStartup.Configure(IApplicationBuilder app)
{ {
@ -126,8 +127,9 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests.TestTrans
public async ValueTask DisposeAsync() public async ValueTask DisposeAsync()
{ {
// The concrete WebHost implements IAsyncDisposable await _host.StopAsync().ConfigureAwait(false);
await ((IAsyncDisposable)_host).ConfigureAwait(false).DisposeAsync(); // The concrete Host implements IAsyncDisposable
await ((IAsyncDisposable)_host).DisposeAsync().ConfigureAwait(false);
_memoryPool.Dispose(); _memoryPool.Dispose();
} }
} }

View File

@ -11,6 +11,7 @@ using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Server.Kestrel.Core; using Microsoft.AspNetCore.Server.Kestrel.Core;
using Microsoft.AspNetCore.Testing; using Microsoft.AspNetCore.Testing;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Testing; using Microsoft.Extensions.Logging.Testing;
using OpenQA.Selenium.Chrome; using OpenQA.Selenium.Chrome;
@ -46,7 +47,7 @@ namespace Interop.FunctionalTests
StartupLogPath = Path.Combine(ResolvedLogOutputDirectory, $"{ResolvedTestMethodName}.su.json"); StartupLogPath = Path.Combine(ResolvedLogOutputDirectory, $"{ResolvedTestMethodName}.su.json");
ShutdownLogPath = Path.Combine(ResolvedLogOutputDirectory, $"{ResolvedTestMethodName}.sd.json"); ShutdownLogPath = Path.Combine(ResolvedLogOutputDirectory, $"{ResolvedTestMethodName}.sd.json");
ChromeArgs = new [] { ChromeArgs = new[] {
$"--headless", $"--headless",
$"--no-sandbox", $"--no-sandbox",
$"--disable-gpu", $"--disable-gpu",
@ -62,7 +63,7 @@ namespace Interop.FunctionalTests
}; };
} }
[ConditionalTheory(Skip="Disabling while debugging. https://github.com/dotnet/aspnetcore-internal/issues/1363")] [ConditionalTheory(Skip = "Disabling while debugging. https://github.com/dotnet/aspnetcore-internal/issues/1363")]
[OSSkipCondition(OperatingSystems.MacOSX, SkipReason = "Missing SslStream ALPN support: https://github.com/dotnet/corefx/issues/30492")] [OSSkipCondition(OperatingSystems.MacOSX, SkipReason = "Missing SslStream ALPN support: https://github.com/dotnet/corefx/issues/30492")]
[MinimumOSVersion(OperatingSystems.Windows, WindowsVersions.Win81, SkipReason = "Missing Windows ALPN support: https://en.wikipedia.org/wiki/Application-Layer_Protocol_Negotiation#Support")] [MinimumOSVersion(OperatingSystems.Windows, WindowsVersions.Win81, SkipReason = "Missing Windows ALPN support: https://en.wikipedia.org/wiki/Application-Layer_Protocol_Negotiation#Support")]
[InlineData("", "Interop HTTP/2 GET")] [InlineData("", "Interop HTTP/2 GET")]
@ -71,27 +72,31 @@ namespace Interop.FunctionalTests
{ {
InitializeArgs(); InitializeArgs();
var hostBuilder = new WebHostBuilder() var hostBuilder = new HostBuilder()
.UseKestrel(options => .ConfigureWebHost(webHostBuilder =>
{ {
options.Listen(IPAddress.Loopback, 0, listenOptions => webHostBuilder
{ .UseKestrel(options =>
listenOptions.Protocols = HttpProtocols.Http2; {
listenOptions.UseHttps(TestResources.GetTestCertificate()); options.Listen(IPAddress.Loopback, 0, listenOptions =>
}); {
listenOptions.Protocols = HttpProtocols.Http2;
listenOptions.UseHttps(TestResources.GetTestCertificate());
});
})
.Configure(app => app.Run(async context =>
{
if (HttpMethods.IsPost(context.Request.Query["TestMethod"]))
{
await context.Response.WriteAsync(_postHtml);
}
else
{
await context.Response.WriteAsync($"Interop {context.Request.Protocol} {context.Request.Method}");
}
}));
}) })
.ConfigureServices(AddTestLogging) .ConfigureServices(AddTestLogging);
.Configure(app => app.Run(async context =>
{
if (HttpMethods.IsPost(context.Request.Query["TestMethod"]))
{
await context.Response.WriteAsync(_postHtml);
}
else
{
await context.Response.WriteAsync($"Interop {context.Request.Protocol} {context.Request.Method}");
}
}));
using (var host = hostBuilder.Build()) using (var host = hostBuilder.Build())
{ {

View File

@ -11,6 +11,7 @@ using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Server.Kestrel.Core; using Microsoft.AspNetCore.Server.Kestrel.Core;
using Microsoft.AspNetCore.Testing; using Microsoft.AspNetCore.Testing;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging.Testing; using Microsoft.Extensions.Logging.Testing;
using Xunit; using Xunit;
using Xunit.Abstractions; using Xunit.Abstractions;
@ -23,20 +24,24 @@ namespace Interop.FunctionalTests
[MemberData(nameof(H2SpecTestCases))] [MemberData(nameof(H2SpecTestCases))]
public async Task RunIndividualTestCase(H2SpecTestCase testCase) public async Task RunIndividualTestCase(H2SpecTestCase testCase)
{ {
var hostBuilder = new WebHostBuilder() var hostBuilder = new HostBuilder()
.UseKestrel(options => .ConfigureWebHost(webHostBuilder =>
{ {
options.Listen(IPAddress.Loopback, 0, listenOptions => webHostBuilder
{ .UseKestrel(options =>
listenOptions.Protocols = HttpProtocols.Http2;
if (testCase.Https)
{ {
listenOptions.UseHttps(TestResources.GetTestCertificate()); options.Listen(IPAddress.Loopback, 0, listenOptions =>
} {
}); listenOptions.Protocols = HttpProtocols.Http2;
if (testCase.Https)
{
listenOptions.UseHttps(TestResources.GetTestCertificate());
}
});
})
.Configure(ConfigureHelloWorld);
}) })
.ConfigureServices(AddTestLogging) .ConfigureServices(AddTestLogging);
.Configure(ConfigureHelloWorld);
using (var host = hostBuilder.Build()) using (var host = hostBuilder.Build())
{ {

View File

@ -4,20 +4,26 @@
using System; using System;
using System.Buffers; using System.Buffers;
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;
namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests
{ {
public static class TransportSelector public static class TransportSelector
{ {
public static IWebHostBuilder GetWebHostBuilder(Func<MemoryPool<byte>> memoryPoolFactory = null, public static IHostBuilder GetHostBuilder(Func<MemoryPool<byte>> memoryPoolFactory = null,
long? maxReadBufferSize = null) long? maxReadBufferSize = null)
{ {
#pragma warning disable CS0618 #pragma warning disable CS0618
return new WebHostBuilder().UseLibuv(options => return new HostBuilder()
{ .ConfigureWebHost(webHostBuilder =>
options.MemoryPoolFactory = memoryPoolFactory ?? options.MemoryPoolFactory; {
options.MaxReadBufferSize = maxReadBufferSize; webHostBuilder
}); .UseLibuv(options =>
{
options.MemoryPoolFactory = memoryPoolFactory ?? options.MemoryPoolFactory;
options.MaxReadBufferSize = maxReadBufferSize;
});
});
#pragma warning restore CS0618 #pragma warning restore CS0618
} }
} }

View File

@ -4,19 +4,25 @@
using System; using System;
using System.Buffers; using System.Buffers;
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;
namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests
{ {
public static class TransportSelector public static class TransportSelector
{ {
public static IWebHostBuilder GetWebHostBuilder(Func<MemoryPool<byte>> memoryPoolFactory = null, public static IHostBuilder GetHostBuilder(Func<MemoryPool<byte>> memoryPoolFactory = null,
long? maxReadBufferSize = null) long? maxReadBufferSize = null)
{ {
return new WebHostBuilder().UseSockets(options => return new HostBuilder()
{ .ConfigureWebHost(webHostBuilder =>
options.MemoryPoolFactory = memoryPoolFactory ?? options.MemoryPoolFactory; {
options.MaxReadBufferSize = maxReadBufferSize; webHostBuilder
}); .UseSockets(options =>
{
options.MemoryPoolFactory = memoryPoolFactory ?? options.MemoryPoolFactory;
options.MaxReadBufferSize = maxReadBufferSize;
});
});
} }
} }
} }