Exemplo n.º 1
0
        public void GetBinderResolvesBindersWithCorrectPrecedence()
        {
            // Proper order of precedence:
            // 1. Binder registered in the global table
            // 2. Binder attribute defined on the type
            // 3. Default binder

            // Arrange
            IModelBinder          registeredFirstBinder = new Mock <IModelBinder>().Object;
            ModelBinderDictionary binders = new ModelBinderDictionary()
            {
                { typeof(MyFirstConvertibleType), registeredFirstBinder }
            };

            IModelBinder defaultBinder = new Mock <IModelBinder>().Object;

            binders.DefaultBinder = defaultBinder;

            // Act
            IModelBinder binder1 = binders.GetBinder(typeof(MyFirstConvertibleType));
            IModelBinder binder2 = binders.GetBinder(typeof(MySecondConvertibleType));
            IModelBinder binder3 = binders.GetBinder(typeof(object));

            // Assert
            Assert.AreSame(registeredFirstBinder, binder1, "First binder should have been matched in the registered binders table.");
            Assert.IsInstanceOfType(binder2, typeof(MySecondBinder), "Second binder should have been matched on the type.");
            Assert.AreSame(defaultBinder, binder3, "Third binder should have been the fallback.");
        }
Exemplo n.º 2
0
        public void GetBinderDoesNotReturnDefaultBinderIfAskedNotTo()
        {
            // Proper order of precedence:
            // 1. Binder registered in the global table
            // 2. Binder attribute defined on the type
            // 3. <null>

            // Arrange
            IModelBinder          registeredFirstBinder = new Mock <IModelBinder>().Object;
            ModelBinderDictionary binders = new ModelBinderDictionary()
            {
                { typeof(MyFirstConvertibleType), registeredFirstBinder }
            };

            // Act
            IModelBinder binder1 = binders.GetBinder(
                typeof(MyFirstConvertibleType),
                false /* fallbackToDefault */
                );
            IModelBinder binder2 = binders.GetBinder(
                typeof(MySecondConvertibleType),
                false /* fallbackToDefault */
                );
            IModelBinder binder3 = binders.GetBinder(
                typeof(object),
                false /* fallbackToDefault */
                );

            // Assert
            Assert.Same(registeredFirstBinder, binder1);
            Assert.IsType <MySecondBinder>(binder2);
            Assert.Null(binder3);
        }
Exemplo n.º 3
0
        public void GetBinderResolvesBindersWithCorrectPrecedence()
        {
            // Proper order of precedence:
            // 1. Binder registered in the global table
            // 2. Binder attribute defined on the type
            // 3. Default binder

            // Arrange
            IModelBinder          registeredFirstBinder = new Mock <IModelBinder>().Object;
            ModelBinderDictionary binders = new ModelBinderDictionary()
            {
                { typeof(MyFirstConvertibleType), registeredFirstBinder }
            };

            IModelBinder defaultBinder = new Mock <IModelBinder>().Object;

            binders.DefaultBinder = defaultBinder;

            // Act
            IModelBinder binder1 = binders.GetBinder(typeof(MyFirstConvertibleType));
            IModelBinder binder2 = binders.GetBinder(typeof(MySecondConvertibleType));
            IModelBinder binder3 = binders.GetBinder(typeof(object));

            // Assert
            Assert.Same(registeredFirstBinder, binder1);
            Assert.IsType <MySecondBinder>(binder2);
            Assert.Same(defaultBinder, binder3);
        }
Exemplo n.º 4
0
        public void GetBinderThrowsIfModelTypeIsNull()
        {
            // Arrange
            ModelBinderDictionary binders = new ModelBinderDictionary();

            // Act & Assert
            Assert.ThrowsArgumentNull(
                delegate { binders.GetBinder(null); }, "modelType");
        }
        public void ShouldResolveBinder()
        {
            var dictionary = new ModelBinderDictionary(new DefaultModelBinder());

            dictionary.Add(typeof(DataSet), FakeBinder);

            var binder = dictionary.GetBinder(typeof(DataSet));

            Assert.AreSame(FakeBinder, binder);
        }
Exemplo n.º 6
0
        public void GetBinderThrowsIfModelTypeContainsMultipleAttributes()
        {
            // Arrange
            ModelBinderDictionary binders = new ModelBinderDictionary();

            // Act & Assert
            Assert.Throws <InvalidOperationException>(
                delegate { binders.GetBinder(typeof(ConvertibleTypeWithSeveralBinders), true /* fallbackToDefault */); },
                "The type 'System.Web.Mvc.Test.ModelBinderDictionaryTest+ConvertibleTypeWithSeveralBinders' contains multiple attributes that inherit from CustomModelBinderAttribute.");
        }
        public void ShouldResolveBindersViaInheritance()
        {
            var dictionary = new ModelBinderDictionary(new DefaultModelBinder());

            dictionary.Add(typeof(IListSource), FakeBinder);

            var binder = dictionary.GetBinder(typeof(DataSet)); // DataSet implements IListSource

            Assert.AreSame(FakeBinder, binder);
        }
        public void ShouldFallBackToDefaultBinder()
        {
            var dictionary = new ModelBinderDictionary(new DefaultModelBinder());

            dictionary.Add(typeof(DataSet), FakeBinder);

            var binder = dictionary.GetBinder(typeof(int));

            Assert.IsInstanceOf <DefaultModelBinder>(binder);
        }
        public void ShouldRespectExplicitDefaultBinder()
        {
            var dictionary = new ModelBinderDictionary(new DefaultModelBinder());

            dictionary.DefaultModelBinder = FakeBinder;

            var binder = dictionary.GetBinder(typeof(int));

            Assert.AreSame(FakeBinder, binder);
        }
Exemplo n.º 10
0
        public void GetBinderThrowsIfModelTypeIsNull()
        {
            // Arrange
            ModelBinderDictionary binders = new ModelBinderDictionary();

            // Act & Assert
            ExceptionHelper.ExpectArgumentNullException(
                delegate {
                binders.GetBinder(null);
            }, "modelType");
        }
Exemplo n.º 11
0
        public T GetModel <T>()
        {
            var binder = _binders.GetBinder(typeof(T));
            var res    = binder.BindModel(_context, new ModelBindingContext
            {
                FallbackToEmptyPrefix = true,
                ModelMetadata         = new ModelMetadata(new EmptyModelMetadataProvider(), null, () => default(T), typeof(T), ""),
                ValueProvider         = _values
            });

            return((res is T) ? (T)res : default(T));
        }
Exemplo n.º 12
0
        public void GetBinderDoesNotReturnDefaultBinderIfAskedNotTo() {
            // Proper order of precedence:
            // 1. Binder registered in the global table
            // 2. Binder attribute defined on the type
            // 3. <null>

            // Arrange
            IModelBinder registeredFirstBinder = new Mock<IModelBinder>().Object;
            ModelBinderDictionary binders = new ModelBinderDictionary() {
                { typeof(MyFirstConvertibleType), registeredFirstBinder }
            };

            // Act
            IModelBinder binder1 = binders.GetBinder(typeof(MyFirstConvertibleType), false /* fallbackToDefault */);
            IModelBinder binder2 = binders.GetBinder(typeof(MySecondConvertibleType), false /* fallbackToDefault */);
            IModelBinder binder3 = binders.GetBinder(typeof(object), false /* fallbackToDefault */);

            // Assert
            Assert.AreSame(registeredFirstBinder, binder1, "First binder should have been matched in the registered binders table.");
            Assert.IsInstanceOfType(binder2, typeof(MySecondBinder), "Second binder should have been matched on the type.");
            Assert.IsNull(binder3, "Third binder should have returned null since asked not to use default.");
        }
Exemplo n.º 13
0
        public void GetBinderDoesNotReturnDefaultBinderIfAskedNotTo()
        {
            // Proper order of precedence:
            // 1. Binder registered in the global table
            // 2. Binder attribute defined on the type
            // 3. <null>

            // Arrange
            IModelBinder          registeredFirstBinder = new Mock <IModelBinder>().Object;
            ModelBinderDictionary binders = new ModelBinderDictionary()
            {
                { typeof(MyFirstConvertibleType), registeredFirstBinder }
            };

            // Act
            IModelBinder binder1 = binders.GetBinder(typeof(MyFirstConvertibleType), false /* fallbackToDefault */);
            IModelBinder binder2 = binders.GetBinder(typeof(MySecondConvertibleType), false /* fallbackToDefault */);
            IModelBinder binder3 = binders.GetBinder(typeof(object), false /* fallbackToDefault */);

            // Assert
            Assert.AreSame(registeredFirstBinder, binder1, "First binder should have been matched in the registered binders table.");
            Assert.IsInstanceOfType(binder2, typeof(MySecondBinder), "Second binder should have been matched on the type.");
            Assert.IsNull(binder3, "Third binder should have returned null since asked not to use default.");
        }
        public void GetBinderConsultsProviders() {

            //Arrange
            Type modelType = typeof(string);
            IModelBinder expectedBinderFromProvider = new Mock<IModelBinder>().Object;

            Mock<IModelBinderProvider> locatedProvider = new Mock<IModelBinderProvider>();
            locatedProvider.Setup(p => p.GetBinder(modelType))
                .Returns(expectedBinderFromProvider);

            Mock<IModelBinderProvider> secondProvider = new Mock<IModelBinderProvider>();

            ModelBinderProviderCollection providers = new ModelBinderProviderCollection(new IModelBinderProvider[] { locatedProvider.Object, secondProvider.Object });
            ModelBinderDictionary binders = new ModelBinderDictionary(providers);

            //Act
            IModelBinder returnedBinder = binders.GetBinder(modelType);

            //Assert
            Assert.AreSame(expectedBinderFromProvider, returnedBinder);
        }
Exemplo n.º 15
0
        public void GetBinderConsultsProviders()
        {
            //Arrange
            Type         modelType = typeof(string);
            IModelBinder expectedBinderFromProvider = new Mock <IModelBinder>().Object;

            Mock <IModelBinderProvider> locatedProvider = new Mock <IModelBinderProvider>();

            locatedProvider.Setup(p => p.GetBinder(modelType))
            .Returns(expectedBinderFromProvider);

            Mock <IModelBinderProvider> secondProvider = new Mock <IModelBinderProvider>();

            ModelBinderProviderCollection providers = new ModelBinderProviderCollection(new IModelBinderProvider[] { locatedProvider.Object, secondProvider.Object });
            ModelBinderDictionary         binders   = new ModelBinderDictionary(providers);

            //Act
            IModelBinder returnedBinder = binders.GetBinder(modelType);

            //Assert
            Assert.AreSame(expectedBinderFromProvider, returnedBinder);
        }
Exemplo n.º 16
0
        public void RegisterBinderPopulatesModelBinderDictionary()
        {
            // Arrange
            ModelBinderDictionary binderDict = new ModelBinderDictionary();

            // Act
            FileCollectionModelBinder.RegisterBinder(binderDict);

            // Assert
            Assert.AreEqual(6, binderDict.Count);
            Assert.IsInstanceOfType(binderDict.GetBinder(typeof(HttpPostedFileBase[])), typeof(FileCollectionModelBinder));
            Assert.IsInstanceOfType(binderDict.GetBinder(typeof(IEnumerable <HttpPostedFileBase>)), typeof(FileCollectionModelBinder));
            Assert.IsInstanceOfType(binderDict.GetBinder(typeof(ICollection <HttpPostedFileBase>)), typeof(FileCollectionModelBinder));
            Assert.IsInstanceOfType(binderDict.GetBinder(typeof(IList <HttpPostedFileBase>)), typeof(FileCollectionModelBinder));
            Assert.IsInstanceOfType(binderDict.GetBinder(typeof(Collection <HttpPostedFileBase>)), typeof(FileCollectionModelBinder));
            Assert.IsInstanceOfType(binderDict.GetBinder(typeof(List <HttpPostedFileBase>)), typeof(FileCollectionModelBinder));
        }
Exemplo n.º 17
0
        public void GetBinderThrowsIfModelTypeContainsMultipleAttributes() {
            // Arrange
            ModelBinderDictionary binders = new ModelBinderDictionary();

            // Act & Assert
            ExceptionHelper.ExpectInvalidOperationException(
                delegate {
                    binders.GetBinder(typeof(ConvertibleTypeWithSeveralBinders), true /* fallbackToDefault */);
                },
                "The type 'System.Web.Mvc.Test.ModelBinderDictionaryTest+ConvertibleTypeWithSeveralBinders' contains multiple attributes that inherit from CustomModelBinderAttribute.");
        }
Exemplo n.º 18
0
 public DefaultEncoding()
 {
     this.binder = binderDictionary.GetBinder(typeof(T), fallbackToDefault: true);
 }
        public void GetBinderDoesNotReturnDefaultBinderIfAskedNotTo()
        {
            // Proper order of precedence:
            // 1. Binder registered in the global table
            // 2. Binder attribute defined on the type
            // 3. <null>

            // Arrange
            IModelBinder registeredFirstBinder = new Mock<IModelBinder>().Object;
            ModelBinderDictionary binders = new ModelBinderDictionary()
            {
                { typeof(MyFirstConvertibleType), registeredFirstBinder }
            };

            // Act
            IModelBinder binder1 = binders.GetBinder(typeof(MyFirstConvertibleType), false /* fallbackToDefault */);
            IModelBinder binder2 = binders.GetBinder(typeof(MySecondConvertibleType), false /* fallbackToDefault */);
            IModelBinder binder3 = binders.GetBinder(typeof(object), false /* fallbackToDefault */);

            // Assert
            Assert.Same(registeredFirstBinder, binder1);
            Assert.IsType<MySecondBinder>(binder2);
            Assert.Null(binder3);
        }
Exemplo n.º 20
0
 private MvcEncoderDecoder()
 {
     this.binder = binderDictionary.GetBinder(typeof(T), fallbackToDefault: true);
 }
Exemplo n.º 21
0
        public void GetBinderThrowsIfModelTypeIsNull() {
            // Arrange
            ModelBinderDictionary binders = new ModelBinderDictionary();

            // Act & Assert
            ExceptionHelper.ExpectArgumentNullException(
                delegate {
                    binders.GetBinder(null);
                }, "modelType");
        }
        public void GetBinderThrowsIfModelTypeIsNull()
        {
            // Arrange
            ModelBinderDictionary binders = new ModelBinderDictionary();

            // Act & Assert
            Assert.ThrowsArgumentNull(
                delegate { binders.GetBinder(null); }, "modelType");
        }
Exemplo n.º 23
0
        public void GetBinderResolvesBindersWithCorrectPrecedence() {
            // Proper order of precedence:
            // 1. Binder registered in the global table
            // 2. Binder attribute defined on the type
            // 3. Default binder

            // Arrange
            IModelBinder registeredFirstBinder = new Mock<IModelBinder>().Object;
            ModelBinderDictionary binders = new ModelBinderDictionary() {
                { typeof(MyFirstConvertibleType), registeredFirstBinder }
            };

            IModelBinder defaultBinder = new Mock<IModelBinder>().Object;
            binders.DefaultBinder = defaultBinder;

            // Act
            IModelBinder binder1 = binders.GetBinder(typeof(MyFirstConvertibleType));
            IModelBinder binder2 = binders.GetBinder(typeof(MySecondConvertibleType));
            IModelBinder binder3 = binders.GetBinder(typeof(object));

            // Assert
            Assert.AreSame(registeredFirstBinder, binder1, "First binder should have been matched in the registered binders table.");
            Assert.IsInstanceOfType(binder2, typeof(MySecondBinder), "Second binder should have been matched on the type.");
            Assert.AreSame(defaultBinder, binder3, "Third binder should have been the fallback.");
        }
        public void GetBinderResolvesBindersWithCorrectPrecedence()
        {
            // Proper order of precedence:
            // 1. Binder registered in the global table
            // 2. Binder attribute defined on the type
            // 3. Default binder

            // Arrange
            IModelBinder registeredFirstBinder = new Mock<IModelBinder>().Object;
            ModelBinderDictionary binders = new ModelBinderDictionary()
            {
                { typeof(MyFirstConvertibleType), registeredFirstBinder }
            };

            IModelBinder defaultBinder = new Mock<IModelBinder>().Object;
            binders.DefaultBinder = defaultBinder;

            // Act
            IModelBinder binder1 = binders.GetBinder(typeof(MyFirstConvertibleType));
            IModelBinder binder2 = binders.GetBinder(typeof(MySecondConvertibleType));
            IModelBinder binder3 = binders.GetBinder(typeof(object));

            // Assert
            Assert.Same(registeredFirstBinder, binder1);
            Assert.IsType<MySecondBinder>(binder2);
            Assert.Same(defaultBinder, binder3);
        }