Skip to content

Commit 08e275b

Browse files
committed
Added support for checking a file stream result's contents.
1 parent 6aec7fa commit 08e275b

File tree

3 files changed

+91
-5
lines changed

3 files changed

+91
-5
lines changed

TestStack.FluentMVCTesting.Tests/ControllerResultTestTests.cs

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.IO;
34
using System.Linq;
45
using System.Linq.Expressions;
56
using System.Net;
7+
using System.Security.Permissions;
68
using System.Web.Mvc;
79
using NUnit.Framework;
810
using TestStack.FluentMVCTesting.Tests.TestControllers;
@@ -39,6 +41,7 @@ class ControllerResultTestShould
3941
ReturnType<FilePathResult>(t => t.ShouldRenderFilePath("")),
4042
ReturnType<FilePathResult>(t => t.ShouldRenderFilePath("", "")),
4143
ReturnType<FileStreamResult>(t => t.ShouldRenderFileStream()),
44+
ReturnType<FileStreamResult>(t => t.ShouldRenderFileStream(new MemoryStream())),
4245
ReturnType<FileResult>(t => t.ShouldRenderAnyFile()),
4346
ReturnType<HttpStatusCodeResult>(t => t.ShouldGiveHttpStatus()),
4447
ReturnType<JsonResult>(t => t.ShouldReturnJson()),
@@ -323,6 +326,52 @@ public void Check_for_file_stream_result()
323326
.ShouldRenderFileStream();
324327
}
325328

329+
[Test]
330+
public void Check_for_file_stream_result_and_check_stream_data()
331+
{
332+
_controller
333+
.WithCallTo(c => c.EmptyStream())
334+
.ShouldRenderFileStream(ControllerResultTestController.EmptyStreamContents);
335+
}
336+
337+
[Test]
338+
public void Check_for_file_stream_result_and_check_invalid_stream_data()
339+
{
340+
var buffer = new byte[] { 1, 2 };
341+
var expectedStream = new MemoryStream(buffer);
342+
343+
var exception = Assert.Throws<ActionResultAssertionException>(() =>
344+
_controller
345+
.WithCallTo(c => c.EmptyStream())
346+
.ShouldRenderFileStream(expectedStream)
347+
);
348+
349+
var expected = string.Format("[{0}]", string.Join(", ", buffer));
350+
var actual = string.Format("[{0}]", string.Join(", ", ControllerResultTestController.EmptyStreamBuffer));
351+
var message = string.Format("Expected stream contents to be equal to {0}, but instead was given {1}.", expected, actual);
352+
353+
Assert.That(exception.Message, Is.EqualTo(message));
354+
}
355+
356+
[Test]
357+
public void Check_for_file_stream_result_with_populated_file_and_check_invalid_stream_data()
358+
{
359+
var buffer = new byte[] { 1, 2 };
360+
var expectedStream = new MemoryStream(buffer);
361+
362+
var exception = Assert.Throws<ActionResultAssertionException>(() =>
363+
_controller
364+
.WithCallTo(c => c.PopulatedStream())
365+
.ShouldRenderFileStream(expectedStream)
366+
);
367+
368+
var expected = string.Format("[{0}]", string.Join(", ", buffer));
369+
var actual = string.Format("[{0}]", string.Join(", ", ControllerResultTestController.EmptyStreamBuffer));
370+
var message = string.Format("Expected stream contents to be equal to {0}, but instead was given {1}.", expected, actual);
371+
372+
Assert.That(exception.Message, Is.EqualTo(message));
373+
}
374+
326375
#region File tests
327376

328377
[Test]
@@ -400,7 +449,7 @@ public void Check_for_file_content_result_and_check_invalid_binary_content_and_c
400449
byte[] contents = { 1, 2 };
401450
const string contentType = "application/dummy";
402451

403-
var exception = Assert.Throws<ActionResultAssertionException>(() =>
452+
var exception = Assert.Throws<ActionResultAssertionException>(() =>
404453
_controller.WithCallTo(c => c.BinaryFile()).ShouldRenderFileContents(contents, contentType));
405454

406455
// Assert that the content type validation occurs before that of the actual contents.

TestStack.FluentMVCTesting.Tests/TestControllers/ControllerResultTestController.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ class ControllerResultTestController : Controller
1818
public const string FileName = "NamedFile";
1919
public static byte[] BinaryFileContents = { 1 };
2020
public static string TextualFileContents = "textual content";
21+
22+
public static readonly byte[] EmptyStreamBuffer = { };
23+
public static readonly byte[] PopulatedStreamBuffer = { 1 };
24+
public static readonly Stream EmptyStreamContents = new MemoryStream(EmptyStreamBuffer);
25+
public static readonly Stream PopulatedStreamContents = new MemoryStream(PopulatedStreamBuffer);
26+
2127
#endregion
2228

2329
#region Empty, Null and Random Results
@@ -180,8 +186,12 @@ public ActionResult TextualFile(Encoding encoding)
180186

181187
public ActionResult EmptyStream()
182188
{
183-
var content = new MemoryStream();
184-
return File(content, FileContentType);
189+
return File(EmptyStreamContents, FileContentType);
190+
}
191+
192+
public ActionResult PopulatedStream()
193+
{
194+
return File(PopulatedStreamContents, FileContentType);
185195
}
186196

187197
public ActionResult EmptyFilePath()

TestStack.FluentMvcTesting/ControllerResultTest.cs

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.IO;
23
using System.Linq;
34
using System.Linq.Expressions;
45
using System.Net;
@@ -214,10 +215,36 @@ public ViewResultTest ShouldRenderDefaultPartialView()
214215

215216
#endregion
216217

217-
public FileStreamResult ShouldRenderFileStream()
218+
public FileStreamResult ShouldRenderFileStream(Stream stream = null)
218219
{
219220
ValidateActionReturnType<FileStreamResult>();
220-
return (FileStreamResult) _actionResult;
221+
222+
var fileResult = (FileStreamResult)_actionResult;
223+
224+
if (stream != null)
225+
{
226+
byte[] expected = ConvertStreamToArray(stream);
227+
byte[] actual = ConvertStreamToArray(fileResult.FileStream);
228+
229+
if (!expected.SequenceEqual(actual))
230+
{
231+
throw new ActionResultAssertionException(string.Format(
232+
"Expected stream contents to be equal to [{0}], but instead was given [{1}].",
233+
string.Join(", ", expected),
234+
string.Join(", ", actual)));
235+
}
236+
}
237+
238+
return fileResult;
239+
}
240+
241+
private static byte[] ConvertStreamToArray(Stream stream)
242+
{
243+
using (var memoryStream = new MemoryStream())
244+
{
245+
stream.CopyTo(memoryStream);
246+
return memoryStream.ToArray();
247+
}
221248
}
222249

223250
#region File Results

0 commit comments

Comments
 (0)