diff --git a/BREAKING_CHANGES.md b/BREAKING_CHANGES.md new file mode 100644 index 0000000..cbe1a76 --- /dev/null +++ b/BREAKING_CHANGES.md @@ -0,0 +1,44 @@ +# Version 2.0.0 + +## ShouldRenderFileStream Method + +The following overload of the `ShouldRenderFileStream` method has been *replaced*: + + public FileStreamResult ShouldRenderFileStream(string contentType = null) + +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 a 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"); + + +## ShouldRenderFileMethod + +The `ShouldRenderFile` method has been removed. + +### Reason + +The `ShouldRenderFile` method was ambiguous 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/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 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.Mvc3/TestStack.FluentMVCTesting.Mvc3.nuspec b/TestStack.FluentMVCTesting.Mvc3/TestStack.FluentMVCTesting.Mvc3.nuspec index 969dc17..6ae3ad9 100644 --- a/TestStack.FluentMVCTesting.Mvc3/TestStack.FluentMVCTesting.Mvc3.nuspec +++ b/TestStack.FluentMVCTesting.Mvc3/TestStack.FluentMVCTesting.Mvc3.nuspec @@ -32,6 +32,10 @@ + + For breaking changes from previous versions see + https://github.com/TestStack/TestStack.FluentMVCTesting/blob/master/BREAKING_CHANGES.md + @@ -40,5 +44,6 @@ + diff --git a/TestStack.FluentMVCTesting.Tests/ControllerResultTestTests.cs b/TestStack.FluentMVCTesting.Tests/ControllerResultTestTests.cs index a4e82db..63acbcc 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.IO; using System.Linq; using System.Linq.Expressions; using System.Net; @@ -27,19 +28,24 @@ 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.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("", "")), + ReturnType(t => t.ShouldRenderFileStream()), + 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.ShouldRenderFileStream("")), + ReturnType(t => t.ShouldRenderFileStream("", "")), + ReturnType(t => t.ShouldRenderFileStream("", "", Encoding.UTF8)), ReturnType(t => t.ShouldRenderAnyFile()), ReturnType(t => t.ShouldGiveHttpStatus()), ReturnType(t => t.ShouldReturnJson()), @@ -393,7 +399,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. @@ -403,7 +409,7 @@ public void Check_for_file_content_result_and_check_invalid_binary_content_and_c [Test] public void Check_for_file_content_result_and_check_textual_contents() { - _controller.WithCallTo(c => c.TextualFile()).ShouldRenderFileContents(ControllerResultTestController.TextualFileContents); + _controller.WithCallTo(c => c.TextualFile()).ShouldRenderFileContents(ControllerResultTestController.TextualFileContent); } [Test] @@ -414,13 +420,13 @@ public void Check_for_file_content_result_and_check_invalid_textual_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))); + 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_and_check_content_result() { - _controller.WithCallTo(c => c.TextualFile()).ShouldRenderFileContents(ControllerResultTestController.TextualFileContents, ControllerResultTestController.FileContentType); + _controller.WithCallTo(c => c.TextualFile()).ShouldRenderFileContents(ControllerResultTestController.TextualFileContent, ControllerResultTestController.FileContentType); } [Test] @@ -429,7 +435,7 @@ public void Check_for_file_content_result_and_check_textual_content_and_check_in const string contentType = "application/dummy"; var exception = Assert.Throws(() => - _controller.WithCallTo(c => c.TextualFile()).ShouldRenderFileContents(ControllerResultTestController.TextualFileContents, contentType)); + _controller.WithCallTo(c => c.TextualFile()).ShouldRenderFileContents(ControllerResultTestController.TextualFileContent, 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))); } @@ -453,7 +459,7 @@ public void Check_for_file_content_result_and_check_textual_content_using_given_ var encoding = Encoding.BigEndianUnicode; _controller.WithCallTo(c => c.TextualFile(encoding)) - .ShouldRenderFileContents(ControllerResultTestController.TextualFileContents, encoding: encoding); + .ShouldRenderFileContents(ControllerResultTestController.TextualFileContent, encoding: encoding); } [Test] @@ -461,38 +467,67 @@ public void Check_for_file_content_result_and_check_textual_content_using_given_ { var encoding = Encoding.BigEndianUnicode; - _controller.WithCallTo(c => c.TextualFile(encoding)).ShouldRenderFileContents(ControllerResultTestController.TextualFileContents, ControllerResultTestController.FileContentType, encoding); + _controller.WithCallTo(c => c.TextualFile(encoding)).ShouldRenderFileContents(ControllerResultTestController.TextualFileContent, 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)); + _controller.WithCallTo(c => c.TextualFile()).ShouldRenderFileContents(ControllerResultTestController.TextualFileContent, ControllerResultTestController.FileContentType, Encoding.BigEndianUnicode)); } [Test] - public void Check_for_file_result() + public void Check_for_file_stream_result() { - _controller.WithCallTo(c => c.EmptyFile()).ShouldRenderFile(); + _controller.WithCallTo(c => c.EmptyStream()) + .ShouldRenderFileStream(); } [Test] - public void Check_for_file_result_and_check_content_type() + public void Check_for_file_stream_result_and_check_stream_content() { - _controller.WithCallTo(c => c.EmptyFile()).ShouldRenderFile(ControllerResultTestController.FileContentType); + _controller.WithCallTo(c => c.EmptyStream()) + .ShouldRenderFileStream(ControllerResultTestController.EmptyStreamContents); } [Test] - public void Check_for_file_stream_result() + public void Check_for_file_stream_result_and_check_invalid_stream_content() { - _controller.WithCallTo(c => c.EmptyStream()).ShouldRenderFileStream(); + 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 file 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_content() + { + var buffer = new byte[] { 1, 2 }; + var stream = new MemoryStream(buffer); + + var exception = Assert.Throws(() => + _controller.WithCallTo(c => c.BinaryStream()).ShouldRenderFileStream(stream)); + + var expected = string.Format("[{0}]", string.Join(", ", buffer)); + var actual = string.Format("[{0}]", string.Join(", ", ControllerResultTestController.BinaryFileContents)); + var message = string.Format("Expected file 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.FileContentType); + _controller.WithCallTo(c => c.EmptyStream()) + .ShouldRenderFileStream(ControllerResultTestController.EmptyStreamContents,ControllerResultTestController.FileContentType); } [Test] @@ -501,9 +536,153 @@ 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)); + _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 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_content_and_check_invalid_content_type() + { + const string contentType = "application/dummy"; + var buffer = new byte[] { 1, 2 }; + var stream = new MemoryStream(buffer); + + var exception = Assert.Throws(() => + _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_stream_result_and_check_binary_content() + { + _controller.WithCallTo(c => c.BinaryStream()) + .ShouldRenderFileStream(ControllerResultTestController.BinaryFileContents); + } + + [Test] + public void Check_for_file_stream_result_and_check_invalid_binary_content() + { + var content = new byte[] { 1, 2 }; + + var exception = Assert.Throws(() => + _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 file 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_content_and_check_content_type() + { + _controller.WithCallTo(c => c.BinaryStream()) + .ShouldRenderFileStream(ControllerResultTestController.BinaryStreamContents, ControllerResultTestController.FileContentType); + } + + [Test] + 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.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))); + } + + [Test] + public void Check_for_file_stream_result_and_check_invalid_binary_content_and_check_invalid_content_type() + { + var content = new byte[] { 1, 2 }; + const string contentType = "application/dummy"; + + var exception = Assert.Throws(() => + _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_stream_result_and_check_textual_content() + { + _controller.WithCallTo(c => c.TextualStream()) + .ShouldRenderFileStream(ControllerResultTestController.TextualFileContent); + } + + [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_stream_result_and_check_textual_content_and_check_content_type() + { + _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)); + + 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_textual_content_and_check_invalid_content_type() + { + 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_stream_result_and_check_textual_content_using_given_char_encoding() + { + var encoding = Encoding.BigEndianUnicode; + + _controller.WithCallTo(c => c.TextualStream(encoding)) + .ShouldRenderFileStream(ControllerResultTestController.TextualFileContent, encoding: encoding); + } + + [Test] + public void Check_for_file_stream_result_and_check_textual_content_using_given_char_encoding_and_check_content_type() + { + 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 6a8b61a..2f1e9e4 100644 --- a/TestStack.FluentMVCTesting.Tests/TestControllers/ControllerResultTestController.cs +++ b/TestStack.FluentMVCTesting.Tests/TestControllers/ControllerResultTestController.cs @@ -17,7 +17,11 @@ 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 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 #region Empty, Null and Random Results @@ -174,14 +178,30 @@ public ActionResult TextualFile() public ActionResult TextualFile(Encoding encoding) { - var encodedContents = encoding.GetBytes(TextualFileContents); + var encodedContents = encoding.GetBytes(TextualFileContent); return File(encodedContents, FileContentType); } public ActionResult EmptyStream() { - var content = new MemoryStream(); - return File(content, FileContentType); + return File(EmptyStreamContents, FileContentType); + } + + public ActionResult BinaryStream() + { + return File(BinaryStreamContents, FileContentType); + } + + public ActionResult TextualStream() + { + return TextualStream(Encoding.UTF8); + } + + public ActionResult TextualStream(Encoding encoding) + { + 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 ecaf91e..0570b06 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; @@ -216,30 +217,42 @@ public ViewResultTest ShouldRenderDefaultPartialView() #region File Results - public FileResult ShouldRenderAnyFile(string contentType = null) + private static void EnsureContentTypeIsSame(string actual, string expected) { - ValidateActionReturnType(); - - var fileResult = (FileResult)_actionResult; + if (expected == null) return; + if (actual != expected) + { + throw new ActionResultAssertionException(string.Format( + "Expected file to be of content type '{0}', but instead was given '{1}'.", expected, actual)); + } + } - if (contentType != null && fileResult.ContentType != contentType) + private static byte[] ConvertStreamToArray(Stream stream) + { + using (var memoryStream = new MemoryStream()) { - throw new ActionResultAssertionException(string.Format("Expected file to be of content type '{0}', but instead was given '{1}'.", contentType, fileResult.ContentType)); + stream.CopyTo(memoryStream); + stream.Position = 0; + return memoryStream.ToArray(); } + } + + public FileResult ShouldRenderAnyFile(string contentType = null) + { + ValidateActionReturnType(); + var fileResult = (FileResult)_actionResult; + EnsureContentTypeIsSame(fileResult.ContentType, contentType); + return fileResult; } public FileContentResult ShouldRenderFileContents(byte[] contents = null, string contentType = null) { ValidateActionReturnType(); - var fileResult = (FileContentResult) _actionResult; - if (contentType != null && fileResult.ContentType != contentType) - { - throw new ActionResultAssertionException(string.Format("Expected file to be of content type '{0}', but instead was given '{1}'.", contentType, fileResult.ContentType)); - } + EnsureContentTypeIsSame(fileResult.ContentType, contentType); if (contents != null && !fileResult.FileContents.SequenceEqual(contents)) { @@ -255,15 +268,9 @@ public FileContentResult ShouldRenderFileContents(byte[] contents = null, string 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)); - } + EnsureContentTypeIsSame(fileResult.ContentType, contentType); if (encoding == null) encoding = Encoding.UTF8; @@ -271,36 +278,62 @@ public FileContentResult ShouldRenderFileContents(string contents, string conten 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)); + 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) + public FileStreamResult ShouldRenderFileStream(byte[] content, string contentType = null) { - ValidateActionReturnType(); + var reconstitutedStream = new MemoryStream(content); + return ShouldRenderFileStream(reconstitutedStream, contentType); + } - var fileResult = (FileContentResult)_actionResult; + public FileStreamResult ShouldRenderFileStream(Stream stream = null, string contentType = null) + { + ValidateActionReturnType(); + var fileResult = (FileStreamResult)_actionResult; + + EnsureContentTypeIsSame(fileResult.ContentType, contentType); - if (contentType != null && fileResult.ContentType != contentType) + if (stream != null) { - throw new ActionResultAssertionException(string.Format("Expected file to be of content type '{0}', but instead was given '{1}'.", contentType, fileResult.ContentType)); + byte[] expected = ConvertStreamToArray(stream); + byte[] actual = ConvertStreamToArray(fileResult.FileStream); + + if (!expected.SequenceEqual(actual)) + { + throw new ActionResultAssertionException(string.Format( + "Expected file contents to be equal to [{0}], but instead was given [{1}].", + string.Join(", ", expected), + string.Join(", ", actual))); + } } return fileResult; } - public FileStreamResult ShouldRenderFileStream(string contentType = null) + public FileStreamResult ShouldRenderFileStream(string contents, string contentType = null, Encoding encoding = null) { ValidateActionReturnType(); - var fileResult = (FileStreamResult)_actionResult; - if (contentType != null && fileResult.ContentType != contentType) + EnsureContentTypeIsSame(fileResult.ContentType, contentType); + + if (encoding == null) + encoding = Encoding.UTF8; + + var reconstitutedText = encoding.GetString(ConvertStreamToArray(fileResult.FileStream)); + if (contents != reconstitutedText) { - 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 contents to be \"{0}\", but instead was \"{1}\".", + contents, + reconstitutedText)); } return fileResult; @@ -309,17 +342,16 @@ public FileStreamResult ShouldRenderFileStream(string contentType = null) public FilePathResult ShouldRenderFilePath(string fileName = null, string contentType = null) { ValidateActionReturnType(); - var fileResult = (FilePathResult)_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)); - } + EnsureContentTypeIsSame(fileResult.ContentType, contentType); if (fileName != null && fileName != fileResult.FileName) { - 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 name to be '{0}', but instead was given '{1}'.", + fileName, + fileResult.FileName)); } return fileResult; diff --git a/TestStack.FluentMvcTesting/TestStack.FluentMVCTesting.csproj b/TestStack.FluentMvcTesting/TestStack.FluentMVCTesting.csproj index eb13407..9f4f300 100644 --- a/TestStack.FluentMvcTesting/TestStack.FluentMVCTesting.csproj +++ b/TestStack.FluentMvcTesting/TestStack.FluentMVCTesting.csproj @@ -83,8 +83,11 @@ - + + + + - + \ No newline at end of file diff --git a/TestStack.FluentMvcTesting/TestStack.FluentMVCTesting.nuspec b/TestStack.FluentMvcTesting/TestStack.FluentMVCTesting.nuspec index 1378510..d0ae86e 100644 --- a/TestStack.FluentMvcTesting/TestStack.FluentMVCTesting.nuspec +++ b/TestStack.FluentMvcTesting/TestStack.FluentMVCTesting.nuspec @@ -32,6 +32,10 @@ + + For breaking changes from previous versions see + https://github.com/TestStack/TestStack.FluentMVCTesting/blob/master/BREAKING_CHANGES.md + @@ -40,5 +44,6 @@ + diff --git a/TestStack.FluentMvcTesting/readme.txt b/TestStack.FluentMvcTesting/readme.txt new file mode 100644 index 0000000..8071f44 --- /dev/null +++ b/TestStack.FluentMvcTesting/readme.txt @@ -0,0 +1,6 @@ +FluentMVCTesting +============== + +* For the project homepage including the current README, see https://github.com/TestStack/TestStack.FluentMVCTesting +* To check for potential breaking changes in this release, see https://github.com/TestStack/TestStack.FluentMVCTesting/blob/master/BREAKING_CHANGES.md +* If you need to raise an issue or check for an existing issue, see https://github.com/TestStack/TestStack.FluentMVCTesting/issues \ No newline at end of file