/src/Nancy.Tests/Unit/IO/RequestStreamFixture.cs

https://github.com/factormystic/Nancy · C# · 545 lines · 318 code · 117 blank · 110 comment · 0 complexity · 79d251964a62fde25108c4ffb31ac957 MD5 · raw file

  1. namespace Nancy.Tests.Unit.IO
  2. {
  3. using System;
  4. using System.IO;
  5. using FakeItEasy;
  6. using Nancy.IO;
  7. using Xunit;
  8. public class RequestStreamFixture
  9. {
  10. private readonly Stream stream;
  11. public RequestStreamFixture()
  12. {
  13. this.stream = A.Fake<Stream>();
  14. A.CallTo(() => this.stream.CanRead).Returns(true);
  15. A.CallTo(() => this.stream.CanSeek).Returns(true);
  16. A.CallTo(() => this.stream.CanTimeout).Returns(true);
  17. A.CallTo(() => this.stream.CanWrite).Returns(true);
  18. }
  19. [Fact]
  20. public void Should_move_non_seekable_stream_into_seekable_stream_when_stream_switching_is_disabled()
  21. {
  22. // Given
  23. A.CallTo(() => this.stream.CanSeek).Returns(false);
  24. // When
  25. var result = RequestStream.FromStream(stream, 0, 1, true);
  26. // Then
  27. result.CanSeek.ShouldBeTrue();
  28. }
  29. [Fact]
  30. public void Should_move_stream_out_of_memory_if_longer_than_threshold_and_stream_switching_is_enabled()
  31. {
  32. // Given
  33. var inputStream = new MemoryStream(new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 });
  34. // When
  35. var result = RequestStream.FromStream(inputStream, 0, 4, false);
  36. // Then
  37. result.IsInMemory.ShouldBeFalse();
  38. }
  39. [Fact]
  40. public void Should_not_move_stream_out_of_memory_if_longer_than_threshold_and_stream_switching_is_disabled()
  41. {
  42. // Given
  43. var inputStream = new MemoryStream(new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 });
  44. // When
  45. var result = RequestStream.FromStream(inputStream, 0, 4, true);
  46. // Then
  47. result.IsInMemory.ShouldBeTrue();
  48. }
  49. [Fact]
  50. public void Should_throw_invalidoperationexception_when_created_with_non_readable_stream()
  51. {
  52. // Given
  53. A.CallTo(() => this.stream.CanRead).Returns(false);
  54. // When
  55. var exception = Record.Exception(() => RequestStream.FromStream(this.stream));
  56. // Then
  57. exception.ShouldBeOfType<InvalidOperationException>();
  58. }
  59. [Fact]
  60. public void Should_throw_argumentoutofrangeexception_when_expected_lenght_is_less_than_zero()
  61. {
  62. // Given
  63. const int expectedLength = -1;
  64. // When
  65. var exception = Record.Exception(() => RequestStream.FromStream(this.stream, expectedLength));
  66. // Then
  67. exception.ShouldBeOfType<ArgumentOutOfRangeException>();
  68. }
  69. [Fact]
  70. public void Should_throw_argumentoutofrangeexception_when_thresholdLength_is_less_than_zero()
  71. {
  72. // Given
  73. const int tresholdLength = -1;
  74. // When
  75. var exception = Record.Exception(() => RequestStream.FromStream(this.stream, 0, tresholdLength));
  76. // Then
  77. exception.ShouldBeOfType<ArgumentOutOfRangeException>();
  78. }
  79. [Fact]
  80. public void Should_work_even_with_a_non_seekable_stream()
  81. {
  82. var str = A.Fake<Stream>();
  83. A.CallTo(() => str.CanRead).Returns(true);
  84. A.CallTo(() => str.CanSeek).Returns(false);
  85. A.CallTo(() => str.CanTimeout).Returns(true);
  86. A.CallTo(() => str.CanWrite).Returns(true);
  87. // Given
  88. var request = RequestStream.FromStream(str, 0, 1, false);
  89. // When
  90. var result = request.CanRead;
  91. // Then
  92. result.ShouldBeTrue();
  93. }
  94. [Fact]
  95. public void Should_return_true_when_queried_about_supporting_reading()
  96. {
  97. // Given
  98. var request = RequestStream.FromStream(this.stream, 0, 1, false);
  99. // When
  100. var result = request.CanRead;
  101. // Then
  102. result.ShouldBeTrue();
  103. }
  104. [Fact]
  105. public void Should_return_state_of_underlaying_stream_when_queried_about_supporting_writing()
  106. {
  107. // Given
  108. A.CallTo(() => this.stream.CanWrite).Returns(true);
  109. var request = RequestStream.FromStream(this.stream, 0, 1, false);
  110. // When
  111. var result = request.CanWrite;
  112. // Then
  113. result.ShouldBeTrue();
  114. }
  115. [Fact]
  116. public void Should_return_true_when_queried_about_supporting_seeking()
  117. {
  118. // Given
  119. var request = RequestStream.FromStream(this.stream, 0, 1, false);
  120. // When
  121. var result = request.CanSeek;
  122. // Then
  123. result.ShouldBeTrue();
  124. }
  125. [Fact]
  126. public void Should_return_false_when_queried_about_supporting_timeout()
  127. {
  128. // Given
  129. var request = RequestStream.FromStream(this.stream, 0, 1, false);
  130. // When
  131. var result = request.CanTimeout;
  132. // Then
  133. result.ShouldBeFalse();
  134. }
  135. [Fact]
  136. public void Should_return_length_of_underlaying_stream()
  137. {
  138. // Given
  139. A.CallTo(() => this.stream.Length).Returns(1234L);
  140. var request = RequestStream.FromStream(this.stream, 0, 1235, false);
  141. // When
  142. var result = request.Length;
  143. // Then
  144. result.ShouldEqual(1234L);
  145. }
  146. [Fact]
  147. public void Should_return_position_of_underlaying_stream()
  148. {
  149. // Given
  150. var request = RequestStream.FromStream(this.stream, 0, 1, false);
  151. A.CallTo(() => this.stream.Position).Returns(1234L);
  152. // When
  153. var result = request.Position;
  154. // Then
  155. result.ShouldEqual(1234L);
  156. }
  157. [Fact]
  158. public void Should_set_position_of_underlaying_stream()
  159. {
  160. // Given
  161. A.CallTo(() => this.stream.Length).Returns(2000L);
  162. var request = RequestStream.FromStream(this.stream, 0, 2001, false);
  163. // When
  164. request.Position = 1234L;
  165. // Then
  166. this.stream.Position.ShouldEqual(1234L);
  167. }
  168. [Fact]
  169. public void Should_throw_argumentoutofrangexception_when_setting_position_to_less_than_zero()
  170. {
  171. // Given
  172. var request = RequestStream.FromStream(this.stream, 0, 1, false);
  173. // When
  174. var exception = Record.Exception(() => request.Position = -1);
  175. // Then
  176. exception.ShouldBeOfType<InvalidOperationException>();
  177. }
  178. [Fact]
  179. public void Should_throw_invalidoperationexception_when_position_is_set_to_greater_than_length_of_stream()
  180. {
  181. // Given
  182. A.CallTo(() => this.stream.Length).Returns(100L);
  183. var request = RequestStream.FromStream(this.stream, 0, 1, false);
  184. // When
  185. var exception = Record.Exception(() => request.Position = 1000);
  186. // Then
  187. exception.ShouldBeOfType<InvalidOperationException>();
  188. }
  189. [Fact]
  190. public void Should_flush_underlaying_stream()
  191. {
  192. // Given
  193. var request = RequestStream.FromStream(this.stream, 0, 1, false);
  194. // When
  195. request.Flush();
  196. // Then
  197. A.CallTo(() => this.stream.Flush()).MustHaveHappened();
  198. }
  199. [Fact]
  200. public void Should_throw_notsupportedexception_when_setting_length()
  201. {
  202. // Given
  203. var request = RequestStream.FromStream(this.stream, 0, 1, false);
  204. // When
  205. var exception = Record.Exception(() => request.SetLength(10L));
  206. // Then
  207. exception.ShouldBeOfType<NotSupportedException>();
  208. }
  209. [Fact]
  210. public void Should_set_position_of_underlaying_stream_to_zero_when_created()
  211. {
  212. // Given
  213. A.CallTo(() => this.stream.Position).Returns(10);
  214. // When
  215. var request = RequestStream.FromStream(this.stream, 0, 1, false);
  216. // Then
  217. this.stream.Position.ShouldEqual(0L);
  218. }
  219. [Fact]
  220. public void Should_seek_in_the_underlaying_stream_when_seek_is_called()
  221. {
  222. // Given
  223. var request = RequestStream.FromStream(this.stream, 0, 1, false);
  224. // When
  225. request.Seek(10L, SeekOrigin.Current);
  226. // Then
  227. A.CallTo(() => this.stream.Seek(10L, SeekOrigin.Current)).MustHaveHappened();
  228. }
  229. [Fact]
  230. public void Should_return_the_new_position_of_the_underlaying_stream_when_seek_is_called()
  231. {
  232. // Given
  233. A.CallTo(() => this.stream.Seek(A<long>.Ignored, A<SeekOrigin>.Ignored)).Returns(100L);
  234. var request = RequestStream.FromStream(this.stream, 0, 1, false);
  235. // When
  236. var result = request.Seek(10L, SeekOrigin.Current);
  237. // Then
  238. result.ShouldEqual(100L);
  239. }
  240. [Fact]
  241. public void Should_read_byte_from_underlaying_stream_when_reading_byte()
  242. {
  243. // Given
  244. var request = RequestStream.FromStream(this.stream, 0, 1, false);
  245. // When
  246. request.ReadByte();
  247. // Then
  248. A.CallTo(() => this.stream.ReadByte()).MustHaveHappened();
  249. }
  250. [Fact]
  251. public void Should_return_read_byte_from_underlaying_stream_when_readbyte_is_called()
  252. {
  253. // Given
  254. A.CallTo(() => this.stream.ReadByte()).Returns(5);
  255. var request = RequestStream.FromStream(this.stream, 0, 1, false);
  256. // When
  257. var result = request.ReadByte();
  258. // Then
  259. result.ShouldEqual(5);
  260. }
  261. [Fact]
  262. public void Should_close_the_underlaying_stream_when_being_closed()
  263. {
  264. // Given
  265. var request = RequestStream.FromStream(this.stream, 0, 1, false);
  266. // When
  267. request.Close();
  268. // Then
  269. A.CallTo(() => this.stream.Close()).MustHaveHappened();
  270. }
  271. [Fact]
  272. public void Should_read_from_underlaying_stream_when_read_is_called()
  273. {
  274. // Given
  275. var buffer = new byte[1];
  276. var request = RequestStream.FromStream(this.stream, 0, 1, false);
  277. // When
  278. request.Read(buffer, 0, buffer.Length);
  279. // Then
  280. A.CallTo(() => this.stream.Read(buffer, 0, buffer.Length)).MustHaveHappened();
  281. }
  282. [Fact]
  283. public void Should_return_result_from_reading_underlaying_stream()
  284. {
  285. // Given
  286. var buffer = new byte[1];
  287. var request = RequestStream.FromStream(this.stream, 0, 1, false);
  288. A.CallTo(() => this.stream.Read(buffer, 0, buffer.Length)).Returns(3);
  289. // When
  290. var result = request.Read(buffer, 0, buffer.Length);
  291. // Then
  292. result.ShouldEqual(3);
  293. }
  294. [Fact]
  295. public void Should_write_to_underlaying_stream_when_write_is_called()
  296. {
  297. // Given
  298. var buffer = new byte[1];
  299. var request = RequestStream.FromStream(this.stream, 0, 1, false);
  300. // When
  301. request.Write(buffer, 0, buffer.Length);
  302. // Then
  303. A.CallTo(() => this.stream.Write(buffer, 0, buffer.Length)).MustHaveHappened();
  304. }
  305. [Fact]
  306. public void Should_no_longer_be_in_memory_if_expected_length_is_greater_or_equal_to_threshold_length()
  307. {
  308. // Given, When
  309. var request = RequestStream.FromStream(this.stream, 1, 0, false);
  310. // Then
  311. request.IsInMemory.ShouldBeFalse();
  312. }
  313. [Fact]
  314. public void Should_no_longer_be_in_memory_when_more_bytes_have_been_written_to_stream_then_size_of_the_threshold_and_stream_swapping_is_enabled()
  315. {
  316. // Given
  317. var buffer = new byte[100];
  318. var request = RequestStream.FromStream(this.stream, 0, 10, false);
  319. A.CallTo(() => this.stream.Length).Returns(100);
  320. // When
  321. request.Write(buffer, 0, buffer.Length);
  322. // Then
  323. request.IsInMemory.ShouldBeFalse();
  324. }
  325. [Fact]
  326. public void Should_still_be_in_memory_when_more_bytes_have_been_written_to_stream_than_size_of_threshold_and_stream_swapping_is_disabled()
  327. {
  328. // Given
  329. var buffer = new byte[100];
  330. var request = RequestStream.FromStream(this.stream, 0, 10, true);
  331. A.CallTo(() => this.stream.Length).Returns(100);
  332. // When
  333. request.Write(buffer, 0, buffer.Length);
  334. // Then
  335. request.IsInMemory.ShouldBeTrue();
  336. }
  337. [Fact]
  338. public void Should_call_beginread_on_underlaying_stream_when_beginread_is_called()
  339. {
  340. // Given
  341. var buffer = new byte[10];
  342. AsyncCallback callback = x => { };
  343. var state = new object();
  344. var request = RequestStream.FromStream(this.stream, 0, 10, true);
  345. // When
  346. request.BeginRead(buffer, 0, buffer.Length, callback, state);
  347. // Then
  348. A.CallTo(() => this.stream.BeginRead(buffer, 0, buffer.Length, callback, state)).MustHaveHappened();
  349. }
  350. [Fact]
  351. public void Should_return_result_from_underlaying_beginread_when_beginread_is_called()
  352. {
  353. // Given
  354. var buffer = new byte[10];
  355. var asyncResult = A.Fake<IAsyncResult>();
  356. AsyncCallback callback = x => { };
  357. var state = new object();
  358. var request = RequestStream.FromStream(this.stream, 0, 10, true);
  359. A.CallTo(() => this.stream.BeginRead(buffer, 0, buffer.Length, callback, state)).Returns(asyncResult);
  360. // When
  361. var result = request.BeginRead(buffer, 0, buffer.Length, callback, state);
  362. // Then
  363. result.ShouldBeSameAs(asyncResult);
  364. }
  365. [Fact]
  366. public void Should_call_beginwrite_on_underlaying_stream_when_beginwrite_is_called()
  367. {
  368. // Given
  369. var buffer = new byte[10];
  370. AsyncCallback callback = x => { };
  371. var state = new object();
  372. var request = RequestStream.FromStream(this.stream, 0, 10, true);
  373. // When
  374. request.BeginWrite(buffer, 0, buffer.Length, callback, state);
  375. // Then
  376. A.CallTo(() => this.stream.BeginWrite(buffer, 0, buffer.Length, callback, state)).MustHaveHappened();
  377. }
  378. [Fact]
  379. public void Should_return_result_from_underlaying_beginwrite_when_beginwrite_is_called()
  380. {
  381. // Given
  382. var buffer = new byte[10];
  383. var asyncResult = A.Fake<IAsyncResult>();
  384. AsyncCallback callback = x => { };
  385. var state = new object();
  386. var request = RequestStream.FromStream(this.stream, 0, 10, true);
  387. A.CallTo(() => this.stream.BeginWrite(buffer, 0, buffer.Length, callback, state)).Returns(asyncResult);
  388. // When
  389. var result = request.BeginWrite(buffer, 0, buffer.Length, callback, state);
  390. // Then
  391. result.ShouldBeSameAs(asyncResult);
  392. }
  393. [Fact]
  394. public void Should_call_endread_on_underlaying_stream_when_endread_is_called()
  395. {
  396. // Given
  397. var asyncResult = A.Fake<IAsyncResult>();
  398. var request = RequestStream.FromStream(this.stream, 0, 10, true);
  399. // When
  400. request.EndRead(asyncResult);
  401. // Then
  402. A.CallTo(() => this.stream.EndRead(asyncResult)).MustHaveHappened();
  403. }
  404. [Fact]
  405. public void Should_return_result_from_underlaying_endread_when_endread_is_called()
  406. {
  407. // Given
  408. var asyncResult = A.Fake<IAsyncResult>();
  409. var request = RequestStream.FromStream(this.stream, 0, 10, true);
  410. A.CallTo(() => this.stream.EndRead(A<IAsyncResult>.Ignored)).Returns(4);
  411. // When
  412. var result = request.EndRead(asyncResult);
  413. // Then
  414. result.ShouldEqual(4);
  415. }
  416. [Fact]
  417. public void Should_call_endwrite_on_underlaying_stream_when_endwrite_is_called()
  418. {
  419. // Given
  420. var asyncResult = A.Fake<IAsyncResult>();
  421. var request = RequestStream.FromStream(this.stream, 0, 10, true);
  422. // When
  423. request.EndWrite(asyncResult);
  424. // Then
  425. A.CallTo(() => this.stream.EndWrite(asyncResult)).MustHaveHappened();
  426. }
  427. }
  428. }