Skip to content

Commit

Permalink
Merge pull request #23 from ByteBlast/master
Browse files Browse the repository at this point in the history
Implemented ShouldRenderFileContents and deprecated ShouldRenderFile
  • Loading branch information
robdmoore committed Aug 24, 2014
2 parents 9154051 + c25d2e7 commit 12747ff
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 4 deletions.
72 changes: 72 additions & 0 deletions TestStack.FluentMVCTesting.Tests/ControllerResultTestTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Net;
using System.Web.Mvc;
Expand Down Expand Up @@ -27,6 +28,9 @@ class ControllerResultTestShould
ReturnType<FileContentResult>(t => t.ShouldRenderFile()),
ReturnType<FileContentResult>(t => t.ShouldRenderFile("")),
ReturnType<FileStreamResult>(t => t.ShouldRenderFileStream()),
ReturnType<FileContentResult>(t => t.ShouldRenderFileContents()),
ReturnType<FileContentResult>(t => t.ShouldRenderFileContents(new byte[0])),
ReturnType<FileContentResult>(t => t.ShouldRenderFileContents(new byte[0], "")),
ReturnType<FileStreamResult>(t => t.ShouldRenderFileStream("")),
ReturnType<FilePathResult>(t => t.ShouldRenderFilePath()),
ReturnType<FilePathResult>(t => t.ShouldRenderFilePath("")),
Expand Down Expand Up @@ -336,6 +340,61 @@ public void Check_for_any_file_result_and_check_invalid_content_type()
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()
{
_controller.WithCallTo(c => c.EmptyFile()).ShouldRenderFileContents();
}

[Test]
public void Check_for_file_content_result_and_check_binary_content()
{
_controller.WithCallTo(c => c.File()).ShouldRenderFileContents(ControllerResultTestController.FileContents);
}

[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));

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));
}

[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);
}

[Test]
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));

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_binary_content_and_check_invalid_content_type()
{
byte[] contents = { 1, 2 };
const string contentType = "application/dummy";

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

// When supplied with both an invalid content type and invalid content, test the content type first.
Assert.That(exception.Message.Contains("content type"));
}

[Test]
public void Check_for_file_result()
{
Expand Down Expand Up @@ -411,6 +470,19 @@ public void Check_for_file_path_result_and_check_file_name_and_check_invalid_con
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_path_result_and_check_invalid_file_name_and_check_invalid_content_type()
{
const string contentType = "application/dummy";
const string name = "dummy";

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(exception.Message.Contains("content type"));
}

#endregion

#region HTTP Status tests
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class ControllerResultTestController : Controller
public const int Code = 403;
public const string JsonValue = "json";
public const string FileName = "NamedFile";
public static byte[] FileContents = { 1 };
#endregion

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

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

public ActionResult EmptyStream()
{
var content = new MemoryStream();
Expand Down
34 changes: 30 additions & 4 deletions TestStack.FluentMvcTesting/ControllerResultTest.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
using System;
using System.Linq;
using System.Linq.Expressions;
using System.Net;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.UI.WebControls;

namespace TestStack.FluentMVCTesting
{
Expand Down Expand Up @@ -228,6 +231,29 @@ public FileResult ShouldRenderAnyFile(string contentType = null)
return fileResult;
}

public FileContentResult ShouldRenderFileContents(byte[] contents = null, string contentType = 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 (contents != null && !fileResult.FileContents.SequenceEqual(contents))
{
throw new ActionResultAssertionException(string.Format(
"Expected file contents to be equal to [{0}], but instead was given [{1}].",
string.Join(", ", contents),
string.Join(", ", fileResult.FileContents)));
}

return fileResult;
}

[Obsolete("Obsolete: Use ShouldRenderFileContents instead.")]
public FileContentResult ShouldRenderFile(string contentType = null)
{
ValidateActionReturnType<FileContentResult>();
Expand Down Expand Up @@ -262,14 +288,14 @@ public FilePathResult ShouldRenderFilePath(string fileName = null, string conten

var fileResult = (FilePathResult)_actionResult;

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

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

return fileResult;
Expand Down

0 comments on commit 12747ff

Please sign in to comment.