Skip to content

Commit 12747ff

Browse files
committed
Merge pull request #23 from ByteBlast/master
Implemented ShouldRenderFileContents and deprecated ShouldRenderFile
2 parents 9154051 + c25d2e7 commit 12747ff

File tree

3 files changed

+108
-4
lines changed

3 files changed

+108
-4
lines changed

TestStack.FluentMVCTesting.Tests/ControllerResultTestTests.cs

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Linq;
34
using System.Linq.Expressions;
45
using System.Net;
56
using System.Web.Mvc;
@@ -27,6 +28,9 @@ class ControllerResultTestShould
2728
ReturnType<FileContentResult>(t => t.ShouldRenderFile()),
2829
ReturnType<FileContentResult>(t => t.ShouldRenderFile("")),
2930
ReturnType<FileStreamResult>(t => t.ShouldRenderFileStream()),
31+
ReturnType<FileContentResult>(t => t.ShouldRenderFileContents()),
32+
ReturnType<FileContentResult>(t => t.ShouldRenderFileContents(new byte[0])),
33+
ReturnType<FileContentResult>(t => t.ShouldRenderFileContents(new byte[0], "")),
3034
ReturnType<FileStreamResult>(t => t.ShouldRenderFileStream("")),
3135
ReturnType<FilePathResult>(t => t.ShouldRenderFilePath()),
3236
ReturnType<FilePathResult>(t => t.ShouldRenderFilePath("")),
@@ -336,6 +340,61 @@ public void Check_for_any_file_result_and_check_invalid_content_type()
336340
Assert.That(exception.Message, Is.EqualTo(string.Format("Expected file to be of content type '{0}', but instead was given '{1}'.", contentType, ControllerResultTestController.FileContentType)));
337341
}
338342

343+
[Test]
344+
public void Check_for_file_content_result()
345+
{
346+
_controller.WithCallTo(c => c.EmptyFile()).ShouldRenderFileContents();
347+
}
348+
349+
[Test]
350+
public void Check_for_file_content_result_and_check_binary_content()
351+
{
352+
_controller.WithCallTo(c => c.File()).ShouldRenderFileContents(ControllerResultTestController.FileContents);
353+
}
354+
355+
[Test]
356+
public void Check_for_file_content_result_and_check_invalid_binary_content()
357+
{
358+
byte[] contents = { 1, 2 };
359+
var exception = Assert.Throws<ActionResultAssertionException>(() =>
360+
_controller.WithCallTo(c => c.File()).ShouldRenderFileContents(contents));
361+
362+
Assert.True(exception.Message.StartsWith("Expected file contents to be equal to ["));
363+
Assert.True(exception.Message.EndsWith("]."));
364+
Assert.True(string.Join(", ", contents).All(exception.Message.Contains));
365+
Assert.True(string.Join(", ", ControllerResultTestController.FileContents).All(exception.Message.Contains));
366+
}
367+
368+
[Test]
369+
public void Check_for_file_content_result_and_check_binary_content_and_check_content_type()
370+
{
371+
_controller.WithCallTo(c => c.File()).ShouldRenderFileContents(ControllerResultTestController.FileContents, ControllerResultTestController.FileContentType);
372+
}
373+
374+
[Test]
375+
public void Check_for_file_content_result_and_check_invalid_content_type()
376+
{
377+
const string contentType = "application/dummy";
378+
379+
var exception = Assert.Throws<ActionResultAssertionException>(() =>
380+
_controller.WithCallTo(c => c.File()).ShouldRenderFileContents(ControllerResultTestController.FileContents, contentType));
381+
382+
Assert.That(exception.Message, Is.EqualTo(string.Format("Expected file to be of content type '{0}', but instead was given '{1}'.", contentType, ControllerResultTestController.FileContentType)));
383+
}
384+
385+
[Test]
386+
public void Check_for_file_content_result_and_check_invalid_binary_content_and_check_invalid_content_type()
387+
{
388+
byte[] contents = { 1, 2 };
389+
const string contentType = "application/dummy";
390+
391+
var exception = Assert.Throws<ActionResultAssertionException>(() =>
392+
_controller.WithCallTo(c => c.File()).ShouldRenderFileContents(contents, contentType));
393+
394+
// When supplied with both an invalid content type and invalid content, test the content type first.
395+
Assert.That(exception.Message.Contains("content type"));
396+
}
397+
339398
[Test]
340399
public void Check_for_file_result()
341400
{
@@ -411,6 +470,19 @@ public void Check_for_file_path_result_and_check_file_name_and_check_invalid_con
411470
Assert.That(exception.Message, Is.EqualTo(string.Format("Expected file to be of content type '{0}', but instead was given '{1}'.", contentType, ControllerResultTestController.FileContentType)));
412471
}
413472

473+
[Test]
474+
public void Check_for_file_path_result_and_check_invalid_file_name_and_check_invalid_content_type()
475+
{
476+
const string contentType = "application/dummy";
477+
const string name = "dummy";
478+
479+
var exception = Assert.Throws<ActionResultAssertionException>(() =>
480+
_controller.WithCallTo(c => c.EmptyFilePath()).ShouldRenderFilePath(name, contentType));
481+
482+
// When supplied with both an invalid content type and invalid file name, test the content type first.
483+
Assert.That(exception.Message.Contains("content type"));
484+
}
485+
414486
#endregion
415487

416488
#region HTTP Status tests

TestStack.FluentMVCTesting.Tests/TestControllers/ControllerResultTestController.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ class ControllerResultTestController : Controller
1515
public const int Code = 403;
1616
public const string JsonValue = "json";
1717
public const string FileName = "NamedFile";
18+
public static byte[] FileContents = { 1 };
1819
#endregion
1920

2021
#region Empty, Null and Random Results
@@ -159,6 +160,11 @@ public ActionResult EmptyFile()
159160
return File(content, FileContentType);
160161
}
161162

163+
public ActionResult File()
164+
{
165+
return File(FileContents, FileContentType);
166+
}
167+
162168
public ActionResult EmptyStream()
163169
{
164170
var content = new MemoryStream();

TestStack.FluentMvcTesting/ControllerResultTest.cs

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
using System;
2+
using System.Linq;
23
using System.Linq.Expressions;
34
using System.Net;
45
using System.Reflection;
6+
using System.Runtime.InteropServices;
57
using System.Text.RegularExpressions;
68
using System.Web.Mvc;
79
using System.Web.Routing;
10+
using System.Web.UI.WebControls;
811

912
namespace TestStack.FluentMVCTesting
1013
{
@@ -228,6 +231,29 @@ public FileResult ShouldRenderAnyFile(string contentType = null)
228231
return fileResult;
229232
}
230233

234+
public FileContentResult ShouldRenderFileContents(byte[] contents = null, string contentType = null)
235+
{
236+
ValidateActionReturnType<FileContentResult>();
237+
238+
var fileResult = (FileContentResult) _actionResult;
239+
240+
if (contentType != null && fileResult.ContentType != contentType)
241+
{
242+
throw new ActionResultAssertionException(string.Format("Expected file to be of content type '{0}', but instead was given '{1}'.", contentType, fileResult.ContentType));
243+
}
244+
245+
if (contents != null && !fileResult.FileContents.SequenceEqual(contents))
246+
{
247+
throw new ActionResultAssertionException(string.Format(
248+
"Expected file contents to be equal to [{0}], but instead was given [{1}].",
249+
string.Join(", ", contents),
250+
string.Join(", ", fileResult.FileContents)));
251+
}
252+
253+
return fileResult;
254+
}
255+
256+
[Obsolete("Obsolete: Use ShouldRenderFileContents instead.")]
231257
public FileContentResult ShouldRenderFile(string contentType = null)
232258
{
233259
ValidateActionReturnType<FileContentResult>();
@@ -262,14 +288,14 @@ public FilePathResult ShouldRenderFilePath(string fileName = null, string conten
262288

263289
var fileResult = (FilePathResult)_actionResult;
264290

265-
if (fileName != null && fileName != fileResult.FileName)
291+
if (contentType != null && fileResult.ContentType != contentType)
266292
{
267-
throw new ActionResultAssertionException(string.Format("Expected file name to be '{0}', but instead was given '{1}'.", fileName, fileResult.FileName));
293+
throw new ActionResultAssertionException(string.Format("Expected file to be of content type '{0}', but instead was given '{1}'.", contentType, fileResult.ContentType));
268294
}
269295

270-
if (contentType != null && fileResult.ContentType != contentType)
296+
if (fileName != null && fileName != fileResult.FileName)
271297
{
272-
throw new ActionResultAssertionException(string.Format("Expected file to be of content type '{0}', but instead was given '{1}'.", contentType, fileResult.ContentType));
298+
throw new ActionResultAssertionException(string.Format("Expected file name to be '{0}', but instead was given '{1}'.", fileName, fileResult.FileName));
273299
}
274300

275301
return fileResult;

0 commit comments

Comments
 (0)