@@ -542,39 +542,56 @@ void TestAction([FromHeader(Name = customHeaderName)] int value)
542
542
Assert . Equal ( originalHeaderParam , deserializedRouteParam ) ;
543
543
}
544
544
545
- [ Fact ]
546
- public async Task RequestDelegatePopulatesFromBodyParameter ( )
545
+ public static object [ ] [ ] FromBodyActions
547
546
{
548
- Todo originalTodo = new ( )
547
+ get
549
548
{
550
- Name = "Write more tests!"
551
- } ;
549
+ void TestExplicitFromBody ( HttpContext httpContext , [ FromBody ] Todo todo )
550
+ {
551
+ httpContext . Items . Add ( "body" , todo ) ;
552
+ }
552
553
553
- Todo ? deserializedRequestBody = null ;
554
+ void TestImpliedFromBody ( HttpContext httpContext , Todo myService )
555
+ {
556
+ httpContext . Items . Add ( "body" , myService ) ;
557
+ }
554
558
555
- void TestAction ( [ FromBody ] Todo todo )
556
- {
557
- deserializedRequestBody = todo ;
559
+ return new [ ]
560
+ {
561
+ new [ ] { ( Action < HttpContext , Todo > ) TestExplicitFromBody } ,
562
+ new [ ] { ( Action < HttpContext , Todo > ) TestImpliedFromBody } ,
563
+ } ;
558
564
}
565
+ }
566
+
567
+ [ Theory ]
568
+ [ MemberData ( nameof ( FromBodyActions ) ) ]
569
+ public async Task RequestDelegatePopulatesFromBodyParameter ( Delegate action )
570
+ {
571
+ Todo originalTodo = new ( )
572
+ {
573
+ Name = "Write more tests!"
574
+ } ;
559
575
560
576
var httpContext = new DefaultHttpContext ( ) ;
561
577
httpContext . Request . Headers [ "Content-Type" ] = "application/json" ;
562
578
563
579
var requestBodyBytes = JsonSerializer . SerializeToUtf8Bytes ( originalTodo ) ;
564
580
httpContext . Request . Body = new MemoryStream ( requestBodyBytes ) ;
565
581
566
- var requestDelegate = RequestDelegateFactory . Create ( ( Action < Todo > ) TestAction ) ;
582
+ var requestDelegate = RequestDelegateFactory . Create ( action ) ;
567
583
568
584
await requestDelegate ( httpContext ) ;
569
585
586
+ var deserializedRequestBody = httpContext . Items [ "body" ] ;
570
587
Assert . NotNull ( deserializedRequestBody ) ;
571
- Assert . Equal ( originalTodo . Name , deserializedRequestBody ! . Name ) ;
588
+ Assert . Equal ( originalTodo . Name , ( ( Todo ) deserializedRequestBody ! ) . Name ) ;
572
589
}
573
590
574
591
[ Fact ]
575
592
public async Task RequestDelegateRejectsEmptyBodyGivenDefaultFromBodyParameter ( )
576
593
{
577
- void TestAction ( [ FromBody ] Todo todo )
594
+ void TestAction ( Todo todo )
578
595
{
579
596
}
580
597
@@ -702,12 +719,14 @@ void TestAction([FromBody] Todo todo)
702
719
[ Fact ]
703
720
public void BuildRequestDelegateThrowsInvalidOperationExceptionGivenFromBodyOnMultipleParameters ( )
704
721
{
705
- void TestAction ( [ FromBody ] int value1 , [ FromBody ] int value2 ) { }
722
+ void TestExplicitlyInvalidAction ( [ FromBody ] int value1 , [ FromBody ] int value2 ) { }
723
+ void TestInferredInvalidAction ( Todo value1 , Todo value2 ) { }
706
724
707
- Assert . Throws < InvalidOperationException > ( ( ) => RequestDelegateFactory . Create ( ( Action < int , int > ) TestAction ) ) ;
725
+ Assert . Throws < InvalidOperationException > ( ( ) => RequestDelegateFactory . Create ( ( Action < int , int > ) TestExplicitlyInvalidAction ) ) ;
726
+ Assert . Throws < InvalidOperationException > ( ( ) => RequestDelegateFactory . Create ( ( Action < Todo , Todo > ) TestInferredInvalidAction ) ) ;
708
727
}
709
728
710
- public static object [ ] [ ] FromServiceParameter
729
+ public static object [ ] [ ] FromServiceActions
711
730
{
712
731
get
713
732
{
@@ -716,7 +735,7 @@ void TestExplicitFromService(HttpContext httpContext, [FromService] MyService my
716
735
httpContext . Items . Add ( "service" , myService ) ;
717
736
}
718
737
719
- void TestImpliedFromService ( HttpContext httpContext , MyService myService )
738
+ void TestImpliedFromService ( HttpContext httpContext , IMyService myService )
720
739
{
721
740
httpContext . Items . Add ( "service" , myService ) ;
722
741
}
@@ -730,13 +749,14 @@ void TestImpliedFromService(HttpContext httpContext, MyService myService)
730
749
}
731
750
732
751
[ Theory ]
733
- [ MemberData ( nameof ( FromServiceParameter ) ) ]
752
+ [ MemberData ( nameof ( FromServiceActions ) ) ]
734
753
public async Task RequestDelegatePopulatesParametersFromServiceWithAndWithoutAttribute ( Delegate action )
735
754
{
736
755
var myOriginalService = new MyService ( ) ;
737
756
738
757
var serviceCollection = new ServiceCollection ( ) ;
739
758
serviceCollection . AddSingleton ( myOriginalService ) ;
759
+ serviceCollection . AddSingleton < IMyService > ( myOriginalService ) ;
740
760
741
761
var httpContext = new DefaultHttpContext ( ) ;
742
762
httpContext . RequestServices = serviceCollection . BuildServiceProvider ( ) ;
@@ -749,7 +769,7 @@ public async Task RequestDelegatePopulatesParametersFromServiceWithAndWithoutAtt
749
769
}
750
770
751
771
[ Theory ]
752
- [ MemberData ( nameof ( FromServiceParameter ) ) ]
772
+ [ MemberData ( nameof ( FromServiceActions ) ) ]
753
773
public async Task RequestDelegateRequiresServiceForAllFromServiceParameters ( Delegate action )
754
774
{
755
775
var httpContext = new DefaultHttpContext ( ) ;
@@ -1058,7 +1078,11 @@ private class FromServiceAttribute : Attribute, IFromServiceMetadata
1058
1078
{
1059
1079
}
1060
1080
1061
- private class MyService
1081
+ private interface IMyService
1082
+ {
1083
+ }
1084
+
1085
+ private class MyService : IMyService
1062
1086
{
1063
1087
}
1064
1088
0 commit comments