Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

155 строки
4.0 KiB

  1. using System;
  2. using System.Reflection;
  3. using DotNetCore.CAP.Diagnostics;
  4. using DotNetCore.CAP.Infrastructure;
  5. using Newtonsoft.Json.Linq;
  6. using Xunit;
  7. namespace DotNetCore.CAP.Test
  8. {
  9. public class HelperTest
  10. {
  11. [Fact]
  12. public void ToTimestampTest()
  13. {
  14. //Arrange
  15. var time = DateTime.Parse("2018-01-01 00:00:00");
  16. //Act
  17. var result = Helper.ToTimestamp(time);
  18. //Assert
  19. Assert.Equal(1514764800, result);
  20. }
  21. [Fact]
  22. public void FromTimestampTest()
  23. {
  24. //Arrange
  25. var time = DateTime.Parse("2018-01-01 00:00:00");
  26. //Act
  27. var result = Helper.FromTimestamp(1514764800);
  28. //Assert
  29. Assert.Equal(time, result);
  30. }
  31. [Fact]
  32. public void IsControllerTest()
  33. {
  34. //Arrange
  35. var typeInfo = typeof(HomeController).GetTypeInfo();
  36. //Act
  37. var result = Helper.IsController(typeInfo);
  38. //Assert
  39. Assert.True(result);
  40. }
  41. [Theory]
  42. [InlineData(typeof(string))]
  43. [InlineData(typeof(decimal))]
  44. [InlineData(typeof(DateTime))]
  45. [InlineData(typeof(DateTimeOffset))]
  46. [InlineData(typeof(Guid))]
  47. [InlineData(typeof(TimeSpan))]
  48. [InlineData(typeof(Uri))]
  49. public void IsSimpleTypeTest(Type type)
  50. {
  51. //Act
  52. var result = Helper.IsComplexType(type);
  53. //Assert
  54. Assert.False(result);
  55. }
  56. [Theory]
  57. [InlineData(typeof(HomeController))]
  58. [InlineData(typeof(Exception))]
  59. [InlineData(typeof(Person))]
  60. public void IsComplexTypeTest(Type type)
  61. {
  62. //Act
  63. var result = Helper.IsComplexType(type);
  64. //Assert
  65. Assert.True(result);
  66. }
  67. [Fact]
  68. public void AddExceptionPropertyTest()
  69. {
  70. //Arrange
  71. var json = "{}";
  72. var exception = new Exception("Test Exception Message")
  73. {
  74. Source = "Test Source",
  75. InnerException = { }
  76. };
  77. var expected = new
  78. {
  79. ExceptionMessage = new
  80. {
  81. Source = "Test Source",
  82. Message = "Test Exception Message",
  83. InnerMessage = new { }
  84. }
  85. };
  86. //Act
  87. var result = Helper.AddExceptionProperty(json, exception);
  88. //Assert
  89. var jObj = JObject.Parse(result);
  90. Assert.Equal(jObj["ExceptionMessage"]["Source"].Value<string>(), expected.ExceptionMessage.Source);
  91. Assert.Equal(jObj["ExceptionMessage"]["Message"].Value<string>(), expected.ExceptionMessage.Message);
  92. }
  93. [Theory]
  94. [InlineData("10.0.0.1")]
  95. [InlineData("172.16.0.1")]
  96. [InlineData("192.168.1.1")]
  97. public void IsInnerIPTest(string ipAddress)
  98. {
  99. Assert.True(Helper.IsInnerIP(ipAddress));
  100. }
  101. [Fact]
  102. public void AddTracingHeaderPropertyTest()
  103. {
  104. //Arrange
  105. var json = "{}";
  106. var header = new TracingHeaders { { "key", "value" } };
  107. //Act
  108. var result = Helper.AddTracingHeaderProperty(json, header);
  109. //Assert
  110. var expected = "{\"TracingHeaders\":{\"key\":\"value\"}}";
  111. Assert.Equal(expected, result);
  112. }
  113. [Fact]
  114. public void TryExtractTracingHeadersTest()
  115. {
  116. //Arrange
  117. var json = "{\"TracingHeaders\":{\"key\":\"value\"}}";
  118. TracingHeaders header = null;
  119. string removedHeadersJson = "";
  120. //Act
  121. var result = Helper.TryExtractTracingHeaders(json, out header, out removedHeadersJson);
  122. //Assert
  123. Assert.True(result);
  124. Assert.NotNull(header);
  125. Assert.Single(header);
  126. Assert.Equal("{}", removedHeadersJson);
  127. }
  128. }
  129. }