|
1 | 1 | using System;
|
2 | 2 | using System.Collections.Generic;
|
| 3 | +using System.Linq; |
3 | 4 | using System.Linq.Expressions;
|
4 | 5 | using System.Net;
|
5 | 6 | using System.Web.Mvc;
|
@@ -27,6 +28,9 @@ class ControllerResultTestShould
|
27 | 28 | ReturnType<FileContentResult>(t => t.ShouldRenderFile()),
|
28 | 29 | ReturnType<FileContentResult>(t => t.ShouldRenderFile("")),
|
29 | 30 | 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], "")), |
30 | 34 | ReturnType<FileStreamResult>(t => t.ShouldRenderFileStream("")),
|
31 | 35 | ReturnType<FilePathResult>(t => t.ShouldRenderFilePath()),
|
32 | 36 | ReturnType<FilePathResult>(t => t.ShouldRenderFilePath("")),
|
@@ -336,6 +340,61 @@ public void Check_for_any_file_result_and_check_invalid_content_type()
|
336 | 340 | Assert.That(exception.Message, Is.EqualTo(string.Format("Expected file to be of content type '{0}', but instead was given '{1}'.", contentType, ControllerResultTestController.FileContentType)));
|
337 | 341 | }
|
338 | 342 |
|
| 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 | + |
339 | 398 | [Test]
|
340 | 399 | public void Check_for_file_result()
|
341 | 400 | {
|
@@ -411,6 +470,19 @@ public void Check_for_file_path_result_and_check_file_name_and_check_invalid_con
|
411 | 470 | Assert.That(exception.Message, Is.EqualTo(string.Format("Expected file to be of content type '{0}', but instead was given '{1}'.", contentType, ControllerResultTestController.FileContentType)));
|
412 | 471 | }
|
413 | 472 |
|
| 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 | + |
414 | 486 | #endregion
|
415 | 487 |
|
416 | 488 | #region HTTP Status tests
|
|
0 commit comments