Beispiel #1
0
        public async Task BindModel_CallsSelectedInputFormatterOnce()
        {
            // Arrange
            var mockInputFormatter = new Mock <IInputFormatter>();

            mockInputFormatter.Setup(f => f.CanRead(It.IsAny <InputFormatterContext>()))
            .Returns(true)
            .Verifiable();
            mockInputFormatter.Setup(o => o.ReadAsync(It.IsAny <InputFormatterContext>()))
            .Returns(InputFormatterResult.SuccessAsync(new Person()))
            .Verifiable();
            var inputFormatter = mockInputFormatter.Object;

            var provider = new TestModelMetadataProvider();

            provider.ForType <Person>().BindingDetails(d => d.BindingSource = BindingSource.Body);

            var bindingContext = GetBindingContext(
                typeof(Person),
                new[] { inputFormatter },
                metadataProvider: provider);

            var binder = new BodyModelBinder();

            // Act
            var binderResult = await binder.BindModelAsync(bindingContext);

            // Assert
            mockInputFormatter.Verify(v => v.CanRead(It.IsAny <InputFormatterContext>()), Times.Once);
            mockInputFormatter.Verify(v => v.ReadAsync(It.IsAny <InputFormatterContext>()), Times.Once);
            Assert.NotNull(binderResult);
            Assert.True(binderResult.IsModelSet);
        }
        public async Task BindModel_CallsSelectedInputFormatterOnce()
        {
            // Arrange
            var mockInputFormatter = new Mock<IInputFormatter>();
            mockInputFormatter.Setup(f => f.CanRead(It.IsAny<InputFormatterContext>()))
                .Returns(true)
                .Verifiable();
            mockInputFormatter.Setup(o => o.ReadAsync(It.IsAny<InputFormatterContext>()))
                              .Returns(InputFormatterResult.SuccessAsync(new Person()))
                              .Verifiable();
            var inputFormatter = mockInputFormatter.Object;

            var provider = new TestModelMetadataProvider();
            provider.ForType<Person>().BindingDetails(d => d.BindingSource = BindingSource.Body);

            var bindingContext = GetBindingContext(
                typeof(Person),
                new[] { inputFormatter },
                metadataProvider: provider);

            var binder = new BodyModelBinder();

            // Act
            var binderResult = await binder.BindModelAsync(bindingContext);

            // Assert
            mockInputFormatter.Verify(v => v.CanRead(It.IsAny<InputFormatterContext>()), Times.Once);
            mockInputFormatter.Verify(v => v.ReadAsync(It.IsAny<InputFormatterContext>()), Times.Once);
            Assert.NotNull(binderResult);
            Assert.True(binderResult.IsModelSet);
        }
Beispiel #3
0
        public async Task BindModel_CallsSelectedInputFormatterOnce()
        {
            // Arrange
            var mockInputFormatter = new Mock <IInputFormatter>();

            mockInputFormatter.Setup(f => f.CanRead(It.IsAny <InputFormatterContext>()))
            .Returns(true)
            .Verifiable();
            mockInputFormatter.Setup(o => o.ReadAsync(It.IsAny <InputFormatterContext>()))
            .Returns(Task.FromResult <object>(new Person()))
            .Verifiable();
            var inputFormatter = mockInputFormatter.Object;

            var provider = new TestModelMetadataProvider();

            provider.ForType <Person>().BindingDetails(d => d.BindingSource = BindingSource.Body);

            var bindingContext = GetBindingContext(
                typeof(Person),
                new[] { inputFormatter },
                metadataProvider: provider);

            var binder = new BodyModelBinder();

            // Act
            var binderResult = await binder.BindModelAsync(bindingContext);

            // Assert
            mockInputFormatter.Verify(v => v.CanRead(It.IsAny <InputFormatterContext>()), Times.Once);
            mockInputFormatter.Verify(v => v.ReadAsync(It.IsAny <InputFormatterContext>()), Times.Once);
            Assert.NotNull(binderResult);
            Assert.True(binderResult.IsModelSet);
            Assert.NotNull(binderResult.ValidationNode);
            Assert.True(binderResult.ValidationNode.ValidateAllProperties);
            Assert.False(binderResult.ValidationNode.SuppressValidation);
            Assert.Empty(binderResult.ValidationNode.ChildNodes);
            Assert.Equal(binderResult.Key, binderResult.ValidationNode.Key);
            Assert.Equal(bindingContext.ModelMetadata, binderResult.ValidationNode.ModelMetadata);
            Assert.Same(binderResult.Model, binderResult.ValidationNode.Model);
        }
Beispiel #4
0
        public async Task BindModel_CallsSelectedInputFormatterOnce()
        {
            // Arrange
            var mockInputFormatter = new Mock<IInputFormatter>();
            mockInputFormatter.Setup(f => f.CanRead(It.IsAny<InputFormatterContext>()))
                .Returns(true)
                .Verifiable();
            mockInputFormatter.Setup(o => o.ReadAsync(It.IsAny<InputFormatterContext>()))
                              .Returns(Task.FromResult<object>(new Person()))
                              .Verifiable();
            var inputFormatter = mockInputFormatter.Object;

            var provider = new TestModelMetadataProvider();
            provider.ForType<Person>().BindingDetails(d => d.BindingSource = BindingSource.Body);

            var bindingContext = GetBindingContext(
                typeof(Person),
                new[] { inputFormatter },
                metadataProvider: provider);

            var binder = new BodyModelBinder();

            // Act
            var binderResult = await binder.BindModelAsync(bindingContext);

            // Assert
            mockInputFormatter.Verify(v => v.CanRead(It.IsAny<InputFormatterContext>()), Times.Once);
            mockInputFormatter.Verify(v => v.ReadAsync(It.IsAny<InputFormatterContext>()), Times.Once);
            Assert.NotNull(binderResult);
            Assert.True(binderResult.IsModelSet);
            Assert.NotNull(binderResult.ValidationNode);
            Assert.True(binderResult.ValidationNode.ValidateAllProperties);
            Assert.False(binderResult.ValidationNode.SuppressValidation);
            Assert.Empty(binderResult.ValidationNode.ChildNodes);
            Assert.Equal(binderResult.Key, binderResult.ValidationNode.Key);
            Assert.Equal(bindingContext.ModelMetadata, binderResult.ValidationNode.ModelMetadata);
            Assert.Same(binderResult.Model, binderResult.ValidationNode.Model);
        }
        private static BodyModelBinder GetBodyBinder(HttpContext httpContext, IInputFormatter inputFormatter)
        {
            var actionContext = CreateActionContext(httpContext);
            var inputFormatterSelector = new Mock<IInputFormatterSelector>();
            inputFormatterSelector
                .Setup(o => o.SelectFormatter(
                    It.IsAny<IReadOnlyList<IInputFormatter>>(),
                    It.IsAny<InputFormatterContext>()))
                .Returns(inputFormatter);

            var bodyValidationPredicatesProvider = new Mock<IValidationExcludeFiltersProvider>();
            bodyValidationPredicatesProvider.SetupGet(o => o.ExcludeFilters)
                                             .Returns(new List<IExcludeTypeValidationFilter>());

            var bindingContext = new ActionBindingContext()
            {
                InputFormatters = new List<IInputFormatter>(),
            };

            var bindingContextAccessor = new MockScopedInstance<ActionBindingContext>()
            {
                Value = bindingContext,
            };

            var binder = new BodyModelBinder(
                actionContext,
                bindingContextAccessor,
                inputFormatterSelector.Object,
                bodyValidationPredicatesProvider.Object);

            return binder;
        }