@@ -361,14 +361,8 @@ public static bool TryParse(string? value, out MyTryParsableRecord? result)
361361 [ MemberData ( nameof ( TryParsableParameters ) ) ]
362362 public async Task RequestDelegatePopulatesUnattributedTryParsableParametersFromRouteValue ( Delegate action , string ? routeValue , object ? expectedParameterValue )
363363 {
364- var invalidDataException = new InvalidDataException ( ) ;
365- var serviceCollection = new ServiceCollection ( ) ;
366- serviceCollection . AddSingleton ( LoggerFactory ) ;
367-
368364 var httpContext = new DefaultHttpContext ( ) ;
369365 httpContext . Request . RouteValues [ "tryParsable" ] = routeValue ;
370- httpContext . Features . Set < IHttpRequestLifetimeFeature > ( new TestHttpRequestLifetimeFeature ( ) ) ;
371- httpContext . RequestServices = serviceCollection . BuildServiceProvider ( ) ;
372366
373367 var requestDelegate = RequestDelegateFactory . Create ( action ) ;
374368
@@ -416,7 +410,7 @@ public async Task RequestDelegatePopulatesUnattributedTryParsableParametersFromR
416410 Assert . Equal ( 42 , httpContext . Items [ "tryParsable" ] ) ;
417411 }
418412
419- public static object [ ] [ ] DelegatesWithInvalidAttributes
413+ public static object [ ] [ ] DelegatesWithAttributesOnNotTryParsableParameters
420414 {
421415 get
422416 {
@@ -434,7 +428,7 @@ void InvalidFromHeader([FromHeader] object notTryParsable) { }
434428 }
435429
436430 [ Theory ]
437- [ MemberData ( nameof ( DelegatesWithInvalidAttributes ) ) ]
431+ [ MemberData ( nameof ( DelegatesWithAttributesOnNotTryParsableParameters ) ) ]
438432 public void CreateThrowsInvalidOperationExceptionWhenAttributeRequiresTryParseMethodThatDoesNotExist ( Delegate action )
439433 {
440434 var ex = Assert . Throws < InvalidOperationException > ( ( ) => RequestDelegateFactory . Create ( action ) ) ;
@@ -460,7 +454,6 @@ void TestAction([FromRoute] int tryParsable, [FromRoute] int tryParsable2)
460454 invoked = true ;
461455 }
462456
463- var invalidDataException = new InvalidDataException ( ) ;
464457 var serviceCollection = new ServiceCollection ( ) ;
465458 serviceCollection . AddSingleton ( LoggerFactory ) ;
466459
@@ -542,47 +535,61 @@ void TestAction([FromHeader(Name = customHeaderName)] int value)
542535 Assert . Equal ( originalHeaderParam , deserializedRouteParam ) ;
543536 }
544537
545- [ Fact ]
546- public async Task RequestDelegatePopulatesFromBodyParameter ( )
538+ public static object [ ] [ ] FromBodyActions
547539 {
548- Todo originalTodo = new ( )
540+ get
549541 {
550- Name = "Write more tests!"
551- } ;
542+ void TestExplicitFromBody ( HttpContext httpContext , [ FromBody ] Todo todo )
543+ {
544+ httpContext . Items . Add ( "body" , todo ) ;
545+ }
552546
553- Todo ? deserializedRequestBody = null ;
547+ void TestImpliedFromBody ( HttpContext httpContext , Todo myService )
548+ {
549+ httpContext . Items . Add ( "body" , myService ) ;
550+ }
554551
555- void TestAction ( [ FromBody ] Todo todo )
556- {
557- deserializedRequestBody = todo ;
552+ return new [ ]
553+ {
554+ new [ ] { ( Action < HttpContext , Todo > ) TestExplicitFromBody } ,
555+ new [ ] { ( Action < HttpContext , Todo > ) TestImpliedFromBody } ,
556+ } ;
558557 }
558+ }
559+
560+ [ Theory ]
561+ [ MemberData ( nameof ( FromBodyActions ) ) ]
562+ public async Task RequestDelegatePopulatesFromBodyParameter ( Delegate action )
563+ {
564+ Todo originalTodo = new ( )
565+ {
566+ Name = "Write more tests!"
567+ } ;
559568
560569 var httpContext = new DefaultHttpContext ( ) ;
561570 httpContext . Request . Headers [ "Content-Type" ] = "application/json" ;
562571
563572 var requestBodyBytes = JsonSerializer . SerializeToUtf8Bytes ( originalTodo ) ;
564573 httpContext . Request . Body = new MemoryStream ( requestBodyBytes ) ;
565574
566- var requestDelegate = RequestDelegateFactory . Create ( ( Action < Todo > ) TestAction ) ;
575+ var requestDelegate = RequestDelegateFactory . Create ( action ) ;
567576
568577 await requestDelegate ( httpContext ) ;
569578
579+ var deserializedRequestBody = httpContext . Items [ "body" ] ;
570580 Assert . NotNull ( deserializedRequestBody ) ;
571- Assert . Equal ( originalTodo . Name , deserializedRequestBody ! . Name ) ;
581+ Assert . Equal ( originalTodo . Name , ( ( Todo ) deserializedRequestBody ! ) . Name ) ;
572582 }
573583
574- [ Fact ]
575- public async Task RequestDelegateRejectsEmptyBodyGivenDefaultFromBodyParameter ( )
584+ [ Theory ]
585+ [ MemberData ( nameof ( FromBodyActions ) ) ]
586+ public async Task RequestDelegateRejectsEmptyBodyGivenFromBodyParameter ( Delegate action )
576587 {
577- void TestAction ( [ FromBody ] Todo todo )
578- {
579- }
580-
581588 var httpContext = new DefaultHttpContext ( ) ;
582589 httpContext . Request . Headers [ "Content-Type" ] = "application/json" ;
583590 httpContext . Request . Headers [ "Content-Length" ] = "0" ;
584591
585- var requestDelegate = RequestDelegateFactory . Create ( ( Action < Todo > ) TestAction ) ;
592+ var requestDelegate = RequestDelegateFactory . Create ( action ) ;
586593
587594 await Assert . ThrowsAsync < JsonException > ( ( ) => requestDelegate ( httpContext ) ) ;
588595 }
@@ -702,12 +709,16 @@ void TestAction([FromBody] Todo todo)
702709 [ Fact ]
703710 public void BuildRequestDelegateThrowsInvalidOperationExceptionGivenFromBodyOnMultipleParameters ( )
704711 {
705- void TestAction ( [ FromBody ] int value1 , [ FromBody ] int value2 ) { }
712+ void TestAttributedInvalidAction ( [ FromBody ] int value1 , [ FromBody ] int value2 ) { }
713+ void TestInferredInvalidAction ( Todo value1 , Todo value2 ) { }
714+ void TestBothInvalidAction ( Todo value1 , [ FromBody ] int value2 ) { }
706715
707- Assert . Throws < InvalidOperationException > ( ( ) => RequestDelegateFactory . Create ( ( Action < int , int > ) TestAction ) ) ;
716+ Assert . Throws < InvalidOperationException > ( ( ) => RequestDelegateFactory . Create ( ( Action < int , int > ) TestAttributedInvalidAction ) ) ;
717+ Assert . Throws < InvalidOperationException > ( ( ) => RequestDelegateFactory . Create ( ( Action < Todo , Todo > ) TestInferredInvalidAction ) ) ;
718+ Assert . Throws < InvalidOperationException > ( ( ) => RequestDelegateFactory . Create ( ( Action < Todo , int > ) TestBothInvalidAction ) ) ;
708719 }
709720
710- public static object [ ] [ ] FromServiceParameter
721+ public static object [ ] [ ] FromServiceActions
711722 {
712723 get
713724 {
@@ -716,7 +727,7 @@ void TestExplicitFromService(HttpContext httpContext, [FromService] MyService my
716727 httpContext . Items . Add ( "service" , myService ) ;
717728 }
718729
719- void TestImpliedFromService ( HttpContext httpContext , MyService myService )
730+ void TestImpliedFromService ( HttpContext httpContext , IMyService myService )
720731 {
721732 httpContext . Items . Add ( "service" , myService ) ;
722733 }
@@ -730,13 +741,14 @@ void TestImpliedFromService(HttpContext httpContext, MyService myService)
730741 }
731742
732743 [ Theory ]
733- [ MemberData ( nameof ( FromServiceParameter ) ) ]
744+ [ MemberData ( nameof ( FromServiceActions ) ) ]
734745 public async Task RequestDelegatePopulatesParametersFromServiceWithAndWithoutAttribute ( Delegate action )
735746 {
736747 var myOriginalService = new MyService ( ) ;
737748
738749 var serviceCollection = new ServiceCollection ( ) ;
739750 serviceCollection . AddSingleton ( myOriginalService ) ;
751+ serviceCollection . AddSingleton < IMyService > ( myOriginalService ) ;
740752
741753 var httpContext = new DefaultHttpContext ( ) ;
742754 httpContext . RequestServices = serviceCollection . BuildServiceProvider ( ) ;
@@ -749,11 +761,11 @@ public async Task RequestDelegatePopulatesParametersFromServiceWithAndWithoutAtt
749761 }
750762
751763 [ Theory ]
752- [ MemberData ( nameof ( FromServiceParameter ) ) ]
764+ [ MemberData ( nameof ( FromServiceActions ) ) ]
753765 public async Task RequestDelegateRequiresServiceForAllFromServiceParameters ( Delegate action )
754766 {
755767 var httpContext = new DefaultHttpContext ( ) ;
756- httpContext . RequestServices = ( new ServiceCollection ( ) ) . BuildServiceProvider ( ) ;
768+ httpContext . RequestServices = new ServiceCollection ( ) . BuildServiceProvider ( ) ;
757769
758770 var requestDelegate = RequestDelegateFactory . Create ( ( Action < HttpContext , MyService > ) action ) ;
759771
@@ -1058,7 +1070,11 @@ private class FromServiceAttribute : Attribute, IFromServiceMetadata
10581070 {
10591071 }
10601072
1061- private class MyService
1073+ private interface IMyService
1074+ {
1075+ }
1076+
1077+ private class MyService : IMyService
10621078 {
10631079 }
10641080
0 commit comments