// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.IO; using System.Threading.Tasks; using Microsoft.AspNet.Http; using Microsoft.AspNet.Mvc.ModelBinding; using Microsoft.AspNet.Mvc.OptionDescriptors; using Microsoft.AspNet.Routing; using Microsoft.Framework.DependencyInjection; using Microsoft.Framework.OptionsModel; using Moq; namespace Microsoft.AspNet.Mvc { public class InputObjectBindingTests { private static ControllerActionInvoker GetControllerActionInvoker( string input, Type parameterType, IInputFormatter selectedFormatter, string contentType) { var mvcOptions = new MvcOptions(); var setup = new MvcOptionsSetup(); setup.Configure(mvcOptions); var accessor = new Mock>(); accessor.SetupGet(a => a.Options) .Returns(mvcOptions); var validatorProvider = new DefaultModelValidatorProviderProvider( accessor.Object, Mock.Of(), Mock.Of()); Func method = x => 1; var actionDescriptor = new ControllerActionDescriptor { MethodInfo = method.Method, Parameters = new List { new ParameterDescriptor { BinderMetadata = new FromBodyAttribute(), Name = "foo", ParameterType = parameterType, } } }; var metadataProvider = new EmptyModelMetadataProvider(); var actionContext = GetActionContext( Encodings.UTF8EncodingWithoutBOM.GetBytes(input), actionDescriptor, contentType); var inputFormatterSelector = new Mock(); inputFormatterSelector.Setup(a => a.SelectFormatter(It.IsAny())) .Returns(selectedFormatter); var bindingContext = new ActionBindingContext(actionContext, metadataProvider, Mock.Of(), Mock.Of(), inputFormatterSelector.Object, new CompositeModelValidatorProvider(validatorProvider)); var actionBindingContextProvider = new Mock(); actionBindingContextProvider.Setup(p => p.GetActionBindingContextAsync(It.IsAny())) .Returns(Task.FromResult(bindingContext)); var inputFormattersProvider = new Mock(); inputFormattersProvider.SetupGet(o => o.InputFormatters) .Returns(new List()); return new ControllerActionInvoker(actionContext, Mock.Of>(), Mock.Of(), actionDescriptor, inputFormattersProvider.Object, Mock.Of()); } private static ActionContext GetActionContext(byte[] contentBytes, ActionDescriptor actionDescriptor, string contentType) { return new ActionContext(GetHttpContext(contentBytes, contentType), new RouteData(), actionDescriptor); } private static HttpContext GetHttpContext(byte[] contentBytes, string contentType) { var request = new Mock(); var headers = new Mock(); request.SetupGet(r => r.Headers).Returns(headers.Object); request.SetupGet(f => f.Body).Returns(new MemoryStream(contentBytes)); request.SetupGet(f => f.ContentType).Returns(contentType); var httpContext = new Mock(); httpContext.SetupGet(c => c.Request).Returns(request.Object); httpContext.SetupGet(c => c.Request).Returns(request.Object); return httpContext.Object; } } public class Person { public string Name { get; set; } } public class User : Person { [MinLength(5)] public string UserName { get; set; } } public class Customers { [Required] public List Users { get; set; } } }