BaseControllerTest.cs 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. using Admin.Core.Common.Cache;
  2. using Admin.Core.Common.Configs;
  3. using Admin.Core.Common.Output;
  4. using Admin.Core.Service.Admin.Auth;
  5. using Admin.Core.Service.Admin.Auth.Input;
  6. using Admin.Core.Service.Admin.Auth.Output;
  7. using Newtonsoft.Json;
  8. using Newtonsoft.Json.Linq;
  9. using System.ComponentModel;
  10. using System.Linq;
  11. using System.Net;
  12. using System.Net.Http;
  13. using System.Net.Http.Headers;
  14. using System.Text;
  15. using System.Threading.Tasks;
  16. namespace Admin.Core.Tests.Controller
  17. {
  18. public class BaseControllerTest : BaseTest
  19. {
  20. private readonly ICache _cache;
  21. private readonly IAuthService _authService;
  22. private readonly AppConfig _appConfig;
  23. private static JToken _token = null;
  24. protected BaseControllerTest()
  25. {
  26. _cache = GetService<ICache>();
  27. _authService = GetService<IAuthService>();
  28. _appConfig = GetService<AppConfig>();
  29. }
  30. public ByteArrayContent GetHttpContent(object input)
  31. {
  32. var content = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(input));
  33. var httpContent = new ByteArrayContent(content);
  34. httpContent.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json;charset=UTF-8");
  35. return httpContent;
  36. }
  37. public async Task Login(AuthLoginInput input = null)
  38. {
  39. if(_token == null)
  40. {
  41. if (input == null && _appConfig.VarifyCode.Enable)
  42. {
  43. var res = await _authService.GetVerifyCodeAsync("") as IResponseOutput<AuthGetVerifyCodeOutput>;
  44. var verifyCodeKey = string.Format(CacheKey.VerifyCodeKey, res.Data.Key);
  45. var verifyCode = await _cache.GetAsync(verifyCodeKey);
  46. input = new AuthLoginInput()
  47. {
  48. UserName = "admin",
  49. Password = "111111",
  50. VerifyCodeKey = res.Data.Key,
  51. VerifyCode = verifyCode
  52. };
  53. }
  54. //Client.DefaultRequestHeaders.Connection.Add("keep-alive");
  55. Client.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36");
  56. var result = await Client.PostAsync($"/api/admin/auth/login", GetHttpContent(input));
  57. var content = await result.Content.ReadAsStringAsync();
  58. var jObject = JsonConvert.DeserializeObject<JObject>(content);
  59. _token = jObject["data"]["token"];
  60. }
  61. Client.DefaultRequestHeaders.Add("Authorization", $"Bearer {_token}");
  62. }
  63. public string ToParams(object source)
  64. {
  65. var stringBuilder = new StringBuilder(string.Empty);
  66. if (source == null)
  67. {
  68. return "";
  69. }
  70. var entries = from PropertyDescriptor property in TypeDescriptor.GetProperties(source)
  71. let value = property.GetValue(source)
  72. where value != null
  73. select (property.Name, value);
  74. foreach (var (name, value) in entries)
  75. {
  76. stringBuilder.Append(WebUtility.UrlEncode(name) + "=" + WebUtility.UrlEncode(value + "") + "&");
  77. }
  78. return stringBuilder.ToString().Trim('&');
  79. }
  80. }
  81. }