예제 #1
0
        public async Task BindModel_ComplexCollection_Succeeds(bool isReadOnly)
        {
            // Arrange
            var valueProvider = new SimpleValueProvider
            {
                { "someName.index", new[] { "foo", "bar", "baz" } },
                { "someName[foo]", "42" },
                { "someName[bar]", "100" },
                { "someName[baz]", "200" }
            };
            var bindingContext = GetModelBindingContext(valueProvider, isReadOnly);
            var modelState     = bindingContext.ModelState;
            var binder         = new CollectionModelBinder <int>(CreateIntBinder(), NullLoggerFactory.Instance);

            // Act
            await binder.BindModelAsync(bindingContext);

            // Assert
            Assert.True(bindingContext.Result.IsModelSet);

            var list = Assert.IsAssignableFrom <IList <int> >(bindingContext.Result.Model);

            Assert.Equal(new[] { 42, 100, 200 }, list.ToArray());

            Assert.True(modelState.IsValid);
        }
예제 #2
0
        public async Task CollectionModelBinder_DoesNotCreateEmptyCollection_IfModelNonNull()
        {
            // Arrange
            var binder = new CollectionModelBinder <string>(
                new StubModelBinder(result: ModelBindingResult.Failed()),
                NullLoggerFactory.Instance);

            var bindingContext = CreateContext();

            bindingContext.IsTopLevelObject = true;

            var list = new List <string>();

            bindingContext.Model = list;

            // Lack of prefix and non-empty model name both ignored.
            bindingContext.ModelName = "modelName";

            var metadataProvider = new TestModelMetadataProvider();

            bindingContext.ModelMetadata = metadataProvider.GetMetadataForType(typeof(List <string>));

            bindingContext.ValueProvider = new TestValueProvider(new Dictionary <string, object>());

            // Act
            await binder.BindModelAsync(bindingContext);

            // Assert
            Assert.Same(list, bindingContext.Result.Model);
            Assert.Empty(list);
            Assert.True(bindingContext.Result.IsModelSet);
        }
예제 #3
0
        public async Task CollectionModelBinder_DoesNotCreateCollection_IfNotIsTopLevelObject(string prefix)
        {
            // Arrange
            var binder = new CollectionModelBinder <string>(
                new StubModelBinder(result: ModelBindingResult.Failed()),
                NullLoggerFactory.Instance);

            var bindingContext = CreateContext();

            bindingContext.ModelName = ModelNames.CreatePropertyModelName(prefix, "ListProperty");

            var metadataProvider = new TestModelMetadataProvider();

            bindingContext.ModelMetadata = metadataProvider.GetMetadataForProperty(
                typeof(ModelWithListProperty),
                nameof(ModelWithListProperty.ListProperty));

            bindingContext.ValueProvider = new TestValueProvider(new Dictionary <string, object>());

            // Act
            await binder.BindModelAsync(bindingContext);

            // Assert
            Assert.False(bindingContext.Result.IsModelSet);
        }
        public async Task CollectionModelBinder_DoesNotCreateCollection_IfNotIsTopLevelObject(
            string prefix,
            bool allowValidatingTopLevelNodes,
            bool isBindingRequired)
        {
            // Arrange
            var binder = new CollectionModelBinder <string>(
                new StubModelBinder(result: ModelBindingResult.Failed()),
                NullLoggerFactory.Instance,
                allowValidatingTopLevelNodes);

            var bindingContext = CreateContext();

            bindingContext.ModelName = ModelNames.CreatePropertyModelName(prefix, "ListProperty");

            var metadataProvider = new TestModelMetadataProvider();

            metadataProvider
            .ForProperty(typeof(ModelWithListProperty), nameof(ModelWithListProperty.ListProperty))
            .BindingDetails(b => b.IsBindingRequired = isBindingRequired);
            bindingContext.ModelMetadata             = metadataProvider.GetMetadataForProperty(
                typeof(ModelWithListProperty),
                nameof(ModelWithListProperty.ListProperty));

            bindingContext.ValueProvider = new TestValueProvider(new Dictionary <string, object>());

            // Act
            await binder.BindModelAsync(bindingContext);

            // Assert
            Assert.False(bindingContext.Result.IsModelSet);
            Assert.Equal(0, bindingContext.ModelState.ErrorCount);
        }
예제 #5
0
        public async Task CollectionModelBinder_CreatesEmptyCollection_IfIsTopLevelObject()
        {
            // Arrange
            var binder = new CollectionModelBinder <string>(new StubModelBinder(result: ModelBindingResult.Failed()));

            var bindingContext = CreateContext();

            bindingContext.IsTopLevelObject = true;

            // Lack of prefix and non-empty model name both ignored.
            bindingContext.ModelName = "modelName";

            var metadataProvider = new TestModelMetadataProvider();

            bindingContext.ModelMetadata = metadataProvider.GetMetadataForType(typeof(List <string>));

            bindingContext.ValueProvider = new TestValueProvider(new Dictionary <string, object>());

            // Act
            await binder.BindModelAsync(bindingContext);

            // Assert
            Assert.Empty(Assert.IsType <List <string> >(bindingContext.Result.Model));
            Assert.True(bindingContext.Result.IsModelSet);
        }
예제 #6
0
        public async Task BindModelAsync_SimpleCollectionWithNullValue_Succeeds()
        {
            // Arrange
            var binder        = new CollectionModelBinder <int>(CreateIntBinder(), NullLoggerFactory.Instance);
            var valueProvider = new SimpleValueProvider
            {
                { "someName", null },
            };
            var bindingContext = GetModelBindingContext(valueProvider, isReadOnly: false);

            // Act
            await binder.BindModelAsync(bindingContext);

            // Assert
            Assert.True(bindingContext.Result.IsModelSet);

            var model = Assert.IsType <List <int> >(bindingContext.Result.Model);

            Assert.Empty(model);
        }
예제 #7
0
        public async Task BindModel_SimpleCollection_Succeeds(bool isReadOnly)
        {
            // Arrange
            var valueProvider = new SimpleValueProvider
            {
                { "someName", new[] { "42", "100", "200" } }
            };
            var bindingContext = GetModelBindingContext(valueProvider, isReadOnly);
            var modelState     = bindingContext.ModelState;
            var binder         = new CollectionModelBinder <int>(CreateIntBinder());

            // Act
            await binder.BindModelAsync(bindingContext);

            // Assert
            Assert.True(bindingContext.Result.IsModelSet);

            var list = Assert.IsAssignableFrom <IList <int> >(bindingContext.Result.Model);

            Assert.Equal(new[] { 42, 100, 200 }, list.ToArray());
        }
        public async Task CollectionModelBinder_CreatesEmptyCollectionAndAddsError_IfIsTopLevelObject()
        {
            // Arrange
            var binder = new CollectionModelBinder <string>(
                new StubModelBinder(result: ModelBindingResult.Failed()),
                NullLoggerFactory.Instance,
                allowValidatingTopLevelNodes: true);

            var bindingContext = CreateContext();

            bindingContext.IsTopLevelObject = true;
            bindingContext.FieldName        = "fieldName";
            bindingContext.ModelName        = "modelName";

            var metadataProvider = new TestModelMetadataProvider();
            var parameter        = typeof(CollectionModelBinderTest)
                                   .GetMethod(nameof(ActionWithListParameter), BindingFlags.Instance | BindingFlags.NonPublic)
                                   .GetParameters()[0];

            metadataProvider
            .ForParameter(parameter)
            .BindingDetails(b => b.IsBindingRequired = true);
            bindingContext.ModelMetadata             = metadataProvider.GetMetadataForParameter(parameter);

            bindingContext.ValueProvider = new TestValueProvider(new Dictionary <string, object>());

            // Act
            await binder.BindModelAsync(bindingContext);

            // Assert
            Assert.Empty(Assert.IsType <List <string> >(bindingContext.Result.Model));
            Assert.True(bindingContext.Result.IsModelSet);

            var keyValuePair = Assert.Single(bindingContext.ModelState);

            Assert.Equal("modelName", keyValuePair.Key);
            var error = Assert.Single(keyValuePair.Value.Errors);

            Assert.Equal("A value for the 'fieldName' parameter or property was not provided.", error.ErrorMessage);
        }
예제 #9
0
        public async Task BindModel_SimpleCollection_BindingContextModelNonNull_Succeeds(bool isReadOnly)
        {
            // Arrange
            var valueProvider = new SimpleValueProvider
            {
                { "someName", new[] { "42", "100", "200" } }
            };
            var bindingContext = GetModelBindingContext(valueProvider, isReadOnly);
            var modelState     = bindingContext.ModelState;
            var list           = new List <int>();

            bindingContext.Model = list;
            var binder = new CollectionModelBinder <int>(CreateIntBinder(), NullLoggerFactory.Instance);

            // Act
            await binder.BindModelAsync(bindingContext);

            // Assert
            Assert.True(bindingContext.Result.IsModelSet);

            Assert.Same(list, bindingContext.Result.Model);
            Assert.Equal(new[] { 42, 100, 200 }, list.ToArray());
        }
        public async Task CollectionModelBinder_CreatesEmptyCollection_IfIsTopLevelObject(
            bool allowValidatingTopLevelNodes,
            bool isBindingRequired)
        {
            // Arrange
            var binder = new CollectionModelBinder <string>(
                new StubModelBinder(result: ModelBindingResult.Failed()),
                NullLoggerFactory.Instance,
                allowValidatingTopLevelNodes);

            var bindingContext = CreateContext();

            bindingContext.IsTopLevelObject = true;

            // Lack of prefix and non-empty model name both ignored.
            bindingContext.ModelName = "modelName";

            var metadataProvider = new TestModelMetadataProvider();
            var parameter        = typeof(CollectionModelBinderTest)
                                   .GetMethod(nameof(ActionWithListParameter), BindingFlags.Instance | BindingFlags.NonPublic)
                                   .GetParameters()[0];

            metadataProvider
            .ForParameter(parameter)
            .BindingDetails(b => b.IsBindingRequired = isBindingRequired);
            bindingContext.ModelMetadata             = metadataProvider.GetMetadataForParameter(parameter);

            bindingContext.ValueProvider = new TestValueProvider(new Dictionary <string, object>());

            // Act
            await binder.BindModelAsync(bindingContext);

            // Assert
            Assert.Empty(Assert.IsType <List <string> >(bindingContext.Result.Model));
            Assert.True(bindingContext.Result.IsModelSet);
            Assert.Equal(0, bindingContext.ModelState.ErrorCount);
        }