public async Task RegisterAddresses_Success(string addressInput, string[] testUrls) { var config = new ConfigurationBuilder() .AddInMemoryCollection(new Dictionary<string, string> { { "server.urls", addressInput } }) .Build(); var applicationBuilder = new WebApplicationBuilder() .UseConfiguration(config) .UseServerFactory("Microsoft.AspNet.Server.Kestrel") .Configure(ConfigureEchoAddress); using (var app = applicationBuilder.Build().Start()) { using (var client = new HttpClient()) { foreach (var testUrl in testUrls) { var responseText = await client.GetStringAsync(testUrl); Assert.Equal(testUrl, responseText); } } } }
public async Task LargeDownload() { var config = new ConfigurationBuilder() .AddInMemoryCollection(new Dictionary<string, string> { { "server.urls", "http://localhost:8792/" } }) .Build(); var applicationBuilder = new WebApplicationBuilder() .UseConfiguration(config) .UseServerFactory("Microsoft.AspNet.Server.Kestrel") .Configure(app => { 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; for (int i = 0; i < 1024; i++) { await context.Response.Body.WriteAsync(bytes, 0, bytes.Length); } }); }); using (var app = applicationBuilder.Build().Start()) { using (var client = new HttpClient()) { var response = await client.GetAsync("http://localhost:8792/"); response.EnsureSuccessStatusCode(); var responseBody = await response.Content.ReadAsStreamAsync(); // Read the full response body var total = 0; var bytes = new byte[1024]; var count = await responseBody.ReadAsync(bytes, 0, bytes.Length); while (count > 0) { for (int i = 0; i < count; i++) { Assert.Equal(total % 256, bytes[i]); total++; } count = await responseBody.ReadAsync(bytes, 0, bytes.Length); } } } }
public async Task LargeUpload() { var config = new ConfigurationBuilder() .AddInMemoryCollection(new Dictionary<string, string> { { "server.urls", "http://localhost:8791/" } }) .Build(); var applicationBuilder = new WebApplicationBuilder() .UseConfiguration(config) .UseServerFactory("Microsoft.AspNet.Server.Kestrel") .Configure(app => { app.Run(async context => { // Read the full request body var total = 0; var bytes = new byte[1024]; var count = await context.Request.Body.ReadAsync(bytes, 0, bytes.Length); while (count > 0) { for (int i = 0; i < count; i++) { Assert.Equal(total % 256, bytes[i]); total++; } count = await context.Request.Body.ReadAsync(bytes, 0, bytes.Length); } await context.Response.WriteAsync(total.ToString(CultureInfo.InvariantCulture)); }); }); using (var app = applicationBuilder.Build().Start()) { using (var client = new HttpClient()) { var bytes = new byte[1024 * 1024]; for (int i = 0; i < bytes.Length; i++) { bytes[i] = (byte)i; } var response = await client.PostAsync("http://localhost:8791/", new ByteArrayContent(bytes)); response.EnsureSuccessStatusCode(); var sizeString = await response.Content.ReadAsStringAsync(); Assert.Equal(sizeString, bytes.Length.ToString(CultureInfo.InvariantCulture)); } } }
public async Task ZeroToTenThreads(int threadCount) { var config = new ConfigurationBuilder() .AddInMemoryCollection(new Dictionary<string, string> { { "server.urls", "http://localhost:8790/" } }) .Build(); var applicationBuilder = new WebApplicationBuilder() .UseConfiguration(config) .UseServerFactory("Microsoft.AspNet.Server.Kestrel") .Configure(app => { var serverInfo = app.ServerFeatures.Get<IKestrelServerInformation>(); serverInfo.ThreadCount = threadCount; app.Run(context => { return context.Response.WriteAsync("Hello World"); }); }); using (var app = applicationBuilder.Build().Start()) { using (var client = new HttpClient()) { // Send 20 requests just to make sure we don't get any failures var requestTasks = new List<Task<string>>(); for (int i = 0; i < 20; i++) { var requestTask = client.GetStringAsync("http://localhost:8790/"); requestTasks.Add(requestTask); } foreach (var result in await Task.WhenAll(requestTasks)) { Assert.Equal("Hello World", result); } } } }
public async Task IgnoreNullHeaderValues(string headerName, StringValues headerValue, string expectedValue) { var config = new ConfigurationBuilder() .AddInMemoryCollection(new Dictionary<string, string> { { "server.urls", "http://localhost:8793/" } }) .Build(); var hostBuilder = new WebApplicationBuilder() .UseConfiguration(config) .UseServerFactory("Microsoft.AspNet.Server.Kestrel") .Configure(app => { app.Run(async context => { context.Response.Headers.Add(headerName, headerValue); await context.Response.WriteAsync(""); }); }); using (var app = hostBuilder.Build().Start()) { using (var client = new HttpClient()) { var response = await client.GetAsync("http://localhost:8793/"); response.EnsureSuccessStatusCode(); var headers = response.Headers; if (expectedValue == null) { Assert.False(headers.Contains(headerName)); } else { Assert.True(headers.Contains(headerName)); Assert.Equal(headers.GetValues(headerName).Single(), expectedValue); } } } }
private async Task TestPathBase(string registerAddress, string requestAddress, string expectedPathBase, string expectedPath) { var config = new ConfigurationBuilder().AddInMemoryCollection( new Dictionary<string, string> { { "server.urls", registerAddress } }).Build(); var builder = new WebApplicationBuilder() .UseConfiguration(config) .UseServerFactory("Microsoft.AspNet.Server.Kestrel") .Configure(app => { app.Run(async context => { await context.Response.WriteAsync(JsonConvert.SerializeObject(new { PathBase = context.Request.PathBase.Value, Path = context.Request.Path.Value })); }); }); using (var app = builder.Build().Start()) { using (var client = new HttpClient()) { var response = await client.GetAsync(requestAddress); response.EnsureSuccessStatusCode(); var responseText = await response.Content.ReadAsStringAsync(); Assert.NotEmpty(responseText); var pathFacts = JsonConvert.DeserializeObject<JObject>(responseText); Assert.Equal(expectedPathBase, pathFacts["PathBase"].Value<string>()); Assert.Equal(expectedPath, pathFacts["Path"].Value<string>()); } } }
private DbContext TryCreateContextUsingAppCode(Type dbContextType, ModelType startupType) { var builder = new WebApplicationBuilder(); if (startupType != null) { var reflectedStartupType = dbContextType.GetTypeInfo().Assembly.GetType(startupType.FullName); if (reflectedStartupType != null) { builder.UseStartup(reflectedStartupType); } } var appServices = builder.Build().Services; return appServices.GetService(dbContextType) as DbContext; }
public async Task DoesNotHangOnConnectionCloseRequest() { var config = new ConfigurationBuilder().AddInMemoryCollection( new Dictionary<string, string> { { "server.urls", "http://localhost:8791" } }).Build(); var builder = new WebApplicationBuilder() .UseConfiguration(config) .UseServerFactory("Microsoft.AspNet.Server.Kestrel") .Configure(app => { app.Run(async context => { var connection = context.Connection; await context.Response.WriteAsync("hello, world"); }); }); using (var app = builder.Build().Start()) using (var client = new HttpClient()) { client.DefaultRequestHeaders.Connection.Clear(); client.DefaultRequestHeaders.Connection.Add("close"); var response = await client.GetAsync("http://localhost:8791/"); response.EnsureSuccessStatusCode(); } }
private async Task TestRemoteIPAddress(string registerAddress, string requestAddress, string expectAddress, string port) { var config = new ConfigurationBuilder().AddInMemoryCollection( new Dictionary<string, string> { { "server.urls", $"http://{registerAddress}:{port}" } }).Build(); var builder = new WebApplicationBuilder() .UseConfiguration(config) .UseServerFactory("Microsoft.AspNet.Server.Kestrel") .Configure(app => { app.Run(async context => { var connection = context.Connection; await context.Response.WriteAsync(JsonConvert.SerializeObject(new { RemoteIPAddress = connection.RemoteIpAddress?.ToString(), RemotePort = connection.RemotePort, LocalIPAddress = connection.LocalIpAddress?.ToString(), LocalPort = connection.LocalPort, IsLocal = connection.IsLocal })); }); }); using (var app = builder.Build().Start()) using (var client = new HttpClient()) { var response = await client.GetAsync($"http://{requestAddress}:{port}/"); response.EnsureSuccessStatusCode(); var connectionFacts = await response.Content.ReadAsStringAsync(); Assert.NotEmpty(connectionFacts); var facts = JsonConvert.DeserializeObject<JObject>(connectionFacts); Assert.Equal(expectAddress, facts["RemoteIPAddress"].Value<string>()); Assert.NotEmpty(facts["RemotePort"].Value<string>()); Assert.True(facts["IsLocal"].Value<bool>()); } }