public void BindModel_Performs_ValidationOnArrays()
        {
            // Arrange
            TypeMatchModelBinder binder        = new TypeMatchModelBinder();
            HttpActionContext    actionContext = new HttpActionContext
            {
                ControllerContext = new HttpControllerContext {
                    Configuration = new HttpConfiguration()
                }
            };

            Customer[] model = new[] { new Customer {
                                           Age = 99999
                                       } };
            ModelBindingContext bindingContext = GetBindingContext(typeof(Customer[]));

            bindingContext.ValueProvider = new SimpleHttpValueProvider
            {
                { "theModelName", model }
            };
            bindingContext.ModelState = actionContext.ModelState;

            // Act
            bool retVal = binder.BindModel(actionContext, bindingContext);

            // Assert
            Assert.True(retVal);
            Assert.Same(model, bindingContext.Model);
            Assert.True(actionContext.ModelState.ContainsKey("theModelName"));
            Assert.False(actionContext.ModelState.IsValid);
            Assert.Equal("The field Age must be between 0 and 100.", actionContext.ModelState["theModelName[0].Age"].Errors[0].ErrorMessage);
        }
        public void BindModel_ValidValueProviderResult_ReturnsTrue()
        {
            // Arrange
            ModelBindingContext bindingContext = GetBindingContext();

            bindingContext.ValueProvider = new SimpleHttpValueProvider
            {
                { "theModelName", 42 }
            };

            TypeMatchModelBinder binder        = new TypeMatchModelBinder();
            HttpActionContext    actionContext = new HttpActionContext
            {
                ControllerContext = new HttpControllerContext {
                    Configuration = new HttpConfiguration()
                }
            };

            // Act
            bool retVal = binder.BindModel(actionContext, bindingContext);

            // Assert
            Assert.True(retVal);
            Assert.Equal(42, bindingContext.Model);
            Assert.True(bindingContext.ModelState.ContainsKey("theModelName"));
        }
Esempio n. 3
0
        public void BindModel_ThrowsArgument_IfConfigurationIsNullOnControllerContext()
        {
            // Arrange
            TypeMatchModelBinder binder        = new TypeMatchModelBinder();
            HttpActionContext    actionContext = new HttpActionContext
            {
                ControllerContext = new HttpControllerContext()
            };
            Customer model = new Customer {
                Age = 99999
            };
            ModelBindingContext bindingContext = GetBindingContext(typeof(Customer));

            bindingContext.ValueProvider = new SimpleHttpValueProvider
            {
                { "theModelName", model }
            };

            // Act
            Assert.ThrowsArgument(
                () => binder.BindModel(actionContext, bindingContext),
                "actionContext",
                "HttpControllerContext.Configuration must not be null."
                );
        }
        public void GetCompatibleValueProviderResult_ValueProviderReturnsNull_ReturnsNull()
        {
            // Arrange
            ModelBindingContext bindingContext = GetBindingContext();

            bindingContext.ValueProvider = new SimpleHttpValueProvider();

            // Act
            ValueProviderResult vpResult = TypeMatchModelBinder.GetCompatibleValueProviderResult(bindingContext);

            // Assert
            Assert.Null(vpResult); // No key matched
        }
        public void GetCompatibleValueProviderResult_ValueProviderResultValid_ReturnsValueProviderResult()
        {
            // Arrange
            ModelBindingContext bindingContext = GetBindingContext();

            bindingContext.ValueProvider = new SimpleHttpValueProvider
            {
                { "theModelName", 42 }
            };

            // Act
            ValueProviderResult vpResult = TypeMatchModelBinder.GetCompatibleValueProviderResult(bindingContext);

            // Assert
            Assert.NotNull(vpResult);
        }
        public void GetCompatibleValueProviderResult_ValueProviderResultRawValueIncorrect_ReturnsNull()
        {
            // Arrange
            ModelBindingContext bindingContext = GetBindingContext();

            bindingContext.ValueProvider = new SimpleHttpValueProvider
            {
                { "theModelName", "not an integer" }
            };

            // Act
            ValueProviderResult vpResult = TypeMatchModelBinder.GetCompatibleValueProviderResult(bindingContext);

            // Assert
            Assert.Null(vpResult); // Raw value is the wrong type
        }
        public void BindModel_InvalidValueProviderResult_ReturnsFalse()
        {
            // Arrange
            ModelBindingContext bindingContext = GetBindingContext();
            bindingContext.ValueProvider = new SimpleHttpValueProvider
            {
                { "theModelName", "not an integer" }
            };

            TypeMatchModelBinder binder = new TypeMatchModelBinder();

            // Act
            bool retVal = binder.BindModel(null, bindingContext);

            // Assert
            Assert.False(retVal);
            Assert.Empty(bindingContext.ModelState);
        }
        public void BindModel_InvalidValueProviderResult_ReturnsFalse()
        {
            // Arrange
            ModelBindingContext bindingContext = GetBindingContext();

            bindingContext.ValueProvider = new SimpleHttpValueProvider
            {
                { "theModelName", "not an integer" }
            };

            TypeMatchModelBinder binder = new TypeMatchModelBinder();

            // Act
            bool retVal = binder.BindModel(null, bindingContext);

            // Assert
            Assert.False(retVal);
            Assert.Empty(bindingContext.ModelState);
        }
        public void BindModel_ValidValueProviderResult_ReturnsTrue()
        {
            // Arrange
            ModelBindingContext bindingContext = GetBindingContext();
            bindingContext.ValueProvider = new SimpleHttpValueProvider
            {
                { "theModelName", 42 }
            };

            TypeMatchModelBinder binder = new TypeMatchModelBinder();

            // Act
            bool retVal = binder.BindModel(null, bindingContext);

            // Assert
            Assert.True(retVal);
            Assert.Equal(42, bindingContext.Model);
            Assert.True(bindingContext.ModelState.ContainsKey("theModelName"));
        }
        public void BindModel_Performs_ValidationOnArrays()
        {
            // Arrange
            TypeMatchModelBinder binder = new TypeMatchModelBinder();
            HttpActionContext actionContext = new HttpActionContext
            {
                ControllerContext = new HttpControllerContext { Configuration = new HttpConfiguration() }
            };

            Customer[] model = new[] { new Customer { Age = 99999 } };
            ModelBindingContext bindingContext = GetBindingContext(typeof(Customer[]));
            bindingContext.ValueProvider = new SimpleHttpValueProvider
            {
                { "theModelName", model }
            };
            bindingContext.ModelState = actionContext.ModelState;

            // Act
            bool retVal = binder.BindModel(actionContext, bindingContext);

            // Assert
            Assert.True(retVal);
            Assert.Same(model, bindingContext.Model);
            Assert.True(actionContext.ModelState.ContainsKey("theModelName"));
            Assert.False(actionContext.ModelState.IsValid);
            Assert.Equal("The field Age must be between 0 and 100.", actionContext.ModelState["theModelName[0].Age"].Errors[0].ErrorMessage);
        }
        public void BindModel_ThrowsArgument_IfConfigurationIsNullOnControllerContext()
        {
            // Arrange
            TypeMatchModelBinder binder = new TypeMatchModelBinder();
            HttpActionContext actionContext = new HttpActionContext { ControllerContext = new HttpControllerContext() };
            Customer model = new Customer { Age = 99999 };
            ModelBindingContext bindingContext = GetBindingContext(typeof(Customer));
            bindingContext.ValueProvider = new SimpleHttpValueProvider
            {
                { "theModelName", model }
            };

            // Act
            Assert.ThrowsArgument(() => binder.BindModel(actionContext, bindingContext),
                "actionContext", "HttpControllerContext.Configuration must not be null.");
        }
Esempio n. 12
0
 public override IModelBinder GetBinder(HttpActionContext actionContext, ModelBindingContext bindingContext)
 {
     return((TypeMatchModelBinder.GetCompatibleValueProviderResult(bindingContext) != null)
                ? new TypeMatchModelBinder()
                : null /* no match */);
 }