public void KeyNotFoundException()
        {
            var filterValueModelBinder = ModelBinders.Binders.GetBinder(typeof(IFilterValue));

            var formCollection = new NameValueCollection
            {
                { "foo.ConditionKey", "euqal" },
                { "foo.LeftValue", "1" },
                { "foo.RightValue", "12" },
                { "foo.TypeName", typeof(Int32).FullName }
            };

            var valueProvider = new NameValueCollectionValueProvider(formCollection, null);

            var bindingContext = new ModelBindingContext
            {
                ModelName     = "foo",
                ValueProvider = valueProvider
            };

            var controllerContext = new ControllerContext();

            try
            {
                filterValueModelBinder.BindModel(controllerContext, bindingContext);
            }
            catch (Exception e)
            {
                Assert.IsTrue(e.GetType() == typeof(KeyNotFoundException));
            }
        }
        public void BindingFilterOptions()
        {
            var formCollection = new NameValueCollection
            {
                { "foo.LoadFilterId", "10" },
                { "foo.FilterType", "FilterType.Fast" },

                { "foo.Filters[filter1].Values", "10" },
                { "foo.Filters[filter1].ConditionKey", "equal" },
                { "foo.Filters[filter1].TypeName", typeof(Int32).FullName },

                { "foo.Filters[filter2].LeftValue", "2345.14" },
                { "foo.Filters[filter2].ConditionKey", "equal" },
                { "foo.Filters[filter2].TypeName", typeof(Decimal).FullName },

                { "foo.Filters[filter3].LeftValue", "testname" },
                { "foo.Filters[filter3].ConditionKey", "equal" },
                { "foo.Filters[filter3].TypeName", typeof(string).FullName }
            };

            var modelBinder = new DefaultModelBinder();

            var valueProvider = new NameValueCollectionValueProvider(formCollection, null);

            var bindingContext = new ModelBindingContext
            {
                ModelName     = "foo",
                ValueProvider = valueProvider,
                ModelMetadata = ModelMetadataProviders.Current.GetMetadataForType(null, typeof(FilterOptions)),
            };

            var controllerContext = new ControllerContext();

            var filterOptions = modelBinder.BindModel(controllerContext, bindingContext) as IFilterOptions;

            Assert.IsNotNull(filterOptions);

            Assert.IsTrue(filterOptions.Filters.Any());

            Assert.IsTrue(filterOptions.Filters.ContainsKey("filter1"));
            var filter1 = filterOptions.Filters["filter1"] as IFilterValue <int?>;

            Assert.IsNotNull(filter1);
            Assert.IsTrue(filter1.Values.Contains(10));
            Assert.IsTrue(filter1.ConditionKey == "equal");

            Assert.IsTrue(filterOptions.Filters.ContainsKey("filter2"));
            var filter2 = filterOptions.Filters["filter2"] as IFilterValue <decimal?>;

            Assert.IsNotNull(filter2);
            Assert.IsTrue(filter2.LeftValue == 2345.14M);
            Assert.IsTrue(filter2.ConditionKey == "equal");

            Assert.IsTrue(filterOptions.Filters.ContainsKey("filter3"));
            var filter3 = filterOptions.Filters["filter3"] as IFilterValue <string>;

            Assert.IsNotNull(filter3);
            Assert.IsTrue(filter3.LeftValue == "testname");
            Assert.IsTrue(filter3.ConditionKey == "equal");
        }
예제 #3
0
        public void BindingNonEmptyValuesReturnsCorrectChoice()
        {
            //// Arrange

            var modelBinder       = new MvcBootstrapModelBinder();
            var controllerContext = new ControllerContext();

            int id             = 1;
            var formCollection = new NameValueCollection
            {
                { "Choice", id.ToString(CultureInfo.InvariantCulture) }
            };

            var valueProvider = new NameValueCollectionValueProvider(formCollection, null);
            var modelMetadata = ModelMetadataProviders.Current.GetMetadataForType(null, typeof(Choice <TestEntityViewModel>));

            var bindingContext = new ModelBindingContext
            {
                ModelName     = "Choice",
                ValueProvider = valueProvider,
                ModelMetadata = modelMetadata
            };

            //// Act

            var binding = modelBinder.BindModel(controllerContext, bindingContext) as Choice <TestEntityViewModel>;

            //// Assert

            Assert.That(binding, Is.Not.Null);
            Assert.That(binding.Selection, Is.Not.Null);
            Assert.That(binding.Selection.Id, Is.EqualTo(id));
        }
        public void CreateModel_QuestionDefinitionIdNotInBindingContext_ThrowsInvalidOperationException()
        {
            // Arrange
            var formCollection = new NameValueCollection
            {
                { "NonExistingProperty", "" }
            };

            var valueProvider = new NameValueCollectionValueProvider(formCollection, null);
            var modelMetadata = ModelMetadataProviders.Current.GetMetadataForType(null, typeof(QuestionBase));

            var bindingContext = new ModelBindingContext
            {
                ModelName     = "",
                ValueProvider = valueProvider,
                ModelMetadata = modelMetadata
            };

            QuestionBaseModelBinder b = new QuestionBaseModelBinder();
            ControllerContext       controllerContext = new ControllerContext();

            // Act

            // Assert
            Assert.Throws <InvalidOperationException>(() => b.BindModel(controllerContext, bindingContext));
        }
        public void CanBindToExpiry()
        {
            // Arrange
            var formCollection = new NameValueCollection {
                { "foo.CardNo", "9999999999999999" },
                { "foo.Code", "999" },
                { "Expiry.Value.Month", "7" },
                { "Expiry.Value.Year", "2014" }
            };     // not sure why it doesn't work with foo prefix on Expiry?

            var valueProvider = new NameValueCollectionValueProvider(formCollection, null);
            var modelMetadata = ModelMetadataProviders.Current.GetMetadataForType(null, typeof(Card));

            var bindingContext = new ModelBindingContext
            {
                ModelName     = "foo",
                ValueProvider = valueProvider,
                ModelMetadata = modelMetadata
            };

            ControllerContext controllerContext = new ControllerContext();
            CardModelBinder   b = new CardModelBinder();

            // Act
            Card result = (Card)b.BindModel(controllerContext, bindingContext);

            // Assert
            Assert.AreEqual(7, result.Expiry.Value.Month, "Incorrect value for Month");
            Assert.AreEqual(2014, result.Expiry.Value.Year, "Incorrect value for Year");
        }
        public void SelfTrackingEntityModelBinder_Returns_The_Updated_Entity_When_There_Is_A_Serialized_Entity_On_The_Request()
        {
            //Arrange
            NameValueCollection postParameters = CreateCollectionParameters();

            postParameters.Add(typeof(T).Name + "STE", GetSerializedEntity());
            NameValueCollectionValueProvider valueProvider =
                new NameValueCollectionValueProvider(postParameters, null);


            ModelMetadata       metaData          = ModelMetadataProviders.Current.GetMetadataForType(null, new T().GetType());
            ControllerContext   controllerContext = new ControllerContext();
            ModelBindingContext bindingContext    = new ModelBindingContext()
            {
                ModelName     = typeof(T).Name,
                ValueProvider = valueProvider,
                ModelMetadata = metaData
            };

            //Act
            T result = ModelBinder.BindModel(controllerContext, bindingContext) as T;

            //Assert
            Assert.IsNotNull(result);
            foreach (var expectation in Expectations())
            {
                Assert.AreEqual(expectation.Item1, expectation.Item2(result));
            }
        }
예제 #7
0
        public void Initialise()
        {
            _auditLogModelBuilder  = A.Fake <IAuditLogModelBuilder>();
            _dateTimeProvider      = A.Fake <IDateTimeProvider>();
            _userPrincipalProvider = A.Fake <IUserPrincipalProvider>();
            _commandDispatcher     = A.Fake <ICommandDispatcher>();
            _unitOfWork            = A.Fake <IUnitOfWork>();
            _auditFormatter        = A.Fake <IAuditFormatter>();

            A.CallTo(() => _dateTimeProvider.Now).Returns(_date);
            A.CallTo(() => _userPrincipalProvider.CurrentUserName).Returns(User);
            A.CallTo(() => _auditFormatter.AuditValues(A <IValueProvider> ._, A <string> ._)).Returns("default");

            var actionDescriptor = A.Fake <ActionDescriptor>();

            _controllerBase = A.Fake <ControllerBase>();

            A.CallTo(() => actionDescriptor.ActionName).Returns(Action);
            A.CallTo(() => actionDescriptor.ControllerDescriptor.ControllerName).Returns(Controller);

            _nameValueCollection          = new NameValueCollection();
            _nameValueProviderCollection  = new NameValueCollectionValueProvider(_nameValueCollection, null);
            _controllerBase.ValueProvider = _nameValueProviderCollection;

            _actionExecutingContext = new ActionExecutedContext()
            {
                ActionDescriptor = actionDescriptor,
                Controller       = _controllerBase
            };
        }
        public void CanBindToMember()
        {
            // Arrange
            var formCollection = new NameValueCollection {
                { "foo.FirstName", "Fernando" },
                { "foo.LastName", "Alonso" }
            };

            var valueProvider = new NameValueCollectionValueProvider(formCollection, null);
            var modelMetadata = ModelMetadataProviders.Current.GetMetadataForType(null, typeof(Member));

            var bindingContext = new ModelBindingContext
            {
                ModelName     = "foo",
                ValueProvider = valueProvider,
                ModelMetadata = modelMetadata
            };

            MemberModelBinder b = new MemberModelBinder();
            ControllerContext controllerContext = new ControllerContext();

            // Act
            Member result = (Member)b.BindModel(controllerContext, bindingContext);

            // Assert
            Assert.AreEqual("Fernando", result.FirstName, "Incorrect value for FirstName");
            Assert.AreEqual("Alonso", result.LastName, "Incorrect value for LastName");
        }
예제 #9
0
        public void TrimModelBinder_TrimsStringValuesOnModel(
            string inserted,
            string expected)
        {
            // Arrange
            var formCollection = new NameValueCollection
            {
                ["model.Name"] = inserted,
            };

            var valueProvider = new NameValueCollectionValueProvider(formCollection, null);
            var modelMetadata = ModelMetadataProviders.Current.GetMetadataForType(null, typeof(TestUserModel));

            var bindingContext = new ModelBindingContext
            {
                ModelName     = "model",
                ValueProvider = valueProvider,
                ModelMetadata = modelMetadata
            };

            var binder            = new TrimModelBinder();
            var controllerContext = new ControllerContext();

            // Act
            var result = (TestUserModel)binder.BindModel(controllerContext, bindingContext);

            // Assert
            result.Name.Should().Be(expected);
        }
예제 #10
0
        public void CanBindSimpleModelWithGuidIdAndNullValue()
        {
            var territoryName = "Someplace, USA";

            // Arrange
            var formCollection = new NameValueCollection
            {
                { "Territory.Id", string.Empty }, { "Territory.Name", territoryName },
            };

            var valueProvider = new NameValueCollectionValueProvider(formCollection, null);
            var modelMetadata = ModelMetadataProviders.Current.GetMetadataForType(null, typeof(Territory));

            var bindingContext = new ModelBindingContext
            {
                ModelName = "Territory", ValueProvider = valueProvider, ModelMetadata = modelMetadata
            };

            DefaultModelBinder target = new SharpModelBinder();

            var controllerContext = new ControllerContext();

            // Act
            var result = (Territory)target.BindModel(controllerContext, bindingContext);

            // Assert
            Assert.AreEqual(territoryName, result.Name);
        }
        public void CreateModel_CreatesModelOfCorrectType_CreatesModelMetaDataOfCorrectQuestionType()
        {
            // Arrange
            var formCollection = new NameValueCollection
            {
                // MaritalStatus here provides better test experience over CurrentLienHolder
                // since CurrentLienHolder is a default value for Enum.TryParse(questionDefinitionIdName
                { "QuestionDefinitionId", "MaritalStatus" }
            };

            var valueProvider = new NameValueCollectionValueProvider(formCollection, null);
            var modelMetadata = ModelMetadataProviders.Current.GetMetadataForType(null, typeof(QuestionBase));

            var bindingContext = new ModelBindingContext
            {
                ModelName     = "",
                ValueProvider = valueProvider,
                ModelMetadata = modelMetadata
            };

            QuestionBaseModelBinder b = new QuestionBaseModelBinder();
            ControllerContext       controllerContext = new ControllerContext();

            // Act
            QuestionBase result = (QuestionBase)b.BindModel(controllerContext, bindingContext);

            // Assert
            Assert.AreEqual(result.QuestionDefinitionId, QuestionDefinitionId.MaritalStatus);
        }
예제 #12
0
        public void CanBindSimpleModel()
        {
            int    id           = 2;
            string employeeName = "Michael";

            // Arrange
            var formCollection = new NameValueCollection
            {
                { "Employee.Id", id.ToString() },
                { "Employee.Name", employeeName },
            };

            var valueProvider = new NameValueCollectionValueProvider(formCollection, null);
            var modelMetadata = ModelMetadataProviders.Current.GetMetadataForType(null, typeof(Employee));

            var bindingContext = new ModelBindingContext
            {
                ModelName     = "Employee",
                ValueProvider = valueProvider,
                ModelMetadata = modelMetadata
            };

            DefaultModelBinder target = new SharpModelBinder();

            ControllerContext controllerContext = new ControllerContext();

            // Act
            Employee result = (Employee)target.BindModel(controllerContext, bindingContext);

            // Assert
            Assert.AreEqual(id, result.Id);
            Assert.AreEqual(employeeName, result.Name);
        }
예제 #13
0
        public void CanBindModelWithEntityCollection()
        {
            var id           = 2;
            var employeeName = "Michael";

            // Arrange
            var formCollection = new NameValueCollection
            {
                { "Employee.Id", id.ToString() },
                { "Employee.Name", employeeName },
                { "Employee.Reports[0].Name", "Michael" },
                { "Employee.Reports[1].Name", "Alec" },
                { "Employee.Manager", "12" }
            };

            var valueProvider = new NameValueCollectionValueProvider(formCollection, null);
            var modelMetadata = ModelMetadataProviders.Current.GetMetadataForType(null, typeof(Employee));

            var bindingContext = new ModelBindingContext
            {
                ModelName = "Employee", ValueProvider = valueProvider, ModelMetadata = modelMetadata
            };

            DefaultModelBinder target = new SharpModelBinder();

            var controllerContext = new ControllerContext();

            // Act
            var result = (Employee)target.BindModel(controllerContext, bindingContext);

            // Assert
            Assert.AreEqual(id, result.Id);
            Assert.AreEqual(employeeName, result.Name);
            Assert.GreaterOrEqual(result.Reports.Count, 2);
        }
        public void ModelBinder()
        {
            var postData =
                new NameValueCollection()
            {
                { "Class", "TempModelType" },
                { "TestValue", "1234" },
                { "Ignored", "IgnoredValue" },
            };

            var controllerContext = new ControllerContext();
            var valueProvider     = new NameValueCollectionValueProvider(postData, null);
            var modelMetadata     = ModelMetadataProviders.Current.GetMetadataForType(null, typeof(object));

            var bindingContext =
                new ModelBindingContext()
            {
                ValueProvider = valueProvider,
                ModelMetadata = modelMetadata,
            };

            var result = (TempModelType) new OnlineController.Binder().BindModel(controllerContext, bindingContext);

            Assert.That(result, Is.Not.Null, "null model returned");
            Assert.That(result.TestValue, Is.EqualTo("1234"));
        }
예제 #15
0
        public void Arrange()
        {
            _controllerContext = new ControllerContext(Mock.Of <HttpContextBase>(), new RouteData(), Mock.Of <ControllerBase>());
            _valueProvider     = new NameValueCollectionValueProvider(new NameValueCollection(), null);

            _bindingContext = new ModelBindingContext
            {
                ModelName     = "",
                ValueProvider = _valueProvider,
                ModelMetadata = ModelMetadataProviders.Current.GetMetadataForType(null, typeof(MembershipMessageStub))
            };

            _callerContextProvider = new Mock <ICallerContextProvider>();

            _callerContext = new CallerContext
            {
                AccountHashedId = "ABC123",
                AccountId       = 111111,
                UserRef         = Guid.NewGuid()
            };

            _callerContextProvider.Setup(p => p.GetCallerContext()).Returns(_callerContext);

            _modelBinder = new MessageModelBinder(() => _callerContextProvider.Object);
        }
예제 #16
0
        public void Setup()
        {
            _auditFormatter = new AuditFormatter();

            _nameValueCollection         = new NameValueCollection();
            _nameValueProviderCollection = new NameValueCollectionValueProvider(_nameValueCollection, null);
        }
        public void BindingEmptyValuesReturnsEmptyChoices()
        {
            //// Arrange

            var modelBinder       = new MvcBootstrapModelBinder();
            var controllerContext = new ControllerContext();

            var formCollection = new NameValueCollection
            {
                // Nothing
            };

            var valueProvider = new NameValueCollectionValueProvider(formCollection, null);
            var modelMetadata = ModelMetadataProviders.Current.GetMetadataForType(null, typeof(Choices <TestEntityViewModel>));

            var bindingContext = new ModelBindingContext
            {
                ModelName     = string.Empty,
                ValueProvider = valueProvider,
                ModelMetadata = modelMetadata
            };

            //// Act

            var binding = modelBinder.BindModel(controllerContext, bindingContext) as Choices <TestEntityViewModel>;

            //// Assert

            Assert.That(binding, Is.Not.Null);
            Assert.That(binding.Selections, Is.Not.Null);
            Assert.That(binding.Selections, Is.Empty);
        }
        public void GetValue_NonValidating_WithArraysInCollection(
            string name,
            string value,
            string index,
            string expectedAttemptedValue
            )
        {
            // Arrange
            string[]            expectedRawValue      = new[] { expectedAttemptedValue };
            NameValueCollection unvalidatedCollection = new NameValueCollection();

            unvalidatedCollection.Add(name, value);

            CultureInfo culture = CultureInfo.GetCultureInfo("fr-FR");
            NameValueCollectionValueProvider valueProvider = new NameValueCollectionValueProvider(
                _backingStore,
                unvalidatedCollection,
                culture,
                true
                );

            // Act
            ValueProviderResult vpResult = valueProvider.GetValue(index, skipValidation: true);

            // Asserts
            Assert.NotNull(vpResult);
            Assert.Equal(culture, vpResult.Culture);
            Assert.Equal(expectedRawValue, (string[])vpResult.RawValue);
            Assert.Equal(expectedAttemptedValue, vpResult.AttemptedValue);
        }
        public void GetValue_NonValidating()
        {
            // Arrange
            NameValueCollection unvalidatedCollection = new NameValueCollection()
            {
                { "foo", "fooValue3" },
                { "foo", "fooValue4" }
            };

            CultureInfo culture = CultureInfo.GetCultureInfo("fr-FR");
            NameValueCollectionValueProvider valueProvider = new NameValueCollectionValueProvider(
                _backingStore,
                unvalidatedCollection,
                culture
                );

            // Act
            ValueProviderResult vpResult = valueProvider.GetValue("foo", skipValidation: true);

            // Assert
            Assert.NotNull(vpResult);
            Assert.Equal(new[] { "fooValue3", "fooValue4" }, (string[])vpResult.RawValue);
            Assert.Equal("fooValue3,fooValue4", vpResult.AttemptedValue);
            Assert.Equal(culture, vpResult.Culture);
        }
        private ModelBindingContext GetBindingContext()
        {
            var valueProvider = new NameValueCollectionValueProvider(_form, null);
            var metadata      = ModelMetadataProviders.Current.GetMetadataForType(null, typeof(DataCollectionViewModelStep2));

            return(new ModelBindingContext {
                ModelName = "", ModelMetadata = metadata, ValueProvider = valueProvider
            });
        }
예제 #21
0
        public void GetValue_ThrowsIfKeyIsNull()
        {
            // Arrange
            NameValueCollectionValueProvider valueProvider = new NameValueCollectionValueProvider(_backingStore, null);

            // Act & assert
            Assert.ThrowsArgumentNull(
                delegate { valueProvider.GetValue(null); }, "key");
        }
예제 #22
0
        public void ContainsPrefix_ThrowsIfPrefixIsNull()
        {
            // Arrange
            NameValueCollectionValueProvider valueProvider = new NameValueCollectionValueProvider(_backingStore, null);

            // Act & assert
            Assert.ThrowsArgumentNull(
                delegate { valueProvider.ContainsPrefix(null); }, "prefix");
        }
예제 #23
0
        public void Init_And_BuildExpression()
        {
            var formCollection = new NameValueCollection
            {
                { "Filters[CategoryId].Values", "1" },
                { "Filters[CategoryId].Values", "14" },
                { "Filters[CategoryId].Values", "5" },
                { "Filters[CategoryId].ConditionKey", "isin" },
                { "Filters[CategoryId].TypeName", typeof(Int32).FullName },

                { "Filters[UnitPrice].LeftValue", "10" },
                { "Filters[UnitPrice].RightValue", "2345.14" },
                { "Filters[UnitPrice].ConditionKey", "between" },
                { "Filters[UnitPrice].TypeName", typeof(Decimal).FullName },

                { "Filters[Name].LeftValue", "testname" },
                { "Filters[Name].ConditionKey", "contains" },
                { "Filters[Name].TypeName", typeof(string).FullName }
            };

            var modelBinder = new DefaultModelBinder();

            var valueProvider = new NameValueCollectionValueProvider(formCollection, null);

            var bindingContext = new ModelBindingContext
            {
                ModelName     = string.Empty,
                ValueProvider = valueProvider,
                ModelMetadata = ModelMetadataProviders.Current.GetMetadataForType(null, typeof(FilterOptions)),
            };

            var controllerContext = new ControllerContext();

            var filterOptions = modelBinder.BindModel(controllerContext, bindingContext) as IFilterOptions;

            Assert.IsNotNull(filterOptions);

            var productFilter = new ProductFilter().Init(filterOptions);

            var expression = productFilter.BuildExpression <Product>();

            Assert.IsNotNull(expression);

            var predicat = expression.Compile();

            Assert.IsFalse(predicat(new Product {
                CategoryId = 24444, Name = string.Empty
            }));

            Assert.IsTrue(predicat(new Product
            {
                Name       = "testname",
                CategoryId = 14,
                UnitPrice  = 23,
            }));
        }
        public void GetValue_ReturnsNullIfKeyNotFound() {
            // Arrange
            NameValueCollectionValueProvider valueProvider = new NameValueCollectionValueProvider(_backingStore, null);

            // Act
            ValueProviderResult vpResult = valueProvider.GetValue("bar");

            // Assert
            Assert.IsNull(vpResult);
        }
        public void ContainsPrefix() {
            // Arrange
            NameValueCollectionValueProvider valueProvider = new NameValueCollectionValueProvider(_backingStore, null);

            // Act
            bool result = valueProvider.ContainsPrefix("bar");

            // Assert
            Assert.IsTrue(result);
        }
        public void ContainsPrefix_DoesNotContainEmptyPrefixIfBackingStoreIsEmpty() {
            // Arrange
            NameValueCollectionValueProvider valueProvider = new NameValueCollectionValueProvider(new NameValueCollection(), null);

            // Act
            bool result = valueProvider.ContainsPrefix("");

            // Assert
            Assert.IsFalse(result, "The '' prefix shouldn't have been present.");
        }
예제 #27
0
        public void ContainsPrefix_DoesNotContainEmptyPrefixIfBackingStoreIsEmpty()
        {
            // Arrange
            NameValueCollectionValueProvider valueProvider = new NameValueCollectionValueProvider(new NameValueCollection(), null);

            // Act
            bool result = valueProvider.ContainsPrefix("");

            // Assert
            Assert.False(result);
        }
예제 #28
0
        public void ContainsPrefix()
        {
            // Arrange
            NameValueCollectionValueProvider valueProvider = new NameValueCollectionValueProvider(_backingStore, null);

            // Act
            bool result = valueProvider.ContainsPrefix("bar");

            // Assert
            Assert.True(result);
        }
예제 #29
0
        public static TModel SetupAndBind <TBinder, TModel>(NameValueCollection nameValueCollection) where TBinder : IModelBinder, new()
        {
            var valueProvider  = new NameValueCollectionValueProvider(nameValueCollection, null);
            var modelType      = typeof(TModel);
            var modelMetaData  = ModelMetadataProviders.Current.GetMetadataForType(null, modelType);
            var bindingContext = new ModelBindingContext {
                ModelName = modelType.Name, ValueProvider = valueProvider, ModelMetadata = modelMetaData
            };

            return((TModel) new TBinder().BindModel(null, bindingContext));
        }
예제 #30
0
        public void GetValue_ReturnsNullIfKeyNotFound()
        {
            // Arrange
            NameValueCollectionValueProvider valueProvider = new NameValueCollectionValueProvider(_backingStore, null);

            // Act
            ValueProviderResult vpResult = valueProvider.GetValue("bar");

            // Assert
            Assert.Null(vpResult);
        }
        public void ContainsPrefix_DoesNotContainEmptyPrefixIfBackingStoreIsEmpty()
        {
            // Arrange
            NameValueCollectionValueProvider valueProvider = new NameValueCollectionValueProvider(new NameValueCollection(), null);

            // Act
            bool result = valueProvider.ContainsPrefix("");

            // Assert
            Assert.IsFalse(result, "The '' prefix shouldn't have been present.");
        }
        /// <summary>
        /// Converts a dictionary to an IValueProvider
        /// </summary>
        /// <param name="dictionary"></param>
        /// <returns></returns>
        public static IValueProvider ToValueProvider(this IDictionary <string, string> dictionary)
        {
            var collection = new NameValueCollection();

            foreach (var f in dictionary)
            {
                collection.Add(f.Key, f.Value);
            }
            var valProvider = new NameValueCollectionValueProvider(collection, CultureInfo.InvariantCulture);

            return(valProvider);
        }
예제 #33
0
        private void SetupBindingContext()
        {
            _formCollection = _context.HttpContext.Request.Form;
            var valueProvider = new NameValueCollectionValueProvider(_formCollection, null);
            var modelMetadata = ModelMetadataProviders.Current.GetMetadataForType(null, typeof(TModel));

            _bindingContext = new ModelBindingContext
            {
                ValueProvider = valueProvider,
                ModelMetadata = modelMetadata
            };
        }
        public void GetValue() {
            // Arrange
            CultureInfo culture = CultureInfo.GetCultureInfo("fr-FR");
            NameValueCollectionValueProvider valueProvider = new NameValueCollectionValueProvider(_backingStore, culture);

            // Act
            ValueProviderResult vpResult = valueProvider.GetValue("foo");

            // Assert
            Assert.IsNotNull(vpResult);
            CollectionAssert.AreEqual(_backingStore.GetValues("foo"), (string[])vpResult.RawValue);
            Assert.AreEqual("fooValue1,fooValue2", vpResult.AttemptedValue);
            Assert.AreEqual(culture, vpResult.Culture);
        }
        public void GetValue_NonValidating_WithArraysInCollection_Error()
        {
            // Arrange
            NameValueCollection unvalidatedCollection = new NameValueCollection()
            {
                { "foo", "fooValue3" },
                { "fooArray[0][bar1", "barValue1" }
            };

            NameValueCollectionValueProvider valueProvider =
                new NameValueCollectionValueProvider(
                                    _backingStore,
                                    unvalidatedCollection,
                                    culture: null,
                                    jQueryToMvcRequestNormalizationRequired: true);

            // Act & Assert
            Assert.ThrowsArgument(
                () => valueProvider.GetValue("foo", skipValidation: true),
                "key",
                "The key is invalid JQuery syntax because it is missing a closing bracket.");
        }
        public void GetValue_NonValidating_NoUnvalidatedCollectionSpecified_UsesDefaultCollectionValue()
        {
            // Arrange
            CultureInfo culture = CultureInfo.GetCultureInfo("fr-FR");
            NameValueCollectionValueProvider valueProvider = new NameValueCollectionValueProvider(_backingStore, null, culture);

            // Act
            ValueProviderResult vpResult = valueProvider.GetValue("foo", skipValidation: true);

            // Assert
            Assert.NotNull(vpResult);
            Assert.Equal(_backingStore.GetValues("foo"), (string[])vpResult.RawValue);
            Assert.Equal("fooValue1,fooValue2", vpResult.AttemptedValue);
            Assert.Equal(culture, vpResult.Culture);
        }
        public void GetValue_NonValidating_WithArraysInCollection(
                            string name, string value, string index, string expectedAttemptedValue)
        {
            // Arrange
            string[] expectedRawValue = new[] { expectedAttemptedValue };
            NameValueCollection unvalidatedCollection = new NameValueCollection();
            unvalidatedCollection.Add(name, value);

            CultureInfo culture = CultureInfo.GetCultureInfo("fr-FR");
            NameValueCollectionValueProvider valueProvider = 
                    new NameValueCollectionValueProvider(_backingStore, unvalidatedCollection, culture, true);

            // Act
            ValueProviderResult vpResult = valueProvider.GetValue(index, skipValidation: true);
            
            // Asserts
            Assert.NotNull(vpResult);
            Assert.Equal(culture, vpResult.Culture);
            Assert.Equal(expectedRawValue, (string[])vpResult.RawValue);
            Assert.Equal(expectedAttemptedValue, vpResult.AttemptedValue);
        }
        public void GetValue_NonValidating()
        {
            // Arrange
            NameValueCollection unvalidatedCollection = new NameValueCollection()
            {
                { "foo", "fooValue3" },
                { "foo", "fooValue4" }
            };

            CultureInfo culture = CultureInfo.GetCultureInfo("fr-FR");
            NameValueCollectionValueProvider valueProvider = new NameValueCollectionValueProvider(_backingStore, unvalidatedCollection, culture);

            // Act
            ValueProviderResult vpResult = valueProvider.GetValue("foo", skipValidation: true);

            // Assert
            Assert.NotNull(vpResult);
            Assert.Equal(new[] { "fooValue3", "fooValue4" }, (string[])vpResult.RawValue);
            Assert.Equal("fooValue3,fooValue4", vpResult.AttemptedValue);
            Assert.Equal(culture, vpResult.Culture);
        }