Beispiel #1
0
        public void Create_ForSupportedTypes_ReturnsBinder()
        {
            // Arrange
            var provider = new ComplexTypeModelBinderProvider();

            var context = new TestModelBinderProviderContext(typeof(Person));

            context.OnCreatingBinder(m =>
            {
                if (m.ModelType == typeof(int) || m.ModelType == typeof(string))
                {
                    return(Mock.Of <IModelBinder>());
                }
                else
                {
                    Assert.False(true, "Not the right model type");
                    return(null);
                }
            });

            // Act
            var result = provider.GetBinder(context);

            // Assert
            Assert.IsType <ComplexTypeModelBinder>(result);
        }
Beispiel #2
0
        public void Create_ForSupportedType_ReturnsBinder_WithExpectedAllowValidatingTopLevelNodes(
            bool allowValidatingTopLevelNodes)
        {
            // Arrange
            var provider = new ComplexTypeModelBinderProvider();

            var context = new TestModelBinderProviderContext(typeof(Person));

            context.MvcOptions.AllowValidatingTopLevelNodes = allowValidatingTopLevelNodes;
            context.OnCreatingBinder(m =>
            {
                if (m.ModelType == typeof(int) || m.ModelType == typeof(string))
                {
                    return(Mock.Of <IModelBinder>());
                }
                else
                {
                    Assert.False(true, "Not the right model type");
                    return(null);
                }
            });

            // Act
            var result = provider.GetBinder(context);

            // Assert
            var binder = Assert.IsType <ComplexTypeModelBinder>(result);

            Assert.Equal(allowValidatingTopLevelNodes, binder.AllowValidatingTopLevelNodes);
        }
Beispiel #3
0
        public void Create_ForDictionaryType_ReturnsBinder_WithExpectedAllowValidatingTopLevelNodes(
            bool allowValidatingTopLevelNodes)
        {
            // Arrange
            var provider = new DictionaryModelBinderProvider();

            var context = new TestModelBinderProviderContext(typeof(Dictionary <string, string>));

            context.MvcOptions.AllowValidatingTopLevelNodes = allowValidatingTopLevelNodes;
            context.OnCreatingBinder(m =>
            {
                if (m.ModelType == typeof(KeyValuePair <string, string>) || m.ModelType == typeof(string))
                {
                    return(Mock.Of <IModelBinder>());
                }
                else
                {
                    Assert.False(true, "Not the right model type");
                    return(null);
                }
            });

            // Act
            var result = provider.GetBinder(context);

            // Assert
            var binder = Assert.IsType <DictionaryModelBinder <string, string> >(result);

            Assert.Equal(allowValidatingTopLevelNodes, binder.AllowValidatingTopLevelNodes);
            Assert.False(((CollectionModelBinder <KeyValuePair <string, string> >)binder).AllowValidatingTopLevelNodes);
        }
        public void Create_ForDictionaryType_ReturnsBinder(Type modelType)
        {
            // Arrange
            var provider = new DictionaryModelBinderProvider();

            var context = new TestModelBinderProviderContext(modelType);
            context.OnCreatingBinder(m =>
            {
                if (m.ModelType == typeof(KeyValuePair<string, int>) ||
                    m.ModelType == typeof(int) ||
                    m.ModelType == typeof(string))
                {
                    return Mock.Of<IModelBinder>();
                }
                else
                {
                    Assert.False(true, "Not the right model type");
                    return null;
                }
            });

            // Act
            var result = provider.GetBinder(context);

            // Assert
            Assert.IsType<DictionaryModelBinder<string, int>>(result);
        }
    private TestModelBinderProviderContext GetTestModelBinderProviderContext(Type modelType)
    {
        var context = new TestModelBinderProviderContext(modelType);
        var options = context.Services.GetRequiredService <IOptions <MvcOptions> >().Value;

        return(context);
    }
        public void Create_ForSupportedTypes_ReturnsBinder()
        {
            // Arrange
            var provider = new ComplexTypeModelBinderProvider();

            var context = new TestModelBinderProviderContext(typeof(Person));
            context.OnCreatingBinder(m =>
            {
                if (m.ModelType == typeof(int) || m.ModelType == typeof(string))
                {
                    return Mock.Of<IModelBinder>();
                }
                else
                {
                    Assert.False(true, "Not the right model type");
                    return null;
                }
            });

            // Act
            var result = provider.GetBinder(context);

            // Assert
            Assert.IsType<ComplexTypeModelBinder>(result);
        }
        public void Create_ForDictionaryType_ReturnsBinder(Type modelType)
        {
            // Arrange
            var provider = new DictionaryModelBinderProvider();

            var context = new TestModelBinderProviderContext(modelType);

            context.OnCreatingBinder(m =>
            {
                if (m.ModelType == typeof(KeyValuePair <string, int>) ||
                    m.ModelType == typeof(int) ||
                    m.ModelType == typeof(string))
                {
                    return(Mock.Of <IModelBinder>());
                }
                else
                {
                    Assert.False(true, "Not the right model type");
                    return(null);
                }
            });

            // Act
            var result = provider.GetBinder(context);

            // Assert
            var binder = Assert.IsType <DictionaryModelBinder <string, int> >(result);

            Assert.False(binder.AllowValidatingTopLevelNodes); // work done in DictionaryModelBinder.
        }
Beispiel #8
0
        public void Create_ForModelMetadataReadOnly_ReturnsNull()
        {
            // Arrange
            var metadataProvider = new TestModelMetadataProvider();

            metadataProvider.ForProperty(
                typeof(ModelWithIntArrayProperty),
                nameof(ModelWithIntArrayProperty.ArrayProperty)).BindingDetails(bd => bd.IsReadOnly = true);

            var modelMetadata = metadataProvider.GetMetadataForProperty(
                typeof(ModelWithIntArrayProperty),
                nameof(ModelWithIntArrayProperty.ArrayProperty));

            var provider = new ArrayModelBinderProvider();
            var context  = new TestModelBinderProviderContext(typeof(int[]));

            context.OnCreatingBinder((m) =>
            {
                // Expect to be called with the element type to create a binder for elements.
                Assert.Equal(typeof(int), m.ModelType);
                return(Mock.Of <IModelBinder>());
            });

            // Act
            var result = provider.GetBinder(context);

            // Assert
            Assert.IsType <ArrayModelBinder <int> >(result);
        }
Beispiel #9
0
    public void Create_ForKeyValuePair_ReturnsBinder()
    {
        // Arrange
        var provider = new KeyValuePairModelBinderProvider();

        var context = new TestModelBinderProviderContext(typeof(KeyValuePair <string, int>));

        context.OnCreatingBinder(m =>
        {
            if (m.ModelType == typeof(string) || m.ModelType == typeof(int))
            {
                return(Mock.Of <IModelBinder>());
            }
            else
            {
                Assert.False(true, "Not the right model type");
                return(null);
            }
        });

        // Act
        var result = provider.GetBinder(context);

        // Assert
        Assert.IsType <KeyValuePairModelBinder <string, int> >(result);
    }
        public void Create_ForModelMetadataReadOnly_ReturnsNull()
        {
            // Arrange
            var metadataProvider = new TestModelMetadataProvider();
            metadataProvider.ForProperty(
                typeof(ModelWithIntArrayProperty),
                nameof(ModelWithIntArrayProperty.ArrayProperty)).BindingDetails(bd => bd.IsReadOnly = true);

            var modelMetadata = metadataProvider.GetMetadataForProperty(
                typeof(ModelWithIntArrayProperty),
                nameof(ModelWithIntArrayProperty.ArrayProperty));

            var provider = new ArrayModelBinderProvider();
            var context = new TestModelBinderProviderContext(typeof(int[]));
            context.OnCreatingBinder((m) =>
            {
                // Expect to be called with the element type to create a binder for elements.
                Assert.Equal(typeof(int), m.ModelType);
                return Mock.Of<IModelBinder>();
            });

            // Act
            var result = provider.GetBinder(context);

            // Assert
            Assert.IsType<ArrayModelBinder<int>>(result);
        }
Beispiel #11
0
        private TestModelBinderProviderContext GetTestModelBinderProviderContext(
            Type modelType,
            bool allowBindingHeaderValuesToNonStringModelTypes = true)
        {
            var context = new TestModelBinderProviderContext(modelType);
            var options = context.Services.GetRequiredService <IOptions <MvcOptions> >().Value;

            options.AllowBindingHeaderValuesToNonStringModelTypes = allowBindingHeaderValuesToNonStringModelTypes;
            return(context);
        }
Beispiel #12
0
    public void Create_ForNullableDateTime_ReturnsBinder()
    {
        // Arrange
        var context = new TestModelBinderProviderContext(typeof(DateTime?));

        // Act
        var result = _provider.GetBinder(context);

        // Assert
        Assert.IsType <DateTimeModelBinder>(result);
    }
Beispiel #13
0
    public void Create_ForNonDateTime_ReturnsNull(Type modelType)
    {
        // Arrange
        var context = new TestModelBinderProviderContext(modelType);

        // Act
        var result = _provider.GetBinder(context);

        // Assert
        Assert.Null(result);
    }
        public void Create_ForNonArrayTypes_ReturnsNull(Type modelType)
        {
            // Arrange
            var provider = new ArrayModelBinderProvider();
            var context = new TestModelBinderProviderContext(modelType);

            // Act
            var result = provider.GetBinder(context);

            // Assert
            Assert.Null(result);
        }
        public void Create_ForSimpleTypes_ReturnsBinder(Type modelType)
        {
            // Arrange
            var provider = new SimpleTypeModelBinderProvider();
            var context = new TestModelBinderProviderContext(modelType);

            // Act
            var result = provider.GetBinder(context);

            // Assert
            Assert.IsType<SimpleTypeModelBinder>(result);
        }
        public void Create_ForCollectionOrComplexTypes_ReturnsNull(Type modelType)
        {
            // Arrange
            var provider = new SimpleTypeModelBinderProvider();
            var context = new TestModelBinderProviderContext(modelType);

            // Act
            var result = provider.GetBinder(context);

            // Assert
            Assert.Null(result);
        }
Beispiel #17
0
        public void Create_ForModelTypeWithNoDefaultPublicConstructor_ReturnsNull(Type modelType)
        {
            // Arrange
            var provider = new ComplexTypeModelBinderProvider();
            var context  = new TestModelBinderProviderContext(modelType);

            // Act
            var result = provider.GetBinder(context);

            // Assert
            Assert.Null(result);
        }
Beispiel #18
0
        public void Create_ForAbstractModelTypeWithDefaultPublicConstructor_ReturnsNull()
        {
            // Arrange
            var provider = new ComplexTypeModelBinderProvider();
            var context  = new TestModelBinderProviderContext(typeof(AbstractClassWithDefaultConstructor));

            // Act
            var result = provider.GetBinder(context);

            // Assert
            Assert.Null(result);
        }
    public void ReturnsBinder_ForFlagsEnumType(Type modelType)
    {
        // Arrange
        var provider = new EnumTypeModelBinderProvider(new MvcOptions());
        var context  = new TestModelBinderProviderContext(modelType);

        // Act
        var result = provider.GetBinder(context);

        // Assert
        Assert.IsType <EnumTypeModelBinder>(result);
    }
    public void DoesNotReturnBinder_ForNonEnumTypes(Type modelType)
    {
        // Arrange
        var provider = new EnumTypeModelBinderProvider(new MvcOptions());
        var context  = new TestModelBinderProviderContext(modelType);

        // Act
        var result = provider.GetBinder(context);

        // Assert
        Assert.Null(result);
    }
    public void Create_ForTypesWithTryParse_ReturnsBinder()
    {
        // Arrange
        var provider = new TryParseModelBinderProvider();
        var context  = new TestModelBinderProviderContext(typeof(TestClassWithTryParse));

        // Act
        var result = provider.GetBinder(context);

        // Assert
        Assert.IsType <TryParseModelBinder>(result);
    }
        public void Create_ForFormCollectionToken_ReturnsBinder()
        {
            // Arrange
            var provider = new FormCollectionModelBinderProvider();
            var context = new TestModelBinderProviderContext(typeof(IFormCollection));

            // Act
            var result = provider.GetBinder(context);

            // Assert
            Assert.IsType<FormCollectionModelBinder>(result);
        }
Beispiel #23
0
        public void Create_ForCancellationToken_ReturnsBinder()
        {
            // Arrange
            var provider = new CancellationTokenModelBinderProvider();
            var context  = new TestModelBinderProviderContext(typeof(CancellationToken));

            // Act
            var result = provider.GetBinder(context);

            // Assert
            Assert.IsType <CancellationTokenModelBinder>(result);
        }
        public void Create_ForSupportedTypes_ReturnsBinder(Type modelType)
        {
            // Arrange
            var provider = new FormFileModelBinderProvider();
            var context  = new TestModelBinderProviderContext(modelType);

            // Act
            var result = provider.GetBinder(context);

            // Assert
            Assert.IsType <FormFileModelBinder>(result);
        }
Beispiel #25
0
        public void Create_ForFloatTypes_ReturnsBinder(Type modelType)
        {
            // Arrange
            var provider = new FloatingPointTypeModelBinderProvider();
            var context  = new TestModelBinderProviderContext(modelType);

            // Act
            var result = provider.GetBinder(context);

            // Assert
            Assert.IsType <FloatModelBinder>(result);
        }
    public void Create_ForFormCollectionToken_ReturnsBinder()
    {
        // Arrange
        var provider = new FormCollectionModelBinderProvider();
        var context  = new TestModelBinderProviderContext(typeof(IFormCollection));

        // Act
        var result = provider.GetBinder(context);

        // Assert
        Assert.IsType <FormCollectionModelBinder>(result);
    }
Beispiel #27
0
        public void Create_ForUnsupportedSimpleTypes_ReturnsNull(Type modelType)
        {
            // Arrange
            var provider = new FloatingPointTypeModelBinderProvider();
            var context  = new TestModelBinderProviderContext(modelType);

            // Act
            var result = provider.GetBinder(context);

            // Assert
            Assert.Null(result);
        }
    public void Create_ForByteArray_ReturnsBinder()
    {
        // Arrange
        var provider = new ByteArrayModelBinderProvider();
        var context  = new TestModelBinderProviderContext(typeof(byte[]));

        // Act
        var result = provider.GetBinder(context);

        // Assert
        Assert.IsType <ByteArrayModelBinder>(result);
    }
Beispiel #29
0
        public void Create_ForCollectionOrComplexTypes_ReturnsNull(Type modelType)
        {
            // Arrange
            var provider = new SimpleTypeModelBinderProvider();
            var context  = new TestModelBinderProviderContext(modelType);

            // Act
            var result = provider.GetBinder(context);

            // Assert
            Assert.Null(result);
        }
Beispiel #30
0
        public void Create_ForNonCancellationTokenTypes_ReturnsNull(Type modelType)
        {
            // Arrange
            var provider = new CancellationTokenModelBinderProvider();
            var context  = new TestModelBinderProviderContext(modelType);

            // Act
            var result = provider.GetBinder(context);

            // Assert
            Assert.Null(result);
        }
        public void Create_WhenBinderTypeIsNull_ReturnsNull()
        {
            // Arrange
            var provider = new BinderTypeModelBinderProvider();

            var context = new TestModelBinderProviderContext(typeof(Person));

            // Act
            var result = provider.GetBinder(context);

            // Assert
            Assert.Null(result);
        }
Beispiel #32
0
        public void Create_WhenBinderTypeIsNull_ReturnsNull()
        {
            // Arrange
            var provider = new BinderTypeModelBinderProvider();

            var context = new TestModelBinderProviderContext(typeof(Person));

            // Act
            var result = provider.GetBinder(context);

            // Assert
            Assert.Null(result);
        }
    public void Create_ThrowsException_ForFormCollectionModelType(Type modelType)
    {
        // Arrange
        var provider = new FormCollectionModelBinderProvider();
        var context  = new TestModelBinderProviderContext(modelType);

        // Act & Assert
        var exception = Assert.Throws <InvalidOperationException>(() => provider.GetBinder(context));

        Assert.Equal(
            $"The '{typeof(FormCollectionModelBinder).FullName}' cannot bind to a model of type '{modelType.FullName}'. Change the model type to '{typeof(IFormCollection).FullName}' instead.",
            exception.Message);
    }
        public void Create_ForNonDictionaryType_ReturnsNull(Type modelType)
        {
            // Arrange
            var provider = new DictionaryModelBinderProvider();

            var context = new TestModelBinderProviderContext(modelType);

            // Act
            var result = provider.GetBinder(context);

            // Assert
            Assert.Null(result);
        }
        public void Create_WhenBinderTypeIsSet_ReturnsBinder()
        {
            // Arrange
            var provider = new BinderTypeModelBinderProvider();

            var context = new TestModelBinderProviderContext(typeof(Person));
            context.BindingInfo.BinderType = typeof(NullModelBinder);

            // Act
            var result = provider.GetBinder(context);

            // Assert
            Assert.IsType<BinderTypeModelBinder>(result);
        }
        public void GetBinder_WhenBindingSourceIsFromBody_ReturnsBinder()
        {
            // Arrange
            var provider = CreateProvider(new TestInputFormatter());
            var context  = new TestModelBinderProviderContext(typeof(Person));

            context.BindingInfo.BindingSource = BindingSource.Body;

            // Act
            var result = provider.GetBinder(context);

            // Assert
            Assert.IsType <BodyModelBinder>(result);
        }
        public void Create_WhenBindingSourceIsNotFromServices_ReturnsNull(BindingSource source)
        {
            // Arrange
            var provider = new ServicesModelBinderProvider();

            var context = new TestModelBinderProviderContext(typeof(IPersonService));
            context.BindingInfo.BindingSource = source;

            // Act
            var result = provider.GetBinder(context);

            // Assert
            Assert.Null(result);
        }
        public void GetBinder_DoesNotThrowNullReferenceException()
        {
            // Arrange
            var context = new TestModelBinderProviderContext(typeof(Person));

            context.BindingInfo.BindingSource = BindingSource.Body;
            var formatter     = new TestInputFormatter();
            var formatterList = new List <IInputFormatter> {
                formatter
            };
            var provider = new BodyModelBinderProvider(formatterList, new TestHttpRequestStreamReaderFactory());

            // Act & Assert (does not throw)
            provider.GetBinder(context);
        }
        public void GetBinder_WhenNoInputFormatters_Throws()
        {
            // Arrange
            var expected = $"'{typeof(MvcOptions).FullName}.{nameof(MvcOptions.InputFormatters)}' must not be empty. " +
                           $"At least one '{typeof(IInputFormatter).FullName}' is required to bind from the body.";
            var provider = CreateProvider();
            var context  = new TestModelBinderProviderContext(typeof(Person));

            context.BindingInfo.BindingSource = BindingSource.Body;

            // Act & Assert
            var exception = Assert.Throws <InvalidOperationException>(() => provider.GetBinder(context));

            Assert.Equal(expected, exception.Message);
        }
Beispiel #40
0
        public void Create_WhenBinderTypeIsSet_ReturnsBinder()
        {
            // Arrange
            var provider = new BinderTypeModelBinderProvider();

            var context = new TestModelBinderProviderContext(typeof(Person));

            context.BindingInfo.BinderType = typeof(NullModelBinder);

            // Act
            var result = provider.GetBinder(context);

            // Assert
            Assert.IsType <BinderTypeModelBinder>(result);
        }
        public void Create_ForArrayTypes_ReturnsBinder(Type modelType)
        {
            // Arrange
            var provider = new ArrayModelBinderProvider();
            var context = new TestModelBinderProviderContext(modelType);
            context.OnCreatingBinder((m) =>
            {
                // Expect to be called with the element type to create a binder for elements.
                Assert.Equal(modelType.GetElementType(), m.ModelType);
                return Mock.Of<IModelBinder>();
            });

            // Act
            var result = provider.GetBinder(context);

            // Assert
            Assert.IsType(typeof(ArrayModelBinder<>).MakeGenericType(modelType.GetElementType()), result);
        }
        public void Create_ForNonICollectionTypes_ReadOnlyProperty_ReturnsNull(string propertyName)
        {
            // Arrange
            var provider = new CollectionModelBinderProvider();

            var metadataProvider = TestModelBinderProviderContext.CachedMetadataProvider;

            var metadata = metadataProvider.GetMetadataForProperty(typeof(ReadOnlyProperties), propertyName);
            Assert.NotNull(metadata);
            Assert.True(metadata.IsReadOnly);

            var context = new TestModelBinderProviderContext(metadata, bindingInfo: null);

            // Act
            var result = provider.GetBinder(context);

            // Assert
            Assert.Null(result);
        }
        public void Create_ForSupportedTypes_ReturnsBinder(Type modelType)
        {
            // Arrange
            var provider = new CollectionModelBinderProvider();

            var context = new TestModelBinderProviderContext(modelType);

            Type elementType = null;
            context.OnCreatingBinder(m =>
            {
                Assert.Equal(typeof(int), m.ModelType);
                elementType = m.ModelType;
                return Mock.Of<IModelBinder>();
            });

            // Act
            var result = provider.GetBinder(context);

            // Assert
            Assert.NotNull(elementType);
            Assert.IsType<CollectionModelBinder<int>>(result);
        }