VirtualFileTests.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. using System.Net;
  2. using System.Net.Http;
  3. using System.Threading.Tasks;
  4. using Xunit;
  5. namespace Masuit.Tools.Core.Test.AspNetCore
  6. {
  7. public class VirtualFileTests : TestBase
  8. {
  9. /// <summary>
  10. /// The full virtual file attachment with entity tag test.
  11. /// </summary>
  12. [Fact]
  13. public async Task FullVirtualFileAttachmentEtagTest()
  14. {
  15. // Act
  16. HttpResponseMessage response = await Client.GetAsync("/file/virtual/true/true");
  17. response.EnsureSuccessStatusCode();
  18. string responseString = await response.Content.ReadAsStringAsync();
  19. // Assert
  20. Assert.Equal(HttpStatusCode.OK, response.StatusCode);
  21. Assert.Equal("0123456789abcdefghijklmnopgrstuvwxyzABCDEFGHIJKLMNOPGRSTUVWXYZ", responseString);
  22. Assert.Equal("bytes", response.Headers.AcceptRanges.ToString());
  23. Assert.Equal(EntityTag, response.Headers.ETag);
  24. Assert.Null(response.Content.Headers.ContentRange);
  25. Assert.Equal(62, response.Content.Headers.ContentLength);
  26. Assert.Equal("attachment", response.Content.Headers.ContentDisposition.DispositionType);
  27. }
  28. [Fact]
  29. public async Task FullVirtualFileAttachmentNoEtagTest()
  30. {
  31. // Act
  32. HttpResponseMessage response = await Client.GetAsync("/file/virtual/true/false");
  33. response.EnsureSuccessStatusCode();
  34. string responseString = await response.Content.ReadAsStringAsync();
  35. // Assert
  36. Assert.Equal(HttpStatusCode.OK, response.StatusCode);
  37. Assert.Equal("0123456789abcdefghijklmnopgrstuvwxyzABCDEFGHIJKLMNOPGRSTUVWXYZ", responseString);
  38. Assert.Equal("bytes", response.Headers.AcceptRanges.ToString());
  39. Assert.Null(response.Headers.ETag);
  40. Assert.Null(response.Content.Headers.ContentRange);
  41. Assert.Equal(62, response.Content.Headers.ContentLength);
  42. Assert.Equal("attachment", response.Content.Headers.ContentDisposition.DispositionType);
  43. }
  44. [Fact]
  45. public async Task FullVirtualFileInlineEtagTest()
  46. {
  47. // Act
  48. HttpResponseMessage response = await Client.GetAsync("/file/virtual/false/true");
  49. response.EnsureSuccessStatusCode();
  50. string responseString = await response.Content.ReadAsStringAsync();
  51. // Assert
  52. Assert.Equal(HttpStatusCode.OK, response.StatusCode);
  53. Assert.Equal("0123456789abcdefghijklmnopgrstuvwxyzABCDEFGHIJKLMNOPGRSTUVWXYZ", responseString);
  54. Assert.Equal("bytes", response.Headers.AcceptRanges.ToString());
  55. Assert.Equal(EntityTag, response.Headers.ETag);
  56. Assert.Null(response.Content.Headers.ContentRange);
  57. Assert.Equal(62, response.Content.Headers.ContentLength);
  58. Assert.Equal("inline", response.Content.Headers.ContentDisposition.DispositionType);
  59. }
  60. [Fact]
  61. public async Task FullVirtualFileInlineFileNameTest()
  62. {
  63. // Act
  64. HttpResponseMessage response = await Client.GetAsync("/file/virtual/false");
  65. response.EnsureSuccessStatusCode();
  66. string responseString = await response.Content.ReadAsStringAsync();
  67. // Assert
  68. Assert.Equal(HttpStatusCode.OK, response.StatusCode);
  69. Assert.Equal("0123456789abcdefghijklmnopgrstuvwxyzABCDEFGHIJKLMNOPGRSTUVWXYZ", responseString);
  70. Assert.Equal("bytes", response.Headers.AcceptRanges.ToString());
  71. Assert.Null(response.Headers.ETag);
  72. Assert.Null(response.Content.Headers.ContentRange);
  73. Assert.Equal(62, response.Content.Headers.ContentLength);
  74. Assert.Equal("inline", response.Content.Headers.ContentDisposition.DispositionType);
  75. Assert.Equal("TestFile.txt", response.Content.Headers.ContentDisposition.FileName);
  76. }
  77. [Fact]
  78. public async Task FullVirtualFileInlineNoEtagTest()
  79. {
  80. // Act
  81. HttpResponseMessage response = await Client.GetAsync("/file/virtual/false/false");
  82. response.EnsureSuccessStatusCode();
  83. string responseString = await response.Content.ReadAsStringAsync();
  84. // Assert
  85. Assert.Equal(HttpStatusCode.OK, response.StatusCode);
  86. Assert.Equal("0123456789abcdefghijklmnopgrstuvwxyzABCDEFGHIJKLMNOPGRSTUVWXYZ", responseString);
  87. Assert.Equal("bytes", response.Headers.AcceptRanges.ToString());
  88. Assert.Null(response.Headers.ETag);
  89. Assert.Null(response.Content.Headers.ContentRange);
  90. Assert.Equal(62, response.Content.Headers.ContentLength);
  91. Assert.Equal("inline", response.Content.Headers.ContentDisposition.DispositionType);
  92. }
  93. [Fact]
  94. public async Task Partial1VirtualFileAttachmentEtagTest()
  95. {
  96. // Arrange
  97. Client.DefaultRequestHeaders.Add("Range", "bytes=0-0");
  98. // Act
  99. HttpResponseMessage response = await Client.GetAsync("/file/virtual/true/true");
  100. response.EnsureSuccessStatusCode();
  101. string responseString = await response.Content.ReadAsStringAsync();
  102. // Assert
  103. Assert.Equal(HttpStatusCode.PartialContent, response.StatusCode);
  104. Assert.Equal("0", responseString);
  105. Assert.Equal("bytes", response.Headers.AcceptRanges.ToString());
  106. Assert.Equal(EntityTag, response.Headers.ETag);
  107. Assert.NotNull(response.Content.Headers.ContentRange);
  108. Assert.Equal("bytes 0-0/62", response.Content.Headers.ContentRange.ToString());
  109. Assert.Equal(1, response.Content.Headers.ContentLength);
  110. Assert.Equal("attachment", response.Content.Headers.ContentDisposition.DispositionType);
  111. }
  112. [Fact]
  113. public async Task Partial1VirtualFileAttachmentNoEtagTest()
  114. {
  115. // Arrange
  116. Client.DefaultRequestHeaders.Add("Range", "bytes=0-0");
  117. // Act
  118. HttpResponseMessage response = await Client.GetAsync("/file/virtual/true/false");
  119. response.EnsureSuccessStatusCode();
  120. string responseString = await response.Content.ReadAsStringAsync();
  121. // Assert
  122. Assert.Equal(HttpStatusCode.PartialContent, response.StatusCode);
  123. Assert.Equal("0", responseString);
  124. Assert.Equal("bytes", response.Headers.AcceptRanges.ToString());
  125. Assert.Null(response.Headers.ETag);
  126. Assert.NotNull(response.Content.Headers.ContentRange);
  127. Assert.Equal("bytes 0-0/62", response.Content.Headers.ContentRange.ToString());
  128. Assert.Equal(1, response.Content.Headers.ContentLength);
  129. Assert.Equal("attachment", response.Content.Headers.ContentDisposition.DispositionType);
  130. }
  131. [Fact]
  132. public async Task Partial2VirtualFileAttachmentEtagTest()
  133. {
  134. // Arrange
  135. Client.DefaultRequestHeaders.Add("Range", "bytes=1-1");
  136. // Act
  137. HttpResponseMessage response = await Client.GetAsync("/file/virtual/true/true");
  138. response.EnsureSuccessStatusCode();
  139. string responseString = await response.Content.ReadAsStringAsync();
  140. // Assert
  141. Assert.Equal(HttpStatusCode.PartialContent, response.StatusCode);
  142. Assert.Equal("1", responseString);
  143. Assert.Equal("bytes", response.Headers.AcceptRanges.ToString());
  144. Assert.Equal(EntityTag, response.Headers.ETag);
  145. Assert.NotNull(response.Content.Headers.ContentRange);
  146. Assert.Equal("bytes 1-1/62", response.Content.Headers.ContentRange.ToString());
  147. Assert.Equal(1, response.Content.Headers.ContentLength);
  148. Assert.Equal("attachment", response.Content.Headers.ContentDisposition.DispositionType);
  149. }
  150. [Fact]
  151. public async Task Partial2VirtualFileAttachmentNoEtagTest()
  152. {
  153. // Arrange
  154. Client.DefaultRequestHeaders.Add("Range", "bytes=1-1");
  155. // Act
  156. HttpResponseMessage response = await Client.GetAsync("/file/virtual/true/false");
  157. response.EnsureSuccessStatusCode();
  158. string responseString = await response.Content.ReadAsStringAsync();
  159. // Assert
  160. Assert.Equal(HttpStatusCode.PartialContent, response.StatusCode);
  161. Assert.Equal("1", responseString);
  162. Assert.Equal("bytes", response.Headers.AcceptRanges.ToString());
  163. Assert.Null(response.Headers.ETag);
  164. Assert.NotNull(response.Content.Headers.ContentRange);
  165. Assert.Equal("bytes 1-1/62", response.Content.Headers.ContentRange.ToString());
  166. Assert.Equal(1, response.Content.Headers.ContentLength);
  167. Assert.Equal("attachment", response.Content.Headers.ContentDisposition.DispositionType);
  168. }
  169. [Fact]
  170. public async Task Partial2VirtualFileInlineEtagTest()
  171. {
  172. // Arrange
  173. Client.DefaultRequestHeaders.Add("Range", "bytes=1-1");
  174. // Act
  175. HttpResponseMessage response = await Client.GetAsync("/file/virtual/false/true");
  176. response.EnsureSuccessStatusCode();
  177. string responseString = await response.Content.ReadAsStringAsync();
  178. // Assert
  179. Assert.Equal("1", responseString);
  180. Assert.Equal("bytes", response.Headers.AcceptRanges.ToString());
  181. Assert.Equal(EntityTag, response.Headers.ETag);
  182. Assert.NotNull(response.Content.Headers.ContentRange);
  183. Assert.Equal("bytes 1-1/62", response.Content.Headers.ContentRange.ToString());
  184. Assert.Equal(1, response.Content.Headers.ContentLength);
  185. Assert.Equal("inline", response.Content.Headers.ContentDisposition.DispositionType);
  186. }
  187. [Fact]
  188. public async Task Partial2VirtualFileInlineNoEtagTest()
  189. {
  190. // Arrange
  191. Client.DefaultRequestHeaders.Add("Range", "bytes=1-1");
  192. // Act
  193. HttpResponseMessage response = await Client.GetAsync("/file/virtual/false/false");
  194. response.EnsureSuccessStatusCode();
  195. string responseString = await response.Content.ReadAsStringAsync();
  196. // Assert
  197. Assert.Equal(HttpStatusCode.PartialContent, response.StatusCode);
  198. Assert.Equal("1", responseString);
  199. Assert.Equal("bytes", response.Headers.AcceptRanges.ToString());
  200. Assert.Null(response.Headers.ETag);
  201. Assert.NotNull(response.Content.Headers.ContentRange);
  202. Assert.Equal("bytes 1-1/62", response.Content.Headers.ContentRange.ToString());
  203. Assert.Equal(1, response.Content.Headers.ContentLength);
  204. Assert.Equal("inline", response.Content.Headers.ContentDisposition.DispositionType);
  205. }
  206. }
  207. }