Skip to content

Commit ba97591

Browse files
committed
Added support for checking a file stream result's content type.
1 parent 08e275b commit ba97591

File tree

2 files changed

+53
-2
lines changed

2 files changed

+53
-2
lines changed

TestStack.FluentMVCTesting.Tests/ControllerResultTestTests.cs

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ class ControllerResultTestShould
4242
ReturnType<FilePathResult>(t => t.ShouldRenderFilePath("", "")),
4343
ReturnType<FileStreamResult>(t => t.ShouldRenderFileStream()),
4444
ReturnType<FileStreamResult>(t => t.ShouldRenderFileStream(new MemoryStream())),
45+
ReturnType<FileStreamResult>(t => t.ShouldRenderFileStream(contentType: "")),
46+
ReturnType<FileStreamResult>(t => t.ShouldRenderFileStream(new MemoryStream(), "")),
4547
ReturnType<FileResult>(t => t.ShouldRenderAnyFile()),
4648
ReturnType<HttpStatusCodeResult>(t => t.ShouldGiveHttpStatus()),
4749
ReturnType<JsonResult>(t => t.ShouldReturnJson()),
@@ -366,12 +368,53 @@ public void Check_for_file_stream_result_with_populated_file_and_check_invalid_s
366368
);
367369

368370
var expected = string.Format("[{0}]", string.Join(", ", buffer));
369-
var actual = string.Format("[{0}]", string.Join(", ", ControllerResultTestController.EmptyStreamBuffer));
371+
var actual = string.Format("[{0}]", string.Join(", ", ControllerResultTestController.PopulatedStreamBuffer));
370372
var message = string.Format("Expected stream contents to be equal to {0}, but instead was given {1}.", expected, actual);
371373

372374
Assert.That(exception.Message, Is.EqualTo(message));
373375
}
374376

377+
[Test]
378+
public void Check_for_file_stream_result_and_check_content_type()
379+
{
380+
_controller
381+
.WithCallTo(c => c.EmptyStream())
382+
.ShouldRenderFileStream(ControllerResultTestController.EmptyStreamContents,
383+
ControllerResultTestController.FileContentType);
384+
}
385+
386+
[Test]
387+
public void Check_for_file_stream_result_and_check_invalid_content_type()
388+
{
389+
const string contentType = "application/dummy";
390+
391+
var exception = Assert.Throws<ActionResultAssertionException>(() =>
392+
_controller
393+
.WithCallTo(c => c.EmptyStream())
394+
.ShouldRenderFileStream(ControllerResultTestController.EmptyStreamContents, contentType));
395+
396+
var message = string.Format(
397+
"Expected stream to be of content type '{0}', but instead was given '{1}'.", contentType, ControllerResultTestController.FileContentType);
398+
399+
Assert.That(exception.Message, Is.EqualTo(message));
400+
}
401+
402+
[Test]
403+
public void Check_for_file_stream_result_and_check_invalid_stream_data_and_check_invalid_content_type()
404+
{
405+
var buffer = new byte[] { 1, 2 };
406+
var expectedStream = new MemoryStream(buffer);
407+
const string contentType = "application/dummy";
408+
409+
var exception = Assert.Throws<ActionResultAssertionException>(() =>
410+
_controller
411+
.WithCallTo(c => c.EmptyStream())
412+
.ShouldRenderFileStream(expectedStream, contentType));
413+
414+
// Assert that the content type validation occurs before that of the actual contents.
415+
Assert.That(exception.Message.Contains("content type"));
416+
}
417+
375418
#region File tests
376419

377420
[Test]

TestStack.FluentMvcTesting/ControllerResultTest.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,12 +215,20 @@ public ViewResultTest ShouldRenderDefaultPartialView()
215215

216216
#endregion
217217

218-
public FileStreamResult ShouldRenderFileStream(Stream stream = null)
218+
public FileStreamResult ShouldRenderFileStream(Stream stream = null, string contentType = null)
219219
{
220220
ValidateActionReturnType<FileStreamResult>();
221221

222222
var fileResult = (FileStreamResult)_actionResult;
223223

224+
if (contentType != null && fileResult.ContentType != contentType)
225+
{
226+
throw new ActionResultAssertionException(string.Format(
227+
"Expected stream to be of content type '{0}', but instead was given '{1}'.",
228+
contentType,
229+
fileResult.ContentType));
230+
}
231+
224232
if (stream != null)
225233
{
226234
byte[] expected = ConvertStreamToArray(stream);

0 commit comments

Comments
 (0)