diff --git a/NextVersion.txt b/NextVersion.txt index 359a5b9..50aea0e 100644 --- a/NextVersion.txt +++ b/NextVersion.txt @@ -1 +1 @@ -2.0.0 \ No newline at end of file +2.1.0 \ No newline at end of file diff --git a/TestStack.FluentMVCTesting.Tests/ControllerResultTestTests.cs b/TestStack.FluentMVCTesting.Tests/ControllerResultTestTests.cs index 63acbcc..217e0e7 100644 --- a/TestStack.FluentMVCTesting.Tests/ControllerResultTestTests.cs +++ b/TestStack.FluentMVCTesting.Tests/ControllerResultTestTests.cs @@ -49,6 +49,10 @@ class ControllerResultTestShould ReturnType(t => t.ShouldRenderAnyFile()), ReturnType(t => t.ShouldGiveHttpStatus()), ReturnType(t => t.ShouldReturnJson()), + ReturnType(t => t.ShouldReturnContent()), + ReturnType(t => t.ShouldReturnContent("")), + ReturnType(t => t.ShouldReturnContent("", "")), + ReturnType(t => t.ShouldReturnContent("", "", Encoding.UTF8)) }; // Different ways that action redirects can be asserted along with the expected method name and the correct controller action call for that assertion private static readonly List ActionRedirects = new List @@ -770,5 +774,84 @@ public void Allow_the_object_that_is_returned_to_be_checked() _controller.WithCallTo(c => c.Json()).ShouldReturnJson(d => Assert.That(d, Is.EqualTo(ControllerResultTestController.JsonValue))); } #endregion + + #region Content tests + + [Test] + public void Check_for_content_result() + { + _controller.WithCallTo(c => c.Content()).ShouldReturnContent(); + } + + [Test] + public void Check_for_content_result_and_check_content() + { + _controller.WithCallTo(c => c.Content()).ShouldReturnContent(ControllerResultTestController.TextualContent); + } + + [Test] + public void Check_for_content_result_and_check_invalid_content() + { + const string content = "dummy contents"; + + var exception = Assert.Throws(() => _controller.WithCallTo(c => c.Content()).ShouldReturnContent(content)); + + Assert.That(exception.Message, Is.EqualTo(string.Format("Expected content to be \"{0}\", but instead was \"{1}\".", content, ControllerResultTestController.TextualContent))); + } + + [Test] + public void Check_for_content_result_and_check_content_and_check_content_type() + { + _controller.WithCallTo(c => c.Content()).ShouldReturnContent(ControllerResultTestController.TextualContent, ControllerResultTestController.ContentType); + } + + [Test] + public void Check_for_content_result_and_check_content_and_check_invalid_content_type() + { + const string contentType = "application/dummy"; + + var exception = Assert.Throws(() => _controller.WithCallTo(c => c.Content()).ShouldReturnContent(ControllerResultTestController.TextualContent, contentType)); + + Assert.That(exception.Message, Is.EqualTo(string.Format("Expected content type to be \"{0}\", but instead was \"{1}\".", contentType, ControllerResultTestController.ContentType))); + } + + [Test] + public void Check_for_content_result_and_check_content_and_check_content_type_and_check_content_encoding() + { + _controller.WithCallTo(c => c.Content()).ShouldReturnContent(ControllerResultTestController.TextualContent, ControllerResultTestController.ContentType, ControllerResultTestController.TextualContentEncoding); + } + + [Test] + public void Check_for_content_result_and_check_content_and_check_content_type_and_check_invalid_content_encoding() + { + var encoding = Encoding.Unicode; + + var exception = Assert.Throws(() => _controller.WithCallTo(c => c.Content()).ShouldReturnContent(ControllerResultTestController.TextualContent, ControllerResultTestController.ContentType, encoding)); + + Assert.That(exception.Message, Is.EqualTo(string.Format("Expected encoding to be equal to {0}, but instead was {1}.", encoding.EncodingName, ControllerResultTestController.TextualContentEncoding.EncodingName))); + } + + [Test] + public void Check_for_content_result_and_check_invalid_content_and_check_invalid_content_type_and_check_invalid_encoding() + { + const string contentType = "application/dummy"; + const string content = "dumb"; + Encoding encoding = Encoding.Unicode; + + var exception = Assert.Throws(() => _controller.WithCallTo(c => c.Content()).ShouldReturnContent(content, contentType, encoding)); + + // Assert that the content type validation occurs before that of the actual content. + Assert.That(exception.Message.Contains("content type")); + } + + [Test] + public void Emit_readable_error_message_when_the_actual_content_encoding_has_not_been_specified() + { + var exception = Assert.Throws(() => _controller.WithCallTo(c => c.ContentWithoutEncodingSpecified()).ShouldReturnContent(encoding: ControllerResultTestController.TextualContentEncoding)); + + Assert.That(exception.Message, Is.EqualTo(string.Format("Expected encoding to be equal to {0}, but instead was null.", ControllerResultTestController.TextualContentEncoding.EncodingName))); + } + + #endregion } } diff --git a/TestStack.FluentMVCTesting.Tests/TestControllers/ControllerResultTestController.cs b/TestStack.FluentMVCTesting.Tests/TestControllers/ControllerResultTestController.cs index 2f1e9e4..8d7aa0f 100644 --- a/TestStack.FluentMVCTesting.Tests/TestControllers/ControllerResultTestController.cs +++ b/TestStack.FluentMVCTesting.Tests/TestControllers/ControllerResultTestController.cs @@ -21,7 +21,9 @@ class ControllerResultTestController : Controller public static byte[] EmptyFileBuffer = { }; public static readonly Stream EmptyStreamContents = new MemoryStream(EmptyFileBuffer); public static readonly Stream BinaryStreamContents = new MemoryStream(BinaryFileContents); - + public const string TextualContent = "textual content"; + public static readonly Encoding TextualContentEncoding = Encoding.UTF8; + public const string ContentType = "application/contentType"; #endregion #region Empty, Null and Random Results @@ -227,6 +229,20 @@ public ActionResult Json() return Json(JsonValue); } #endregion + + #region Content + + public ActionResult Content() + { + return Content(TextualContent, ContentType, TextualContentEncoding); + } + + public ActionResult ContentWithoutEncodingSpecified() + { + return Content(TextualContent, ContentType); + } + + #endregion } #region Test Classes diff --git a/TestStack.FluentMvcTesting/ControllerResultTest.cs b/TestStack.FluentMvcTesting/ControllerResultTest.cs index 0570b06..38a778a 100644 --- a/TestStack.FluentMvcTesting/ControllerResultTest.cs +++ b/TestStack.FluentMvcTesting/ControllerResultTest.cs @@ -396,5 +396,41 @@ public void ShouldReturnJson(Action assertion) assertion(jsonResult.Data); } #endregion + + #region Content + + public ContentResult ShouldReturnContent(string content = null, string contentType = null, Encoding encoding = null) + { + ValidateActionReturnType(); + var contentResult = (ContentResult) _actionResult; + + if (contentType != null && contentType != contentResult.ContentType) + { + throw new ActionResultAssertionException(string.Format( + "Expected content type to be \"{0}\", but instead was \"{1}\".", + contentType, + contentResult.ContentType)); + } + + if (content != null && content != contentResult.Content) + { + throw new ActionResultAssertionException(string.Format( + "Expected content to be \"{0}\", but instead was \"{1}\".", + content, + contentResult.Content)); + } + + if (encoding != null && encoding != contentResult.ContentEncoding) + { + throw new ActionResultAssertionException(string.Format( + "Expected encoding to be equal to {0}, but instead was {1}.", + encoding.EncodingName, + contentResult.ContentEncoding != null ? contentResult.ContentEncoding.EncodingName : "null")); + } + + return contentResult; + } + + #endregion } } \ No newline at end of file