using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Mvc.Testing; using Microsoft.AspNetCore.Mvc.Testing.Handlers; using System; using System.Net; using System.Net.Http; using UnitTest; using Xunit; namespace Bootstrap.Admin { [CollectionDefinition("SQLServerContext")] public class BootstrapAdminTestContext : ICollectionFixture { } [CollectionDefinition("SQLiteContext")] public class SQLiteContext : ICollectionFixture { } [CollectionDefinition("MySqlContext")] public class MySqlContext : ICollectionFixture { } public class MySqlBAWebHost : BAWebHost { protected override void ConfigureWebHost(IWebHostBuilder builder) { base.ConfigureWebHost(builder); TestHelper.ConfigureWebHost(builder, Longbow.Data.DatabaseProviderType.MySql); } } public class SQLiteBAWebHost : BAWebHost { protected override void ConfigureWebHost(IWebHostBuilder builder) { base.ConfigureWebHost(builder); TestHelper.ConfigureWebHost(builder, Longbow.Data.DatabaseProviderType.SQLite); } } /// /// /// public class BAWebHost : WebApplicationFactory { /// /// /// static BAWebHost() { // Copy license TestHelper.CopyLicense(); } public BAWebHost() { var client = CreateClient("Account/Login"); var login = client.LoginAsync(); login.Wait(); } /// /// 获得已经登录的HttpClient /// /// /// public HttpClient CreateClient(string baseAddress) { var client = CreateDefaultClient(new Uri($"http://localhost/{baseAddress}/"), new RedirectHandler(7), new CookieContainerHandler(_cookie)); client.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_1) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/12.0.1 Safari/605.1.15"); return client; } private readonly CookieContainer _cookie = new CookieContainer(); protected override void ConfigureWebHost(IWebHostBuilder builder) { base.ConfigureWebHost(builder); TestHelper.ConfigureWebHost(builder); } } }