From 907b1ebcf61127466b3bab56d9673e5fd5e2f537 Mon Sep 17 00:00:00 2001 From: ByteBlast Date: Thu, 21 Aug 2014 18:47:31 +0100 Subject: [PATCH 1/6] Added support for checking for a file content result. Partial implementation of #3. --- .../ControllerResultTestTests.cs | 7 +++++++ .../TestControllers/ControllerResultTestController.cs | 5 +++++ TestStack.FluentMvcTesting/ControllerResultTest.cs | 7 +++++++ 3 files changed, 19 insertions(+) diff --git a/TestStack.FluentMVCTesting.Tests/ControllerResultTestTests.cs b/TestStack.FluentMVCTesting.Tests/ControllerResultTestTests.cs index 05bd8ba..c423b3d 100644 --- a/TestStack.FluentMVCTesting.Tests/ControllerResultTestTests.cs +++ b/TestStack.FluentMVCTesting.Tests/ControllerResultTestTests.cs @@ -27,6 +27,7 @@ class ControllerResultTestShould ReturnType(t => t.ShouldRenderFile()), ReturnType(t => t.ShouldRenderFile("")), ReturnType(t => t.ShouldRenderFileStream()), + ReturnType(t => t.ShouldRenderFileContents()), ReturnType(t => t.ShouldRenderFileStream("")), ReturnType(t => t.ShouldRenderFilePath()), ReturnType(t => t.ShouldRenderFilePath("")), @@ -336,6 +337,12 @@ 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_result() { diff --git a/TestStack.FluentMVCTesting.Tests/TestControllers/ControllerResultTestController.cs b/TestStack.FluentMVCTesting.Tests/TestControllers/ControllerResultTestController.cs index 8a203cf..c7b9b0f 100644 --- a/TestStack.FluentMVCTesting.Tests/TestControllers/ControllerResultTestController.cs +++ b/TestStack.FluentMVCTesting.Tests/TestControllers/ControllerResultTestController.cs @@ -159,6 +159,11 @@ public ActionResult EmptyFile() return File(content, FileContentType); } + public ActionResult File() + { + return EmptyFile(); + } + public ActionResult EmptyStream() { var content = new MemoryStream(); diff --git a/TestStack.FluentMvcTesting/ControllerResultTest.cs b/TestStack.FluentMvcTesting/ControllerResultTest.cs index 1b66084..26fce53 100644 --- a/TestStack.FluentMvcTesting/ControllerResultTest.cs +++ b/TestStack.FluentMvcTesting/ControllerResultTest.cs @@ -2,6 +2,7 @@ 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; @@ -228,6 +229,12 @@ public FileResult ShouldRenderAnyFile(string contentType = null) return fileResult; } + public FileContentResult ShouldRenderFileContents() + { + ValidateActionReturnType(); + return (FileContentResult) _actionResult; + } + public FileContentResult ShouldRenderFile(string contentType = null) { ValidateActionReturnType(); From dbd746a8a5665683ed65da93aaee035200a2f175 Mon Sep 17 00:00:00 2001 From: ByteBlast Date: Thu, 21 Aug 2014 19:11:50 +0100 Subject: [PATCH 2/6] Added support for checking a file content result's binary contents. Partial implementation of #3 --- .../ControllerResultTestTests.cs | 21 +++++++++++++++++++ .../ControllerResultTestController.cs | 3 ++- .../ControllerResultTest.cs | 16 ++++++++++++-- 3 files changed, 37 insertions(+), 3 deletions(-) diff --git a/TestStack.FluentMVCTesting.Tests/ControllerResultTestTests.cs b/TestStack.FluentMVCTesting.Tests/ControllerResultTestTests.cs index c423b3d..9b86dd4 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; @@ -28,6 +29,7 @@ class ControllerResultTestShould ReturnType(t => t.ShouldRenderFile("")), ReturnType(t => t.ShouldRenderFileStream()), ReturnType(t => t.ShouldRenderFileContents()), + ReturnType(t => t.ShouldRenderFileContents(new byte[0])), ReturnType(t => t.ShouldRenderFileStream("")), ReturnType(t => t.ShouldRenderFilePath()), ReturnType(t => t.ShouldRenderFilePath("")), @@ -343,6 +345,25 @@ 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 ")); + 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_result() { diff --git a/TestStack.FluentMVCTesting.Tests/TestControllers/ControllerResultTestController.cs b/TestStack.FluentMVCTesting.Tests/TestControllers/ControllerResultTestController.cs index c7b9b0f..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 @@ -161,7 +162,7 @@ public ActionResult EmptyFile() public ActionResult File() { - return EmptyFile(); + return File(FileContents, FileContentType); } public ActionResult EmptyStream() diff --git a/TestStack.FluentMvcTesting/ControllerResultTest.cs b/TestStack.FluentMvcTesting/ControllerResultTest.cs index 26fce53..5dbe3a9 100644 --- a/TestStack.FluentMvcTesting/ControllerResultTest.cs +++ b/TestStack.FluentMvcTesting/ControllerResultTest.cs @@ -1,4 +1,5 @@ using System; +using System.Linq; using System.Linq.Expressions; using System.Net; using System.Reflection; @@ -229,10 +230,21 @@ public FileResult ShouldRenderAnyFile(string contentType = null) return fileResult; } - public FileContentResult ShouldRenderFileContents() + public FileContentResult ShouldRenderFileContents(byte[] contents = null) { ValidateActionReturnType(); - return (FileContentResult) _actionResult; + + var fileResult = (FileContentResult) _actionResult; + + 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; } public FileContentResult ShouldRenderFile(string contentType = null) From 45e1155690c71d754949343daa046de9da3af441 Mon Sep 17 00:00:00 2001 From: ByteBlast Date: Thu, 21 Aug 2014 19:35:07 +0100 Subject: [PATCH 3/6] Added support for checking a file content result's content type. Partial implementation of #3. --- .../ControllerResultTestTests.cs | 18 ++++++++++++++++++ .../ControllerResultTest.cs | 8 +++++++- 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/TestStack.FluentMVCTesting.Tests/ControllerResultTestTests.cs b/TestStack.FluentMVCTesting.Tests/ControllerResultTestTests.cs index 9b86dd4..844d4f1 100644 --- a/TestStack.FluentMVCTesting.Tests/ControllerResultTestTests.cs +++ b/TestStack.FluentMVCTesting.Tests/ControllerResultTestTests.cs @@ -30,6 +30,7 @@ class ControllerResultTestShould 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("")), @@ -364,6 +365,23 @@ public void Check_for_file_content_result_and_check_invalid_binary_content() 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_result() { diff --git a/TestStack.FluentMvcTesting/ControllerResultTest.cs b/TestStack.FluentMvcTesting/ControllerResultTest.cs index 5dbe3a9..d5f0e7a 100644 --- a/TestStack.FluentMvcTesting/ControllerResultTest.cs +++ b/TestStack.FluentMvcTesting/ControllerResultTest.cs @@ -7,6 +7,7 @@ using System.Text.RegularExpressions; using System.Web.Mvc; using System.Web.Routing; +using System.Web.UI.WebControls; namespace TestStack.FluentMVCTesting { @@ -230,7 +231,7 @@ public FileResult ShouldRenderAnyFile(string contentType = null) return fileResult; } - public FileContentResult ShouldRenderFileContents(byte[] contents = null) + public FileContentResult ShouldRenderFileContents(byte[] contents = null, string contentType = null) { ValidateActionReturnType(); @@ -244,6 +245,11 @@ public FileContentResult ShouldRenderFileContents(byte[] contents = null) string.Join(",", fileResult.FileContents))); } + 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)); + } + return fileResult; } From 8c2c2c7bc1e5e19ff49cf61ae2b10585ecbedbb4 Mon Sep 17 00:00:00 2001 From: ByteBlast Date: Thu, 21 Aug 2014 19:39:54 +0100 Subject: [PATCH 4/6] Deprecated ShouldRenderFile. Partial implementation of #3. --- TestStack.FluentMvcTesting/ControllerResultTest.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/TestStack.FluentMvcTesting/ControllerResultTest.cs b/TestStack.FluentMvcTesting/ControllerResultTest.cs index d5f0e7a..a691a9a 100644 --- a/TestStack.FluentMvcTesting/ControllerResultTest.cs +++ b/TestStack.FluentMvcTesting/ControllerResultTest.cs @@ -253,6 +253,7 @@ public FileContentResult ShouldRenderFileContents(byte[] contents = null, string return fileResult; } + [Obsolete("Obsolete: Use ShouldRenderFileContents instead.")] public FileContentResult ShouldRenderFile(string contentType = null) { ValidateActionReturnType(); From 2dd41eb021af56732c78d35346f9b573a1d95313 Mon Sep 17 00:00:00 2001 From: ByteBlast Date: Fri, 22 Aug 2014 01:55:06 +0100 Subject: [PATCH 5/6] Tweaked file contents error message formatting. --- .../ControllerResultTestTests.cs | 8 ++++---- TestStack.FluentMvcTesting/ControllerResultTest.cs | 6 +++--- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/TestStack.FluentMVCTesting.Tests/ControllerResultTestTests.cs b/TestStack.FluentMVCTesting.Tests/ControllerResultTestTests.cs index 844d4f1..fd5a519 100644 --- a/TestStack.FluentMVCTesting.Tests/ControllerResultTestTests.cs +++ b/TestStack.FluentMVCTesting.Tests/ControllerResultTestTests.cs @@ -359,10 +359,10 @@ public void Check_for_file_content_result_and_check_invalid_binary_content() var exception = Assert.Throws(() => _controller.WithCallTo(c => c.File()).ShouldRenderFileContents(contents)); - Assert.True(exception.Message.StartsWith("Expected file contents to be ")); - 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(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] diff --git a/TestStack.FluentMvcTesting/ControllerResultTest.cs b/TestStack.FluentMvcTesting/ControllerResultTest.cs index a691a9a..c0784b9 100644 --- a/TestStack.FluentMvcTesting/ControllerResultTest.cs +++ b/TestStack.FluentMvcTesting/ControllerResultTest.cs @@ -240,9 +240,9 @@ public FileContentResult ShouldRenderFileContents(byte[] contents = null, string 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))); + "Expected file contents to be equal to [{0}], but instead was given [{1}].", + string.Join(", ", contents), + string.Join(", ", fileResult.FileContents))); } if (contentType != null && fileResult.ContentType != contentType) From c25d2e7902e93e13321584f4f73c921e583369e9 Mon Sep 17 00:00:00 2001 From: ByteBlast Date: Fri, 22 Aug 2014 02:08:11 +0100 Subject: [PATCH 6/6] Refactored certain file methods to test the content type first. Partial implementation of #3 --- .../ControllerResultTestTests.cs | 26 +++++++++++++++++++ .../ControllerResultTest.cs | 18 ++++++------- 2 files changed, 35 insertions(+), 9 deletions(-) diff --git a/TestStack.FluentMVCTesting.Tests/ControllerResultTestTests.cs b/TestStack.FluentMVCTesting.Tests/ControllerResultTestTests.cs index fd5a519..fdf266c 100644 --- a/TestStack.FluentMVCTesting.Tests/ControllerResultTestTests.cs +++ b/TestStack.FluentMVCTesting.Tests/ControllerResultTestTests.cs @@ -382,6 +382,19 @@ public void Check_for_file_content_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_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() { @@ -457,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/ControllerResultTest.cs b/TestStack.FluentMvcTesting/ControllerResultTest.cs index c0784b9..3af48f9 100644 --- a/TestStack.FluentMvcTesting/ControllerResultTest.cs +++ b/TestStack.FluentMvcTesting/ControllerResultTest.cs @@ -237,6 +237,11 @@ public FileContentResult ShouldRenderFileContents(byte[] contents = null, string 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( @@ -245,11 +250,6 @@ public FileContentResult ShouldRenderFileContents(byte[] contents = null, string string.Join(", ", fileResult.FileContents))); } - 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)); - } - return fileResult; } @@ -288,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;