2020-02-12 21:56:35 +08:00
using Longbow.Web.SMS ;
2019-05-13 12:11:15 +08:00
using Microsoft.AspNetCore.Hosting ;
2019-01-21 17:33:20 +08:00
using Microsoft.AspNetCore.Mvc.Testing ;
using Microsoft.AspNetCore.Mvc.Testing.Handlers ;
2019-05-12 14:28:56 +08:00
using Microsoft.Extensions.Configuration ;
2019-11-01 21:39:40 +08:00
using Microsoft.Extensions.DependencyInjection ;
2019-01-21 17:33:20 +08:00
using System ;
2019-05-17 18:04:46 +08:00
using System.IO ;
2019-01-21 17:33:20 +08:00
using System.Net ;
using System.Net.Http ;
using Xunit ;
namespace Bootstrap.Admin
{
2020-02-12 21:56:35 +08:00
/// <summary>
/// 未登录
/// </summary>
[CollectionDefinition("BA-Logout")]
public class BootstrapAdminLogoutContext : ICollectionFixture < BAWebHost >
2019-05-13 12:11:15 +08:00
{
2019-01-24 17:58:06 +08:00
}
2019-01-21 17:33:20 +08:00
/// <summary>
///
/// </summary>
public class BAWebHost : WebApplicationFactory < Startup >
{
/// <summary>
/// 获得已经登录的HttpClient
/// </summary>
/// <param name="baseAddress"></param>
/// <returns></returns>
2019-03-02 15:15:47 +08:00
public HttpClient CreateClient ( string baseAddress )
{
var client = CreateDefaultClient ( new Uri ( $"http://localhost/{baseAddress}/" ) , new RedirectHandler ( 7 ) , new CookieContainerHandler ( _cookie ) ) ;
return client ;
}
2019-01-21 17:33:20 +08:00
2019-03-05 13:05:19 +08:00
protected override void ConfigureClient ( HttpClient client )
{
base . ConfigureClient ( client ) ;
2019-05-11 12:15:03 +08:00
client . DefaultRequestHeaders . Add ( "User-Agent" , "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_1_UnitTest) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/12.0.1 Safari/605.1.15" ) ;
2019-03-05 13:05:19 +08:00
}
2019-01-21 17:33:20 +08:00
private readonly CookieContainer _cookie = new CookieContainer ( ) ;
protected override void ConfigureWebHost ( IWebHostBuilder builder )
{
base . ConfigureWebHost ( builder ) ;
2019-05-12 14:28:56 +08:00
var config = new ConfigurationBuilder ( ) ;
config . AddEnvironmentVariables ( ) ;
var con = config . Build ( ) ;
2019-09-02 23:13:57 +08:00
// 增加单元测试本身的配置文件
2019-05-18 21:21:03 +08:00
builder . ConfigureAppConfiguration ( app = > app . AddJsonFile ( TestHelper . RetrievePath ( $"UnitTest{Path.DirectorySeparatorChar}appsettings.json" ) , false , true ) ) ;
2019-05-12 14:28:56 +08:00
if ( con . GetValue ( "Appveyor" , false ) )
{
2019-05-18 21:21:03 +08:00
builder . ConfigureAppConfiguration ( app = > app . AddJsonFile ( TestHelper . RetrievePath ( $"UnitTest{Path.DirectorySeparatorChar}appsettings.appveyor.json" ) , false , true ) ) ;
2019-05-12 14:28:56 +08:00
}
2019-10-08 19:38:17 +08:00
// 替换 SMS 服务
builder . ConfigureServices ( services = >
{
services . AddTransient < ISMSProvider , DefaultSMSProvider > ( ) ;
} ) ;
}
2019-01-21 17:33:20 +08:00
}
}