0
0

BaseControllerTest.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. protected BaseControllerTest()
  24. {
  25. _cache = GetService<ICache>();
  26. _authService = GetService<IAuthService>();
  27. _appConfig = GetService<AppConfig>();
  28. }
  29. public ByteArrayContent GetHttpContent(object input)
  30. {
  31. var content = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(input));
  32. var httpContent = new ByteArrayContent(content);
  33. httpContent.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json;charset=UTF-8");
  34. return httpContent;
  35. }
  36. public async Task Login(AuthLoginInput input = null)
  37. {
  38. if (input == null && _appConfig.VarifyCode.Enable)
  39. {
  40. var res = await _authService.GetVerifyCodeAsync("") as IResponseOutput<AuthGetVerifyCodeOutput>;
  41. var verifyCodeKey = string.Format(CacheKey.VerifyCodeKey, res.Data.Key);
  42. var verifyCode = await _cache.GetAsync(verifyCodeKey);
  43. input = new AuthLoginInput()
  44. {
  45. UserName = "admin",
  46. Password = "111111",
  47. VerifyCodeKey = res.Data.Key,
  48. VerifyCode = verifyCode
  49. };
  50. }
  51. //Client.DefaultRequestHeaders.Connection.Add("keep-alive");
  52. 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");
  53. var result = await Client.PostAsync($"/api/admin/auth/login", GetHttpContent(input));
  54. var content = await result.Content.ReadAsStringAsync();
  55. var jObject = JsonConvert.DeserializeObject<JObject>(content);
  56. Client.DefaultRequestHeaders.Add("Authorization", $"Bearer {jObject["data"]["token"]}");
  57. }
  58. public string ToParams(object source)
  59. {
  60. var stringBuilder = new StringBuilder(string.Empty);
  61. if (source == null)
  62. {
  63. return "";
  64. }
  65. var entries = from PropertyDescriptor property in TypeDescriptor.GetProperties(source)
  66. let value = property.GetValue(source)
  67. where value != null
  68. select (property.Name, value);
  69. foreach (var (name, value) in entries)
  70. {
  71. stringBuilder.Append(WebUtility.UrlEncode(name) + "=" + WebUtility.UrlEncode(value + "") + "&");
  72. }
  73. return stringBuilder.ToString().Trim('&');
  74. }
  75. }
  76. }