public async Task FindBindAsyncMethod_FindsSingleArgBindAsync() { var type = typeof(BindAsyncSingleArgStruct); var cache = new ParameterBindingMethodCache(); var parameter = new MockParameterInfo(type, "bindAsyncSingleArgStruct"); var methodFound = cache.FindBindAsyncMethod(parameter); Assert.NotNull(methodFound.Expression); Assert.Equal(1, methodFound.ParamCount); var parsedValue = Expression.Variable(type, "parsedValue"); var parseHttpContext = Expression.Lambda <Func <HttpContext, ValueTask <object> > >( Expression.Block(new[] { parsedValue }, methodFound.Expression !), ParameterBindingMethodCache.HttpContextExpr).Compile(); var httpContext = new DefaultHttpContext { Request = { Headers = { ["ETag"] = "42", }, }, }; Assert.Equal(new BindAsyncSingleArgStruct(42), await parseHttpContext(httpContext)); }
public void FindBindAsyncMethod_IgnoresInvalidBindAsyncIfGoodOneFound(Type type) { var cache = new ParameterBindingMethodCache(); var parameter = new MockParameterInfo(type, "anything"); var(expression, _) = cache.FindBindAsyncMethod(parameter); Assert.NotNull(expression); }
public void FindBindAsyncMethod_ThrowsIfMultipleInterfacesMatch() { var cache = new ParameterBindingMethodCache(); var parameter = new MockParameterInfo(typeof(BindAsyncFromMultipleInterfaces), "anything"); var ex = Assert.Throws <InvalidOperationException>(() => cache.FindBindAsyncMethod(parameter)); Assert.Equal("BindAsyncFromMultipleInterfaces implements multiple interfaces defining a static System.Threading.Tasks.ValueTask`1[Microsoft.AspNetCore.Http.Extensions.Tests.ParameterBindingMethodCacheTests+BindAsyncFromMultipleInterfaces] BindAsync(Microsoft.AspNetCore.Http.HttpContext) method causing ambiguity.", ex.Message); }
public static MockMethodBase <T> AddParameter <T>(this MockMethodBase <T> @this, Action <MockParameterInfo>?setup = null) where T : MethodBase { var mockParameterInfo = new MockParameterInfo(); setup?.Invoke(mockParameterInfo); @this.MockParameters.Add(mockParameterInfo); return(@this); }
public void FindBindAsyncMethod_ThrowsIfInvalidBindAsyncOnType(Type type) { var cache = new ParameterBindingMethodCache(); var parameter = new MockParameterInfo(type, "anything"); var ex = Assert.Throws <InvalidOperationException>( () => cache.FindBindAsyncMethod(parameter)); Assert.StartsWith($"BindAsync method found on {TypeNameHelper.GetTypeDisplayName(type, fullName: false)} with incorrect format. Must be a static method with format", ex.Message); Assert.Contains($"ValueTask<{TypeNameHelper.GetTypeDisplayName(type, fullName: false)}> BindAsync(HttpContext context, ParameterInfo parameter)", ex.Message); Assert.Contains($"ValueTask<{TypeNameHelper.GetTypeDisplayName(type, fullName: false)}> BindAsync(HttpContext context)", ex.Message); Assert.Contains($"ValueTask<{TypeNameHelper.GetTypeDisplayName(type, fullName: false)}?> BindAsync(HttpContext context, ParameterInfo parameter)", ex.Message); Assert.Contains($"ValueTask<{TypeNameHelper.GetTypeDisplayName(type, fullName: false)}?> BindAsync(HttpContext context)", ex.Message); }
public static MockParameterInfo SetParameterType(this MockParameterInfo @this, Type parameterType) { @this.ParameterType = parameterType; return(@this); }