diff --git a/TestStack.FluentMVCTesting.Tests/ControllerResultTestTests.cs b/TestStack.FluentMVCTesting.Tests/ControllerResultTestTests.cs index fdf266c..a4e82db 100644 --- a/TestStack.FluentMVCTesting.Tests/ControllerResultTestTests.cs +++ b/TestStack.FluentMVCTesting.Tests/ControllerResultTestTests.cs @@ -6,6 +6,8 @@ using System.Web.Mvc; using NUnit.Framework; using TestStack.FluentMVCTesting.Tests.TestControllers; +using System.Text; + namespace TestStack.FluentMVCTesting.Tests { @@ -31,6 +33,9 @@ class ControllerResultTestShould ReturnType(t => t.ShouldRenderFileContents()), ReturnType(t => t.ShouldRenderFileContents(new byte[0])), ReturnType(t => t.ShouldRenderFileContents(new byte[0], "")), + ReturnType(t => t.ShouldRenderFileContents("")), + ReturnType(t => t.ShouldRenderFileContents("", "")), + ReturnType(t => t.ShouldRenderFileContents("", "", Encoding.UTF8)), ReturnType(t => t.ShouldRenderFileStream("")), ReturnType(t => t.ShouldRenderFilePath()), ReturnType(t => t.ShouldRenderFilePath("")), @@ -349,7 +354,7 @@ 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] @@ -357,18 +362,18 @@ 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)); + _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] @@ -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(() => - _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))); } @@ -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(() => - _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(() => + _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(() => + _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(() => + _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(() => + _controller.WithCallTo(c => c.TextualFile()).ShouldRenderFileContents(ControllerResultTestController.TextualFileContents, ControllerResultTestController.FileContentType, Encoding.BigEndianUnicode)); + } + [Test] public void Check_for_file_result() { @@ -479,7 +555,7 @@ public void Check_for_file_path_result_and_check_invalid_file_name_and_check_inv 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 the content type validation occurs before that of the file name. Assert.That(exception.Message.Contains("content type")); } diff --git a/TestStack.FluentMVCTesting.Tests/TestControllers/ControllerResultTestController.cs b/TestStack.FluentMVCTesting.Tests/TestControllers/ControllerResultTestController.cs index e8d1252..6a8b61a 100644 --- a/TestStack.FluentMVCTesting.Tests/TestControllers/ControllerResultTestController.cs +++ b/TestStack.FluentMVCTesting.Tests/TestControllers/ControllerResultTestController.cs @@ -1,4 +1,5 @@ using System.IO; +using System.Text; using System.Web.Mvc; namespace TestStack.FluentMVCTesting.Tests.TestControllers @@ -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 @@ -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() diff --git a/TestStack.FluentMvcTesting/ControllerResultTest.cs b/TestStack.FluentMvcTesting/ControllerResultTest.cs index 3af48f9..ecaf91e 100644 --- a/TestStack.FluentMvcTesting/ControllerResultTest.cs +++ b/TestStack.FluentMvcTesting/ControllerResultTest.cs @@ -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 { @@ -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(); + + 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) {