@@ -361,14 +361,8 @@ public static bool TryParse(string? value, out MyTryParsableRecord? result)
361
361
[ MemberData ( nameof ( TryParsableParameters ) ) ]
362
362
public async Task RequestDelegatePopulatesUnattributedTryParsableParametersFromRouteValue ( Delegate action , string ? routeValue , object ? expectedParameterValue )
363
363
{
364
- var invalidDataException = new InvalidDataException ( ) ;
365
- var serviceCollection = new ServiceCollection ( ) ;
366
- serviceCollection . AddSingleton ( LoggerFactory ) ;
367
-
368
364
var httpContext = new DefaultHttpContext ( ) ;
369
365
httpContext . Request . RouteValues [ "tryParsable" ] = routeValue ;
370
- httpContext . Features . Set < IHttpRequestLifetimeFeature > ( new TestHttpRequestLifetimeFeature ( ) ) ;
371
- httpContext . RequestServices = serviceCollection . BuildServiceProvider ( ) ;
372
366
373
367
var requestDelegate = RequestDelegateFactory . Create ( action ) ;
374
368
@@ -416,7 +410,7 @@ public async Task RequestDelegatePopulatesUnattributedTryParsableParametersFromR
416
410
Assert . Equal ( 42 , httpContext . Items [ "tryParsable" ] ) ;
417
411
}
418
412
419
- public static object [ ] [ ] DelegatesWithInvalidAttributes
413
+ public static object [ ] [ ] DelegatesWithAttributesOnNotTryParsableParameters
420
414
{
421
415
get
422
416
{
@@ -434,7 +428,7 @@ void InvalidFromHeader([FromHeader] object notTryParsable) { }
434
428
}
435
429
436
430
[ Theory ]
437
- [ MemberData ( nameof ( DelegatesWithInvalidAttributes ) ) ]
431
+ [ MemberData ( nameof ( DelegatesWithAttributesOnNotTryParsableParameters ) ) ]
438
432
public void CreateThrowsInvalidOperationExceptionWhenAttributeRequiresTryParseMethodThatDoesNotExist ( Delegate action )
439
433
{
440
434
var ex = Assert . Throws < InvalidOperationException > ( ( ) => RequestDelegateFactory . Create ( action ) ) ;
@@ -460,7 +454,6 @@ void TestAction([FromRoute] int tryParsable, [FromRoute] int tryParsable2)
460
454
invoked = true ;
461
455
}
462
456
463
- var invalidDataException = new InvalidDataException ( ) ;
464
457
var serviceCollection = new ServiceCollection ( ) ;
465
458
serviceCollection . AddSingleton ( LoggerFactory ) ;
466
459
@@ -542,47 +535,61 @@ void TestAction([FromHeader(Name = customHeaderName)] int value)
542
535
Assert . Equal ( originalHeaderParam , deserializedRouteParam ) ;
543
536
}
544
537
545
- [ Fact ]
546
- public async Task RequestDelegatePopulatesFromBodyParameter ( )
538
+ public static object [ ] [ ] FromBodyActions
547
539
{
548
- Todo originalTodo = new ( )
540
+ get
549
541
{
550
- Name = "Write more tests!"
551
- } ;
542
+ void TestExplicitFromBody ( HttpContext httpContext , [ FromBody ] Todo todo )
543
+ {
544
+ httpContext . Items . Add ( "body" , todo ) ;
545
+ }
552
546
553
- Todo ? deserializedRequestBody = null ;
547
+ void TestImpliedFromBody ( HttpContext httpContext , Todo myService )
548
+ {
549
+ httpContext . Items . Add ( "body" , myService ) ;
550
+ }
554
551
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
+ } ;
558
557
}
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
+ } ;
559
568
560
569
var httpContext = new DefaultHttpContext ( ) ;
561
570
httpContext . Request . Headers [ "Content-Type" ] = "application/json" ;
562
571
563
572
var requestBodyBytes = JsonSerializer . SerializeToUtf8Bytes ( originalTodo ) ;
564
573
httpContext . Request . Body = new MemoryStream ( requestBodyBytes ) ;
565
574
566
- var requestDelegate = RequestDelegateFactory . Create ( ( Action < Todo > ) TestAction ) ;
575
+ var requestDelegate = RequestDelegateFactory . Create ( action ) ;
567
576
568
577
await requestDelegate ( httpContext ) ;
569
578
579
+ var deserializedRequestBody = httpContext . Items [ "body" ] ;
570
580
Assert . NotNull ( deserializedRequestBody ) ;
571
- Assert . Equal ( originalTodo . Name , deserializedRequestBody ! . Name ) ;
581
+ Assert . Equal ( originalTodo . Name , ( ( Todo ) deserializedRequestBody ! ) . Name ) ;
572
582
}
573
583
574
- [ Fact ]
575
- public async Task RequestDelegateRejectsEmptyBodyGivenDefaultFromBodyParameter ( )
584
+ [ Theory ]
585
+ [ MemberData ( nameof ( FromBodyActions ) ) ]
586
+ public async Task RequestDelegateRejectsEmptyBodyGivenFromBodyParameter ( Delegate action )
576
587
{
577
- void TestAction ( [ FromBody ] Todo todo )
578
- {
579
- }
580
-
581
588
var httpContext = new DefaultHttpContext ( ) ;
582
589
httpContext . Request . Headers [ "Content-Type" ] = "application/json" ;
583
590
httpContext . Request . Headers [ "Content-Length" ] = "0" ;
584
591
585
- var requestDelegate = RequestDelegateFactory . Create ( ( Action < Todo > ) TestAction ) ;
592
+ var requestDelegate = RequestDelegateFactory . Create ( action ) ;
586
593
587
594
await Assert . ThrowsAsync < JsonException > ( ( ) => requestDelegate ( httpContext ) ) ;
588
595
}
@@ -702,12 +709,16 @@ void TestAction([FromBody] Todo todo)
702
709
[ Fact ]
703
710
public void BuildRequestDelegateThrowsInvalidOperationExceptionGivenFromBodyOnMultipleParameters ( )
704
711
{
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 ) { }
706
715
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 ) ) ;
708
719
}
709
720
710
- public static object [ ] [ ] FromServiceParameter
721
+ public static object [ ] [ ] FromServiceActions
711
722
{
712
723
get
713
724
{
@@ -716,7 +727,7 @@ void TestExplicitFromService(HttpContext httpContext, [FromService] MyService my
716
727
httpContext . Items . Add ( "service" , myService ) ;
717
728
}
718
729
719
- void TestImpliedFromService ( HttpContext httpContext , MyService myService )
730
+ void TestImpliedFromService ( HttpContext httpContext , IMyService myService )
720
731
{
721
732
httpContext . Items . Add ( "service" , myService ) ;
722
733
}
@@ -730,13 +741,14 @@ void TestImpliedFromService(HttpContext httpContext, MyService myService)
730
741
}
731
742
732
743
[ Theory ]
733
- [ MemberData ( nameof ( FromServiceParameter ) ) ]
744
+ [ MemberData ( nameof ( FromServiceActions ) ) ]
734
745
public async Task RequestDelegatePopulatesParametersFromServiceWithAndWithoutAttribute ( Delegate action )
735
746
{
736
747
var myOriginalService = new MyService ( ) ;
737
748
738
749
var serviceCollection = new ServiceCollection ( ) ;
739
750
serviceCollection . AddSingleton ( myOriginalService ) ;
751
+ serviceCollection . AddSingleton < IMyService > ( myOriginalService ) ;
740
752
741
753
var httpContext = new DefaultHttpContext ( ) ;
742
754
httpContext . RequestServices = serviceCollection . BuildServiceProvider ( ) ;
@@ -749,11 +761,11 @@ public async Task RequestDelegatePopulatesParametersFromServiceWithAndWithoutAtt
749
761
}
750
762
751
763
[ Theory ]
752
- [ MemberData ( nameof ( FromServiceParameter ) ) ]
764
+ [ MemberData ( nameof ( FromServiceActions ) ) ]
753
765
public async Task RequestDelegateRequiresServiceForAllFromServiceParameters ( Delegate action )
754
766
{
755
767
var httpContext = new DefaultHttpContext ( ) ;
756
- httpContext . RequestServices = ( new ServiceCollection ( ) ) . BuildServiceProvider ( ) ;
768
+ httpContext . RequestServices = new ServiceCollection ( ) . BuildServiceProvider ( ) ;
757
769
758
770
var requestDelegate = RequestDelegateFactory . Create ( ( Action < HttpContext , MyService > ) action ) ;
759
771
@@ -1058,7 +1070,11 @@ private class FromServiceAttribute : Attribute, IFromServiceMetadata
1058
1070
{
1059
1071
}
1060
1072
1061
- private class MyService
1073
+ private interface IMyService
1074
+ {
1075
+ }
1076
+
1077
+ private class MyService : IMyService
1062
1078
{
1063
1079
}
1064
1080
0 commit comments