From 1d55cb030a011d5f1d51593ceda1011e00c2bbf2 Mon Sep 17 00:00:00 2001 From: ByteBlast Date: Mon, 1 Sep 2014 11:49:29 +0100 Subject: [PATCH 01/17] Removed ShouldRenderFileStream. --- .../ControllerResultTestTests.cs | 25 ------------------- .../ControllerResultTest.cs | 14 ----------- 2 files changed, 39 deletions(-) diff --git a/TestStack.FluentMVCTesting.Tests/ControllerResultTestTests.cs b/TestStack.FluentMVCTesting.Tests/ControllerResultTestTests.cs index a4e82db..99c3cd5 100644 --- a/TestStack.FluentMVCTesting.Tests/ControllerResultTestTests.cs +++ b/TestStack.FluentMVCTesting.Tests/ControllerResultTestTests.cs @@ -29,14 +29,12 @@ class ControllerResultTestShould ReturnType(t => t.ShouldRenderDefaultPartialView()), 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.ShouldRenderFileContents("")), ReturnType(t => t.ShouldRenderFileContents("", "")), ReturnType(t => t.ShouldRenderFileContents("", "", Encoding.UTF8)), - ReturnType(t => t.ShouldRenderFileStream("")), ReturnType(t => t.ShouldRenderFilePath()), ReturnType(t => t.ShouldRenderFilePath("")), ReturnType(t => t.ShouldRenderFilePath("", "")), @@ -483,29 +481,6 @@ public void Check_for_file_result_and_check_content_type() _controller.WithCallTo(c => c.EmptyFile()).ShouldRenderFile(ControllerResultTestController.FileContentType); } - [Test] - public void Check_for_file_stream_result() - { - _controller.WithCallTo(c => c.EmptyStream()).ShouldRenderFileStream(); - } - - [Test] - public void Check_for_file_stream_result_and_check_content_type() - { - _controller.WithCallTo(c => c.EmptyStream()).ShouldRenderFileStream(ControllerResultTestController.FileContentType); - } - - [Test] - public void Check_for_file_stream_result_and_check_invalid_content_type() - { - const string contentType = "application/dummy"; - - var exception = Assert.Throws(() => - _controller.WithCallTo(c => c.EmptyFile()).ShouldRenderAnyFile(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_path_result() { diff --git a/TestStack.FluentMvcTesting/ControllerResultTest.cs b/TestStack.FluentMvcTesting/ControllerResultTest.cs index ecaf91e..0cf7842 100644 --- a/TestStack.FluentMvcTesting/ControllerResultTest.cs +++ b/TestStack.FluentMvcTesting/ControllerResultTest.cs @@ -292,20 +292,6 @@ public FileContentResult ShouldRenderFile(string contentType = null) return fileResult; } - public FileStreamResult ShouldRenderFileStream(string contentType = null) - { - ValidateActionReturnType(); - - var fileResult = (FileStreamResult)_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)); - } - - return fileResult; - } - public FilePathResult ShouldRenderFilePath(string fileName = null, string contentType = null) { ValidateActionReturnType(); From 6aec7fad23d62ecfd5198e15affed6f8dbeb2f05 Mon Sep 17 00:00:00 2001 From: ByteBlast Date: Mon, 1 Sep 2014 11:53:07 +0100 Subject: [PATCH 02/17] Added support for checking for a file stream result. --- .../ControllerResultTestTests.cs | 9 +++++++++ TestStack.FluentMvcTesting/ControllerResultTest.cs | 6 ++++++ 2 files changed, 15 insertions(+) diff --git a/TestStack.FluentMVCTesting.Tests/ControllerResultTestTests.cs b/TestStack.FluentMVCTesting.Tests/ControllerResultTestTests.cs index 99c3cd5..07d2ba7 100644 --- a/TestStack.FluentMVCTesting.Tests/ControllerResultTestTests.cs +++ b/TestStack.FluentMVCTesting.Tests/ControllerResultTestTests.cs @@ -38,6 +38,7 @@ class ControllerResultTestShould ReturnType(t => t.ShouldRenderFilePath()), ReturnType(t => t.ShouldRenderFilePath("")), ReturnType(t => t.ShouldRenderFilePath("", "")), + ReturnType(t => t.ShouldRenderFileStream()), ReturnType(t => t.ShouldRenderAnyFile()), ReturnType(t => t.ShouldGiveHttpStatus()), ReturnType(t => t.ShouldReturnJson()), @@ -314,6 +315,14 @@ public void Check_for_invalid_partial_name() } #endregion + [Test] + public void Check_for_file_stream_result() + { + _controller + .WithCallTo(c => c.EmptyStream()) + .ShouldRenderFileStream(); + } + #region File tests [Test] diff --git a/TestStack.FluentMvcTesting/ControllerResultTest.cs b/TestStack.FluentMvcTesting/ControllerResultTest.cs index 0cf7842..a72ef1a 100644 --- a/TestStack.FluentMvcTesting/ControllerResultTest.cs +++ b/TestStack.FluentMvcTesting/ControllerResultTest.cs @@ -214,6 +214,12 @@ public ViewResultTest ShouldRenderDefaultPartialView() #endregion + public FileStreamResult ShouldRenderFileStream() + { + ValidateActionReturnType(); + return (FileStreamResult) _actionResult; + } + #region File Results public FileResult ShouldRenderAnyFile(string contentType = null) From 08e275b3697e6e8528064815ce96fba4634b74e6 Mon Sep 17 00:00:00 2001 From: ByteBlast Date: Mon, 1 Sep 2014 13:39:42 +0100 Subject: [PATCH 03/17] Added support for checking a file stream result's contents. --- .../ControllerResultTestTests.cs | 51 ++++++++++++++++++- .../ControllerResultTestController.cs | 14 ++++- .../ControllerResultTest.cs | 31 ++++++++++- 3 files changed, 91 insertions(+), 5 deletions(-) diff --git a/TestStack.FluentMVCTesting.Tests/ControllerResultTestTests.cs b/TestStack.FluentMVCTesting.Tests/ControllerResultTestTests.cs index 07d2ba7..af378b1 100644 --- a/TestStack.FluentMVCTesting.Tests/ControllerResultTestTests.cs +++ b/TestStack.FluentMVCTesting.Tests/ControllerResultTestTests.cs @@ -1,8 +1,10 @@ using System; using System.Collections.Generic; +using System.IO; using System.Linq; using System.Linq.Expressions; using System.Net; +using System.Security.Permissions; using System.Web.Mvc; using NUnit.Framework; using TestStack.FluentMVCTesting.Tests.TestControllers; @@ -39,6 +41,7 @@ class ControllerResultTestShould ReturnType(t => t.ShouldRenderFilePath("")), ReturnType(t => t.ShouldRenderFilePath("", "")), ReturnType(t => t.ShouldRenderFileStream()), + ReturnType(t => t.ShouldRenderFileStream(new MemoryStream())), ReturnType(t => t.ShouldRenderAnyFile()), ReturnType(t => t.ShouldGiveHttpStatus()), ReturnType(t => t.ShouldReturnJson()), @@ -323,6 +326,52 @@ public void Check_for_file_stream_result() .ShouldRenderFileStream(); } + [Test] + public void Check_for_file_stream_result_and_check_stream_data() + { + _controller + .WithCallTo(c => c.EmptyStream()) + .ShouldRenderFileStream(ControllerResultTestController.EmptyStreamContents); + } + + [Test] + public void Check_for_file_stream_result_and_check_invalid_stream_data() + { + var buffer = new byte[] { 1, 2 }; + var expectedStream = new MemoryStream(buffer); + + var exception = Assert.Throws(() => + _controller + .WithCallTo(c => c.EmptyStream()) + .ShouldRenderFileStream(expectedStream) + ); + + var expected = string.Format("[{0}]", string.Join(", ", buffer)); + var actual = string.Format("[{0}]", string.Join(", ", ControllerResultTestController.EmptyStreamBuffer)); + var message = string.Format("Expected stream contents to be equal to {0}, but instead was given {1}.", expected, actual); + + Assert.That(exception.Message, Is.EqualTo(message)); + } + + [Test] + public void Check_for_file_stream_result_with_populated_file_and_check_invalid_stream_data() + { + var buffer = new byte[] { 1, 2 }; + var expectedStream = new MemoryStream(buffer); + + var exception = Assert.Throws(() => + _controller + .WithCallTo(c => c.PopulatedStream()) + .ShouldRenderFileStream(expectedStream) + ); + + var expected = string.Format("[{0}]", string.Join(", ", buffer)); + var actual = string.Format("[{0}]", string.Join(", ", ControllerResultTestController.EmptyStreamBuffer)); + var message = string.Format("Expected stream contents to be equal to {0}, but instead was given {1}.", expected, actual); + + Assert.That(exception.Message, Is.EqualTo(message)); + } + #region File tests [Test] @@ -400,7 +449,7 @@ public void Check_for_file_content_result_and_check_invalid_binary_content_and_c byte[] contents = { 1, 2 }; const string contentType = "application/dummy"; - var exception = Assert.Throws(() => + var exception = Assert.Throws(() => _controller.WithCallTo(c => c.BinaryFile()).ShouldRenderFileContents(contents, contentType)); // Assert that the content type validation occurs before that of the actual contents. diff --git a/TestStack.FluentMVCTesting.Tests/TestControllers/ControllerResultTestController.cs b/TestStack.FluentMVCTesting.Tests/TestControllers/ControllerResultTestController.cs index 6a8b61a..4ff2ca0 100644 --- a/TestStack.FluentMVCTesting.Tests/TestControllers/ControllerResultTestController.cs +++ b/TestStack.FluentMVCTesting.Tests/TestControllers/ControllerResultTestController.cs @@ -18,6 +18,12 @@ class ControllerResultTestController : Controller public const string FileName = "NamedFile"; public static byte[] BinaryFileContents = { 1 }; public static string TextualFileContents = "textual content"; + + public static readonly byte[] EmptyStreamBuffer = { }; + public static readonly byte[] PopulatedStreamBuffer = { 1 }; + public static readonly Stream EmptyStreamContents = new MemoryStream(EmptyStreamBuffer); + public static readonly Stream PopulatedStreamContents = new MemoryStream(PopulatedStreamBuffer); + #endregion #region Empty, Null and Random Results @@ -180,8 +186,12 @@ public ActionResult TextualFile(Encoding encoding) public ActionResult EmptyStream() { - var content = new MemoryStream(); - return File(content, FileContentType); + return File(EmptyStreamContents, FileContentType); + } + + public ActionResult PopulatedStream() + { + return File(PopulatedStreamContents, FileContentType); } public ActionResult EmptyFilePath() diff --git a/TestStack.FluentMvcTesting/ControllerResultTest.cs b/TestStack.FluentMvcTesting/ControllerResultTest.cs index a72ef1a..cf39298 100644 --- a/TestStack.FluentMvcTesting/ControllerResultTest.cs +++ b/TestStack.FluentMvcTesting/ControllerResultTest.cs @@ -1,4 +1,5 @@ using System; +using System.IO; using System.Linq; using System.Linq.Expressions; using System.Net; @@ -214,10 +215,36 @@ public ViewResultTest ShouldRenderDefaultPartialView() #endregion - public FileStreamResult ShouldRenderFileStream() + public FileStreamResult ShouldRenderFileStream(Stream stream = null) { ValidateActionReturnType(); - return (FileStreamResult) _actionResult; + + var fileResult = (FileStreamResult)_actionResult; + + if (stream != null) + { + byte[] expected = ConvertStreamToArray(stream); + byte[] actual = ConvertStreamToArray(fileResult.FileStream); + + if (!expected.SequenceEqual(actual)) + { + throw new ActionResultAssertionException(string.Format( + "Expected stream contents to be equal to [{0}], but instead was given [{1}].", + string.Join(", ", expected), + string.Join(", ", actual))); + } + } + + return fileResult; + } + + private static byte[] ConvertStreamToArray(Stream stream) + { + using (var memoryStream = new MemoryStream()) + { + stream.CopyTo(memoryStream); + return memoryStream.ToArray(); + } } #region File Results From ba97591c72a9afef363742e2f4bb6301c4193c8e Mon Sep 17 00:00:00 2001 From: ByteBlast Date: Mon, 1 Sep 2014 13:54:51 +0100 Subject: [PATCH 04/17] Added support for checking a file stream result's content type. --- .../ControllerResultTestTests.cs | 45 ++++++++++++++++++- .../ControllerResultTest.cs | 10 ++++- 2 files changed, 53 insertions(+), 2 deletions(-) diff --git a/TestStack.FluentMVCTesting.Tests/ControllerResultTestTests.cs b/TestStack.FluentMVCTesting.Tests/ControllerResultTestTests.cs index af378b1..d1a028f 100644 --- a/TestStack.FluentMVCTesting.Tests/ControllerResultTestTests.cs +++ b/TestStack.FluentMVCTesting.Tests/ControllerResultTestTests.cs @@ -42,6 +42,8 @@ class ControllerResultTestShould ReturnType(t => t.ShouldRenderFilePath("", "")), ReturnType(t => t.ShouldRenderFileStream()), ReturnType(t => t.ShouldRenderFileStream(new MemoryStream())), + ReturnType(t => t.ShouldRenderFileStream(contentType: "")), + ReturnType(t => t.ShouldRenderFileStream(new MemoryStream(), "")), ReturnType(t => t.ShouldRenderAnyFile()), ReturnType(t => t.ShouldGiveHttpStatus()), ReturnType(t => t.ShouldReturnJson()), @@ -366,12 +368,53 @@ public void Check_for_file_stream_result_with_populated_file_and_check_invalid_s ); var expected = string.Format("[{0}]", string.Join(", ", buffer)); - var actual = string.Format("[{0}]", string.Join(", ", ControllerResultTestController.EmptyStreamBuffer)); + var actual = string.Format("[{0}]", string.Join(", ", ControllerResultTestController.PopulatedStreamBuffer)); var message = string.Format("Expected stream contents to be equal to {0}, but instead was given {1}.", expected, actual); Assert.That(exception.Message, Is.EqualTo(message)); } + [Test] + public void Check_for_file_stream_result_and_check_content_type() + { + _controller + .WithCallTo(c => c.EmptyStream()) + .ShouldRenderFileStream(ControllerResultTestController.EmptyStreamContents, + ControllerResultTestController.FileContentType); + } + + [Test] + public void Check_for_file_stream_result_and_check_invalid_content_type() + { + const string contentType = "application/dummy"; + + var exception = Assert.Throws(() => + _controller + .WithCallTo(c => c.EmptyStream()) + .ShouldRenderFileStream(ControllerResultTestController.EmptyStreamContents, contentType)); + + var message = string.Format( + "Expected stream to be of content type '{0}', but instead was given '{1}'.", contentType, ControllerResultTestController.FileContentType); + + Assert.That(exception.Message, Is.EqualTo(message)); + } + + [Test] + public void Check_for_file_stream_result_and_check_invalid_stream_data_and_check_invalid_content_type() + { + var buffer = new byte[] { 1, 2 }; + var expectedStream = new MemoryStream(buffer); + const string contentType = "application/dummy"; + + var exception = Assert.Throws(() => + _controller + .WithCallTo(c => c.EmptyStream()) + .ShouldRenderFileStream(expectedStream, contentType)); + + // Assert that the content type validation occurs before that of the actual contents. + Assert.That(exception.Message.Contains("content type")); + } + #region File tests [Test] diff --git a/TestStack.FluentMvcTesting/ControllerResultTest.cs b/TestStack.FluentMvcTesting/ControllerResultTest.cs index cf39298..b03bda9 100644 --- a/TestStack.FluentMvcTesting/ControllerResultTest.cs +++ b/TestStack.FluentMvcTesting/ControllerResultTest.cs @@ -215,12 +215,20 @@ public ViewResultTest ShouldRenderDefaultPartialView() #endregion - public FileStreamResult ShouldRenderFileStream(Stream stream = null) + public FileStreamResult ShouldRenderFileStream(Stream stream = null, string contentType = null) { ValidateActionReturnType(); var fileResult = (FileStreamResult)_actionResult; + if (contentType != null && fileResult.ContentType != contentType) + { + throw new ActionResultAssertionException(string.Format( + "Expected stream to be of content type '{0}', but instead was given '{1}'.", + contentType, + fileResult.ContentType)); + } + if (stream != null) { byte[] expected = ConvertStreamToArray(stream); From adf90919c68a68ebb5d79a89b076c0f27a6bd3c2 Mon Sep 17 00:00:00 2001 From: ByteBlast Date: Mon, 1 Sep 2014 17:11:17 +0100 Subject: [PATCH 05/17] Added support for checking a file stream result's binary contents. --- .../ControllerResultTestTests.cs | 73 +++++++++++++++++++ .../ControllerResultTest.cs | 6 ++ 2 files changed, 79 insertions(+) diff --git a/TestStack.FluentMVCTesting.Tests/ControllerResultTestTests.cs b/TestStack.FluentMVCTesting.Tests/ControllerResultTestTests.cs index d1a028f..b91b2f2 100644 --- a/TestStack.FluentMVCTesting.Tests/ControllerResultTestTests.cs +++ b/TestStack.FluentMVCTesting.Tests/ControllerResultTestTests.cs @@ -44,6 +44,8 @@ class ControllerResultTestShould ReturnType(t => t.ShouldRenderFileStream(new MemoryStream())), ReturnType(t => t.ShouldRenderFileStream(contentType: "")), ReturnType(t => t.ShouldRenderFileStream(new MemoryStream(), "")), + ReturnType(t => t.ShouldRenderFileStream(new byte[0])), + ReturnType(t => t.ShouldRenderFileStream(new byte[0], "")), ReturnType(t => t.ShouldRenderAnyFile()), ReturnType(t => t.ShouldGiveHttpStatus()), ReturnType(t => t.ShouldReturnJson()), @@ -415,6 +417,77 @@ public void Check_for_file_stream_result_and_check_invalid_stream_data_and_check Assert.That(exception.Message.Contains("content type")); } + [Test] + public void Check_for_file_stream_result_and_check_binary_contents() + { + _controller + .WithCallTo(c => c.PopulatedStream()) + .ShouldRenderFileStream(ControllerResultTestController.PopulatedStreamBuffer); + } + + [Test] + public void Check_for_file_stream_result_and_check_invalid_binary_contents() + { + var contents = new byte[] { 1, 2 }; + + var exception = Assert.Throws(() => + _controller + .WithCallTo(c => c.PopulatedStream()) + .ShouldRenderFileStream(contents) + ); + + var expected = string.Format("[{0}]", string.Join(", ", contents)); + var actual = string.Format("[{0}]", string.Join(", ", ControllerResultTestController.PopulatedStreamBuffer)); + var message = string.Format("Expected stream contents to be equal to {0}, but instead was given {1}.", expected, actual); + + Assert.That(exception.Message, Is.EqualTo(message)); + } + + [Test] + public void Check_for_file_stream_result_and_check_binary_contents_and_check_content_type() + { + _controller + .WithCallTo(c => c.PopulatedStream()) + .ShouldRenderFileStream( + ControllerResultTestController.PopulatedStreamBuffer, + ControllerResultTestController.FileContentType); + } + + [Test] + public void Check_for_file_stream_result_and_check_binary_contents_and_check_invalid_content_type() + { + const string contentType = "application/dummy"; + + var exception = Assert.Throws(() => + _controller + .WithCallTo(c => c.EmptyStream()) + .ShouldRenderFileStream( + ControllerResultTestController.PopulatedStreamBuffer, + contentType) + ); + + var message = + string.Format("Expected stream to be of content type '{0}', but instead was given '{1}'.", + contentType, ControllerResultTestController.FileContentType); + + Assert.That(exception.Message, Is.EqualTo(message)); + } + + [Test] + public void Check_for_file_stream_result_and_check_invalid_binary_contents_and_check_invalid_content_type() + { + var contents = new byte[] { 1, 2 }; + const string contentType = "application/dummy"; + + var exception = Assert.Throws(() => + _controller + .WithCallTo(c => c.EmptyStream()) + .ShouldRenderFileStream(contents, contentType)); + + // Assert that the content type validation occurs before that of the actual contents. + Assert.That(exception.Message.Contains("content type")); + } + #region File tests [Test] diff --git a/TestStack.FluentMvcTesting/ControllerResultTest.cs b/TestStack.FluentMvcTesting/ControllerResultTest.cs index b03bda9..3bec692 100644 --- a/TestStack.FluentMvcTesting/ControllerResultTest.cs +++ b/TestStack.FluentMvcTesting/ControllerResultTest.cs @@ -215,6 +215,11 @@ public ViewResultTest ShouldRenderDefaultPartialView() #endregion + public FileStreamResult ShouldRenderFileStream(byte[] content, string contentType = null) + { + return ShouldRenderFileStream(new MemoryStream(content), contentType); + } + public FileStreamResult ShouldRenderFileStream(Stream stream = null, string contentType = null) { ValidateActionReturnType(); @@ -251,6 +256,7 @@ private static byte[] ConvertStreamToArray(Stream stream) using (var memoryStream = new MemoryStream()) { stream.CopyTo(memoryStream); + stream.Position = 0; return memoryStream.ToArray(); } } From c87a3d77a5e68d567e78483967d6d9e68be47743 Mon Sep 17 00:00:00 2001 From: ByteBlast Date: Mon, 1 Sep 2014 20:00:20 +0100 Subject: [PATCH 06/17] Added support for checking a file stream result's textual content. --- .../ControllerResultTestTests.cs | 344 ++++++++++-------- .../ControllerResultTestController.cs | 28 +- TestStack.FluentMVCTesting.sln | 4 +- .../ControllerResultTest.cs | 120 +++--- 4 files changed, 290 insertions(+), 206 deletions(-) diff --git a/TestStack.FluentMVCTesting.Tests/ControllerResultTestTests.cs b/TestStack.FluentMVCTesting.Tests/ControllerResultTestTests.cs index b91b2f2..42c5b32 100644 --- a/TestStack.FluentMVCTesting.Tests/ControllerResultTestTests.cs +++ b/TestStack.FluentMVCTesting.Tests/ControllerResultTestTests.cs @@ -4,7 +4,6 @@ using System.Linq; using System.Linq.Expressions; using System.Net; -using System.Security.Permissions; using System.Web.Mvc; using NUnit.Framework; using TestStack.FluentMVCTesting.Tests.TestControllers; @@ -46,6 +45,9 @@ class ControllerResultTestShould ReturnType(t => t.ShouldRenderFileStream(new MemoryStream(), "")), ReturnType(t => t.ShouldRenderFileStream(new byte[0])), ReturnType(t => t.ShouldRenderFileStream(new byte[0], "")), + ReturnType(t => t.ShouldRenderFileStream("")), + ReturnType(t => t.ShouldRenderFileStream("", "")), + ReturnType(t => t.ShouldRenderFileStream("", "", Encoding.UTF8)), ReturnType(t => t.ShouldRenderAnyFile()), ReturnType(t => t.ShouldGiveHttpStatus()), ReturnType(t => t.ShouldReturnJson()), @@ -322,337 +324,379 @@ public void Check_for_invalid_partial_name() } #endregion + #region File tests + [Test] - public void Check_for_file_stream_result() + public void Check_for_any_file_result() { - _controller - .WithCallTo(c => c.EmptyStream()) - .ShouldRenderFileStream(); + _controller.WithCallTo(c => c.EmptyFile()).ShouldRenderAnyFile(); + _controller.WithCallTo(c => c.EmptyFilePath()).ShouldRenderAnyFile(); + _controller.WithCallTo(c => c.EmptyStream()).ShouldRenderAnyFile(); } [Test] - public void Check_for_file_stream_result_and_check_stream_data() + public void Check_for_any_file_result_and_check_content_type() { - _controller - .WithCallTo(c => c.EmptyStream()) - .ShouldRenderFileStream(ControllerResultTestController.EmptyStreamContents); + _controller.WithCallTo(c => c.EmptyFile()).ShouldRenderAnyFile(ControllerResultTestController.FileContentType); + _controller.WithCallTo(c => c.EmptyFilePath()).ShouldRenderAnyFile(ControllerResultTestController.FileContentType); + _controller.WithCallTo(c => c.EmptyStream()).ShouldRenderAnyFile(ControllerResultTestController.FileContentType); } [Test] - public void Check_for_file_stream_result_and_check_invalid_stream_data() + public void Check_for_any_file_result_and_check_invalid_content_type() { - var buffer = new byte[] { 1, 2 }; - var expectedStream = new MemoryStream(buffer); + const string contentType = "application/dummy"; var exception = Assert.Throws(() => - _controller - .WithCallTo(c => c.EmptyStream()) - .ShouldRenderFileStream(expectedStream) - ); + _controller.WithCallTo(c => c.EmptyFile()).ShouldRenderAnyFile(contentType)); - var expected = string.Format("[{0}]", string.Join(", ", buffer)); - var actual = string.Format("[{0}]", string.Join(", ", ControllerResultTestController.EmptyStreamBuffer)); - var message = string.Format("Expected stream contents to be equal to {0}, but instead was given {1}.", expected, actual); + Assert.That(exception.Message, Is.EqualTo(string.Format("Expected file to be of content type '{0}', but instead was given '{1}'.", contentType, ControllerResultTestController.FileContentType))); + } - Assert.That(exception.Message, Is.EqualTo(message)); + [Test] + public void Check_for_file_content_result() + { + _controller.WithCallTo(c => c.EmptyFile()).ShouldRenderFileContents(); } [Test] - public void Check_for_file_stream_result_with_populated_file_and_check_invalid_stream_data() + public void Check_for_file_content_result_and_check_binary_content() { - var buffer = new byte[] { 1, 2 }; - var expectedStream = new MemoryStream(buffer); + _controller.WithCallTo(c => c.BinaryFile()).ShouldRenderFileContents(ControllerResultTestController.BinaryFileContents); + } + [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.PopulatedStream()) - .ShouldRenderFileStream(expectedStream) - ); - - var expected = string.Format("[{0}]", string.Join(", ", buffer)); - var actual = string.Format("[{0}]", string.Join(", ", ControllerResultTestController.PopulatedStreamBuffer)); - var message = string.Format("Expected stream contents to be equal to {0}, but instead was given {1}.", expected, actual); + _controller.WithCallTo(c => c.BinaryFile()).ShouldRenderFileContents(contents)); - Assert.That(exception.Message, Is.EqualTo(message)); + 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.BinaryFileContents).All(exception.Message.Contains)); } [Test] - public void Check_for_file_stream_result_and_check_content_type() + public void Check_for_file_content_result_and_check_binary_content_and_check_content_type() { - _controller - .WithCallTo(c => c.EmptyStream()) - .ShouldRenderFileStream(ControllerResultTestController.EmptyStreamContents, - ControllerResultTestController.FileContentType); + _controller.WithCallTo(c => c.BinaryFile()).ShouldRenderFileContents(ControllerResultTestController.BinaryFileContents, ControllerResultTestController.FileContentType); } [Test] - public void Check_for_file_stream_result_and_check_invalid_content_type() + 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.EmptyStream()) - .ShouldRenderFileStream(ControllerResultTestController.EmptyStreamContents, contentType)); - - var message = string.Format( - "Expected stream to be of content type '{0}', but instead was given '{1}'.", contentType, ControllerResultTestController.FileContentType); + _controller.WithCallTo(c => c.BinaryFile()).ShouldRenderFileContents(ControllerResultTestController.BinaryFileContents, contentType)); - Assert.That(exception.Message, Is.EqualTo(message)); + 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_stream_result_and_check_invalid_stream_data_and_check_invalid_content_type() + public void Check_for_file_content_result_and_check_invalid_binary_content_and_check_invalid_content_type() { - var buffer = new byte[] { 1, 2 }; - var expectedStream = new MemoryStream(buffer); + byte[] contents = { 1, 2 }; const string contentType = "application/dummy"; var exception = Assert.Throws(() => - _controller - .WithCallTo(c => c.EmptyStream()) - .ShouldRenderFileStream(expectedStream, 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_stream_result_and_check_binary_contents() + public void Check_for_file_content_result_and_check_textual_contents() { - _controller - .WithCallTo(c => c.PopulatedStream()) - .ShouldRenderFileStream(ControllerResultTestController.PopulatedStreamBuffer); + _controller.WithCallTo(c => c.TextualFile()).ShouldRenderFileContents(ControllerResultTestController.TextualFileContent); } [Test] - public void Check_for_file_stream_result_and_check_invalid_binary_contents() + public void Check_for_file_content_result_and_check_invalid_textual_contents() { - var contents = new byte[] { 1, 2 }; + const string contents = "dummy contents"; var exception = Assert.Throws(() => - _controller - .WithCallTo(c => c.PopulatedStream()) - .ShouldRenderFileStream(contents) - ); - - var expected = string.Format("[{0}]", string.Join(", ", contents)); - var actual = string.Format("[{0}]", string.Join(", ", ControllerResultTestController.PopulatedStreamBuffer)); - var message = string.Format("Expected stream contents to be equal to {0}, but instead was given {1}.", expected, actual); + _controller.WithCallTo(c => c.TextualFile()).ShouldRenderFileContents(contents)); - Assert.That(exception.Message, Is.EqualTo(message)); + Assert.That(exception.Message, Is.EqualTo(string.Format("Expected file contents to be \"{0}\", but instead was \"{1}\".", contents, ControllerResultTestController.TextualFileContent))); } [Test] - public void Check_for_file_stream_result_and_check_binary_contents_and_check_content_type() + public void Check_for_file_content_result_and_check_textual_content_and_check_content_result() { - _controller - .WithCallTo(c => c.PopulatedStream()) - .ShouldRenderFileStream( - ControllerResultTestController.PopulatedStreamBuffer, - ControllerResultTestController.FileContentType); + _controller.WithCallTo(c => c.TextualFile()).ShouldRenderFileContents(ControllerResultTestController.TextualFileContent, ControllerResultTestController.FileContentType); } [Test] - public void Check_for_file_stream_result_and_check_binary_contents_and_check_invalid_content_type() + 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.EmptyStream()) - .ShouldRenderFileStream( - ControllerResultTestController.PopulatedStreamBuffer, - contentType) - ); - - var message = - string.Format("Expected stream to be of content type '{0}', but instead was given '{1}'.", - contentType, ControllerResultTestController.FileContentType); + _controller.WithCallTo(c => c.TextualFile()).ShouldRenderFileContents(ControllerResultTestController.TextualFileContent, contentType)); - Assert.That(exception.Message, Is.EqualTo(message)); + 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_stream_result_and_check_invalid_binary_contents_and_check_invalid_content_type() + public void Check_for_file_content_result_and_check_invalid_textual_content_and_check_invalid_content_type() { - var contents = new byte[] { 1, 2 }; + const string contents = "dummy content"; const string contentType = "application/dummy"; var exception = Assert.Throws(() => - _controller - .WithCallTo(c => c.EmptyStream()) - .ShouldRenderFileStream(contents, contentType)); + _controller.WithCallTo(c => c.TextualFile()).ShouldRenderFileContents(contents, contentType)); // Assert that the content type validation occurs before that of the actual contents. Assert.That(exception.Message.Contains("content type")); } - #region File tests + [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.TextualFileContent, encoding: encoding); + } [Test] - public void Check_for_any_file_result() + public void Check_for_file_content_result_and_check_textual_content_using_given_char_encoding_and_check_content_type() { - _controller.WithCallTo(c => c.EmptyFile()).ShouldRenderAnyFile(); - _controller.WithCallTo(c => c.EmptyFilePath()).ShouldRenderAnyFile(); - _controller.WithCallTo(c => c.EmptyStream()).ShouldRenderAnyFile(); + var encoding = Encoding.BigEndianUnicode; + + _controller.WithCallTo(c => c.TextualFile(encoding)).ShouldRenderFileContents(ControllerResultTestController.TextualFileContent, ControllerResultTestController.FileContentType, encoding); } [Test] - public void Check_for_any_file_result_and_check_content_type() + public void Check_for_file_content_result_and_check_textual_content_using_invalid_given_char_encoding() { - _controller.WithCallTo(c => c.EmptyFile()).ShouldRenderAnyFile(ControllerResultTestController.FileContentType); - _controller.WithCallTo(c => c.EmptyFilePath()).ShouldRenderAnyFile(ControllerResultTestController.FileContentType); - _controller.WithCallTo(c => c.EmptyStream()).ShouldRenderAnyFile(ControllerResultTestController.FileContentType); + Assert.Throws(() => + _controller.WithCallTo(c => c.TextualFile()).ShouldRenderFileContents(ControllerResultTestController.TextualFileContent, ControllerResultTestController.FileContentType, Encoding.BigEndianUnicode)); } [Test] - public void Check_for_any_file_result_and_check_invalid_content_type() + public void Check_for_file_result() { - const string contentType = "application/dummy"; + _controller.WithCallTo(c => c.EmptyFile()).ShouldRenderFile(); + } - var exception = Assert.Throws(() => - _controller.WithCallTo(c => c.EmptyFile()).ShouldRenderAnyFile(contentType)); + [Test] + public void Check_for_file_result_and_check_content_type() + { + _controller.WithCallTo(c => c.EmptyFile()).ShouldRenderFile(ControllerResultTestController.FileContentType); + } - 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_stream_result() + { + _controller.WithCallTo(c => c.EmptyStream()) + .ShouldRenderFileStream(); } [Test] - public void Check_for_file_content_result() + public void Check_for_file_stream_result_and_check_stream_content() { - _controller.WithCallTo(c => c.EmptyFile()).ShouldRenderFileContents(); + _controller.WithCallTo(c => c.EmptyStream()) + .ShouldRenderFileStream(ControllerResultTestController.EmptyStreamContents); } [Test] - public void Check_for_file_content_result_and_check_binary_content() + public void Check_for_file_stream_result_and_check_invalid_stream_content() { - _controller.WithCallTo(c => c.BinaryFile()).ShouldRenderFileContents(ControllerResultTestController.BinaryFileContents); + var buffer = new byte[] { 1, 2 }; + var stream = new MemoryStream(buffer); + + var exception = Assert.Throws(() => + _controller.WithCallTo(c => c.EmptyStream()).ShouldRenderFileStream(stream)); + + var expected = string.Format("[{0}]", string.Join(", ", buffer)); + var actual = string.Format("[{0}]", string.Join(", ", ControllerResultTestController.EmptyFileBuffer)); + var message = string.Format("Expected stream contents to be equal to {0}, but instead was given {1}.", expected, actual); + + Assert.That(exception.Message, Is.EqualTo(message)); } [Test] - public void Check_for_file_content_result_and_check_invalid_binary_content() + public void Check_for_file_stream_result_with_populated_file_and_check_invalid_stream_content() { - byte[] contents = { 1, 2 }; + var buffer = new byte[] { 1, 2 }; + var stream = new MemoryStream(buffer); + var exception = Assert.Throws(() => - _controller.WithCallTo(c => c.BinaryFile()).ShouldRenderFileContents(contents)); + _controller.WithCallTo(c => c.BinaryStream()).ShouldRenderFileStream(stream)); - 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.BinaryFileContents).All(exception.Message.Contains)); + var expected = string.Format("[{0}]", string.Join(", ", buffer)); + var actual = string.Format("[{0}]", string.Join(", ", ControllerResultTestController.BinaryFileContents)); + var message = string.Format("Expected stream contents to be equal to {0}, but instead was given {1}.", expected, actual); + + Assert.That(exception.Message, Is.EqualTo(message)); } [Test] - public void Check_for_file_content_result_and_check_binary_content_and_check_content_type() + public void Check_for_file_stream_result_and_check_content_type() { - _controller.WithCallTo(c => c.BinaryFile()).ShouldRenderFileContents(ControllerResultTestController.BinaryFileContents, ControllerResultTestController.FileContentType); + _controller.WithCallTo(c => c.EmptyStream()) + .ShouldRenderFileStream(ControllerResultTestController.EmptyStreamContents,ControllerResultTestController.FileContentType); } [Test] - public void Check_for_file_content_result_and_check_invalid_content_type() + public void Check_for_file_stream_result_and_check_invalid_content_type() { const string contentType = "application/dummy"; var exception = Assert.Throws(() => - _controller.WithCallTo(c => c.BinaryFile()).ShouldRenderFileContents(ControllerResultTestController.BinaryFileContents, contentType)); + _controller.WithCallTo(c => c.EmptyStream()).ShouldRenderFileStream(ControllerResultTestController.EmptyStreamContents, 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))); + Assert.That(exception.Message, Is.EqualTo(string.Format( + "Expected stream 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() + public void Check_for_file_stream_result_and_check_invalid_stream_content_and_check_invalid_content_type() { - byte[] contents = { 1, 2 }; const string contentType = "application/dummy"; + var buffer = new byte[] { 1, 2 }; + var stream = new MemoryStream(buffer); var exception = Assert.Throws(() => - _controller.WithCallTo(c => c.BinaryFile()).ShouldRenderFileContents(contents, contentType)); + _controller.WithCallTo(c => c.EmptyStream()).ShouldRenderFileStream(stream, 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() + public void Check_for_file_stream_result_and_check_binary_content() { - _controller.WithCallTo(c => c.TextualFile()).ShouldRenderFileContents(ControllerResultTestController.TextualFileContents); + _controller.WithCallTo(c => c.BinaryStream()) + .ShouldRenderFileStream(ControllerResultTestController.BinaryFileContents); } [Test] - public void Check_for_file_content_result_and_check_invalid_textual_contents() + public void Check_for_file_stream_result_and_check_invalid_binary_content() { - const string contents = "dummy contents"; + var content = new byte[] { 1, 2 }; var exception = Assert.Throws(() => - _controller.WithCallTo(c => c.TextualFile()).ShouldRenderFileContents(contents)); + _controller.WithCallTo(c => c.BinaryStream()).ShouldRenderFileStream(content)); + + var expected = string.Format("[{0}]", string.Join(", ", content)); + var actual = string.Format("[{0}]", string.Join(", ", ControllerResultTestController.BinaryFileContents)); + var message = string.Format("Expected stream contents to be equal to {0}, but instead was given {1}.", expected, actual); - Assert.That(exception.Message, Is.EqualTo(string.Format("Expected file contents to be \"{0}\", but instead was \"{1}\".", contents, ControllerResultTestController.TextualFileContents))); + Assert.That(exception.Message, Is.EqualTo(message)); } [Test] - public void Check_for_file_content_result_and_check_textual_content_and_check_content_result() + public void Check_for_file_stream_result_and_check_binary_content_and_check_content_type() { - _controller.WithCallTo(c => c.TextualFile()).ShouldRenderFileContents(ControllerResultTestController.TextualFileContents, ControllerResultTestController.FileContentType); + _controller.WithCallTo(c => c.BinaryStream()) + .ShouldRenderFileStream(ControllerResultTestController.BinaryStreamContents, ControllerResultTestController.FileContentType); } [Test] - public void Check_for_file_content_result_and_check_textual_content_and_check_invalid_content_typet() + public void Check_for_file_stream_result_and_check_binary_content_and_check_invalid_content_type() { const string contentType = "application/dummy"; var exception = Assert.Throws(() => - _controller.WithCallTo(c => c.TextualFile()).ShouldRenderFileContents(ControllerResultTestController.TextualFileContents, contentType)); + _controller.WithCallTo(c => c.BinaryStream()).ShouldRenderFileStream(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))); + Assert.That(exception.Message, Is.EqualTo(string.Format("Expected stream 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() + public void Check_for_file_stream_result_and_check_invalid_binary_content_and_check_invalid_content_type() { - const string contents = "dummy content"; + var content = new byte[] { 1, 2 }; const string contentType = "application/dummy"; var exception = Assert.Throws(() => - _controller.WithCallTo(c => c.TextualFile()).ShouldRenderFileContents(contents, contentType)); + _controller.WithCallTo(c => c.BinaryStream()).ShouldRenderFileStream(content, 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_content_using_given_char_encoding() + public void Check_for_file_stream_result_and_check_textual_content() { - var encoding = Encoding.BigEndianUnicode; + _controller.WithCallTo(c => c.TextualStream()) + .ShouldRenderFileStream(ControllerResultTestController.TextualFileContent); + } - _controller.WithCallTo(c => c.TextualFile(encoding)) - .ShouldRenderFileContents(ControllerResultTestController.TextualFileContents, encoding: encoding); + [Test] + public void Check_for_file_stream_result_and_check_invalid_textual_content() + { + const string contents = "dummy contents"; + + var exception = Assert.Throws(() => + _controller.WithCallTo(c => c.TextualStream()).ShouldRenderFileStream(contents)); + + Assert.That(exception.Message, Is.EqualTo(string.Format("Expected file contents to be \"{0}\", but instead was \"{1}\".", contents, ControllerResultTestController.TextualFileContent))); } [Test] - public void Check_for_file_content_result_and_check_textual_content_using_given_char_encoding_and_check_content_type() + public void Check_for_file_stream_result_and_check_textual_content_and_check_content_type() { - var encoding = Encoding.BigEndianUnicode; + _controller.WithCallTo(c => c.TextualStream()) + .ShouldRenderFileStream(ControllerResultTestController.TextualFileContent, ControllerResultTestController.FileContentType); + } + + [Test] + public void Check_for_file_stream_result_and_check_textual_content_and_check_invalid_content_type() + { + const string contentType = "application/dummy"; + + var exception = Assert.Throws(() => + _controller.WithCallTo(c => c.TextualStream()).ShouldRenderFileStream(ControllerResultTestController.TextualFileContent, contentType)); - _controller.WithCallTo(c => c.TextualFile(encoding)).ShouldRenderFileContents(ControllerResultTestController.TextualFileContents, ControllerResultTestController.FileContentType, encoding); + Assert.That(exception.Message, Is.EqualTo(string.Format("Expected stream to be of content type '{0}', but instead was given '{1}'.", + contentType, ControllerResultTestController.FileContentType))); } [Test] - public void Check_for_file_content_result_and_check_textual_content_using_invalid_given_char_encoding() + public void Check_for_file_stream_result_and_check_invalid_textual_content_and_check_invalid_content_type() { - Assert.Throws(() => - _controller.WithCallTo(c => c.TextualFile()).ShouldRenderFileContents(ControllerResultTestController.TextualFileContents, ControllerResultTestController.FileContentType, Encoding.BigEndianUnicode)); + const string contentType = "application/dummy"; + const string content = "dummy contents"; + + var exception = Assert.Throws(() => + _controller.WithCallTo(c => c.TextualStream()).ShouldRenderFileStream(content, 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_result() + public void Check_for_file_stream_result_and_check_textual_content_using_given_char_encoding() { - _controller.WithCallTo(c => c.EmptyFile()).ShouldRenderFile(); + var encoding = Encoding.BigEndianUnicode; + + _controller.WithCallTo(c => c.TextualStream(encoding)) + .ShouldRenderFileStream(ControllerResultTestController.TextualFileContent, encoding: encoding); } [Test] - public void Check_for_file_result_and_check_content_type() + public void Check_for_file_stream_result_and_check_textual_content_using_given_char_encoding_and_check_content_type() { - _controller.WithCallTo(c => c.EmptyFile()).ShouldRenderFile(ControllerResultTestController.FileContentType); + var encoding = Encoding.BigEndianUnicode; + + _controller.WithCallTo(c => c.TextualStream(encoding)) + .ShouldRenderFileStream(ControllerResultTestController.TextualFileContent, ControllerResultTestController.FileContentType, encoding); + } + + [Test] + public void Check_for_file_stream_result_and_check_textual_content_using_invalid_given_char_encoding_and_check_content_type() + { + Assert.Throws(() => + _controller.WithCallTo(c => c.TextualStream()).ShouldRenderFileStream(ControllerResultTestController.TextualFileContent, encoding: Encoding.BigEndianUnicode)); } [Test] diff --git a/TestStack.FluentMVCTesting.Tests/TestControllers/ControllerResultTestController.cs b/TestStack.FluentMVCTesting.Tests/TestControllers/ControllerResultTestController.cs index 4ff2ca0..2f1e9e4 100644 --- a/TestStack.FluentMVCTesting.Tests/TestControllers/ControllerResultTestController.cs +++ b/TestStack.FluentMVCTesting.Tests/TestControllers/ControllerResultTestController.cs @@ -17,12 +17,10 @@ class ControllerResultTestController : Controller public const string JsonValue = "json"; public const string FileName = "NamedFile"; public static byte[] BinaryFileContents = { 1 }; - public static string TextualFileContents = "textual content"; - - public static readonly byte[] EmptyStreamBuffer = { }; - public static readonly byte[] PopulatedStreamBuffer = { 1 }; - public static readonly Stream EmptyStreamContents = new MemoryStream(EmptyStreamBuffer); - public static readonly Stream PopulatedStreamContents = new MemoryStream(PopulatedStreamBuffer); + public static string TextualFileContent = "textual content"; + public static byte[] EmptyFileBuffer = { }; + public static readonly Stream EmptyStreamContents = new MemoryStream(EmptyFileBuffer); + public static readonly Stream BinaryStreamContents = new MemoryStream(BinaryFileContents); #endregion @@ -180,7 +178,7 @@ public ActionResult TextualFile() public ActionResult TextualFile(Encoding encoding) { - var encodedContents = encoding.GetBytes(TextualFileContents); + var encodedContents = encoding.GetBytes(TextualFileContent); return File(encodedContents, FileContentType); } @@ -189,9 +187,21 @@ public ActionResult EmptyStream() return File(EmptyStreamContents, FileContentType); } - public ActionResult PopulatedStream() + public ActionResult BinaryStream() + { + return File(BinaryStreamContents, FileContentType); + } + + public ActionResult TextualStream() + { + return TextualStream(Encoding.UTF8); + } + + public ActionResult TextualStream(Encoding encoding) { - return File(PopulatedStreamContents, FileContentType); + var encondedContent = encoding.GetBytes(TextualFileContent); + var stream = new MemoryStream(encondedContent); + return File(stream, FileContentType); } public ActionResult EmptyFilePath() diff --git a/TestStack.FluentMVCTesting.sln b/TestStack.FluentMVCTesting.sln index 43b5131..2259b6d 100644 --- a/TestStack.FluentMVCTesting.sln +++ b/TestStack.FluentMVCTesting.sln @@ -1,6 +1,8 @@  Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio 2012 +# Visual Studio 2013 +VisualStudioVersion = 12.0.30501.0 +MinimumVisualStudioVersion = 10.0.40219.1 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestStack.FluentMVCTesting", "TestStack.FluentMVCTesting\TestStack.FluentMVCTesting.csproj", "{152CA00F-18D3-4CF5-8CA0-2C5B70CBEA19}" EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestStack.FluentMVCTesting.Tests", "TestStack.FluentMVCTesting.Tests\TestStack.FluentMVCTesting.Tests.csproj", "{4DCC907C-A08A-4139-BB6B-2D34B21F318B}" diff --git a/TestStack.FluentMvcTesting/ControllerResultTest.cs b/TestStack.FluentMvcTesting/ControllerResultTest.cs index 3bec692..522bd57 100644 --- a/TestStack.FluentMvcTesting/ControllerResultTest.cs +++ b/TestStack.FluentMvcTesting/ControllerResultTest.cs @@ -215,52 +215,6 @@ public ViewResultTest ShouldRenderDefaultPartialView() #endregion - public FileStreamResult ShouldRenderFileStream(byte[] content, string contentType = null) - { - return ShouldRenderFileStream(new MemoryStream(content), contentType); - } - - public FileStreamResult ShouldRenderFileStream(Stream stream = null, string contentType = null) - { - ValidateActionReturnType(); - - var fileResult = (FileStreamResult)_actionResult; - - if (contentType != null && fileResult.ContentType != contentType) - { - throw new ActionResultAssertionException(string.Format( - "Expected stream to be of content type '{0}', but instead was given '{1}'.", - contentType, - fileResult.ContentType)); - } - - if (stream != null) - { - byte[] expected = ConvertStreamToArray(stream); - byte[] actual = ConvertStreamToArray(fileResult.FileStream); - - if (!expected.SequenceEqual(actual)) - { - throw new ActionResultAssertionException(string.Format( - "Expected stream contents to be equal to [{0}], but instead was given [{1}].", - string.Join(", ", expected), - string.Join(", ", actual))); - } - } - - return fileResult; - } - - private static byte[] ConvertStreamToArray(Stream stream) - { - using (var memoryStream = new MemoryStream()) - { - stream.CopyTo(memoryStream); - stream.Position = 0; - return memoryStream.ToArray(); - } - } - #region File Results public FileResult ShouldRenderAnyFile(string contentType = null) @@ -339,6 +293,80 @@ public FileContentResult ShouldRenderFile(string contentType = null) return fileResult; } + public FileStreamResult ShouldRenderFileStream(byte[] content, string contentType = null) + { + return ShouldRenderFileStream(new MemoryStream(content), contentType); + } + + public FileStreamResult ShouldRenderFileStream(Stream stream = null, string contentType = null) + { + ValidateActionReturnType(); + var fileResult = (FileStreamResult)_actionResult; + + if (contentType != null && fileResult.ContentType != contentType) + { + throw new ActionResultAssertionException(string.Format( + "Expected stream to be of content type '{0}', but instead was given '{1}'.", + contentType, + fileResult.ContentType)); + } + + if (stream != null) + { + // This is not optimal but I do not want to pre-optimize in this case. + byte[] expected = ConvertStreamToArray(stream); + byte[] actual = ConvertStreamToArray(fileResult.FileStream); + + if (!expected.SequenceEqual(actual)) + { + throw new ActionResultAssertionException(string.Format( + "Expected stream contents to be equal to [{0}], but instead was given [{1}].", + string.Join(", ", expected), + string.Join(", ", actual))); + } + } + + return fileResult; + } + + public FileStreamResult ShouldRenderFileStream(string contents, string contentType = null, Encoding encoding = null) + { + ValidateActionReturnType(); + var fileResult = (FileStreamResult)_actionResult; + + if (contentType != null && fileResult.ContentType != contentType) + { + throw new ActionResultAssertionException(string.Format( + "Expected stream to be of content type '{0}', but instead was given '{1}'.", + contentType, + fileResult.ContentType)); + } + + if (encoding == null) + encoding = Encoding.UTF8; + + var reconstitutedText = encoding.GetString(ConvertStreamToArray(fileResult.FileStream)); + if (contents != reconstitutedText) + { + throw new ActionResultAssertionException(string.Format( + "Expected file contents to be \"{0}\", but instead was \"{1}\".", + contents, + reconstitutedText)); + } + + return fileResult; + } + + private static byte[] ConvertStreamToArray(Stream stream) + { + using (var memoryStream = new MemoryStream()) + { + stream.CopyTo(memoryStream); + stream.Position = 0; + return memoryStream.ToArray(); + } + } + public FilePathResult ShouldRenderFilePath(string fileName = null, string contentType = null) { ValidateActionReturnType(); From 7484d7344774d5b2a797fd7e3c5e52612b721a62 Mon Sep 17 00:00:00 2001 From: ByteBlast Date: Mon, 1 Sep 2014 20:02:38 +0100 Subject: [PATCH 07/17] Updated solution file to reflect the nuspec file relocation. --- .../TestStack.FluentMVCTesting.Mvc3.csproj | 4 ++-- TestStack.FluentMvcTesting/TestStack.FluentMVCTesting.csproj | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/TestStack.FluentMVCTesting.Mvc3/TestStack.FluentMVCTesting.Mvc3.csproj b/TestStack.FluentMVCTesting.Mvc3/TestStack.FluentMVCTesting.Mvc3.csproj index 845ddd4..b6989a6 100644 --- a/TestStack.FluentMVCTesting.Mvc3/TestStack.FluentMVCTesting.Mvc3.csproj +++ b/TestStack.FluentMVCTesting.Mvc3/TestStack.FluentMVCTesting.Mvc3.csproj @@ -94,8 +94,8 @@ - + - + \ No newline at end of file diff --git a/TestStack.FluentMvcTesting/TestStack.FluentMVCTesting.csproj b/TestStack.FluentMvcTesting/TestStack.FluentMVCTesting.csproj index eb13407..a3e6b20 100644 --- a/TestStack.FluentMvcTesting/TestStack.FluentMVCTesting.csproj +++ b/TestStack.FluentMvcTesting/TestStack.FluentMVCTesting.csproj @@ -83,8 +83,8 @@ - + - + \ No newline at end of file From 6df127ca5f19456601bf4ef01be3f4aa5a4b360b Mon Sep 17 00:00:00 2001 From: ByteBlast Date: Mon, 1 Sep 2014 20:29:16 +0100 Subject: [PATCH 08/17] Updated version and documented breaking change. --- BREAKING_CHANGES.md | 26 ++++++++++++++++++++++++++ NextVersion.txt | 2 +- 2 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 BREAKING_CHANGES.md diff --git a/BREAKING_CHANGES.md b/BREAKING_CHANGES.md new file mode 100644 index 0000000..124fc6d --- /dev/null +++ b/BREAKING_CHANGES.md @@ -0,0 +1,26 @@ +# Version 2.0.0 + +## ShouldRenderFileStream Method + +The following overload of the `ShouldRenderFile` method has been *replaced*: + + public FileStreamResult ShouldRenderFileStream(string contentType = null) + +I place emphasis on the word "replace" because it is important to note that this overload has not been removed but replaced - this means that you will not encounter a compile-time error but you will encounter a logical error when you run the test. + +### Reason + +The aforementioned overload has been replaced in order to enable an overload that takes an actual stream for comparison in a way that is consistent with the existing convention. + +### Fix + +Use a [named argument](http://msdn.microsoft.com/en-gb/library/dd264739.aspx). + +As where you would have previously done this: + + .ShouldRenderFileStream("application/json"); + +You must now do this: + + .ShouldRenderFileStream(contentType: "application/json"); + \ No newline at end of file diff --git a/NextVersion.txt b/NextVersion.txt index 1cc5f65..359a5b9 100644 --- a/NextVersion.txt +++ b/NextVersion.txt @@ -1 +1 @@ -1.1.0 \ No newline at end of file +2.0.0 \ No newline at end of file From 3ffd3c09dc175e1fd8bf36e55ec5d5e893be09a5 Mon Sep 17 00:00:00 2001 From: ByteBlast Date: Tue, 2 Sep 2014 07:47:58 +0100 Subject: [PATCH 09/17] Removed errant comment. --- TestStack.FluentMvcTesting/ControllerResultTest.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/TestStack.FluentMvcTesting/ControllerResultTest.cs b/TestStack.FluentMvcTesting/ControllerResultTest.cs index 522bd57..a20d39c 100644 --- a/TestStack.FluentMvcTesting/ControllerResultTest.cs +++ b/TestStack.FluentMvcTesting/ControllerResultTest.cs @@ -313,7 +313,6 @@ public FileStreamResult ShouldRenderFileStream(Stream stream = null, string cont if (stream != null) { - // This is not optimal but I do not want to pre-optimize in this case. byte[] expected = ConvertStreamToArray(stream); byte[] actual = ConvertStreamToArray(fileResult.FileStream); From fddfd5f16ccd390617997bdfc19752cf613faf05 Mon Sep 17 00:00:00 2001 From: ByteBlast Date: Tue, 2 Sep 2014 08:06:53 +0100 Subject: [PATCH 10/17] Removed ShouldRenderFile and documented breaking change. --- BREAKING_CHANGES.md | 22 +++++++++++++++++-- .../ControllerResultTestTests.cs | 14 ------------ .../ControllerResultTest.cs | 15 ------------- 3 files changed, 20 insertions(+), 31 deletions(-) diff --git a/BREAKING_CHANGES.md b/BREAKING_CHANGES.md index 124fc6d..5f88852 100644 --- a/BREAKING_CHANGES.md +++ b/BREAKING_CHANGES.md @@ -18,9 +18,27 @@ Use a [named argument](http://msdn.microsoft.com/en-gb/library/dd264739.aspx). As where you would have previously done this: - .ShouldRenderFileStream("application/json"); + ShouldRenderFileStream("application/json"); You must now do this: - .ShouldRenderFileStream(contentType: "application/json"); + ShouldRenderFileStream(contentType: "application/json"); + + +## ShouldRenderFileMethod + +The `ShouldRenderFile` method has been removed. + +### Reason + +The `ShouldRenderFile` method was equivocal because it had the possibility to be interperted to test for a `FileResult` when in fact, it tested for a `FileContentResult`. + +It is for this reason that we introduced two unequivocal methods namely, `ShouldRenderAnyFile` and `ShouldRenderFileContents`. + +### Fix + +Use the `ShouldRenderFileContents` method instead: + + ShouldRenderAnyFile() + ShouldRenderAnyFile(contentType: "application/json") \ No newline at end of file diff --git a/TestStack.FluentMVCTesting.Tests/ControllerResultTestTests.cs b/TestStack.FluentMVCTesting.Tests/ControllerResultTestTests.cs index 42c5b32..ae2355d 100644 --- a/TestStack.FluentMVCTesting.Tests/ControllerResultTestTests.cs +++ b/TestStack.FluentMVCTesting.Tests/ControllerResultTestTests.cs @@ -28,8 +28,6 @@ class ControllerResultTestShould ReturnType(t => t.ShouldRenderDefaultView()), ReturnType(t => t.ShouldRenderPartialView("")), ReturnType(t => t.ShouldRenderDefaultPartialView()), - ReturnType(t => t.ShouldRenderFile()), - ReturnType(t => t.ShouldRenderFile("")), ReturnType(t => t.ShouldRenderFileContents()), ReturnType(t => t.ShouldRenderFileContents(new byte[0])), ReturnType(t => t.ShouldRenderFileContents(new byte[0], "")), @@ -479,18 +477,6 @@ public void Check_for_file_content_result_and_check_textual_content_using_invali _controller.WithCallTo(c => c.TextualFile()).ShouldRenderFileContents(ControllerResultTestController.TextualFileContent, ControllerResultTestController.FileContentType, Encoding.BigEndianUnicode)); } - [Test] - public void Check_for_file_result() - { - _controller.WithCallTo(c => c.EmptyFile()).ShouldRenderFile(); - } - - [Test] - public void Check_for_file_result_and_check_content_type() - { - _controller.WithCallTo(c => c.EmptyFile()).ShouldRenderFile(ControllerResultTestController.FileContentType); - } - [Test] public void Check_for_file_stream_result() { diff --git a/TestStack.FluentMvcTesting/ControllerResultTest.cs b/TestStack.FluentMvcTesting/ControllerResultTest.cs index a20d39c..4d1bb1c 100644 --- a/TestStack.FluentMvcTesting/ControllerResultTest.cs +++ b/TestStack.FluentMvcTesting/ControllerResultTest.cs @@ -278,21 +278,6 @@ public FileContentResult ShouldRenderFileContents(string contents, string conten return fileResult; } - [Obsolete("Obsolete: Use ShouldRenderFileContents instead.")] - public FileContentResult ShouldRenderFile(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)); - } - - return fileResult; - } - public FileStreamResult ShouldRenderFileStream(byte[] content, string contentType = null) { return ShouldRenderFileStream(new MemoryStream(content), contentType); From be16de51ea9cbffc876628e2b70031259c008de7 Mon Sep 17 00:00:00 2001 From: ByteBlast Date: Tue, 2 Sep 2014 08:13:29 +0100 Subject: [PATCH 11/17] Tweaked BREAKING_CHANGES.MD. --- BREAKING_CHANGES.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/BREAKING_CHANGES.md b/BREAKING_CHANGES.md index 5f88852..d775341 100644 --- a/BREAKING_CHANGES.md +++ b/BREAKING_CHANGES.md @@ -2,15 +2,15 @@ ## ShouldRenderFileStream Method -The following overload of the `ShouldRenderFile` method has been *replaced*: +The following overload of the `ShouldRenderFileStream` method has been *replaced*: public FileStreamResult ShouldRenderFileStream(string contentType = null) -I place emphasis on the word "replace" because it is important to note that this overload has not been removed but replaced - this means that you will not encounter a compile-time error but you will encounter a logical error when you run the test. +We place emphasis on the word "replace" because it is important to note that this overload has not been removed but replaced - you will not encounter a compile-time error if you upgrade, but you will encounter a logical error when you run your existing test. ### Reason -The aforementioned overload has been replaced in order to enable an overload that takes an actual stream for comparison in a way that is consistent with the existing convention. +The aforementioned overload has been replaced in order to enable an overload that takes a stream for comparison in a way that is consistent with the existing convention. ### Fix From f641439a577d0eaf46e5d495d6abf4995381e92c Mon Sep 17 00:00:00 2001 From: ByteBlast Date: Tue, 2 Sep 2014 08:34:03 +0100 Subject: [PATCH 12/17] Added readme.txt to the root package. --- .../TestStack.FluentMVCTesting.Mvc3.csproj | 3 +++ TestStack.FluentMVCTesting.Mvc3/readme.txt | 6 ++++++ .../TestStack.FluentMVCTesting.csproj | 3 +++ TestStack.FluentMvcTesting/readme.txt | 6 ++++++ 4 files changed, 18 insertions(+) create mode 100644 TestStack.FluentMVCTesting.Mvc3/readme.txt create mode 100644 TestStack.FluentMvcTesting/readme.txt diff --git a/TestStack.FluentMVCTesting.Mvc3/TestStack.FluentMVCTesting.Mvc3.csproj b/TestStack.FluentMVCTesting.Mvc3/TestStack.FluentMVCTesting.Mvc3.csproj index b6989a6..d410b17 100644 --- a/TestStack.FluentMVCTesting.Mvc3/TestStack.FluentMVCTesting.Mvc3.csproj +++ b/TestStack.FluentMVCTesting.Mvc3/TestStack.FluentMVCTesting.Mvc3.csproj @@ -97,6 +97,9 @@ + + +