更新
更旧
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
#nullable enable
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq.Expressions;
using System.Net;
using System.Net.Sockets;
using System.Numerics;
using System.Reflection;
using System.Reflection.Metadata;
using System.Text;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Features;
using Microsoft.AspNetCore.Http.Metadata;
using Microsoft.AspNetCore.Testing;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Primitives;
using Xunit;
namespace Microsoft.AspNetCore.Routing.Internal
{
public class RequestDelegateFactoryTests : LoggedTest
public static IEnumerable<object[]> NoResult
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
void TestAction(HttpContext httpContext)
{
MarkAsInvoked(httpContext);
}
Task TaskTestAction(HttpContext httpContext)
{
MarkAsInvoked(httpContext);
return Task.CompletedTask;
}
ValueTask ValueTaskTestAction(HttpContext httpContext)
{
MarkAsInvoked(httpContext);
return ValueTask.CompletedTask;
}
void StaticTestAction(HttpContext httpContext)
{
MarkAsInvoked(httpContext);
}
Task StaticTaskTestAction(HttpContext httpContext)
{
MarkAsInvoked(httpContext);
return Task.CompletedTask;
}
ValueTask StaticValueTaskTestAction(HttpContext httpContext)
{
MarkAsInvoked(httpContext);
return ValueTask.CompletedTask;
}
void MarkAsInvoked(HttpContext httpContext)
{
httpContext.Items.Add("invoked", true);
}
return new List<object[]>
{
new object[] { (Action<HttpContext>)TestAction },
new object[] { (Func<HttpContext, Task>)TaskTestAction },
new object[] { (Func<HttpContext, ValueTask>)ValueTaskTestAction },
new object[] { (Action<HttpContext>)StaticTestAction },
new object[] { (Func<HttpContext, Task>)StaticTaskTestAction },
new object[] { (Func<HttpContext, ValueTask>)StaticValueTaskTestAction },
};
}
[Theory]
[MemberData(nameof(NoResult))]
public async Task RequestDelegateInvokesAction(Delegate @delegate)
{
var httpContext = new DefaultHttpContext();
var requestDelegate = RequestDelegateFactory.Create(@delegate);
Assert.True(httpContext.Items["invoked"] as bool?);
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
private static void StaticTestActionBasicReflection(HttpContext httpContext)
{
httpContext.Items.Add("invoked", true);
}
[Fact]
public async Task StaticMethodInfoOverloadWorksWithBasicReflection()
{
var methodInfo = typeof(RequestDelegateFactoryTests).GetMethod(
nameof(StaticTestActionBasicReflection),
BindingFlags.NonPublic | BindingFlags.Static,
new[] { typeof(HttpContext) });
var requestDelegate = RequestDelegateFactory.Create(methodInfo!);
var httpContext = new DefaultHttpContext();
await requestDelegate(httpContext);
Assert.True(httpContext.Items["invoked"] as bool?);
}
private class TestNonStaticActionClass
{
private readonly object _invokedValue;
public TestNonStaticActionClass(object invokedValue)
{
_invokedValue = invokedValue;
}
public void NonStaticTestAction(HttpContext httpContext)
{
httpContext.Items.Add("invoked", _invokedValue);
}
}
[Fact]
public async Task NonStaticMethodInfoOverloadWorksWithBasicReflection()
{
var methodInfo = typeof(TestNonStaticActionClass).GetMethod(
nameof(TestNonStaticActionClass.NonStaticTestAction),
BindingFlags.Public | BindingFlags.Instance,
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
new[] { typeof(HttpContext) });
var invoked = false;
object GetTarget()
{
if (!invoked)
{
invoked = true;
return new TestNonStaticActionClass(1);
}
return new TestNonStaticActionClass(2);
}
var requestDelegate = RequestDelegateFactory.Create(methodInfo!, _ => GetTarget());
var httpContext = new DefaultHttpContext();
await requestDelegate(httpContext);
Assert.Equal(1, httpContext.Items["invoked"]);
httpContext = new DefaultHttpContext();
await requestDelegate(httpContext);
Assert.Equal(2, httpContext.Items["invoked"]);
}
[Fact]
public void BuildRequestDelegateThrowsArgumentNullExceptions()
{
var methodInfo = typeof(RequestDelegateFactoryTests).GetMethod(
nameof(StaticTestActionBasicReflection),
BindingFlags.NonPublic | BindingFlags.Static,
new[] { typeof(HttpContext) });
var exNullAction = Assert.Throws<ArgumentNullException>(() => RequestDelegateFactory.Create(action: null!));
var exNullMethodInfo1 = Assert.Throws<ArgumentNullException>(() => RequestDelegateFactory.Create(methodInfo: null!));
var exNullMethodInfo2 = Assert.Throws<ArgumentNullException>(() => RequestDelegateFactory.Create(methodInfo: null!, _ => 0));
var exNullTargetFactory = Assert.Throws<ArgumentNullException>(() => RequestDelegateFactory.Create(methodInfo!, targetFactory: null!));
Assert.Equal("action", exNullAction.ParamName);
Assert.Equal("methodInfo", exNullMethodInfo1.ParamName);
Assert.Equal("methodInfo", exNullMethodInfo2.ParamName);
Assert.Equal("targetFactory", exNullTargetFactory.ParamName);
}
[Fact]
public async Task RequestDelegatePopulatesFromRouteParameterBasedOnParameterName()
{
const string paramName = "value";
const int originalRouteParam = 42;
void TestAction(HttpContext httpContext, [FromRoute] int value)
{
httpContext.Items.Add("input", value);
}
var httpContext = new DefaultHttpContext();
httpContext.Request.RouteValues[paramName] = originalRouteParam.ToString(NumberFormatInfo.InvariantInfo);
var requestDelegate = RequestDelegateFactory.Create((Action<HttpContext, int>)TestAction);
await requestDelegate(httpContext);
Assert.Equal(originalRouteParam, httpContext.Items["input"]);
}
private static void TestAction(HttpContext httpContext, [FromRoute] int value = 42)
{
httpContext.Items.Add("input", value);
[Fact]
public async Task RequestDelegatePopulatesFromRouteOptionalParameter()
var requestDelegate = RequestDelegateFactory.Create((Action<HttpContext, int>)TestAction);
await requestDelegate(httpContext);
Assert.Equal(42, httpContext.Items["input"]);
[Fact]
public async Task RequestDelegatePopulatesFromRouteOptionalParameterBasedOnParameterName()
{
const string paramName = "value";
const int originalRouteParam = 47;
var httpContext = new DefaultHttpContext();
httpContext.Request.RouteValues[paramName] = originalRouteParam.ToString(NumberFormatInfo.InvariantInfo);
var requestDelegate = RequestDelegateFactory.Create((Action<HttpContext, int>)TestAction);
Assert.Equal(47, httpContext.Items["input"]);
}
[Fact]
public async Task RequestDelegatePopulatesFromRouteParameterBasedOnAttributeNameProperty()
{
const string specifiedName = "value";
const int originalRouteParam = 42;
int? deserializedRouteParam = null;
void TestAction([FromRoute(Name = specifiedName)] int foo)
{
deserializedRouteParam = foo;
}
var httpContext = new DefaultHttpContext();
httpContext.Request.RouteValues[specifiedName] = originalRouteParam.ToString(NumberFormatInfo.InvariantInfo);
var requestDelegate = RequestDelegateFactory.Create((Action<int>)TestAction);
await requestDelegate(httpContext);
Assert.Equal(originalRouteParam, deserializedRouteParam);
}
[Fact]
public async Task UsesDefaultValueIfNoMatchingRouteValue()
{
const string unmatchedName = "value";
const int unmatchedRouteParam = 42;
int? deserializedRouteParam = null;
void TestAction([FromRoute] int foo)
{
deserializedRouteParam = foo;
}
var httpContext = new DefaultHttpContext();
httpContext.Request.RouteValues[unmatchedName] = unmatchedRouteParam.ToString(NumberFormatInfo.InvariantInfo);
var requestDelegate = RequestDelegateFactory.Create((Action<int>)TestAction);
await requestDelegate(httpContext);
Assert.Equal(0, deserializedRouteParam);
}
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
public static object?[][] TryParsableParameters
{
get
{
static void Store<T>(HttpContext httpContext, T tryParsable)
{
httpContext.Items["tryParsable"] = tryParsable;
}
var now = DateTime.Now;
return new[]
{
// string is not technically "TryParsable", but it's the special case.
new object[] { (Action<HttpContext, string>)Store, "plain string", "plain string" },
new object[] { (Action<HttpContext, int>)Store, "-42", -42 },
new object[] { (Action<HttpContext, uint>)Store, "42", 42U },
new object[] { (Action<HttpContext, bool>)Store, "true", true },
new object[] { (Action<HttpContext, short>)Store, "-42", (short)-42 },
new object[] { (Action<HttpContext, ushort>)Store, "42", (ushort)42 },
new object[] { (Action<HttpContext, long>)Store, "-42", -42L },
new object[] { (Action<HttpContext, ulong>)Store, "42", 42UL },
new object[] { (Action<HttpContext, IntPtr>)Store, "-42", new IntPtr(-42) },
new object[] { (Action<HttpContext, char>)Store, "A", 'A' },
new object[] { (Action<HttpContext, double>)Store, "0.5", 0.5 },
new object[] { (Action<HttpContext, float>)Store, "0.5", 0.5f },
new object[] { (Action<HttpContext, Half>)Store, "0.5", (Half)0.5f },
new object[] { (Action<HttpContext, decimal>)Store, "0.5", 0.5m },
new object[] { (Action<HttpContext, DateTime>)Store, now.ToString("o"), now },
new object[] { (Action<HttpContext, DateTimeOffset>)Store, "1970-01-01T00:00:00.0000000+00:00", DateTimeOffset.UnixEpoch },
new object[] { (Action<HttpContext, TimeSpan>)Store, "00:00:42", TimeSpan.FromSeconds(42) },
new object[] { (Action<HttpContext, Guid>)Store, "00000000-0000-0000-0000-000000000000", Guid.Empty },
new object[] { (Action<HttpContext, Version>)Store, "6.0.0.42", new Version("6.0.0.42") },
new object[] { (Action<HttpContext, BigInteger>)Store, "-42", new BigInteger(-42) },
new object[] { (Action<HttpContext, IPAddress>)Store, "127.0.0.1", IPAddress.Loopback },
new object[] { (Action<HttpContext, IPEndPoint>)Store, "127.0.0.1:80", new IPEndPoint(IPAddress.Loopback, 80) },
new object[] { (Action<HttpContext, AddressFamily>)Store, "Unix", AddressFamily.Unix },
new object[] { (Action<HttpContext, ILOpCode>)Store, "Nop", ILOpCode.Nop },
new object[] { (Action<HttpContext, AssemblyFlags>)Store, "PublicKey,Retargetable", AssemblyFlags.PublicKey | AssemblyFlags.Retargetable },
new object[] { (Action<HttpContext, int?>)Store, "42", 42 },
new object[] { (Action<HttpContext, MyEnum>)Store, "ValueB", MyEnum.ValueB },
new object[] { (Action<HttpContext, MyTryParsableRecord>)Store, "https://example.org", new MyTryParsableRecord(new Uri("https://example.org")) },
new object?[] { (Action<HttpContext, int>)Store, null, 0 },
new object?[] { (Action<HttpContext, int?>)Store, null, null },
};
}
}
private enum MyEnum { ValueA, ValueB, }
private record MyTryParsableRecord(Uri Uri)
{
public static bool TryParse(string? value, out MyTryParsableRecord? result)
{
if (!Uri.TryCreate(value, UriKind.Absolute, out var uri))
{
result = null;
return false;
}
result = new MyTryParsableRecord(uri);
return true;
}
}
[Theory]
[MemberData(nameof(TryParsableParameters))]
public async Task RequestDelegatePopulatesUnattributedTryParsableParametersFromRouteValue(Delegate action, string? routeValue, object? expectedParameterValue)
{
var httpContext = new DefaultHttpContext();
httpContext.Request.RouteValues["tryParsable"] = routeValue;
var requestDelegate = RequestDelegateFactory.Create(action);
await requestDelegate(httpContext);
Assert.Equal(expectedParameterValue, httpContext.Items["tryParsable"]);
}
[Theory]
[MemberData(nameof(TryParsableParameters))]
public async Task RequestDelegatePopulatesUnattributedTryParsableParametersFromQueryString(Delegate action, string? routeValue, object? expectedParameterValue)
{
var httpContext = new DefaultHttpContext();
httpContext.Request.Query = new QueryCollection(new Dictionary<string, StringValues>
{
["tryParsable"] = routeValue
});
var requestDelegate = RequestDelegateFactory.Create(action);
await requestDelegate(httpContext);
Assert.Equal(expectedParameterValue, httpContext.Items["tryParsable"]);
}
[Fact]
public async Task RequestDelegatePopulatesUnattributedTryParsableParametersFromRouteValueBeforeQueryString()
{
var httpContext = new DefaultHttpContext();
httpContext.Request.RouteValues["tryParsable"] = "42";
httpContext.Request.Query = new QueryCollection(new Dictionary<string, StringValues>
{
["tryParsable"] = "invalid!"
});
var requestDelegate = RequestDelegateFactory.Create((Action<HttpContext, int>)((httpContext, tryParsable) =>
{
httpContext.Items["tryParsable"] = tryParsable;
}));
await requestDelegate(httpContext);
Assert.Equal(42, httpContext.Items["tryParsable"]);
}
public static object[][] DelegatesWithAttributesOnNotTryParsableParameters
{
get
{
void InvalidFromRoute([FromRoute] object notTryParsable) { }
void InvalidFromQuery([FromQuery] object notTryParsable) { }
void InvalidFromHeader([FromHeader] object notTryParsable) { }
return new[]
{
new object[] { (Action<object>)InvalidFromRoute },
new object[] { (Action<object>)InvalidFromQuery },
new object[] { (Action<object>)InvalidFromHeader },
};
}
}
[Theory]
[MemberData(nameof(DelegatesWithAttributesOnNotTryParsableParameters))]
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
public void CreateThrowsInvalidOperationExceptionWhenAttributeRequiresTryParseMethodThatDoesNotExist(Delegate action)
{
var ex = Assert.Throws<InvalidOperationException>(() => RequestDelegateFactory.Create(action));
Assert.Equal("No public static bool Object.TryParse(string, out Object) method found for notTryParsable.", ex.Message);
}
[Fact]
public void CreateThrowsInvalidOperationExceptionGivenUnnamedArgument()
{
var unnamedParameter = Expression.Parameter(typeof(int));
var lambda = Expression.Lambda(Expression.Block(), unnamedParameter);
var ex = Assert.Throws<InvalidOperationException>(() => RequestDelegateFactory.Create((Action<int>)lambda.Compile()));
Assert.Equal("A parameter does not have a name! Was it genererated? All parameters must be named.", ex.Message);
}
[Fact]
public async Task RequestDelegateLogsTryParsableFailuresAsDebugAndSets400Response()
{
var invoked = false;
void TestAction([FromRoute] int tryParsable, [FromRoute] int tryParsable2)
{
invoked = true;
}
var serviceCollection = new ServiceCollection();
serviceCollection.AddSingleton(LoggerFactory);
var httpContext = new DefaultHttpContext();
httpContext.Request.RouteValues["tryParsable"] = "invalid!";
httpContext.Request.RouteValues["tryParsable2"] = "invalid again!";
httpContext.Features.Set<IHttpRequestLifetimeFeature>(new TestHttpRequestLifetimeFeature());
httpContext.RequestServices = serviceCollection.BuildServiceProvider();
var requestDelegate = RequestDelegateFactory.Create((Action<int, int>)TestAction);
await requestDelegate(httpContext);
Assert.False(invoked);
Assert.False(httpContext.RequestAborted.IsCancellationRequested);
Assert.Equal(400, httpContext.Response.StatusCode);
var logs = TestSink.Writes.ToArray();
Assert.Equal(2, logs.Length);
Assert.Equal(new EventId(3, "ParamaterBindingFailed"), logs[0].EventId);
Assert.Equal(LogLevel.Debug, logs[0].LogLevel);
Assert.Equal(@"Failed to bind parameter ""Int32 tryParsable"" from ""invalid!"".", logs[0].Message);
Assert.Equal(new EventId(3, "ParamaterBindingFailed"), logs[0].EventId);
Assert.Equal(LogLevel.Debug, logs[0].LogLevel);
Assert.Equal(@"Failed to bind parameter ""Int32 tryParsable2"" from ""invalid again!"".", logs[1].Message);
}
[Fact]
public async Task RequestDelegatePopulatesFromQueryParameterBasedOnParameterName()
{
const string paramName = "value";
const int originalQueryParam = 42;
int? deserializedRouteParam = null;
void TestAction([FromQuery] int value)
{
deserializedRouteParam = value;
}
var query = new QueryCollection(new Dictionary<string, StringValues>()
{
[paramName] = originalQueryParam.ToString(NumberFormatInfo.InvariantInfo)
});
var httpContext = new DefaultHttpContext();
httpContext.Request.Query = query;
var requestDelegate = RequestDelegateFactory.Create((Action<int>)TestAction);
await requestDelegate(httpContext);
Assert.Equal(originalQueryParam, deserializedRouteParam);
}
[Fact]
public async Task RequestDelegatePopulatesFromHeaderParameterBasedOnParameterName()
{
const string customHeaderName = "X-Custom-Header";
const int originalHeaderParam = 42;
int? deserializedRouteParam = null;
void TestAction([FromHeader(Name = customHeaderName)] int value)
{
deserializedRouteParam = value;
}
var httpContext = new DefaultHttpContext();
httpContext.Request.Headers[customHeaderName] = originalHeaderParam.ToString(NumberFormatInfo.InvariantInfo);
var requestDelegate = RequestDelegateFactory.Create((Action<int>)TestAction);
await requestDelegate(httpContext);
Assert.Equal(originalHeaderParam, deserializedRouteParam);
}
public static object[][] FromBodyActions
void TestExplicitFromBody(HttpContext httpContext, [FromBody] Todo todo)
{
httpContext.Items.Add("body", todo);
}
void TestImpliedFromBody(HttpContext httpContext, Todo myService)
{
httpContext.Items.Add("body", myService);
}
return new[]
{
new[] { (Action<HttpContext, Todo>)TestExplicitFromBody },
new[] { (Action<HttpContext, Todo>)TestImpliedFromBody },
};
}
[Theory]
[MemberData(nameof(FromBodyActions))]
public async Task RequestDelegatePopulatesFromBodyParameter(Delegate action)
{
Todo originalTodo = new()
{
Name = "Write more tests!"
};
var httpContext = new DefaultHttpContext();
httpContext.Request.Headers["Content-Type"] = "application/json";
var requestBodyBytes = JsonSerializer.SerializeToUtf8Bytes(originalTodo);
httpContext.Request.Body = new MemoryStream(requestBodyBytes);
var requestDelegate = RequestDelegateFactory.Create(action);
var deserializedRequestBody = httpContext.Items["body"];
Assert.Equal(originalTodo.Name, ((Todo)deserializedRequestBody!).Name);
[Theory]
[MemberData(nameof(FromBodyActions))]
public async Task RequestDelegateRejectsEmptyBodyGivenFromBodyParameter(Delegate action)
{
var httpContext = new DefaultHttpContext();
httpContext.Request.Headers["Content-Type"] = "application/json";
httpContext.Request.Headers["Content-Length"] = "0";
var requestDelegate = RequestDelegateFactory.Create(action);
await Assert.ThrowsAsync<JsonException>(() => requestDelegate(httpContext));
}
[Fact]
public async Task RequestDelegateAllowsEmptyBodyGivenCorrectyConfiguredFromBodyParameter()
{
var todoToBecomeNull = new Todo();
void TestAction([FromBody(AllowEmpty = true)] Todo todo)
{
todoToBecomeNull = todo;
}
var httpContext = new DefaultHttpContext();
httpContext.Request.Headers["Content-Type"] = "application/json";
httpContext.Request.Headers["Content-Length"] = "0";
var requestDelegate = RequestDelegateFactory.Create((Action<Todo>)TestAction);
await requestDelegate(httpContext);
Assert.Null(todoToBecomeNull);
}
[Fact]
public async Task RequestDelegateAllowsEmptyBodyStructGivenCorrectyConfiguredFromBodyParameter()
{
var structToBeZeroed = new BodyStruct
{
Id = 42
};
void TestAction([FromBody(AllowEmpty = true)] BodyStruct bodyStruct)
{
structToBeZeroed = bodyStruct;
}
var httpContext = new DefaultHttpContext();
httpContext.Request.Headers["Content-Type"] = "application/json";
httpContext.Request.Headers["Content-Length"] = "0";
var requestDelegate = RequestDelegateFactory.Create((Action<BodyStruct>)TestAction);
await requestDelegate(httpContext);
Assert.Equal(default, structToBeZeroed);
}
[Fact]
public async Task RequestDelegateLogsFromBodyIOExceptionsAsDebugAndDoesNotAbort()
{
var invoked = false;
void TestAction([FromBody] Todo todo)
{
invoked = true;
}
var ioException = new IOException();
var serviceCollection = new ServiceCollection();
serviceCollection.AddSingleton(LoggerFactory);
var httpContext = new DefaultHttpContext();
httpContext.Request.Headers["Content-Type"] = "application/json";
httpContext.Request.Body = new IOExceptionThrowingRequestBodyStream(ioException);
httpContext.Features.Set<IHttpRequestLifetimeFeature>(new TestHttpRequestLifetimeFeature());
httpContext.RequestServices = serviceCollection.BuildServiceProvider();
var requestDelegate = RequestDelegateFactory.Create((Action<Todo>)TestAction);
await requestDelegate(httpContext);
Assert.False(invoked);
Assert.False(httpContext.RequestAborted.IsCancellationRequested);
var logMessage = Assert.Single(TestSink.Writes);
Assert.Equal(new EventId(1, "RequestBodyIOException"), logMessage.EventId);
Assert.Equal(LogLevel.Debug, logMessage.LogLevel);
Assert.Same(ioException, logMessage.Exception);
}
[Fact]
public async Task RequestDelegateLogsFromBodyInvalidDataExceptionsAsDebugAndSets400Response()
{
var invoked = false;
void TestAction([FromBody] Todo todo)
{
invoked = true;
}
var invalidDataException = new InvalidDataException();
var serviceCollection = new ServiceCollection();
serviceCollection.AddSingleton(LoggerFactory);
var httpContext = new DefaultHttpContext();
httpContext.Request.Headers["Content-Type"] = "application/json";
httpContext.Request.Body = new IOExceptionThrowingRequestBodyStream(invalidDataException);
httpContext.Features.Set<IHttpRequestLifetimeFeature>(new TestHttpRequestLifetimeFeature());
httpContext.RequestServices = serviceCollection.BuildServiceProvider();
var requestDelegate = RequestDelegateFactory.Create((Action<Todo>)TestAction);
await requestDelegate(httpContext);
Assert.False(invoked);
Assert.False(httpContext.RequestAborted.IsCancellationRequested);
Assert.Equal(400, httpContext.Response.StatusCode);
var logMessage = Assert.Single(TestSink.Writes);
Assert.Equal(new EventId(2, "RequestBodyInvalidDataException"), logMessage.EventId);
Assert.Equal(LogLevel.Debug, logMessage.LogLevel);
Assert.Same(invalidDataException, logMessage.Exception);
}
[Fact]
public void BuildRequestDelegateThrowsInvalidOperationExceptionGivenFromBodyOnMultipleParameters()
{
void TestAttributedInvalidAction([FromBody] int value1, [FromBody] int value2) { }
void TestInferredInvalidAction(Todo value1, Todo value2) { }
void TestBothInvalidAction(Todo value1, [FromBody] int value2) { }
Assert.Throws<InvalidOperationException>(() => RequestDelegateFactory.Create((Action<int, int>)TestAttributedInvalidAction));
Assert.Throws<InvalidOperationException>(() => RequestDelegateFactory.Create((Action<Todo, Todo>)TestInferredInvalidAction));
Assert.Throws<InvalidOperationException>(() => RequestDelegateFactory.Create((Action<Todo, int>)TestBothInvalidAction));
public static object[][] FromServiceActions
void TestExplicitFromService(HttpContext httpContext, [FromService] MyService myService)
{
httpContext.Items.Add("service", myService);
}
void TestImpliedFromService(HttpContext httpContext, IMyService myService)
{
httpContext.Items.Add("service", myService);
}
return new[]
{
new[] { (Action<HttpContext, MyService>)TestExplicitFromService },
new[] { (Action<HttpContext, MyService>)TestImpliedFromService },
};
}
[Theory]
[MemberData(nameof(FromServiceActions))]
public async Task RequestDelegatePopulatesParametersFromServiceWithAndWithoutAttribute(Delegate action)
{
var myOriginalService = new MyService();
var serviceCollection = new ServiceCollection();
serviceCollection.AddSingleton(myOriginalService);
serviceCollection.AddSingleton<IMyService>(myOriginalService);
var httpContext = new DefaultHttpContext();
httpContext.RequestServices = serviceCollection.BuildServiceProvider();
var requestDelegate = RequestDelegateFactory.Create((Action<HttpContext, MyService>)action);
Assert.Same(myOriginalService, httpContext.Items["service"]);
}
[Theory]
[MemberData(nameof(FromServiceActions))]
public async Task RequestDelegateRequiresServiceForAllFromServiceParameters(Delegate action)
{
var httpContext = new DefaultHttpContext();
httpContext.RequestServices = new ServiceCollection().BuildServiceProvider();
var requestDelegate = RequestDelegateFactory.Create((Action<HttpContext, MyService>)action);
await Assert.ThrowsAsync<InvalidOperationException>(() => requestDelegate(httpContext));
}
[Fact]
public async Task RequestDelegatePopulatesHttpContextParameterWithoutAttribute()
{
HttpContext? httpContextArgument = null;
void TestAction(HttpContext httpContext)
{
httpContextArgument = httpContext;
}
var httpContext = new DefaultHttpContext();
var requestDelegate = RequestDelegateFactory.Create((Action<HttpContext>)TestAction);
await requestDelegate(httpContext);
Assert.Same(httpContext, httpContextArgument);
}
[Fact]
public async Task RequestDelegatePassHttpContextRequestAbortedAsCancelationToken()
{
CancellationToken? cancellationTokenArgument = null;
void TestAction(CancellationToken cancellationToken)
{
cancellationTokenArgument = cancellationToken;
}
using var cts = new CancellationTokenSource();
var httpContext = new DefaultHttpContext
{
RequestAborted = cts.Token
};
var requestDelegate = RequestDelegateFactory.Create((Action<CancellationToken>)TestAction);
await requestDelegate(httpContext);
Assert.Equal(httpContext.RequestAborted, cancellationTokenArgument);
}
public static IEnumerable<object[]> ComplexResult
Todo originalTodo = new()
{
Name = "Write even more tests!"
};
Todo TestAction() => originalTodo;
Task<Todo> TaskTestAction() => Task.FromResult(originalTodo);
ValueTask<Todo> ValueTaskTestAction() => ValueTask.FromResult(originalTodo);
static Todo StaticTestAction() => new Todo { Name = "Write even more tests!" };
static Task<Todo> StaticTaskTestAction() => Task.FromResult(new Todo { Name = "Write even more tests!" });
static ValueTask<Todo> StaticValueTaskTestAction() => ValueTask.FromResult(new Todo { Name = "Write even more tests!" });
return new List<object[]>
{
new object[] { (Func<Todo>)TestAction },
new object[] { (Func<Task<Todo>>)TaskTestAction},
new object[] { (Func<ValueTask<Todo>>)ValueTaskTestAction},
new object[] { (Func<Todo>)StaticTestAction},
new object[] { (Func<Task<Todo>>)StaticTaskTestAction},
new object[] { (Func<ValueTask<Todo>>)StaticValueTaskTestAction},
};
}
}
[Theory]
[MemberData(nameof(ComplexResult))]
public async Task RequestDelegateWritesComplexReturnValueAsJsonResponseBody(Delegate @delegate)
{
var httpContext = new DefaultHttpContext();
var responseBodyStream = new MemoryStream();
httpContext.Response.Body = responseBodyStream;
var requestDelegate = RequestDelegateFactory.Create(@delegate);
await requestDelegate(httpContext);
var deserializedResponseBody = JsonSerializer.Deserialize<Todo>(responseBodyStream.ToArray(), new JsonSerializerOptions
{
// TODO: the output is "{\"id\":0,\"name\":\"Write even more tests!\",\"isComplete\":false}"
// Verify that the camelCased property names are consistent with MVC and if so whether we should keep the behavior.
PropertyNameCaseInsensitive = true
});
Assert.NotNull(deserializedResponseBody);
Assert.Equal("Write even more tests!", deserializedResponseBody!.Name);
public static IEnumerable<object[]> CustomResults
get
{
var resultString = "Still not enough tests!";
CustomResult TestAction() => new CustomResult(resultString);
Task<CustomResult> TaskTestAction() => Task.FromResult(new CustomResult(resultString));
ValueTask<CustomResult> ValueTaskTestAction() => ValueTask.FromResult(new CustomResult(resultString));
static CustomResult StaticTestAction() => new CustomResult("Still not enough tests!");
static Task<CustomResult> StaticTaskTestAction() => Task.FromResult(new CustomResult("Still not enough tests!"));
static ValueTask<CustomResult> StaticValueTaskTestAction() => ValueTask.FromResult(new CustomResult("Still not enough tests!"));
return new List<object[]>
{
new object[] { (Func<CustomResult>)TestAction },
new object[] { (Func<Task<CustomResult>>)TaskTestAction},
new object[] { (Func<ValueTask<CustomResult>>)ValueTaskTestAction},
new object[] { (Func<CustomResult>)StaticTestAction},
new object[] { (Func<Task<CustomResult>>)StaticTaskTestAction},
new object[] { (Func<ValueTask<CustomResult>>)StaticValueTaskTestAction},
};
}
}
[Theory]
[MemberData(nameof(CustomResults))]
public async Task RequestDelegateUsesCustomIResult(Delegate @delegate)
{
var httpContext = new DefaultHttpContext();
var responseBodyStream = new MemoryStream();
httpContext.Response.Body = responseBodyStream;
var requestDelegate = RequestDelegateFactory.Create(@delegate);
await requestDelegate(httpContext);
var decodedResponseBody = Encoding.UTF8.GetString(responseBodyStream.ToArray());
Assert.Equal("Still not enough tests!", decodedResponseBody);
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
public static IEnumerable<object[]> StringResult
{
get
{
var test = "String Test";
string TestAction() => test;
Task<string> TaskTestAction() => Task.FromResult(test);
ValueTask<string> ValueTaskTestAction() => ValueTask.FromResult(test);
static string StaticTestAction() => "String Test";
static Task<string> StaticTaskTestAction() => Task.FromResult("String Test");
static ValueTask<string> StaticValueTaskTestAction() => ValueTask.FromResult("String Test");
return new List<object[]>
{
new object[] { (Func<string>)TestAction },
new object[] { (Func<Task<string>>)TaskTestAction },
new object[] { (Func<ValueTask<string>>)ValueTaskTestAction },
new object[] { (Func<string>)StaticTestAction },
new object[] { (Func<Task<string>>)StaticTaskTestAction },
new object[] { (Func<ValueTask<string>>)StaticValueTaskTestAction },
};
}
}
[Theory]
[MemberData(nameof(StringResult))]
public async Task RequestDelegateWritesStringReturnValueAsJsonResponseBody(Delegate @delegate)
{
var httpContext = new DefaultHttpContext();
var responseBodyStream = new MemoryStream();
httpContext.Response.Body = responseBodyStream;
var requestDelegate = RequestDelegateFactory.Create(@delegate);
await requestDelegate(httpContext);
var responseBody = Encoding.UTF8.GetString(responseBodyStream.ToArray());
Assert.Equal("String Test", responseBody);
}
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
public static IEnumerable<object[]> IntResult
{
get
{
int TestAction() => 42;
Task<int> TaskTestAction() => Task.FromResult(42);
ValueTask<int> ValueTaskTestAction() => ValueTask.FromResult(42);
static int StaticTestAction() => 42;
static Task<int> StaticTaskTestAction() => Task.FromResult(42);
static ValueTask<int> StaticValueTaskTestAction() => ValueTask.FromResult(42);
return new List<object[]>
{
new object[] { (Func<int>)TestAction },
new object[] { (Func<Task<int>>)TaskTestAction },
new object[] { (Func<ValueTask<int>>)ValueTaskTestAction },
new object[] { (Func<int>)StaticTestAction },
new object[] { (Func<Task<int>>)StaticTaskTestAction },
new object[] { (Func<ValueTask<int>>)StaticValueTaskTestAction },
};
}
}
[Theory]
[MemberData(nameof(IntResult))]
public async Task RequestDelegateWritesIntReturnValue(Delegate @delegate)
{
var httpContext = new DefaultHttpContext();
var responseBodyStream = new MemoryStream();
httpContext.Response.Body = responseBodyStream;
var requestDelegate = RequestDelegateFactory.Create(@delegate);
await requestDelegate(httpContext);
var responseBody = Encoding.UTF8.GetString(responseBodyStream.ToArray());
Assert.Equal("42", responseBody);
}
public static IEnumerable<object[]> BoolResult
{
get
{
bool TestAction() => true;