Skip to content

Commit adf9091

Browse files
committed
Added support for checking a file stream result's binary contents.
1 parent ba97591 commit adf9091

File tree

2 files changed

+79
-0
lines changed

2 files changed

+79
-0
lines changed

TestStack.FluentMVCTesting.Tests/ControllerResultTestTests.cs

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ class ControllerResultTestShould
4444
ReturnType<FileStreamResult>(t => t.ShouldRenderFileStream(new MemoryStream())),
4545
ReturnType<FileStreamResult>(t => t.ShouldRenderFileStream(contentType: "")),
4646
ReturnType<FileStreamResult>(t => t.ShouldRenderFileStream(new MemoryStream(), "")),
47+
ReturnType<FileStreamResult>(t => t.ShouldRenderFileStream(new byte[0])),
48+
ReturnType<FileStreamResult>(t => t.ShouldRenderFileStream(new byte[0], "")),
4749
ReturnType<FileResult>(t => t.ShouldRenderAnyFile()),
4850
ReturnType<HttpStatusCodeResult>(t => t.ShouldGiveHttpStatus()),
4951
ReturnType<JsonResult>(t => t.ShouldReturnJson()),
@@ -415,6 +417,77 @@ public void Check_for_file_stream_result_and_check_invalid_stream_data_and_check
415417
Assert.That(exception.Message.Contains("content type"));
416418
}
417419

420+
[Test]
421+
public void Check_for_file_stream_result_and_check_binary_contents()
422+
{
423+
_controller
424+
.WithCallTo(c => c.PopulatedStream())
425+
.ShouldRenderFileStream(ControllerResultTestController.PopulatedStreamBuffer);
426+
}
427+
428+
[Test]
429+
public void Check_for_file_stream_result_and_check_invalid_binary_contents()
430+
{
431+
var contents = new byte[] { 1, 2 };
432+
433+
var exception = Assert.Throws<ActionResultAssertionException>(() =>
434+
_controller
435+
.WithCallTo(c => c.PopulatedStream())
436+
.ShouldRenderFileStream(contents)
437+
);
438+
439+
var expected = string.Format("[{0}]", string.Join(", ", contents));
440+
var actual = string.Format("[{0}]", string.Join(", ", ControllerResultTestController.PopulatedStreamBuffer));
441+
var message = string.Format("Expected stream contents to be equal to {0}, but instead was given {1}.", expected, actual);
442+
443+
Assert.That(exception.Message, Is.EqualTo(message));
444+
}
445+
446+
[Test]
447+
public void Check_for_file_stream_result_and_check_binary_contents_and_check_content_type()
448+
{
449+
_controller
450+
.WithCallTo(c => c.PopulatedStream())
451+
.ShouldRenderFileStream(
452+
ControllerResultTestController.PopulatedStreamBuffer,
453+
ControllerResultTestController.FileContentType);
454+
}
455+
456+
[Test]
457+
public void Check_for_file_stream_result_and_check_binary_contents_and_check_invalid_content_type()
458+
{
459+
const string contentType = "application/dummy";
460+
461+
var exception = Assert.Throws<ActionResultAssertionException>(() =>
462+
_controller
463+
.WithCallTo(c => c.EmptyStream())
464+
.ShouldRenderFileStream(
465+
ControllerResultTestController.PopulatedStreamBuffer,
466+
contentType)
467+
);
468+
469+
var message =
470+
string.Format("Expected stream to be of content type '{0}', but instead was given '{1}'.",
471+
contentType, ControllerResultTestController.FileContentType);
472+
473+
Assert.That(exception.Message, Is.EqualTo(message));
474+
}
475+
476+
[Test]
477+
public void Check_for_file_stream_result_and_check_invalid_binary_contents_and_check_invalid_content_type()
478+
{
479+
var contents = new byte[] { 1, 2 };
480+
const string contentType = "application/dummy";
481+
482+
var exception = Assert.Throws<ActionResultAssertionException>(() =>
483+
_controller
484+
.WithCallTo(c => c.EmptyStream())
485+
.ShouldRenderFileStream(contents, contentType));
486+
487+
// Assert that the content type validation occurs before that of the actual contents.
488+
Assert.That(exception.Message.Contains("content type"));
489+
}
490+
418491
#region File tests
419492

420493
[Test]

TestStack.FluentMvcTesting/ControllerResultTest.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,11 @@ public ViewResultTest ShouldRenderDefaultPartialView()
215215

216216
#endregion
217217

218+
public FileStreamResult ShouldRenderFileStream(byte[] content, string contentType = null)
219+
{
220+
return ShouldRenderFileStream(new MemoryStream(content), contentType);
221+
}
222+
218223
public FileStreamResult ShouldRenderFileStream(Stream stream = null, string contentType = null)
219224
{
220225
ValidateActionReturnType<FileStreamResult>();
@@ -251,6 +256,7 @@ private static byte[] ConvertStreamToArray(Stream stream)
251256
using (var memoryStream = new MemoryStream())
252257
{
253258
stream.CopyTo(memoryStream);
259+
stream.Position = 0;
254260
return memoryStream.ToArray();
255261
}
256262
}

0 commit comments

Comments
 (0)