Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Added support for checking a file stream result's contents.
  • Loading branch information
AlexArchive committed Sep 1, 2014
commit 08e275b3697e6e8528064815ce96fba4634b74e6
51 changes: 50 additions & 1 deletion TestStack.FluentMVCTesting.Tests/ControllerResultTestTests.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Net;
using System.Security.Permissions;
using System.Web.Mvc;
using NUnit.Framework;
using TestStack.FluentMVCTesting.Tests.TestControllers;
Expand Down Expand Up @@ -39,6 +41,7 @@ class ControllerResultTestShould
ReturnType<FilePathResult>(t => t.ShouldRenderFilePath("")),
ReturnType<FilePathResult>(t => t.ShouldRenderFilePath("", "")),
ReturnType<FileStreamResult>(t => t.ShouldRenderFileStream()),
ReturnType<FileStreamResult>(t => t.ShouldRenderFileStream(new MemoryStream())),
ReturnType<FileResult>(t => t.ShouldRenderAnyFile()),
ReturnType<HttpStatusCodeResult>(t => t.ShouldGiveHttpStatus()),
ReturnType<JsonResult>(t => t.ShouldReturnJson()),
Expand Down Expand Up @@ -323,6 +326,52 @@ public void Check_for_file_stream_result()
.ShouldRenderFileStream();
}

[Test]
public void Check_for_file_stream_result_and_check_stream_data()
{
_controller
.WithCallTo(c => c.EmptyStream())
.ShouldRenderFileStream(ControllerResultTestController.EmptyStreamContents);
}

[Test]
public void Check_for_file_stream_result_and_check_invalid_stream_data()
{
var buffer = new byte[] { 1, 2 };
var expectedStream = new MemoryStream(buffer);

var exception = Assert.Throws<ActionResultAssertionException>(() =>
_controller
.WithCallTo(c => c.EmptyStream())
.ShouldRenderFileStream(expectedStream)
);

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

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

[Test]
public void Check_for_file_stream_result_with_populated_file_and_check_invalid_stream_data()
{
var buffer = new byte[] { 1, 2 };
var expectedStream = new MemoryStream(buffer);

var exception = Assert.Throws<ActionResultAssertionException>(() =>
_controller
.WithCallTo(c => c.PopulatedStream())
.ShouldRenderFileStream(expectedStream)
);

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

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

#region File tests

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

var exception = Assert.Throws<ActionResultAssertionException>(() =>
var exception = Assert.Throws<ActionResultAssertionException>(() =>
_controller.WithCallTo(c => c.BinaryFile()).ShouldRenderFileContents(contents, contentType));

// Assert that the content type validation occurs before that of the actual contents.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ class ControllerResultTestController : Controller
public const string FileName = "NamedFile";
public static byte[] BinaryFileContents = { 1 };
public static string TextualFileContents = "textual content";

public static readonly byte[] EmptyStreamBuffer = { };
public static readonly byte[] PopulatedStreamBuffer = { 1 };
public static readonly Stream EmptyStreamContents = new MemoryStream(EmptyStreamBuffer);
public static readonly Stream PopulatedStreamContents = new MemoryStream(PopulatedStreamBuffer);

#endregion

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

public ActionResult EmptyStream()
{
var content = new MemoryStream();
return File(content, FileContentType);
return File(EmptyStreamContents, FileContentType);
}

public ActionResult PopulatedStream()
{
return File(PopulatedStreamContents, FileContentType);
}

public ActionResult EmptyFilePath()
Expand Down
31 changes: 29 additions & 2 deletions TestStack.FluentMvcTesting/ControllerResultTest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Net;
Expand Down Expand Up @@ -214,10 +215,36 @@ public ViewResultTest ShouldRenderDefaultPartialView()

#endregion

public FileStreamResult ShouldRenderFileStream()
public FileStreamResult ShouldRenderFileStream(Stream stream = null)
{
ValidateActionReturnType<FileStreamResult>();
return (FileStreamResult) _actionResult;

var fileResult = (FileStreamResult)_actionResult;

if (stream != null)
{
byte[] expected = ConvertStreamToArray(stream);
byte[] actual = ConvertStreamToArray(fileResult.FileStream);

if (!expected.SequenceEqual(actual))
{
throw new ActionResultAssertionException(string.Format(
"Expected stream contents to be equal to [{0}], but instead was given [{1}].",
string.Join(", ", expected),
string.Join(", ", actual)));
}
}

return fileResult;
}

private static byte[] ConvertStreamToArray(Stream stream)
{
using (var memoryStream = new MemoryStream())
{
stream.CopyTo(memoryStream);
return memoryStream.ToArray();
}
}

#region File Results
Expand Down