From c659b4afa989bf5831bef0149d5198567c4bcc36 Mon Sep 17 00:00:00 2001 From: ByteBlast Date: Mon, 25 Aug 2014 14:21:42 +0100 Subject: [PATCH 1/4] Added support for checking a file content result's textual contents. --- .../ControllerResultTestTests.cs | 62 +++++++++++++++++-- .../ControllerResultTestController.cs | 14 ++++- .../ControllerResultTest.cs | 21 +++++++ 3 files changed, 88 insertions(+), 9 deletions(-) diff --git a/TestStack.FluentMVCTesting.Tests/ControllerResultTestTests.cs b/TestStack.FluentMVCTesting.Tests/ControllerResultTestTests.cs index fdf266c..d085fa6 100644 --- a/TestStack.FluentMVCTesting.Tests/ControllerResultTestTests.cs +++ b/TestStack.FluentMVCTesting.Tests/ControllerResultTestTests.cs @@ -3,6 +3,7 @@ using System.Linq; using System.Linq.Expressions; using System.Net; +using System.Text.RegularExpressions; using System.Web.Mvc; using NUnit.Framework; using TestStack.FluentMVCTesting.Tests.TestControllers; @@ -31,6 +32,8 @@ 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.ShouldRenderFileStream("")), ReturnType(t => t.ShouldRenderFilePath()), ReturnType(t => t.ShouldRenderFilePath("")), @@ -349,7 +352,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 +360,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 +380,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,7 +392,54 @@ 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)); + + // 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_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_result() + { + 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_result() + { + 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(exception.Message.Contains("content type")); diff --git a/TestStack.FluentMVCTesting.Tests/TestControllers/ControllerResultTestController.cs b/TestStack.FluentMVCTesting.Tests/TestControllers/ControllerResultTestController.cs index e8d1252..6e1f641 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,15 @@ public ActionResult EmptyFile() return File(content, FileContentType); } - public ActionResult File() + public ActionResult BinaryFile() { - return File(FileContents, FileContentType); + return File(BinaryFileContents, FileContentType); + } + + public ActionResult TextualFile() + { + var encodedContents = Encoding.UTF8.GetBytes(TextualFileContents); + return File(encodedContents, FileContentType); } public ActionResult EmptyStream() diff --git a/TestStack.FluentMvcTesting/ControllerResultTest.cs b/TestStack.FluentMvcTesting/ControllerResultTest.cs index 3af48f9..f697350 100644 --- a/TestStack.FluentMvcTesting/ControllerResultTest.cs +++ b/TestStack.FluentMvcTesting/ControllerResultTest.cs @@ -4,6 +4,7 @@ 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; @@ -215,6 +216,26 @@ public ViewResultTest ShouldRenderDefaultPartialView() #endregion + public FileContentResult ShouldRenderFileContents(string contents, 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)); + } + + var reconstitutedText = Encoding.UTF8.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; + } + #region File Results public FileResult ShouldRenderAnyFile(string contentType = null) From fb51fb5400c2ee79d296a7e221abbf766c5c3f41 Mon Sep 17 00:00:00 2001 From: ByteBlast Date: Mon, 25 Aug 2014 14:23:18 +0100 Subject: [PATCH 2/4] Tweaked assertion comment. --- .../ControllerResultTestTests.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/TestStack.FluentMVCTesting.Tests/ControllerResultTestTests.cs b/TestStack.FluentMVCTesting.Tests/ControllerResultTestTests.cs index d085fa6..5812c97 100644 --- a/TestStack.FluentMVCTesting.Tests/ControllerResultTestTests.cs +++ b/TestStack.FluentMVCTesting.Tests/ControllerResultTestTests.cs @@ -313,7 +313,7 @@ public void Check_for_invalid_partial_name() Assert.That(exception.Message, Is.EqualTo(string.Format("Expected result view to be '{0}', but instead was given '{1}'.", ControllerResultTestController.PartialName, ControllerResultTestController.RandomViewName))); } #endregion - + #region File tests [Test] @@ -394,7 +394,7 @@ public void Check_for_file_content_result_and_check_invalid_binary_content_and_c var exception = Assert.Throws(() => _controller.WithCallTo(c => c.BinaryFile()).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")); } @@ -441,7 +441,7 @@ public void Check_for_file_content_result_and_check_invalid_textual_content_and_ 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")); } @@ -529,7 +529,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")); } From e17744c896dd5850d77ca0a210b3ac0adf24bffe Mon Sep 17 00:00:00 2001 From: ByteBlast Date: Mon, 25 Aug 2014 14:44:53 +0100 Subject: [PATCH 3/4] Added support for checking a file content result's textual contents using a specific encoding. --- .../ControllerResultTestTests.cs | 33 ++++++++++++++++++- .../ControllerResultTestController.cs | 7 +++- .../ControllerResultTest.cs | 5 +-- 3 files changed, 41 insertions(+), 4 deletions(-) diff --git a/TestStack.FluentMVCTesting.Tests/ControllerResultTestTests.cs b/TestStack.FluentMVCTesting.Tests/ControllerResultTestTests.cs index 5812c97..40c66a4 100644 --- a/TestStack.FluentMVCTesting.Tests/ControllerResultTestTests.cs +++ b/TestStack.FluentMVCTesting.Tests/ControllerResultTestTests.cs @@ -7,6 +7,8 @@ using System.Web.Mvc; using NUnit.Framework; using TestStack.FluentMVCTesting.Tests.TestControllers; +using System.Text; + namespace TestStack.FluentMVCTesting.Tests { @@ -34,6 +36,7 @@ class ControllerResultTestShould 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("")), @@ -313,7 +316,9 @@ public void Check_for_invalid_partial_name() Assert.That(exception.Message, Is.EqualTo(string.Format("Expected result view to be '{0}', but instead was given '{1}'.", ControllerResultTestController.PartialName, ControllerResultTestController.RandomViewName))); } #endregion - + + + #region File tests [Test] @@ -445,6 +450,32 @@ public void Check_for_file_content_result_and_check_invalid_textual_content_and_ 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() { diff --git a/TestStack.FluentMVCTesting.Tests/TestControllers/ControllerResultTestController.cs b/TestStack.FluentMVCTesting.Tests/TestControllers/ControllerResultTestController.cs index 6e1f641..6a8b61a 100644 --- a/TestStack.FluentMVCTesting.Tests/TestControllers/ControllerResultTestController.cs +++ b/TestStack.FluentMVCTesting.Tests/TestControllers/ControllerResultTestController.cs @@ -169,7 +169,12 @@ public ActionResult BinaryFile() public ActionResult TextualFile() { - var encodedContents = Encoding.UTF8.GetBytes(TextualFileContents); + return TextualFile(Encoding.UTF8); + } + + public ActionResult TextualFile(Encoding encoding) + { + var encodedContents = encoding.GetBytes(TextualFileContents); return File(encodedContents, FileContentType); } diff --git a/TestStack.FluentMvcTesting/ControllerResultTest.cs b/TestStack.FluentMvcTesting/ControllerResultTest.cs index f697350..ecfb27b 100644 --- a/TestStack.FluentMvcTesting/ControllerResultTest.cs +++ b/TestStack.FluentMvcTesting/ControllerResultTest.cs @@ -216,7 +216,7 @@ public ViewResultTest ShouldRenderDefaultPartialView() #endregion - public FileContentResult ShouldRenderFileContents(string contents, string contentType = null) + public FileContentResult ShouldRenderFileContents(string contents, string contentType = null, Encoding encoding = null) { ValidateActionReturnType(); @@ -227,7 +227,8 @@ public FileContentResult ShouldRenderFileContents(string contents, string conten throw new ActionResultAssertionException(string.Format("Expected file to be of content type '{0}', but instead was given '{1}'.", contentType, fileResult.ContentType)); } - var reconstitutedText = Encoding.UTF8.GetString(fileResult.FileContents); + 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)); From 0b6d64095ba8fa65305fb4658dd78d475474575b Mon Sep 17 00:00:00 2001 From: ByteBlast Date: Sun, 31 Aug 2014 12:56:56 +0100 Subject: [PATCH 4/4] Readability tweaks. Removed superflous using directives. Moved test method into appropriate region. Indented if statement (as per code review.) Removed unintended whitespace between region. Corrected test name. Corrected errant test body formatting. --- .../ControllerResultTestTests.cs | 11 ++--- .../ControllerResultTest.cs | 48 ++++++++++--------- 2 files changed, 28 insertions(+), 31 deletions(-) diff --git a/TestStack.FluentMVCTesting.Tests/ControllerResultTestTests.cs b/TestStack.FluentMVCTesting.Tests/ControllerResultTestTests.cs index 40c66a4..a4e82db 100644 --- a/TestStack.FluentMVCTesting.Tests/ControllerResultTestTests.cs +++ b/TestStack.FluentMVCTesting.Tests/ControllerResultTestTests.cs @@ -3,7 +3,6 @@ using System.Linq; using System.Linq.Expressions; using System.Net; -using System.Text.RegularExpressions; using System.Web.Mvc; using NUnit.Framework; using TestStack.FluentMVCTesting.Tests.TestControllers; @@ -317,8 +316,6 @@ public void Check_for_invalid_partial_name() } #endregion - - #region File tests [Test] @@ -427,7 +424,7 @@ public void Check_for_file_content_result_and_check_textual_content_and_check_co } [Test] - public void Check_for_file_content_result_and_check_textual_content_and_check_invalid_content_result() + public void Check_for_file_content_result_and_check_textual_content_and_check_invalid_content_typet() { const string contentType = "application/dummy"; @@ -438,7 +435,7 @@ public void Check_for_file_content_result_and_check_textual_content_and_check_in } [Test] - public void Check_for_file_content_result_and_check_invalid_textual_content_and_check_invalid_content_result() + 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"; @@ -471,9 +468,7 @@ public void Check_for_file_content_result_and_check_textual_content_using_given_ 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)); + _controller.WithCallTo(c => c.TextualFile()).ShouldRenderFileContents(ControllerResultTestController.TextualFileContents, ControllerResultTestController.FileContentType, Encoding.BigEndianUnicode)); } [Test] diff --git a/TestStack.FluentMvcTesting/ControllerResultTest.cs b/TestStack.FluentMvcTesting/ControllerResultTest.cs index ecfb27b..ecaf91e 100644 --- a/TestStack.FluentMvcTesting/ControllerResultTest.cs +++ b/TestStack.FluentMvcTesting/ControllerResultTest.cs @@ -3,12 +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 { @@ -216,27 +214,6 @@ public ViewResultTest ShouldRenderDefaultPartialView() #endregion - 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; - } - #region File Results public FileResult ShouldRenderAnyFile(string contentType = null) @@ -275,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) {