1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 |
- using Autofac.Extensions.DependencyInjection;
- using Microsoft.AspNetCore.Hosting;
- using Microsoft.AspNetCore.TestHost;
- using Microsoft.Extensions.Hosting;
- using Microsoft.Extensions.Options;
- using MS.Component.Jwt;
- using MS.Component.Jwt.UserClaim;
- using MS.WebApi;
- using System.Net.Http;
- namespace WebApiTests
- {
- public static class TestHostBuild
- {
- public static readonly JwtService jwtService = new JwtService(Options.Create(new JwtSetting
- {
- Audience = "MS.Audience",
- Issuer = "MS.WebHost",
- LifeTime = 1440,
- SecurityKey = "MS.WebHost SecurityKey"
- }));
- public static readonly UserData userData = new UserData
- {
- Account = "test",
- Email = "test@qq.com",
- Id = 1,
- Name = "测试用户",
- Phone = "123456789111",
- RoleDisplayName = "testuserRole",
- RoleName = "testuser"
- };
- public static IHostBuilder GetTestHost()
- {
-
-
-
- return new HostBuilder()
- .UseServiceProviderFactory(new AutofacServiceProviderFactory())
- .ConfigureWebHostDefaults(webBuilder =>
- {
- webBuilder
- .UseTestServer()
- .UseStartup<Startup>();
- });
- }
-
-
-
-
-
- public static HttpClient GetTestClientWithToken(this IHost host)
- {
- var client = host.GetTestClient();
- client.DefaultRequestHeaders.Add("Authorization", $"Bearer {GenerateToken()}");
- return client;
- }
-
-
-
-
- public static string GenerateToken()
- {
- return jwtService.BuildToken(jwtService.BuildClaims(userData));
- }
- }
- }
|