public void GetBinder_TypeMatches_PrefixSuppressed_ReturnsFactoryInstance()
        {
            // Arrange
            int                 numExecutions     = 0;
            IModelBinder        theBinderInstance = new Mock <IModelBinder>().Object;
            Func <IModelBinder> factory           = delegate
            {
                numExecutions++;
                return(theBinderInstance);
            };

            SimpleModelBinderProvider provider = new SimpleModelBinderProvider(typeof(string), factory)
            {
                SuppressPrefixCheck = true
            };
            ModelBindingContext bindingContext = GetBindingContext(typeof(string));

            // Act
            IModelBinder returnedBinder = provider.GetBinder(null, bindingContext);

            returnedBinder = provider.GetBinder(null, bindingContext);

            // Assert
            Assert.Equal(2, numExecutions);
            Assert.Equal(theBinderInstance, returnedBinder);
        }
Esempio n. 2
0
 private static void AddWebApiModelBinder(
     HttpConfiguration config,
     Type modelType,
     WebApi.ModelBinding.IModelBinder modelBinder)
 {
     var modelBinderProvider = new SimpleModelBinderProvider(modelType, modelBinder);
     config.Services.Insert(typeof(ModelBinderProvider), 0, modelBinderProvider);
 }
        public void ModelTypeProperty()
        {
            // Arrange
            SimpleModelBinderProvider provider = new SimpleModelBinderProvider(typeof(string), new Mock <IModelBinder>().Object);

            // Act & assert
            Assert.Equal(typeof(string), provider.ModelType);
        }
        public void GetBinderThrowsIfBindingContextIsNull()
        {
            // Arrange
            SimpleModelBinderProvider provider = new SimpleModelBinderProvider(typeof(string), new Mock <IModelBinder>().Object);

            // Act & assert
            Assert.ThrowsArgumentNull(
                delegate { provider.GetBinder(null, null); }, "bindingContext");
        }
Esempio n. 5
0
        public static void Register(HttpConfiguration config)
        {
            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional });

            var provider = new SimpleModelBinderProvider(
            typeof(GetPagedListParams), new GetPagedListParamsModelBinder());
            config.Services.Insert(typeof(ModelBinderProvider), 0, provider);
        }
Esempio n. 6
0
        public static void Register(HttpConfiguration config)
        {
            // Web API routes
            config.MapHttpAttributeRoutes();

            foreach (var type in Filter.SupportedRangeTypes)
            {
                var intRangeProvider = new SimpleModelBinderProvider(
                    typeof(Range<>).MakeGenericType(type),
                    (IModelBinder)Activator.CreateInstance(typeof(RangeModelBinder<>).MakeGenericType(type)));

                config.Services.Insert(typeof(ModelBinderProvider), 0, intRangeProvider);
            }
        }
        public void GetBinder_TypeDoesNotMatch_ReturnsNull()
        {
            // Arrange
            SimpleModelBinderProvider provider = new SimpleModelBinderProvider(typeof(string), new Mock <IModelBinder>().Object)
            {
                SuppressPrefixCheck = true
            };
            ModelBindingContext bindingContext = GetBindingContext(typeof(object));

            // Act
            IModelBinder binder = provider.GetBinder(null, bindingContext);

            // Assert
            Assert.Null(binder);
        }
        public void GetBinder_TypeMatches_PrefixNotFound_ReturnsNull()
        {
            // Arrange
            IModelBinder binderInstance = new Mock<IModelBinder>().Object;
            SimpleModelBinderProvider provider = new SimpleModelBinderProvider(typeof(string), binderInstance);

            ModelBindingContext bindingContext = GetBindingContext(typeof(string));
            bindingContext.ValueProvider = new SimpleHttpValueProvider();

            // Act
            IModelBinder returnedBinder = provider.GetBinder(null, bindingContext);

            // Assert
            Assert.Null(returnedBinder);
        }
        public void GetBinder_TypeDoesNotMatch_ReturnsNull()
        {
            // Arrange
            SimpleModelBinderProvider provider = new SimpleModelBinderProvider(typeof(string), new Mock<IModelBinder>().Object)
            {
                SuppressPrefixCheck = true
            };
            ModelBindingContext bindingContext = GetBindingContext(typeof(object));

            // Act
            IModelBinder binder = provider.GetBinder(null, bindingContext.ModelType);

            // Assert
            Assert.Null(binder);
        }
        public void GetBinder_TypeMatches_PrefixNotFound_ReturnsNull()
        {
            // Arrange
            IModelBinder binderInstance        = new Mock <IModelBinder>().Object;
            SimpleModelBinderProvider provider = new SimpleModelBinderProvider(typeof(string), binderInstance);

            ModelBindingContext bindingContext = GetBindingContext(typeof(string));

            bindingContext.ValueProvider = new SimpleHttpValueProvider();

            // Act
            IModelBinder returnedBinder = provider.GetBinder(null, bindingContext);

            // Assert
            Assert.Null(returnedBinder);
        }
        public void GetBinder_TypeMatches_PrefixSuppressed_ReturnsInstance()
        {
            // Arrange
            IModelBinder theBinderInstance     = new Mock <IModelBinder>().Object;
            SimpleModelBinderProvider provider = new SimpleModelBinderProvider(typeof(string), theBinderInstance)
            {
                SuppressPrefixCheck = true
            };
            ModelBindingContext bindingContext = GetBindingContext(typeof(string));

            // Act
            IModelBinder returnedBinder = provider.GetBinder(null, bindingContext);

            // Assert
            Assert.Equal(theBinderInstance, returnedBinder);
        }
        public static void Register(HttpConfiguration config)
        {
            var corsAttr = new EnableCorsAttribute("*", "*", "*");
            config.EnableCors(corsAttr);

            config.MapHttpAttributeRoutes();

            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );

            ConfiguraJson.Configurar(config);
            ConfiguraPoliticaExcecoes.Configurar(config);
            ConfiguraMongo.Configurar();
            ConfiguraIoC.Configurar(config);

            var provider = new SimpleModelBinderProvider(typeof(EquipamentoDto), new EquipamentoDtoModelBinder());
            config.Services.Insert(typeof(ModelBinderProvider), 0, provider);
        }
        public void ModelTypeProperty()
        {
            // Arrange
            SimpleModelBinderProvider provider = new SimpleModelBinderProvider(typeof(string), new Mock<IModelBinder>().Object);

            // Act & assert
            Assert.Equal(typeof(string), provider.ModelType);
        }
 public SimpleModelBinder(SimpleModelBinderProvider parent)
 {
     _parent = parent;
 }
        public void GetBinder_TypeMatches_PrefixSuppressed_ReturnsFactoryInstance()
        {
            // Arrange
            int numExecutions = 0;
            IModelBinder theBinderInstance = new Mock<IModelBinder>().Object;
            Func<IModelBinder> factory = delegate
            {
                numExecutions++;
                return theBinderInstance;
            };

            SimpleModelBinderProvider provider = new SimpleModelBinderProvider(typeof(string), factory)
            {
                SuppressPrefixCheck = true
            };
            ModelBindingContext bindingContext = GetBindingContext(typeof(string));

            // Act
            IModelBinder returnedBinder = provider.GetBinder(null, bindingContext.ModelType);
            returnedBinder.BindModel(null, bindingContext);

            returnedBinder = provider.GetBinder(null, bindingContext.ModelType);
            returnedBinder.BindModel(null, bindingContext);

            // Assert
            Assert.Equal(2, numExecutions);
        }
        public void GetBinderThrowsIfBindingContextIsNull()
        {
            // Arrange
            SimpleModelBinderProvider provider = new SimpleModelBinderProvider(typeof(string), new Mock<IModelBinder>().Object);

            // Act & assert
            Assert.ThrowsArgumentNull(
                delegate { provider.GetBinder(null, null); }, "modelType");
        }
        public void GetBinder_TypeMatches_PrefixSuppressed_ReturnsInstance()
        {
            // Arrange
            IModelBinder theBinderInstance = new Mock<IModelBinder>().Object;
            SimpleModelBinderProvider provider = new SimpleModelBinderProvider(typeof(string), theBinderInstance)
            {
                SuppressPrefixCheck = true
            };
            ModelBindingContext bindingContext = GetBindingContext(typeof(string));

            // Act
            IModelBinder returnedBinder = provider.GetBinder(null, bindingContext.ModelType);

            // Assert
            Assert.NotNull(returnedBinder);
        }
        public void ModelBinderToString_With_CompositeModelBinder_Formats()
        {
            // Arrange
            ModelBinderProvider innerProvider1 = new SimpleModelBinderProvider(typeof(int), () => null);
            ModelBinderProvider innerProvider2 = new ArrayModelBinderProvider();
            CompositeModelBinderProvider compositeProvider = new CompositeModelBinderProvider(new ModelBinderProvider[] { innerProvider1, innerProvider2 });
            string expected = String.Format(
                                "{0}({1}, {2})", 
                                typeof(CompositeModelBinderProvider).Name, 
                                typeof(SimpleModelBinderProvider).Name,
                                typeof(ArrayModelBinderProvider).Name);

            // Act
            string actual = FormattingUtilities.ModelBinderToString(compositeProvider);

            // Assert
            Assert.Equal(expected, actual);
        }
        public void ModelBinderToString_Formats()
        {
            // Arrange
            ModelBinderProvider provider = new SimpleModelBinderProvider(typeof (int), () => null);
            string expected = typeof (SimpleModelBinderProvider).Name;

            // Act
            string actual = FormattingUtilities.ModelBinderToString(provider);

            // Assert
            Assert.Equal(expected, actual);
        }
 public SimpleModelBinder(SimpleModelBinderProvider parent)
 {
     _parent = parent;
 }