isn/test/isnd.tests/WebServerFixture.cs

64 lines
1.9 KiB
C#
Raw Normal View History

2023-01-29 13:04:50 +00:00
using System;
2023-02-06 21:43:18 +00:00
using System.Collections.Generic;
2023-01-29 13:04:50 +00:00
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Hosting.Server;
using Microsoft.AspNetCore.Hosting.Server.Features;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Xunit;
namespace isnd.tests
{
[CollectionDefinition("Web server collection")]
public class WebServerFixture : IDisposable
{
public IWebHost Host { get; private set;}
2023-02-06 21:43:18 +00:00
public List<string> Addresses { get; private set; } = new List<string>();
2023-01-29 13:04:50 +00:00
public WebServerFixture()
{
SetupHost();
}
public void Dispose()
{
Host.StopAsync().Wait();
Host.Dispose();
}
public void SetupHost()
{
var webhostBuilder = new WebHostBuilder()
.UseKestrel()
.UseIISIntegration()
2023-02-06 21:43:18 +00:00
// .UseContentRoot("../../../../../src/isnd")
2023-01-29 13:04:50 +00:00
.UseStartup(typeof(Startup))
.ConfigureAppConfiguration((builderContext, config) =>
{
config.AddJsonFile("appsettings.json", false);
config.AddJsonFile("appsettings.Development.json", false);
});
Host = webhostBuilder.Build();
Host.Start(); //Starts listening on the configured addresses.
PrintAddresses(Host.Services);
}
void PrintAddresses(IServiceProvider services)
{
2023-02-06 21:43:18 +00:00
Addresses.Clear();
2023-01-29 13:04:50 +00:00
Console.WriteLine("Checking addresses...");
var server = services.GetRequiredService<IServer>();
var addressFeature = server.Features.Get<IServerAddressesFeature>();
foreach (var address in addressFeature.Addresses)
{
Console.WriteLine("Listing on address: " + address);
2023-02-06 21:43:18 +00:00
Addresses.Add(address);
2023-01-29 13:04:50 +00:00
}
}
}
}