BaseControllerTest.cs 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. 
  2. using System.Net;
  3. using System.Text;
  4. using System.Linq;
  5. using System.Net.Http;
  6. using System.ComponentModel;
  7. using System.Threading.Tasks;
  8. using System.Net.Http.Headers;
  9. using Newtonsoft.Json;
  10. using Newtonsoft.Json.Linq;
  11. using Admin.Core.Service.Admin.Auth.Input;
  12. using Admin.Core.Service.Admin.Auth;
  13. using Admin.Core.Common.Cache;
  14. using Admin.Core.Common.Output;
  15. using Admin.Core.Service.Admin.Auth.Output;
  16. using Admin.Core.Common.Configs;
  17. namespace Admin.Core.Tests.Controller
  18. {
  19. public class BaseControllerTest : BaseTest
  20. {
  21. private readonly ICache _cache;
  22. private readonly IAuthService _authService;
  23. private readonly AppConfig _appConfig;
  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(input == null && _appConfig.VarifyCode.Enable)
  40. {
  41. var res = await _authService.GetVerifyCodeAsync("") as IResponseOutput<AuthGetVerifyCodeOutput>;
  42. var verifyCodeKey = string.Format(CacheKey.VerifyCodeKey, res.Data.Key);
  43. var verifyCode = await _cache.GetAsync(verifyCodeKey);
  44. input = new AuthLoginInput()
  45. {
  46. UserName = "admin",
  47. Password = "111111",
  48. VerifyCodeKey = res.Data.Key,
  49. VerifyCode = verifyCode
  50. };
  51. }
  52. //Client.DefaultRequestHeaders.Connection.Add("keep-alive");
  53. 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");
  54. var result = await Client.PostAsync($"/api/admin/auth/login", GetHttpContent(input));
  55. var content = await result.Content.ReadAsStringAsync();
  56. var jObject = JsonConvert.DeserializeObject<JObject>(content);
  57. Client.DefaultRequestHeaders.Add("Authorization", $"Bearer {jObject["data"]["token"]}");
  58. }
  59. public string ToParams(object source)
  60. {
  61. var stringBuilder = new StringBuilder(string.Empty);
  62. if (source == null)
  63. {
  64. return "";
  65. }
  66. var entries = from PropertyDescriptor property in TypeDescriptor.GetProperties(source)
  67. let value = property.GetValue(source)
  68. where value != null
  69. select (property.Name, value);
  70. foreach (var (name, value) in entries)
  71. {
  72. stringBuilder.Append(WebUtility.UrlEncode(name) + "=" + WebUtility.UrlEncode(value + "") + "&");
  73. }
  74. return stringBuilder.ToString().Trim('&');
  75. }
  76. }
  77. }