Skip to content

Commit c659b4a

Browse files
committed
Added support for checking a file content result's textual contents.
1 parent 12747ff commit c659b4a

File tree

3 files changed

+88
-9
lines changed

3 files changed

+88
-9
lines changed

TestStack.FluentMVCTesting.Tests/ControllerResultTestTests.cs

Lines changed: 56 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Linq;
44
using System.Linq.Expressions;
55
using System.Net;
6+
using System.Text.RegularExpressions;
67
using System.Web.Mvc;
78
using NUnit.Framework;
89
using TestStack.FluentMVCTesting.Tests.TestControllers;
@@ -31,6 +32,8 @@ class ControllerResultTestShould
3132
ReturnType<FileContentResult>(t => t.ShouldRenderFileContents()),
3233
ReturnType<FileContentResult>(t => t.ShouldRenderFileContents(new byte[0])),
3334
ReturnType<FileContentResult>(t => t.ShouldRenderFileContents(new byte[0], "")),
35+
ReturnType<FileContentResult>(t => t.ShouldRenderFileContents("")),
36+
ReturnType<FileContentResult>(t => t.ShouldRenderFileContents("", "")),
3437
ReturnType<FileStreamResult>(t => t.ShouldRenderFileStream("")),
3538
ReturnType<FilePathResult>(t => t.ShouldRenderFilePath()),
3639
ReturnType<FilePathResult>(t => t.ShouldRenderFilePath("")),
@@ -349,26 +352,26 @@ public void Check_for_file_content_result()
349352
[Test]
350353
public void Check_for_file_content_result_and_check_binary_content()
351354
{
352-
_controller.WithCallTo(c => c.File()).ShouldRenderFileContents(ControllerResultTestController.FileContents);
355+
_controller.WithCallTo(c => c.BinaryFile()).ShouldRenderFileContents(ControllerResultTestController.BinaryFileContents);
353356
}
354357

355358
[Test]
356359
public void Check_for_file_content_result_and_check_invalid_binary_content()
357360
{
358361
byte[] contents = { 1, 2 };
359362
var exception = Assert.Throws<ActionResultAssertionException>(() =>
360-
_controller.WithCallTo(c => c.File()).ShouldRenderFileContents(contents));
363+
_controller.WithCallTo(c => c.BinaryFile()).ShouldRenderFileContents(contents));
361364

362365
Assert.True(exception.Message.StartsWith("Expected file contents to be equal to ["));
363366
Assert.True(exception.Message.EndsWith("]."));
364367
Assert.True(string.Join(", ", contents).All(exception.Message.Contains));
365-
Assert.True(string.Join(", ", ControllerResultTestController.FileContents).All(exception.Message.Contains));
368+
Assert.True(string.Join(", ", ControllerResultTestController.BinaryFileContents).All(exception.Message.Contains));
366369
}
367370

368371
[Test]
369372
public void Check_for_file_content_result_and_check_binary_content_and_check_content_type()
370373
{
371-
_controller.WithCallTo(c => c.File()).ShouldRenderFileContents(ControllerResultTestController.FileContents, ControllerResultTestController.FileContentType);
374+
_controller.WithCallTo(c => c.BinaryFile()).ShouldRenderFileContents(ControllerResultTestController.BinaryFileContents, ControllerResultTestController.FileContentType);
372375
}
373376

374377
[Test]
@@ -377,7 +380,7 @@ public void Check_for_file_content_result_and_check_invalid_content_type()
377380
const string contentType = "application/dummy";
378381

379382
var exception = Assert.Throws<ActionResultAssertionException>(() =>
380-
_controller.WithCallTo(c => c.File()).ShouldRenderFileContents(ControllerResultTestController.FileContents, contentType));
383+
_controller.WithCallTo(c => c.BinaryFile()).ShouldRenderFileContents(ControllerResultTestController.BinaryFileContents, contentType));
381384

382385
Assert.That(exception.Message, Is.EqualTo(string.Format("Expected file to be of content type '{0}', but instead was given '{1}'.", contentType, ControllerResultTestController.FileContentType)));
383386
}
@@ -389,7 +392,54 @@ public void Check_for_file_content_result_and_check_invalid_binary_content_and_c
389392
const string contentType = "application/dummy";
390393

391394
var exception = Assert.Throws<ActionResultAssertionException>(() =>
392-
_controller.WithCallTo(c => c.File()).ShouldRenderFileContents(contents, contentType));
395+
_controller.WithCallTo(c => c.BinaryFile()).ShouldRenderFileContents(contents, contentType));
396+
397+
// When supplied with both an invalid content type and invalid content, test the content type first.
398+
Assert.That(exception.Message.Contains("content type"));
399+
}
400+
401+
[Test]
402+
public void Check_for_file_content_result_and_check_textual_contents()
403+
{
404+
_controller.WithCallTo(c => c.TextualFile()).ShouldRenderFileContents(ControllerResultTestController.TextualFileContents);
405+
}
406+
407+
[Test]
408+
public void Check_for_file_content_result_and_check_invalid_textual_contents()
409+
{
410+
const string contents = "dummy contents";
411+
412+
var exception = Assert.Throws<ActionResultAssertionException>(() =>
413+
_controller.WithCallTo(c => c.TextualFile()).ShouldRenderFileContents(contents));
414+
415+
Assert.That(exception.Message, Is.EqualTo(string.Format("Expected file contents to be \"{0}\", but instead was \"{1}\".", contents, ControllerResultTestController.TextualFileContents)));
416+
}
417+
418+
[Test]
419+
public void Check_for_file_content_result_and_check_textual_content_and_check_content_result()
420+
{
421+
_controller.WithCallTo(c => c.TextualFile()).ShouldRenderFileContents(ControllerResultTestController.TextualFileContents, ControllerResultTestController.FileContentType);
422+
}
423+
424+
[Test]
425+
public void Check_for_file_content_result_and_check_textual_content_and_check_invalid_content_result()
426+
{
427+
const string contentType = "application/dummy";
428+
429+
var exception = Assert.Throws<ActionResultAssertionException>(() =>
430+
_controller.WithCallTo(c => c.TextualFile()).ShouldRenderFileContents(ControllerResultTestController.TextualFileContents, contentType));
431+
432+
Assert.That(exception.Message, Is.EqualTo(string.Format("Expected file to be of content type '{0}', but instead was given '{1}'.", contentType, ControllerResultTestController.FileContentType)));
433+
}
434+
435+
[Test]
436+
public void Check_for_file_content_result_and_check_invalid_textual_content_and_check_invalid_content_result()
437+
{
438+
const string contents = "dummy content";
439+
const string contentType = "application/dummy";
440+
441+
var exception = Assert.Throws<ActionResultAssertionException>(() =>
442+
_controller.WithCallTo(c => c.TextualFile()).ShouldRenderFileContents(contents, contentType));
393443

394444
// When supplied with both an invalid content type and invalid content, test the content type first.
395445
Assert.That(exception.Message.Contains("content type"));

TestStack.FluentMVCTesting.Tests/TestControllers/ControllerResultTestController.cs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.IO;
2+
using System.Text;
23
using System.Web.Mvc;
34

45
namespace TestStack.FluentMVCTesting.Tests.TestControllers
@@ -15,7 +16,8 @@ class ControllerResultTestController : Controller
1516
public const int Code = 403;
1617
public const string JsonValue = "json";
1718
public const string FileName = "NamedFile";
18-
public static byte[] FileContents = { 1 };
19+
public static byte[] BinaryFileContents = { 1 };
20+
public static string TextualFileContents = "textual content";
1921
#endregion
2022

2123
#region Empty, Null and Random Results
@@ -160,9 +162,15 @@ public ActionResult EmptyFile()
160162
return File(content, FileContentType);
161163
}
162164

163-
public ActionResult File()
165+
public ActionResult BinaryFile()
164166
{
165-
return File(FileContents, FileContentType);
167+
return File(BinaryFileContents, FileContentType);
168+
}
169+
170+
public ActionResult TextualFile()
171+
{
172+
var encodedContents = Encoding.UTF8.GetBytes(TextualFileContents);
173+
return File(encodedContents, FileContentType);
166174
}
167175

168176
public ActionResult EmptyStream()

TestStack.FluentMvcTesting/ControllerResultTest.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Net;
55
using System.Reflection;
66
using System.Runtime.InteropServices;
7+
using System.Text;
78
using System.Text.RegularExpressions;
89
using System.Web.Mvc;
910
using System.Web.Routing;
@@ -215,6 +216,26 @@ public ViewResultTest ShouldRenderDefaultPartialView()
215216

216217
#endregion
217218

219+
public FileContentResult ShouldRenderFileContents(string contents, string contentType = null)
220+
{
221+
ValidateActionReturnType<FileContentResult>();
222+
223+
var fileResult = (FileContentResult) _actionResult;
224+
225+
if (contentType != null && fileResult.ContentType != contentType)
226+
{
227+
throw new ActionResultAssertionException(string.Format("Expected file to be of content type '{0}', but instead was given '{1}'.", contentType, fileResult.ContentType));
228+
}
229+
230+
var reconstitutedText = Encoding.UTF8.GetString(fileResult.FileContents);
231+
if (contents != reconstitutedText)
232+
{
233+
throw new ActionResultAssertionException(string.Format("Expected file contents to be \"{0}\", but instead was \"{1}\".", contents, reconstitutedText));
234+
}
235+
236+
return fileResult;
237+
}
238+
218239
#region File Results
219240

220241
public FileResult ShouldRenderAnyFile(string contentType = null)

0 commit comments

Comments
 (0)