Skip to content

Commit e17744c

Browse files
committed
Added support for checking a file content result's textual contents using a specific encoding.
1 parent fb51fb5 commit e17744c

File tree

3 files changed

+41
-4
lines changed

3 files changed

+41
-4
lines changed

TestStack.FluentMVCTesting.Tests/ControllerResultTestTests.cs

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
using System.Web.Mvc;
88
using NUnit.Framework;
99
using TestStack.FluentMVCTesting.Tests.TestControllers;
10+
using System.Text;
11+
1012

1113
namespace TestStack.FluentMVCTesting.Tests
1214
{
@@ -34,6 +36,7 @@ class ControllerResultTestShould
3436
ReturnType<FileContentResult>(t => t.ShouldRenderFileContents(new byte[0], "")),
3537
ReturnType<FileContentResult>(t => t.ShouldRenderFileContents("")),
3638
ReturnType<FileContentResult>(t => t.ShouldRenderFileContents("", "")),
39+
ReturnType<FileContentResult>(t => t.ShouldRenderFileContents("", "", Encoding.UTF8)),
3740
ReturnType<FileStreamResult>(t => t.ShouldRenderFileStream("")),
3841
ReturnType<FilePathResult>(t => t.ShouldRenderFilePath()),
3942
ReturnType<FilePathResult>(t => t.ShouldRenderFilePath("")),
@@ -313,7 +316,9 @@ public void Check_for_invalid_partial_name()
313316
Assert.That(exception.Message, Is.EqualTo(string.Format("Expected result view to be '{0}', but instead was given '{1}'.", ControllerResultTestController.PartialName, ControllerResultTestController.RandomViewName)));
314317
}
315318
#endregion
316-
319+
320+
321+
317322
#region File tests
318323

319324
[Test]
@@ -445,6 +450,32 @@ public void Check_for_file_content_result_and_check_invalid_textual_content_and_
445450
Assert.That(exception.Message.Contains("content type"));
446451
}
447452

453+
[Test]
454+
public void Check_for_file_content_result_and_check_textual_content_using_given_char_encoding()
455+
{
456+
var encoding = Encoding.BigEndianUnicode;
457+
458+
_controller.WithCallTo(c => c.TextualFile(encoding))
459+
.ShouldRenderFileContents(ControllerResultTestController.TextualFileContents, encoding: encoding);
460+
}
461+
462+
[Test]
463+
public void Check_for_file_content_result_and_check_textual_content_using_given_char_encoding_and_check_content_type()
464+
{
465+
var encoding = Encoding.BigEndianUnicode;
466+
467+
_controller.WithCallTo(c => c.TextualFile(encoding)).ShouldRenderFileContents(ControllerResultTestController.TextualFileContents, ControllerResultTestController.FileContentType, encoding);
468+
}
469+
470+
[Test]
471+
public void Check_for_file_content_result_and_check_textual_content_using_invalid_given_char_encoding()
472+
{
473+
Assert.Throws<ActionResultAssertionException>(() =>
474+
_controller.WithCallTo(c => c.TextualFile())
475+
.ShouldRenderFileContents(ControllerResultTestController.TextualFileContents,
476+
ControllerResultTestController.FileContentType, Encoding.BigEndianUnicode));
477+
}
478+
448479
[Test]
449480
public void Check_for_file_result()
450481
{

TestStack.FluentMVCTesting.Tests/TestControllers/ControllerResultTestController.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,12 @@ public ActionResult BinaryFile()
169169

170170
public ActionResult TextualFile()
171171
{
172-
var encodedContents = Encoding.UTF8.GetBytes(TextualFileContents);
172+
return TextualFile(Encoding.UTF8);
173+
}
174+
175+
public ActionResult TextualFile(Encoding encoding)
176+
{
177+
var encodedContents = encoding.GetBytes(TextualFileContents);
173178
return File(encodedContents, FileContentType);
174179
}
175180

TestStack.FluentMvcTesting/ControllerResultTest.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ public ViewResultTest ShouldRenderDefaultPartialView()
216216

217217
#endregion
218218

219-
public FileContentResult ShouldRenderFileContents(string contents, string contentType = null)
219+
public FileContentResult ShouldRenderFileContents(string contents, string contentType = null, Encoding encoding = null)
220220
{
221221
ValidateActionReturnType<FileContentResult>();
222222

@@ -227,7 +227,8 @@ public FileContentResult ShouldRenderFileContents(string contents, string conten
227227
throw new ActionResultAssertionException(string.Format("Expected file to be of content type '{0}', but instead was given '{1}'.", contentType, fileResult.ContentType));
228228
}
229229

230-
var reconstitutedText = Encoding.UTF8.GetString(fileResult.FileContents);
230+
if (encoding == null) encoding = Encoding.UTF8;
231+
var reconstitutedText = encoding.GetString(fileResult.FileContents);
231232
if (contents != reconstitutedText)
232233
{
233234
throw new ActionResultAssertionException(string.Format("Expected file contents to be \"{0}\", but instead was \"{1}\".", contents, reconstitutedText));

0 commit comments

Comments
 (0)