diff --git a/TestStack.FluentMVCTesting.Tests/ControllerResultTestTests.cs b/TestStack.FluentMVCTesting.Tests/ControllerResultTestTests.cs index 05bd8ba..fdf266c 100644 --- a/TestStack.FluentMVCTesting.Tests/ControllerResultTestTests.cs +++ b/TestStack.FluentMVCTesting.Tests/ControllerResultTestTests.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Linq; using System.Linq.Expressions; using System.Net; using System.Web.Mvc; @@ -27,6 +28,9 @@ class ControllerResultTestShould ReturnType(t => t.ShouldRenderFile()), ReturnType(t => t.ShouldRenderFile("")), ReturnType(t => t.ShouldRenderFileStream()), + ReturnType(t => t.ShouldRenderFileContents()), + ReturnType(t => t.ShouldRenderFileContents(new byte[0])), + ReturnType(t => t.ShouldRenderFileContents(new byte[0], "")), ReturnType(t => t.ShouldRenderFileStream("")), ReturnType(t => t.ShouldRenderFilePath()), ReturnType(t => t.ShouldRenderFilePath("")), @@ -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(() => + _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(() => + _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(() => + _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() { @@ -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(() => + _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 diff --git a/TestStack.FluentMVCTesting.Tests/TestControllers/ControllerResultTestController.cs b/TestStack.FluentMVCTesting.Tests/TestControllers/ControllerResultTestController.cs index 8a203cf..e8d1252 100644 --- a/TestStack.FluentMVCTesting.Tests/TestControllers/ControllerResultTestController.cs +++ b/TestStack.FluentMVCTesting.Tests/TestControllers/ControllerResultTestController.cs @@ -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 @@ -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(); diff --git a/TestStack.FluentMvcTesting/ControllerResultTest.cs b/TestStack.FluentMvcTesting/ControllerResultTest.cs index 1b66084..3af48f9 100644 --- a/TestStack.FluentMvcTesting/ControllerResultTest.cs +++ b/TestStack.FluentMvcTesting/ControllerResultTest.cs @@ -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 { @@ -228,6 +231,29 @@ public FileResult ShouldRenderAnyFile(string contentType = null) return fileResult; } + public FileContentResult ShouldRenderFileContents(byte[] contents = null, string contentType = null) + { + ValidateActionReturnType(); + + 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(); @@ -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;