Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
92 changes: 84 additions & 8 deletions TestStack.FluentMVCTesting.Tests/ControllerResultTestTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
using System.Web.Mvc;
using NUnit.Framework;
using TestStack.FluentMVCTesting.Tests.TestControllers;
using System.Text;


namespace TestStack.FluentMVCTesting.Tests
{
Expand All @@ -31,6 +33,9 @@ class ControllerResultTestShould
ReturnType<FileContentResult>(t => t.ShouldRenderFileContents()),
ReturnType<FileContentResult>(t => t.ShouldRenderFileContents(new byte[0])),
ReturnType<FileContentResult>(t => t.ShouldRenderFileContents(new byte[0], "")),
ReturnType<FileContentResult>(t => t.ShouldRenderFileContents("")),
ReturnType<FileContentResult>(t => t.ShouldRenderFileContents("", "")),
ReturnType<FileContentResult>(t => t.ShouldRenderFileContents("", "", Encoding.UTF8)),
ReturnType<FileStreamResult>(t => t.ShouldRenderFileStream("")),
ReturnType<FilePathResult>(t => t.ShouldRenderFilePath()),
ReturnType<FilePathResult>(t => t.ShouldRenderFilePath("")),
Expand Down Expand Up @@ -349,26 +354,26 @@ public void Check_for_file_content_result()
[Test]
public void Check_for_file_content_result_and_check_binary_content()
{
_controller.WithCallTo(c => c.File()).ShouldRenderFileContents(ControllerResultTestController.FileContents);
_controller.WithCallTo(c => c.BinaryFile()).ShouldRenderFileContents(ControllerResultTestController.BinaryFileContents);
}

[Test]
public void Check_for_file_content_result_and_check_invalid_binary_content()
{
byte[] contents = { 1, 2 };
var exception = Assert.Throws<ActionResultAssertionException>(() =>
_controller.WithCallTo(c => c.File()).ShouldRenderFileContents(contents));
_controller.WithCallTo(c => c.BinaryFile()).ShouldRenderFileContents(contents));

Assert.True(exception.Message.StartsWith("Expected file contents to be equal to ["));
Assert.True(exception.Message.EndsWith("]."));
Assert.True(string.Join(", ", contents).All(exception.Message.Contains));
Assert.True(string.Join(", ", ControllerResultTestController.FileContents).All(exception.Message.Contains));
Assert.True(string.Join(", ", ControllerResultTestController.BinaryFileContents).All(exception.Message.Contains));
}

[Test]
public void Check_for_file_content_result_and_check_binary_content_and_check_content_type()
{
_controller.WithCallTo(c => c.File()).ShouldRenderFileContents(ControllerResultTestController.FileContents, ControllerResultTestController.FileContentType);
_controller.WithCallTo(c => c.BinaryFile()).ShouldRenderFileContents(ControllerResultTestController.BinaryFileContents, ControllerResultTestController.FileContentType);
}

[Test]
Expand All @@ -377,7 +382,7 @@ public void Check_for_file_content_result_and_check_invalid_content_type()
const string contentType = "application/dummy";

var exception = Assert.Throws<ActionResultAssertionException>(() =>
_controller.WithCallTo(c => c.File()).ShouldRenderFileContents(ControllerResultTestController.FileContents, contentType));
_controller.WithCallTo(c => c.BinaryFile()).ShouldRenderFileContents(ControllerResultTestController.BinaryFileContents, contentType));

Assert.That(exception.Message, Is.EqualTo(string.Format("Expected file to be of content type '{0}', but instead was given '{1}'.", contentType, ControllerResultTestController.FileContentType)));
}
Expand All @@ -389,12 +394,83 @@ public void Check_for_file_content_result_and_check_invalid_binary_content_and_c
const string contentType = "application/dummy";

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

// Assert that the content type validation occurs before that of the actual contents.
Assert.That(exception.Message.Contains("content type"));
}

[Test]
public void Check_for_file_content_result_and_check_textual_contents()
{
_controller.WithCallTo(c => c.TextualFile()).ShouldRenderFileContents(ControllerResultTestController.TextualFileContents);
}

[Test]
public void Check_for_file_content_result_and_check_invalid_textual_contents()
{
const string contents = "dummy contents";

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

Assert.That(exception.Message, Is.EqualTo(string.Format("Expected file contents to be \"{0}\", but instead was \"{1}\".", contents, ControllerResultTestController.TextualFileContents)));
}

[Test]
public void Check_for_file_content_result_and_check_textual_content_and_check_content_result()
{
_controller.WithCallTo(c => c.TextualFile()).ShouldRenderFileContents(ControllerResultTestController.TextualFileContents, ControllerResultTestController.FileContentType);
}

[Test]
public void Check_for_file_content_result_and_check_textual_content_and_check_invalid_content_typet()
{
const string contentType = "application/dummy";

var exception = Assert.Throws<ActionResultAssertionException>(() =>
_controller.WithCallTo(c => c.TextualFile()).ShouldRenderFileContents(ControllerResultTestController.TextualFileContents, contentType));

Assert.That(exception.Message, Is.EqualTo(string.Format("Expected file to be of content type '{0}', but instead was given '{1}'.", contentType, ControllerResultTestController.FileContentType)));
}

[Test]
public void Check_for_file_content_result_and_check_invalid_textual_content_and_check_invalid_content_type()
{
const string contents = "dummy content";
const string contentType = "application/dummy";

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

// When supplied with both an invalid content type and invalid content, test the content type first.
// Assert that the content type validation occurs before that of the actual contents.
Assert.That(exception.Message.Contains("content type"));
}

[Test]
public void Check_for_file_content_result_and_check_textual_content_using_given_char_encoding()
{
var encoding = Encoding.BigEndianUnicode;

_controller.WithCallTo(c => c.TextualFile(encoding))
.ShouldRenderFileContents(ControllerResultTestController.TextualFileContents, encoding: encoding);
}

[Test]
public void Check_for_file_content_result_and_check_textual_content_using_given_char_encoding_and_check_content_type()
{
var encoding = Encoding.BigEndianUnicode;

_controller.WithCallTo(c => c.TextualFile(encoding)).ShouldRenderFileContents(ControllerResultTestController.TextualFileContents, ControllerResultTestController.FileContentType, encoding);
}

[Test]
public void Check_for_file_content_result_and_check_textual_content_using_invalid_given_char_encoding()
{
Assert.Throws<ActionResultAssertionException>(() =>
_controller.WithCallTo(c => c.TextualFile()).ShouldRenderFileContents(ControllerResultTestController.TextualFileContents, ControllerResultTestController.FileContentType, Encoding.BigEndianUnicode));
}

[Test]
public void Check_for_file_result()
{
Expand Down Expand Up @@ -479,7 +555,7 @@ public void Check_for_file_path_result_and_check_invalid_file_name_and_check_inv
var exception = Assert.Throws<ActionResultAssertionException>(() =>
_controller.WithCallTo(c => c.EmptyFilePath()).ShouldRenderFilePath(name, contentType));

// When supplied with both an invalid content type and invalid file name, test the content type first.
// Assert that the content type validation occurs before that of the file name.
Assert.That(exception.Message.Contains("content type"));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.IO;
using System.Text;
using System.Web.Mvc;

namespace TestStack.FluentMVCTesting.Tests.TestControllers
Expand All @@ -15,7 +16,8 @@ class ControllerResultTestController : Controller
public const int Code = 403;
public const string JsonValue = "json";
public const string FileName = "NamedFile";
public static byte[] FileContents = { 1 };
public static byte[] BinaryFileContents = { 1 };
public static string TextualFileContents = "textual content";
#endregion

#region Empty, Null and Random Results
Expand Down Expand Up @@ -160,9 +162,20 @@ public ActionResult EmptyFile()
return File(content, FileContentType);
}

public ActionResult File()
public ActionResult BinaryFile()
{
return File(FileContents, FileContentType);
return File(BinaryFileContents, FileContentType);
}

public ActionResult TextualFile()
{
return TextualFile(Encoding.UTF8);
}

public ActionResult TextualFile(Encoding encoding)
{
var encodedContents = encoding.GetBytes(TextualFileContents);
return File(encodedContents, FileContentType);
}

public ActionResult EmptyStream()
Expand Down
28 changes: 26 additions & 2 deletions TestStack.FluentMvcTesting/ControllerResultTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
using System.Linq.Expressions;
using System.Net;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.RegularExpressions;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.UI.WebControls;

namespace TestStack.FluentMVCTesting
{
Expand Down Expand Up @@ -253,6 +252,31 @@ public FileContentResult ShouldRenderFileContents(byte[] contents = null, string
return fileResult;
}

public FileContentResult ShouldRenderFileContents(string contents, string contentType = null, Encoding encoding = null)
{
ValidateActionReturnType<FileContentResult>();

var fileResult = (FileContentResult)_actionResult;

if (contentType != null && fileResult.ContentType != contentType)
{
throw new ActionResultAssertionException(
string.Format("Expected file to be of content type '{0}', but instead was given '{1}'.", contentType,
fileResult.ContentType));
}

if (encoding == null)
encoding = Encoding.UTF8;

var reconstitutedText = encoding.GetString(fileResult.FileContents);
if (contents != reconstitutedText)
{
throw new ActionResultAssertionException(string.Format("Expected file contents to be \"{0}\", but instead was \"{1}\".", contents, reconstitutedText));
}

return fileResult;
}

[Obsolete("Obsolete: Use ShouldRenderFileContents instead.")]
public FileContentResult ShouldRenderFile(string contentType = null)
{
Expand Down