Skip to content

Commit 45e1155

Browse files
committed
Added support for checking a file content result's content type.
Partial implementation of #3.
1 parent dbd746a commit 45e1155

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

TestStack.FluentMVCTesting.Tests/ControllerResultTestTests.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ class ControllerResultTestShould
3030
ReturnType<FileStreamResult>(t => t.ShouldRenderFileStream()),
3131
ReturnType<FileContentResult>(t => t.ShouldRenderFileContents()),
3232
ReturnType<FileContentResult>(t => t.ShouldRenderFileContents(new byte[0])),
33+
ReturnType<FileContentResult>(t => t.ShouldRenderFileContents(new byte[0], "")),
3334
ReturnType<FileStreamResult>(t => t.ShouldRenderFileStream("")),
3435
ReturnType<FilePathResult>(t => t.ShouldRenderFilePath()),
3536
ReturnType<FilePathResult>(t => t.ShouldRenderFilePath("")),
@@ -364,6 +365,23 @@ public void Check_for_file_content_result_and_check_invalid_binary_content()
364365
Assert.True(string.Join(",", ControllerResultTestController.FileContents).All(exception.Message.Contains));
365366
}
366367

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+
367385
[Test]
368386
public void Check_for_file_result()
369387
{

TestStack.FluentMvcTesting/ControllerResultTest.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using System.Text.RegularExpressions;
88
using System.Web.Mvc;
99
using System.Web.Routing;
10+
using System.Web.UI.WebControls;
1011

1112
namespace TestStack.FluentMVCTesting
1213
{
@@ -230,7 +231,7 @@ public FileResult ShouldRenderAnyFile(string contentType = null)
230231
return fileResult;
231232
}
232233

233-
public FileContentResult ShouldRenderFileContents(byte[] contents = null)
234+
public FileContentResult ShouldRenderFileContents(byte[] contents = null, string contentType = null)
234235
{
235236
ValidateActionReturnType<FileContentResult>();
236237

@@ -244,6 +245,11 @@ public FileContentResult ShouldRenderFileContents(byte[] contents = null)
244245
string.Join(",", fileResult.FileContents)));
245246
}
246247

248+
if (contentType != null && fileResult.ContentType != contentType)
249+
{
250+
throw new ActionResultAssertionException(string.Format("Expected file to be of content type '{0}', but instead was given '{1}'.", contentType, fileResult.ContentType));
251+
}
252+
247253
return fileResult;
248254
}
249255

0 commit comments

Comments
 (0)