Ejemplo n.º 1
0
        public void BindModel_UnsuccessfulBind_SimpleTypeNoFallback_ReturnsNull()
        {
            // Arrange
            HttpActionContext          actionContext      = ContextUtil.CreateActionContext(GetHttpControllerContext());
            Mock <ModelBinderProvider> mockBinderProvider = new Mock <ModelBinderProvider>();

            mockBinderProvider.Setup(o => o.GetBinder(actionContext, It.IsAny <ModelBindingContext>())).Returns((IModelBinder)null).Verifiable();
            List <ModelBinderProvider> binderProviders = new List <ModelBinderProvider>()
            {
                mockBinderProvider.Object
            };
            CompositeModelBinder shimBinder = new CompositeModelBinder(binderProviders);

            ModelBindingContext bindingContext = new ModelBindingContext
            {
                FallbackToEmptyPrefix = true,
                ModelMetadata         = new EmptyModelMetadataProvider().GetMetadataForType(null, typeof(int)),
                //ModelState = executionContext.Controller.ViewData.ModelState
            };

            // Act
            bool isBound = shimBinder.BindModel(actionContext, bindingContext);

            // Assert
            Assert.False(isBound);
            Assert.Null(bindingContext.Model);
            Assert.True(bindingContext.ModelState.IsValid);
            mockBinderProvider.Verify();
            mockBinderProvider.Verify(o => o.GetBinder(actionContext, It.IsAny <ModelBindingContext>()), Times.AtMostOnce());
        }
Ejemplo n.º 2
0
        public void BindModel_SuccessfulBind_ComplexTypeFallback_RunsValidationAndReturnsModel()
        {
            // Arrange
            HttpActionContext actionContext = ContextUtil.CreateActionContext(GetHttpControllerContext());

            bool       validationCalled = false;
            List <int> expectedModel    = new List <int> {
                1, 2, 3, 4, 5
            };

            ModelBindingContext bindingContext = new ModelBindingContext
            {
                FallbackToEmptyPrefix = true,
                ModelMetadata         = new EmptyModelMetadataProvider().GetMetadataForType(null, typeof(List <int>)),
                ModelName             = "someName",
                //ModelState = executionContext.Controller.ViewData.ModelState,
                //PropertyFilter = _ => true,
                ValueProvider = new SimpleValueProvider
                {
                    { "someOtherName", "dummyValue" }
                }
            };

            Mock <IModelBinder> mockIntBinder = new Mock <IModelBinder>();

            mockIntBinder
            .Setup(o => o.BindModel(actionContext, It.IsAny <ModelBindingContext>()))
            .Returns(
                delegate(HttpActionContext cc, ModelBindingContext mbc)
            {
                Assert.Same(bindingContext.ModelMetadata, mbc.ModelMetadata);
                Assert.Equal("", mbc.ModelName);
                Assert.Same(bindingContext.ValueProvider, mbc.ValueProvider);

                mbc.Model = expectedModel;
                mbc.ValidationNode.Validating += delegate { validationCalled = true; };
                return(true);
            });

            List <ModelBinderProvider> binderProviders = new List <ModelBinderProvider>()
            {
                new SimpleModelBinderProvider()
                {
                    Binder = mockIntBinder.Object,
                    OnlyWithEmptyModelName = true
                }
            };

            //binderProviders.RegisterBinderForType(typeof(List<int>), mockIntBinder.Object, false /* suppressPrefixCheck */);
            CompositeModelBinder shimBinder = new CompositeModelBinder(binderProviders);

            // Act
            bool isBound = shimBinder.BindModel(actionContext, bindingContext);

            // Assert
            Assert.True(isBound);
            Assert.Equal(expectedModel, bindingContext.Model);
            Assert.True(validationCalled);
            Assert.True(bindingContext.ModelState.IsValid);
        }
Ejemplo n.º 3
0
        public XElement Get()
        {
            HttpActionContext actionContext = new HttpActionContext
            {
                ControllerContext = this.ControllerContext
            };
            ModelMetadataProvider metadataProvider = this.Configuration.Services.GetModelMetadataProvider();
            ModelMetadata         metadata         = metadataProvider.GetMetadataForType(null, typeof(IEnumerable <Contact>));
            IValueProvider        valueProvider    = this.CreateValueProvider();
            ModelBindingContext   bindingContext   = new ModelBindingContext
            {
                ModelMetadata = metadata,
                ValueProvider = valueProvider,
                ModelState    = actionContext.ModelState
            };

            CompositeModelBinder modelBinder = (CompositeModelBinder) new ModelBinderAttribute().GetModelBinder(this.Configuration, typeof(IEnumerable <Contact>));

            modelBinder.BindModel(actionContext, bindingContext);

            XElement root = new XElement("Root");

            this.AddChildNodes(root, bindingContext.ValidationNode);
            return(root);
        }
Ejemplo n.º 4
0
        public async Task BindModel_SubBindingSucceeds()
        {
            // Arrange
            var innerBinder    = new CompositeModelBinder(new[] { CreateStringBinder(), CreateIntBinder() });
            var valueProvider  = new SimpleHttpValueProvider();
            var bindingContext = GetBindingContext(valueProvider, innerBinder);

            var binder = new KeyValuePairModelBinder <int, string>();

            // Act
            var result = await binder.BindModelAsync(bindingContext);

            // Assert
            Assert.NotNull(result);
            Assert.Equal(new KeyValuePair <int, string>(42, "some-value"), result.Model);
            Assert.NotNull(result.ValidationNode);
            Assert.Equal(new KeyValuePair <int, string>(42, "some-value"), result.ValidationNode.Model);
            Assert.Equal("someName", result.ValidationNode.Key);

            var validationNode = result.ValidationNode.ChildNodes[0];

            Assert.Equal("someName.Key", validationNode.Key);
            Assert.Equal(42, validationNode.Model);
            Assert.Empty(validationNode.ChildNodes);

            validationNode = result.ValidationNode.ChildNodes[1];
            Assert.Equal("someName.Value", validationNode.Key);
            Assert.Equal("some-value", validationNode.Model);
            Assert.Empty(validationNode.ChildNodes);
        }
Ejemplo n.º 5
0
        private static CompositeModelBinder CreateBinderWithDefaults()
        {
            var serviceProvider = Mock.Of <IServiceProvider>();
            var typeActivator   = new Mock <ITypeActivator>();

            typeActivator
            .Setup(t => t.CreateInstance(serviceProvider, It.IsAny <Type>(), It.IsAny <object[]>()))
            .Returns((IServiceProvider sp, Type t, object[] args) => Activator.CreateInstance(t));
            var binders = new IModelBinder[]
            {
                new TypeMatchModelBinder(),
                new ByteArrayModelBinder(),
                new GenericModelBinder(serviceProvider, typeActivator.Object),
                new ComplexModelDtoModelBinder(),
                new TypeConverterModelBinder(),
                new MutableObjectModelBinder()
            };
            var binderProviders = new Mock <IModelBinderProvider>();

            binderProviders.SetupGet(p => p.ModelBinders)
            .Returns(binders);
            var binder = new CompositeModelBinder(binderProviders.Object);

            return(binder);
        }
Ejemplo n.º 6
0
        private static CompositeModelBinder CreateCompositeBinder(IModelBinder mockIntBinder)
        {
            var binderProvider = new Mock <IModelBinderProvider>();

            binderProvider.SetupGet(p => p.ModelBinders)
            .Returns(new[] { mockIntBinder });

            var shimBinder = new CompositeModelBinder(binderProvider.Object);

            return(shimBinder);
        }
Ejemplo n.º 7
0
        private static CompositeModelBinder CreateBinderWithDefaults()
        {
            var binders = new IModelBinder[]
            {
                new ByteArrayModelBinder(),
                new GenericModelBinder(),
                new SimpleTypeModelBinder(),
                new MutableObjectModelBinder()
            };

            var binder = new CompositeModelBinder(binders);

            return(binder);
        }
        public async Task BindModel_SubBindingSucceeds()
        {
            // Arrange
            var innerBinder    = new CompositeModelBinder(new[] { CreateStringBinder(), CreateIntBinder() });
            var valueProvider  = new SimpleValueProvider();
            var bindingContext = GetBindingContext(valueProvider, innerBinder);

            var binder = new KeyValuePairModelBinder <int, string>();

            // Act
            var result = await binder.BindModelAsync(bindingContext);

            // Assert
            Assert.NotEqual(ModelBindingResult.NoResult, result);
            Assert.Equal(new KeyValuePair <int, string>(42, "some-value"), result.Model);
        }
Ejemplo n.º 9
0
        public async Task BindModel_SubBindingSucceeds()
        {
            // Arrange
            var innerBinder    = new CompositeModelBinder(new[] { CreateStringBinder(), CreateIntBinder() });
            var valueProvider  = new SimpleHttpValueProvider();
            var bindingContext = GetBindingContext(valueProvider, innerBinder);

            var binder = new KeyValuePairModelBinder <int, string>();

            // Act
            var retVal = await binder.BindModelAsync(bindingContext);

            // Assert
            Assert.True(retVal);
            Assert.Equal(new KeyValuePair <int, string>(42, "some-value"), bindingContext.Model);
            Assert.Equal(new[] { "someName.key", "someName.value" }, bindingContext.ValidationNode.ChildNodes.Select(n => n.ModelStateKey).ToArray());
        }
Ejemplo n.º 10
0
        protected Task <bool> TryUpdateModelAsync <TModel>(TModel model)
        {
            var binder         = new CompositeModelBinder(_modelBinders);
            var requestContext = new RequestContext(Context, ActionContext.RouteValues);
            var bindingContext = new ModelBindingContext
            {
                MetadataProvider   = _modelMetadataProvider,
                Model              = model,
                ModelState         = ModelState,
                ValidatorProviders = _validatorProviders,
                ModelBinder        = binder,
                HttpContext        = Context,
                ValueProvider      = new CompositeValueProvider(_valueProviderFactories.Select(
                                                                    vpf => vpf.GetValueProviderAsync(requestContext).Result))
            };

            return(binder.BindModelAsync(bindingContext));
        }
Ejemplo n.º 11
0
        public async Task BindModel_SubBindingSucceeds()
        {
            // Arrange
            var binderProviders = new Mock<IModelBinderProvider>();
            binderProviders.SetupGet(b => b.ModelBinders)
                           .Returns(new[] { CreateStringBinder(), CreateIntBinder() });
            var innerBinder = new CompositeModelBinder(binderProviders.Object);
            var valueProvider = new SimpleHttpValueProvider();
            var bindingContext = GetBindingContext(valueProvider, innerBinder);
            
            var binder = new KeyValuePairModelBinder<int, string>();

            // Act
            var retVal = await binder.BindModelAsync(bindingContext);

            // Assert
            Assert.True(retVal);
            Assert.Equal(new KeyValuePair<int, string>(42, "some-value"), bindingContext.Model);
            Assert.Equal(new[] { "someName.key", "someName.value" }, bindingContext.ValidationNode.ChildNodes.Select(n => n.ModelStateKey).ToArray());
        }
Ejemplo n.º 12
0
        private static CompositeModelBinder CreateCompositeBinder(IModelBinder mockIntBinder)
        {
            var shimBinder = new CompositeModelBinder(new[] { mockIntBinder });

            return(shimBinder);
        }
Ejemplo n.º 13
0
        public async Task BindModel_SubBindingSucceeds()
        {
            // Arrange
            var innerBinder = new CompositeModelBinder(new[] { CreateStringBinder(), CreateIntBinder() });
            var valueProvider = new SimpleValueProvider();
            var bindingContext = GetBindingContext(valueProvider, innerBinder);

            var binder = new KeyValuePairModelBinder<int, string>();

            // Act
            var result = await binder.BindModelAsync(bindingContext);

            // Assert
            Assert.NotEqual(ModelBindingResult.NoResult, result);
            Assert.Equal(new KeyValuePair<int, string>(42, "some-value"), result.Model);
        }
Ejemplo n.º 14
0
 private static CompositeModelBinder CreateBinderWithDefaults()
 {
     var serviceProvider = Mock.Of<IServiceProvider>();
     var typeActivator = new Mock<ITypeActivator>();
     typeActivator
         .Setup(t => t.CreateInstance(serviceProvider, It.IsAny<Type>(), It.IsAny<object[]>()))
         .Returns((IServiceProvider sp, Type t, object[] args) => Activator.CreateInstance(t));
     var binders = new IModelBinder[] 
     { 
         new TypeMatchModelBinder(),
         new GenericModelBinder(serviceProvider, typeActivator.Object),
         new ComplexModelDtoModelBinder(),
         new TypeConverterModelBinder(),
         new MutableObjectModelBinder()
     };
     var binderProviders = new Mock<IModelBinderProvider>();
     binderProviders.SetupGet(p => p.ModelBinders)
                    .Returns(binders);
     var binder = new CompositeModelBinder(binderProviders.Object);
     return binder;
 }
Ejemplo n.º 15
0
        private static CompositeModelBinder CreateCompositeBinder(IModelBinder mockIntBinder)
        {
            var binderProvider = new Mock<IModelBinderProvider>();
            binderProvider.SetupGet(p => p.ModelBinders)
                          .Returns(new[] { mockIntBinder });

            var shimBinder = new CompositeModelBinder(binderProvider.Object);
            return shimBinder;
        }
        private static CompositeModelBinder CreateBinderWithDefaults()
        {
            var binders = new IModelBinder[]
            {
                new TypeMatchModelBinder(),
                new ByteArrayModelBinder(),
                new GenericModelBinder(),
                new ComplexModelDtoModelBinder(),
                new TypeConverterModelBinder(),
                new MutableObjectModelBinder()
            };

            var binder = new CompositeModelBinder(binders);
            return binder;
        }
        public async Task BindModel_SubBindingSucceeds()
        {
            // Arrange
            var innerBinder = new CompositeModelBinder(new[] { CreateStringBinder(), CreateIntBinder() });
            var valueProvider = new SimpleHttpValueProvider();
            var bindingContext = GetBindingContext(valueProvider, innerBinder);

            var binder = new KeyValuePairModelBinder<int, string>();

            // Act
            var result = await binder.BindModelAsync(bindingContext);

            // Assert
            Assert.NotNull(result);
            Assert.Equal(new KeyValuePair<int, string>(42, "some-value"), result.Model);
            Assert.NotNull(result.ValidationNode);
            Assert.Equal(new KeyValuePair<int, string>(42, "some-value"), result.ValidationNode.Model);
            Assert.Equal("someName", result.ValidationNode.Key);

            var validationNode = result.ValidationNode.ChildNodes[0];
            Assert.Equal("someName.Key", validationNode.Key);
            Assert.Equal(42, validationNode.Model);
            Assert.Empty(validationNode.ChildNodes);

            validationNode = result.ValidationNode.ChildNodes[1];
            Assert.Equal("someName.Value", validationNode.Key);
            Assert.Equal("some-value", validationNode.Model);
            Assert.Empty(validationNode.ChildNodes);
        }
 private static CompositeModelBinder CreateCompositeBinder(IModelBinder mockIntBinder)
 {
     var shimBinder = new CompositeModelBinder(new[] { mockIntBinder });
     return shimBinder;
 }