Exemple #1
0
        public async Task FormCollectionModelBinder_ValidType_BindSuccessful()
        {
            // Arrange
            var formCollection = new FormCollection(new Dictionary <string, StringValues>
            {
                { "field1", "value1" },
                { "field2", "value2" }
            });
            var httpContext    = GetMockHttpContext(formCollection);
            var bindingContext = GetBindingContext(typeof(IFormCollection), httpContext);
            var binder         = new FormCollectionModelBinder();

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

            // Assert
            Assert.NotEqual(default(ModelBindingResult), result);
            Assert.True(result.IsModelSet);

            var entry = bindingContext.ValidationState[result.Model];

            Assert.True(entry.SuppressValidation);
            Assert.Null(entry.Key);
            Assert.Null(entry.Metadata);

            var form = Assert.IsAssignableFrom <IFormCollection>(result.Model);

            Assert.Equal(2, form.Count);
            Assert.Equal("value1", form["field1"]);
            Assert.Equal("value2", form["field2"]);
        }
Exemple #2
0
        public async Task FormCollectionModelBinder_NoForm_BindSuccessful_ReturnsEmptyFormCollection()
        {
            // Arrange
            var httpContext    = GetMockHttpContext(null, hasForm: false);
            var bindingContext = GetBindingContext(typeof(IFormCollection), httpContext);
            var binder         = new FormCollectionModelBinder();

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

            // Assert
            Assert.NotEqual(default(ModelBindingResult), result);
            var form = Assert.IsAssignableFrom <IFormCollection>(result.Model);

            Assert.Empty(form);
        }
Exemple #3
0
        public async Task FormCollectionModelBinder_FormCollectionConcreteType_BindFails()
        {
            // Arrange
            var formCollection = new FormCollection(new Dictionary <string, StringValues>
            {
                { "field1", "value1" },
                { "field2", new string[] { "value2" } }
            });
            var httpContext    = GetMockHttpContext(formCollection);
            var bindingContext = GetBindingContext(typeof(FormCollection), httpContext);
            var binder         = new FormCollectionModelBinder();

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

            // Assert
            Assert.Equal(default(ModelBindingResult), result);
        }