Skip to content

Commit 24a748c

Browse files
committed
Add tests!
1 parent 77e5784 commit 24a748c

File tree

2 files changed

+44
-20
lines changed

2 files changed

+44
-20
lines changed

src/Http/Http.Extensions/src/RequestDelegateFactory.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,7 @@ private static Expression AddResponseWritingToMethodCall(Expression methodCall,
428428
var invoker = Expression.Lambda<Func<object?, HttpContext, object?, Task>>(
429429
responseWritingMethodCall, TargetExpr, HttpContextExpr, BodyValueExpr).Compile();
430430

431-
var bodyType = factoryContext.JsonRequestBodyType!;
431+
var bodyType = factoryContext.JsonRequestBodyType;
432432
object? defaultBodyValue = null;
433433

434434
if (factoryContext.AllowEmptyRequestBody && bodyType.IsValueType)

src/Http/Http.Extensions/test/RequestDelegateFactoryTests.cs

Lines changed: 43 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -542,39 +542,56 @@ void TestAction([FromHeader(Name = customHeaderName)] int value)
542542
Assert.Equal(originalHeaderParam, deserializedRouteParam);
543543
}
544544

545-
[Fact]
546-
public async Task RequestDelegatePopulatesFromBodyParameter()
545+
public static object[][] FromBodyActions
547546
{
548-
Todo originalTodo = new()
547+
get
549548
{
550-
Name = "Write more tests!"
551-
};
549+
void TestExplicitFromBody(HttpContext httpContext, [FromBody] Todo todo)
550+
{
551+
httpContext.Items.Add("body", todo);
552+
}
552553

553-
Todo? deserializedRequestBody = null;
554+
void TestImpliedFromBody(HttpContext httpContext, Todo myService)
555+
{
556+
httpContext.Items.Add("body", myService);
557+
}
554558

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+
};
558564
}
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+
};
559575

560576
var httpContext = new DefaultHttpContext();
561577
httpContext.Request.Headers["Content-Type"] = "application/json";
562578

563579
var requestBodyBytes = JsonSerializer.SerializeToUtf8Bytes(originalTodo);
564580
httpContext.Request.Body = new MemoryStream(requestBodyBytes);
565581

566-
var requestDelegate = RequestDelegateFactory.Create((Action<Todo>)TestAction);
582+
var requestDelegate = RequestDelegateFactory.Create(action);
567583

568584
await requestDelegate(httpContext);
569585

586+
var deserializedRequestBody = httpContext.Items["body"];
570587
Assert.NotNull(deserializedRequestBody);
571-
Assert.Equal(originalTodo.Name, deserializedRequestBody!.Name);
588+
Assert.Equal(originalTodo.Name, ((Todo)deserializedRequestBody!).Name);
572589
}
573590

574591
[Fact]
575592
public async Task RequestDelegateRejectsEmptyBodyGivenDefaultFromBodyParameter()
576593
{
577-
void TestAction([FromBody] Todo todo)
594+
void TestAction(Todo todo)
578595
{
579596
}
580597

@@ -702,12 +719,14 @@ void TestAction([FromBody] Todo todo)
702719
[Fact]
703720
public void BuildRequestDelegateThrowsInvalidOperationExceptionGivenFromBodyOnMultipleParameters()
704721
{
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) { }
706724

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));
708727
}
709728

710-
public static object[][] FromServiceParameter
729+
public static object[][] FromServiceActions
711730
{
712731
get
713732
{
@@ -716,7 +735,7 @@ void TestExplicitFromService(HttpContext httpContext, [FromService] MyService my
716735
httpContext.Items.Add("service", myService);
717736
}
718737

719-
void TestImpliedFromService(HttpContext httpContext, MyService myService)
738+
void TestImpliedFromService(HttpContext httpContext, IMyService myService)
720739
{
721740
httpContext.Items.Add("service", myService);
722741
}
@@ -730,13 +749,14 @@ void TestImpliedFromService(HttpContext httpContext, MyService myService)
730749
}
731750

732751
[Theory]
733-
[MemberData(nameof(FromServiceParameter))]
752+
[MemberData(nameof(FromServiceActions))]
734753
public async Task RequestDelegatePopulatesParametersFromServiceWithAndWithoutAttribute(Delegate action)
735754
{
736755
var myOriginalService = new MyService();
737756

738757
var serviceCollection = new ServiceCollection();
739758
serviceCollection.AddSingleton(myOriginalService);
759+
serviceCollection.AddSingleton<IMyService>(myOriginalService);
740760

741761
var httpContext = new DefaultHttpContext();
742762
httpContext.RequestServices = serviceCollection.BuildServiceProvider();
@@ -749,7 +769,7 @@ public async Task RequestDelegatePopulatesParametersFromServiceWithAndWithoutAtt
749769
}
750770

751771
[Theory]
752-
[MemberData(nameof(FromServiceParameter))]
772+
[MemberData(nameof(FromServiceActions))]
753773
public async Task RequestDelegateRequiresServiceForAllFromServiceParameters(Delegate action)
754774
{
755775
var httpContext = new DefaultHttpContext();
@@ -1058,7 +1078,11 @@ private class FromServiceAttribute : Attribute, IFromServiceMetadata
10581078
{
10591079
}
10601080

1061-
private class MyService
1081+
private interface IMyService
1082+
{
1083+
}
1084+
1085+
private class MyService : IMyService
10621086
{
10631087
}
10641088

0 commit comments

Comments
 (0)