Skip to content

Commit 49b5b84

Browse files
committed
Merge pull request #24 from ByteBlast/master
Added support for checking a file content result's textual contents.
2 parents 4c21351 + 0b6d640 commit 49b5b84

File tree

3 files changed

+126
-13
lines changed

3 files changed

+126
-13
lines changed

TestStack.FluentMVCTesting.Tests/ControllerResultTestTests.cs

Lines changed: 84 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
using System.Web.Mvc;
77
using NUnit.Framework;
88
using TestStack.FluentMVCTesting.Tests.TestControllers;
9+
using System.Text;
10+
911

1012
namespace TestStack.FluentMVCTesting.Tests
1113
{
@@ -31,6 +33,9 @@ class ControllerResultTestShould
3133
ReturnType<FileContentResult>(t => t.ShouldRenderFileContents()),
3234
ReturnType<FileContentResult>(t => t.ShouldRenderFileContents(new byte[0])),
3335
ReturnType<FileContentResult>(t => t.ShouldRenderFileContents(new byte[0], "")),
36+
ReturnType<FileContentResult>(t => t.ShouldRenderFileContents("")),
37+
ReturnType<FileContentResult>(t => t.ShouldRenderFileContents("", "")),
38+
ReturnType<FileContentResult>(t => t.ShouldRenderFileContents("", "", Encoding.UTF8)),
3439
ReturnType<FileStreamResult>(t => t.ShouldRenderFileStream("")),
3540
ReturnType<FilePathResult>(t => t.ShouldRenderFilePath()),
3641
ReturnType<FilePathResult>(t => t.ShouldRenderFilePath("")),
@@ -349,26 +354,26 @@ public void Check_for_file_content_result()
349354
[Test]
350355
public void Check_for_file_content_result_and_check_binary_content()
351356
{
352-
_controller.WithCallTo(c => c.File()).ShouldRenderFileContents(ControllerResultTestController.FileContents);
357+
_controller.WithCallTo(c => c.BinaryFile()).ShouldRenderFileContents(ControllerResultTestController.BinaryFileContents);
353358
}
354359

355360
[Test]
356361
public void Check_for_file_content_result_and_check_invalid_binary_content()
357362
{
358363
byte[] contents = { 1, 2 };
359364
var exception = Assert.Throws<ActionResultAssertionException>(() =>
360-
_controller.WithCallTo(c => c.File()).ShouldRenderFileContents(contents));
365+
_controller.WithCallTo(c => c.BinaryFile()).ShouldRenderFileContents(contents));
361366

362367
Assert.True(exception.Message.StartsWith("Expected file contents to be equal to ["));
363368
Assert.True(exception.Message.EndsWith("]."));
364369
Assert.True(string.Join(", ", contents).All(exception.Message.Contains));
365-
Assert.True(string.Join(", ", ControllerResultTestController.FileContents).All(exception.Message.Contains));
370+
Assert.True(string.Join(", ", ControllerResultTestController.BinaryFileContents).All(exception.Message.Contains));
366371
}
367372

368373
[Test]
369374
public void Check_for_file_content_result_and_check_binary_content_and_check_content_type()
370375
{
371-
_controller.WithCallTo(c => c.File()).ShouldRenderFileContents(ControllerResultTestController.FileContents, ControllerResultTestController.FileContentType);
376+
_controller.WithCallTo(c => c.BinaryFile()).ShouldRenderFileContents(ControllerResultTestController.BinaryFileContents, ControllerResultTestController.FileContentType);
372377
}
373378

374379
[Test]
@@ -377,7 +382,7 @@ public void Check_for_file_content_result_and_check_invalid_content_type()
377382
const string contentType = "application/dummy";
378383

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

382387
Assert.That(exception.Message, Is.EqualTo(string.Format("Expected file to be of content type '{0}', but instead was given '{1}'.", contentType, ControllerResultTestController.FileContentType)));
383388
}
@@ -389,12 +394,83 @@ public void Check_for_file_content_result_and_check_invalid_binary_content_and_c
389394
const string contentType = "application/dummy";
390395

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

394-
// When supplied with both an invalid content type and invalid content, test the content type first.
446+
// Assert that the content type validation occurs before that of the actual contents.
395447
Assert.That(exception.Message.Contains("content type"));
396448
}
397449

450+
[Test]
451+
public void Check_for_file_content_result_and_check_textual_content_using_given_char_encoding()
452+
{
453+
var encoding = Encoding.BigEndianUnicode;
454+
455+
_controller.WithCallTo(c => c.TextualFile(encoding))
456+
.ShouldRenderFileContents(ControllerResultTestController.TextualFileContents, encoding: encoding);
457+
}
458+
459+
[Test]
460+
public void Check_for_file_content_result_and_check_textual_content_using_given_char_encoding_and_check_content_type()
461+
{
462+
var encoding = Encoding.BigEndianUnicode;
463+
464+
_controller.WithCallTo(c => c.TextualFile(encoding)).ShouldRenderFileContents(ControllerResultTestController.TextualFileContents, ControllerResultTestController.FileContentType, encoding);
465+
}
466+
467+
[Test]
468+
public void Check_for_file_content_result_and_check_textual_content_using_invalid_given_char_encoding()
469+
{
470+
Assert.Throws<ActionResultAssertionException>(() =>
471+
_controller.WithCallTo(c => c.TextualFile()).ShouldRenderFileContents(ControllerResultTestController.TextualFileContents, ControllerResultTestController.FileContentType, Encoding.BigEndianUnicode));
472+
}
473+
398474
[Test]
399475
public void Check_for_file_result()
400476
{
@@ -479,7 +555,7 @@ public void Check_for_file_path_result_and_check_invalid_file_name_and_check_inv
479555
var exception = Assert.Throws<ActionResultAssertionException>(() =>
480556
_controller.WithCallTo(c => c.EmptyFilePath()).ShouldRenderFilePath(name, contentType));
481557

482-
// When supplied with both an invalid content type and invalid file name, test the content type first.
558+
// Assert that the content type validation occurs before that of the file name.
483559
Assert.That(exception.Message.Contains("content type"));
484560
}
485561

TestStack.FluentMVCTesting.Tests/TestControllers/ControllerResultTestController.cs

Lines changed: 16 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,20 @@ 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+
return TextualFile(Encoding.UTF8);
173+
}
174+
175+
public ActionResult TextualFile(Encoding encoding)
176+
{
177+
var encodedContents = encoding.GetBytes(TextualFileContents);
178+
return File(encodedContents, FileContentType);
166179
}
167180

168181
public ActionResult EmptyStream()

TestStack.FluentMvcTesting/ControllerResultTest.cs

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,10 @@
33
using System.Linq.Expressions;
44
using System.Net;
55
using System.Reflection;
6-
using System.Runtime.InteropServices;
6+
using System.Text;
77
using System.Text.RegularExpressions;
88
using System.Web.Mvc;
99
using System.Web.Routing;
10-
using System.Web.UI.WebControls;
1110

1211
namespace TestStack.FluentMVCTesting
1312
{
@@ -253,6 +252,31 @@ public FileContentResult ShouldRenderFileContents(byte[] contents = null, string
253252
return fileResult;
254253
}
255254

255+
public FileContentResult ShouldRenderFileContents(string contents, string contentType = null, Encoding encoding = null)
256+
{
257+
ValidateActionReturnType<FileContentResult>();
258+
259+
var fileResult = (FileContentResult)_actionResult;
260+
261+
if (contentType != null && fileResult.ContentType != contentType)
262+
{
263+
throw new ActionResultAssertionException(
264+
string.Format("Expected file to be of content type '{0}', but instead was given '{1}'.", contentType,
265+
fileResult.ContentType));
266+
}
267+
268+
if (encoding == null)
269+
encoding = Encoding.UTF8;
270+
271+
var reconstitutedText = encoding.GetString(fileResult.FileContents);
272+
if (contents != reconstitutedText)
273+
{
274+
throw new ActionResultAssertionException(string.Format("Expected file contents to be \"{0}\", but instead was \"{1}\".", contents, reconstitutedText));
275+
}
276+
277+
return fileResult;
278+
}
279+
256280
[Obsolete("Obsolete: Use ShouldRenderFileContents instead.")]
257281
public FileContentResult ShouldRenderFile(string contentType = null)
258282
{

0 commit comments

Comments
 (0)