コード例 #1
0
        public void NullCheckFailedHandler_ModelStateValid_CallbackReturnsNull_DoesNothing()
        {
            // Arrange
            HttpActionContext       context        = ContextUtil.CreateActionContext();
            ModelMetadata           modelMetadata  = GetMetadataForType(typeof(Person));
            ModelValidationNode     validationNode = new ModelValidationNode(modelMetadata, "foo");
            ModelValidatedEventArgs e = new ModelValidatedEventArgs(context, null /* parentNode */);

            // Act
            ModelBinderErrorMessageProvider originalProvider = ModelBinderConfig.ValueRequiredErrorMessageProvider;

            try
            {
                ModelBinderConfig.ValueRequiredErrorMessageProvider = (ec, mm, value) => null;
                EventHandler <ModelValidatedEventArgs> handler = MutableObjectModelBinder.CreateNullCheckFailedHandler(modelMetadata, null /* incomingValue */);
                handler(validationNode, e);
            }
            finally
            {
                ModelBinderConfig.ValueRequiredErrorMessageProvider = originalProvider;
            }

            // Assert
            Assert.True(context.ModelState.IsValid);
        }
コード例 #2
0
        public void NullCheckFailedHandler_ModelStateAlreadyInvalid_DoesNothing()
        {
            // Arrange
            HttpActionContext context = ContextUtil.CreateActionContext();

            context.ModelState.AddModelError("foo.bar", "Some existing error.");

            ModelMetadata           modelMetadata  = GetMetadataForType(typeof(Person));
            ModelValidationNode     validationNode = new ModelValidationNode(modelMetadata, "foo");
            ModelValidatedEventArgs e = new ModelValidatedEventArgs(
                context,
                null /* parentNode */
                );

            // Act
            EventHandler <ModelValidatedEventArgs> handler =
                MutableObjectModelBinder.CreateNullCheckFailedHandler(
                    modelMetadata,
                    null /* incomingValue */
                    );

            handler(validationNode, e);

            // Assert
            Assert.False(context.ModelState.ContainsKey("foo"));
        }
コード例 #3
0
        public void GetRequiredPropertiesCollection_MixedAttributes()
        {
            // Arrange
            HttpActionContext   actionContext  = ContextUtil.CreateActionContext();
            ModelBindingContext bindingContext = new ModelBindingContext
            {
                ModelMetadata = GetMetadataForObject(new ModelWithMixedBindingBehaviors())
            };

            // Act
            HashSet <string> requiredProperties;
            Dictionary <string, ModelValidator> requiredValidators;
            HashSet <string> skipProperties;

            MutableObjectModelBinder.GetRequiredPropertiesCollection(
                actionContext,
                bindingContext,
                out requiredProperties,
                out requiredValidators,
                out skipProperties
                );

            // Assert
            Assert.Equal(new[] { "Required" }, requiredProperties.ToArray());
            Assert.Equal(new[] { "Never" }, skipProperties.ToArray());
        }
コード例 #4
0
        public override IModelBinder GetBinder(HttpConfiguration configuration, Type modelType)
        {
            if (!MutableObjectModelBinder.CanBindType(modelType))
            {
                return(null);
            }

            return(new MutableObjectModelBinder());
        }
コード例 #5
0
        public void CanUpdateProperty_ReadOnlyReferenceTypeNotBlacklisted_ReturnsTrue()
        {
            // Arrange
            ModelMetadata propertyMetadata = GetMetadataForCanUpdateProperty("ReadOnlyObject");

            // Act
            bool canUpdate = MutableObjectModelBinder.CanUpdatePropertyInternal(propertyMetadata);

            // Assert
            Assert.True(canUpdate);
        }
コード例 #6
0
        public void CanUpdateProperty_ReadOnlyArray_ReturnsFalse()
        {
            // Arrange
            ModelMetadata propertyMetadata = GetMetadataForCanUpdateProperty("ReadOnlyArray");

            // Act
            bool canUpdate = MutableObjectModelBinder.CanUpdatePropertyInternal(propertyMetadata);

            // Assert
            Assert.False(canUpdate);
        }
コード例 #7
0
        public void CanUpdateProperty_HasPublicSetter_ReturnsTrue()
        {
            // Arrange
            ModelMetadata propertyMetadata = GetMetadataForCanUpdateProperty("ReadWriteString");

            // Act
            bool canUpdate = MutableObjectModelBinder.CanUpdatePropertyInternal(propertyMetadata);

            // Assert
            Assert.True(canUpdate);
        }
コード例 #8
0
        public void NullCheckFailedHandler_ModelStateValid_AddsErrorString()
        {
            // Arrange
            HttpActionContext       context        = ContextUtil.CreateActionContext();
            ModelMetadata           modelMetadata  = GetMetadataForType(typeof(Person));
            ModelValidationNode     validationNode = new ModelValidationNode(modelMetadata, "foo");
            ModelValidatedEventArgs e = new ModelValidatedEventArgs(context, null /* parentNode */);

            // Act
            EventHandler <ModelValidatedEventArgs> handler = MutableObjectModelBinder.CreateNullCheckFailedHandler(modelMetadata, null /* incomingValue */);

            handler(validationNode, e);

            // Assert
            Assert.True(context.ModelState.ContainsKey("foo"));
            Assert.Equal("A value is required.", context.ModelState["foo"].Errors[0].ErrorMessage);
        }