@@ -423,6 +423,33 @@ public void Created_WithRelativeUriParameter_SetsCreatedLocation()
423
423
Assert . Equal ( uri . OriginalString , result . Location ) ;
424
424
}
425
425
426
+ [ Fact ]
427
+ public void Created_IDisposableObject_RegistersForDispose ( )
428
+ {
429
+ // Arrange
430
+ var mockHttpContext = new Mock < DefaultHttpContext > ( ) ;
431
+ mockHttpContext . Setup ( x => x . Response . OnResponseCompleted ( It . IsAny < Action < object > > ( ) , It . IsAny < object > ( ) ) ) ;
432
+ var uri = new Uri ( "/test/url" , UriKind . Relative ) ;
433
+
434
+ var controller = new TestableController ( )
435
+ {
436
+ ActionContext = new ActionContext ( mockHttpContext . Object , new RouteData ( ) , new ActionDescriptor ( ) )
437
+ } ;
438
+ var input = new DisposableObject ( ) ;
439
+
440
+ // Act
441
+ var result = controller . Created ( uri , input ) ;
442
+
443
+ // Assert
444
+ Assert . IsType < CreatedResult > ( result ) ;
445
+ Assert . Equal ( StatusCodes . Status201Created , result . StatusCode ) ;
446
+ Assert . Equal ( uri . OriginalString , result . Location ) ;
447
+ Assert . Same ( input , result . Value ) ;
448
+ mockHttpContext . Verify (
449
+ x => x . Response . OnResponseCompleted ( It . IsAny < Action < object > > ( ) , It . IsAny < object > ( ) ) ,
450
+ Times . Once ( ) ) ;
451
+ }
452
+
426
453
[ Fact ]
427
454
public void CreatedAtAction_WithParameterActionName_SetsResultActionName ( )
428
455
{
@@ -483,6 +510,32 @@ public void CreatedAtAction_WithActionControllerRouteValues_SetsSameValues()
483
510
Assert . Equal ( expected , result . RouteValues ) ;
484
511
}
485
512
513
+ [ Fact ]
514
+ public void CreatedAtAction_IDisposableObject_RegistersForDispose ( )
515
+ {
516
+ // Arrange
517
+ var mockHttpContext = new Mock < DefaultHttpContext > ( ) ;
518
+ mockHttpContext . Setup ( x => x . Response . OnResponseCompleted ( It . IsAny < Action < object > > ( ) , It . IsAny < object > ( ) ) ) ;
519
+
520
+ var controller = new TestableController ( )
521
+ {
522
+ ActionContext = new ActionContext ( mockHttpContext . Object , new RouteData ( ) , new ActionDescriptor ( ) )
523
+ } ;
524
+ var input = new DisposableObject ( ) ;
525
+
526
+ // Act
527
+ var result = controller . CreatedAtAction ( "SampleAction" , input ) ;
528
+
529
+ // Assert
530
+ Assert . IsType < CreatedAtActionResult > ( result ) ;
531
+ Assert . Equal ( StatusCodes . Status201Created , result . StatusCode ) ;
532
+ Assert . Equal ( "SampleAction" , result . ActionName ) ;
533
+ Assert . Same ( input , result . Value ) ;
534
+ mockHttpContext . Verify (
535
+ x => x . Response . OnResponseCompleted ( It . IsAny < Action < object > > ( ) , It . IsAny < object > ( ) ) ,
536
+ Times . Once ( ) ) ;
537
+ }
538
+
486
539
[ Fact ]
487
540
public void CreatedAtRoute_WithParameterRouteName_SetsResultSameRouteName ( )
488
541
{
@@ -540,6 +593,32 @@ public void CreatedAtRoute_WithParameterRouteNameAndValues_SetsResultSamePropert
540
593
Assert . Equal ( expected , result . RouteValues ) ;
541
594
}
542
595
596
+ [ Fact ]
597
+ public void CreatedAtRoute_IDisposableObject_RegistersForDispose ( )
598
+ {
599
+ // Arrange
600
+ var mockHttpContext = new Mock < DefaultHttpContext > ( ) ;
601
+ mockHttpContext . Setup ( x => x . Response . OnResponseCompleted ( It . IsAny < Action < object > > ( ) , It . IsAny < object > ( ) ) ) ;
602
+
603
+ var controller = new TestableController ( )
604
+ {
605
+ ActionContext = new ActionContext ( mockHttpContext . Object , new RouteData ( ) , new ActionDescriptor ( ) )
606
+ } ;
607
+ var input = new DisposableObject ( ) ;
608
+
609
+ // Act
610
+ var result = controller . CreatedAtRoute ( "SampleRoute" , input ) ;
611
+
612
+ // Assert
613
+ Assert . IsType < CreatedAtRouteResult > ( result ) ;
614
+ Assert . Equal ( StatusCodes . Status201Created , result . StatusCode ) ;
615
+ Assert . Equal ( "SampleRoute" , result . RouteName ) ;
616
+ Assert . Same ( input , result . Value ) ;
617
+ mockHttpContext . Verify (
618
+ x => x . Response . OnResponseCompleted ( It . IsAny < Action < object > > ( ) , It . IsAny < object > ( ) ) ,
619
+ Times . Once ( ) ) ;
620
+ }
621
+
543
622
[ Fact ]
544
623
public void File_WithContents ( )
545
624
{
@@ -612,7 +691,12 @@ public void File_WithPathAndFileDownloadName()
612
691
public void File_WithStream ( )
613
692
{
614
693
// Arrange
615
- var controller = new TestableController ( ) ;
694
+ var mockHttpContext = new Mock < DefaultHttpContext > ( ) ;
695
+ mockHttpContext . Setup ( x => x . Response . OnResponseCompleted ( It . IsAny < Action < object > > ( ) , It . IsAny < object > ( ) ) ) ;
696
+ var controller = new TestableController ( )
697
+ {
698
+ ActionContext = new ActionContext ( mockHttpContext . Object , new RouteData ( ) , new ActionDescriptor ( ) )
699
+ } ;
616
700
var fileStream = Stream . Null ;
617
701
618
702
// Act
@@ -629,7 +713,13 @@ public void File_WithStream()
629
713
public void File_WithStreamAndFileDownloadName ( )
630
714
{
631
715
// Arrange
632
- var controller = new TestableController ( ) ;
716
+ var mockHttpContext = new Mock < DefaultHttpContext > ( ) ;
717
+ mockHttpContext . Setup ( x => x . Response . OnResponseCompleted ( It . IsAny < Action < object > > ( ) , It . IsAny < object > ( ) ) ) ;
718
+
719
+ var controller = new TestableController ( )
720
+ {
721
+ ActionContext = new ActionContext ( mockHttpContext . Object , new RouteData ( ) , new ActionDescriptor ( ) )
722
+ } ;
633
723
var fileStream = Stream . Null ;
634
724
635
725
// Act
@@ -640,6 +730,9 @@ public void File_WithStreamAndFileDownloadName()
640
730
Assert . Same ( fileStream , result . FileStream ) ;
641
731
Assert . Equal ( "someContentType" , result . ContentType ) ;
642
732
Assert . Equal ( "someDownloadName" , result . FileDownloadName ) ;
733
+ mockHttpContext . Verify (
734
+ x => x . Response . OnResponseCompleted ( It . IsAny < Action < object > > ( ) , It . IsAny < object > ( ) ) ,
735
+ Times . Once ( ) ) ;
643
736
}
644
737
645
738
[ Fact ]
@@ -685,6 +778,31 @@ public void HttpNotFound_SetsStatusCodeAndResponseContent()
685
778
Assert . Equal ( "Test Content" , result . Value ) ;
686
779
}
687
780
781
+ [ Fact ]
782
+ public void HttpNotFound_IDisposableObject_RegistersForDispose ( )
783
+ {
784
+ // Arrange
785
+ var mockHttpContext = new Mock < DefaultHttpContext > ( ) ;
786
+ mockHttpContext . Setup ( x => x . Response . OnResponseCompleted ( It . IsAny < Action < object > > ( ) , It . IsAny < object > ( ) ) ) ;
787
+
788
+ var controller = new TestableController ( )
789
+ {
790
+ ActionContext = new ActionContext ( mockHttpContext . Object , new RouteData ( ) , new ActionDescriptor ( ) )
791
+ } ;
792
+ var input = new DisposableObject ( ) ;
793
+
794
+ // Act
795
+ var result = controller . HttpNotFound ( input ) ;
796
+
797
+ // Assert
798
+ Assert . IsType < HttpNotFoundObjectResult > ( result ) ;
799
+ Assert . Equal ( StatusCodes . Status404NotFound , result . StatusCode ) ;
800
+ Assert . Same ( input , result . Value ) ;
801
+ mockHttpContext . Verify (
802
+ x => x . Response . OnResponseCompleted ( It . IsAny < Action < object > > ( ) , It . IsAny < object > ( ) ) ,
803
+ Times . Once ( ) ) ;
804
+ }
805
+
688
806
[ Fact ]
689
807
public void BadRequest_SetsStatusCode ( )
690
808
{
@@ -715,6 +833,31 @@ public void BadRequest_SetsStatusCodeAndValue_Object()
715
833
Assert . Equal ( obj , result . Value ) ;
716
834
}
717
835
836
+ [ Fact ]
837
+ public void BadRequest_IDisposableObject_RegistersForDispose ( )
838
+ {
839
+ // Arrange
840
+ var mockHttpContext = new Mock < DefaultHttpContext > ( ) ;
841
+ mockHttpContext . Setup ( x => x . Response . OnResponseCompleted ( It . IsAny < Action < object > > ( ) , It . IsAny < object > ( ) ) ) ;
842
+
843
+ var controller = new TestableController ( )
844
+ {
845
+ ActionContext = new ActionContext ( mockHttpContext . Object , new RouteData ( ) , new ActionDescriptor ( ) )
846
+ } ;
847
+ var input = new DisposableObject ( ) ;
848
+
849
+ // Act
850
+ var result = controller . HttpBadRequest ( input ) ;
851
+
852
+ // Assert
853
+ Assert . IsType < BadRequestObjectResult > ( result ) ;
854
+ Assert . Equal ( StatusCodes . Status400BadRequest , result . StatusCode ) ;
855
+ Assert . Same ( input , result . Value ) ;
856
+ mockHttpContext . Verify (
857
+ x => x . Response . OnResponseCompleted ( It . IsAny < Action < object > > ( ) , It . IsAny < object > ( ) ) ,
858
+ Times . Once ( ) ) ;
859
+ }
860
+
718
861
[ Fact ]
719
862
public void BadRequest_SetsStatusCodeAndValue_ModelState ( )
720
863
{
@@ -888,6 +1031,30 @@ public void Controller_Json_WithParameterValue_SetsResultData()
888
1031
Assert . Same ( data , actualJsonResult . Value ) ;
889
1032
}
890
1033
1034
+ [ Fact ]
1035
+ public void Controller_Json_IDisposableObject_RegistersForDispose ( )
1036
+ {
1037
+ // Arrange
1038
+ var mockHttpContext = new Mock < DefaultHttpContext > ( ) ;
1039
+ mockHttpContext . Setup ( x => x . Response . OnResponseCompleted ( It . IsAny < Action < object > > ( ) , It . IsAny < object > ( ) ) ) ;
1040
+
1041
+ var controller = new TestableController ( )
1042
+ {
1043
+ ActionContext = new ActionContext ( mockHttpContext . Object , new RouteData ( ) , new ActionDescriptor ( ) )
1044
+ } ;
1045
+ var input = new DisposableObject ( ) ;
1046
+
1047
+ // Act
1048
+ var result = controller . Json ( input ) ;
1049
+
1050
+ // Assert
1051
+ Assert . IsType < JsonResult > ( result ) ;
1052
+ Assert . Same ( input , result . Value ) ;
1053
+ mockHttpContext . Verify (
1054
+ x => x . Response . OnResponseCompleted ( It . IsAny < Action < object > > ( ) , It . IsAny < object > ( ) ) ,
1055
+ Times . Once ( ) ) ;
1056
+ }
1057
+
891
1058
public static IEnumerable < object [ ] > RedirectTestData
892
1059
{
893
1060
get
@@ -1402,7 +1569,7 @@ public void TryValidateModelWithInvalidModelWithPrefix_ReturnsFalse()
1402
1569
{
1403
1570
// Arrange
1404
1571
var model = new TryValidateModelModel ( ) ;
1405
- var validationResult = new [ ]
1572
+ var validationResult = new [ ]
1406
1573
{
1407
1574
new ModelValidationResult ( string . Empty , "Out of range!" )
1408
1575
} ;
@@ -1569,5 +1736,13 @@ private class TestableController : Controller
1569
1736
{
1570
1737
1571
1738
}
1739
+
1740
+ private class DisposableObject : IDisposable
1741
+ {
1742
+ public void Dispose ( )
1743
+ {
1744
+ throw new NotImplementedException ( ) ;
1745
+ }
1746
+ }
1572
1747
}
1573
1748
}
0 commit comments