Пример #1
0
        public async Task BindModelAsync_ReturnsNonEmptyResult_ForAllTypes_WithHeaderBindingSource(Type type)
        {
            // Arrange
            var binder = new HeaderModelBinder();
            var modelBindingContext = GetBindingContext(type);

            // Act
            var result = await binder.BindModelAsync(modelBindingContext);

            // Assert
            Assert.NotEqual(ModelBindingResult.NoResult, result);
        }
        public async Task BindModelAsync_ReturnsNotNull_ForAllTypes(Type type)
        {
            // Arrange
            var binder = new HeaderModelBinder();
            var modelBindingContext = GetBindingContext(type);

            // Act
            var result = await binder.BindModelAsync(modelBindingContext);

            // Assert
            Assert.NotNull(result);
        }
Пример #3
0
        public async Task BindModelAsync_ReturnsNonEmptyResult_ForAllTypes_WithHeaderBindingSource(Type type)
        {
            // Arrange
            var binder = new HeaderModelBinder();
            var modelBindingContext = GetBindingContext(type);

            // Act
            var result = await binder.BindModelAsync(modelBindingContext);

            // Assert
            Assert.NotEqual(ModelBindingResult.NoResult, result);
        }
Пример #4
0
        public async Task BindModelAsync_ReturnsTrue_ForAllTypes(Type type)
        {
            // Arrange
            var binder = new HeaderModelBinder();
            var modelBindingContext = GetBindingContext(type);

            // Act
            var result = await binder.BindModelAsync(modelBindingContext);

            // Assert
            Assert.NotNull(result);
        }
Пример #5
0
        public async Task HeaderBinder_BindsHeaders_ToStringType()
        {
            // Arrange
            var type = typeof(string);
            var header = "User-Agent";
            var headerValue = "UnitTest";
            var binder = new HeaderModelBinder();
            var modelBindingContext = GetBindingContext(type);

            modelBindingContext.FieldName = header;
            modelBindingContext.OperationBindingContext.HttpContext.Request.Headers.Add(header, new[] { headerValue });

            // Act
            var result = await binder.BindModelAsync(modelBindingContext);

            // Assert
            Assert.NotEqual(ModelBindingResult.NoResult, result);
            Assert.Equal(headerValue, result.Model);
        }
Пример #6
0
        public async Task HeaderBinder_BindsHeaders_ToStringCollection()
        {
            // Arrange
            var type = typeof(string[]);
            var header = "Accept";
            var headerValue = "application/json,text/json";
            var binder = new HeaderModelBinder();
            var modelBindingContext = GetBindingContext(type);

            modelBindingContext.FieldName = header;
            modelBindingContext.OperationBindingContext.HttpContext.Request.Headers.Add(header, new[] { headerValue });

            // Act
            var result = await binder.BindModelAsync(modelBindingContext);

            // Assert
            Assert.NotEqual(ModelBindingResult.NoResult, result);
            Assert.Equal(headerValue.Split(','), result.Model);
        }
Пример #7
0
        public async Task HeaderBinder_BindsHeaders_ToStringType()
        {
            // Arrange
            var type                = typeof(string);
            var header              = "User-Agent";
            var headerValue         = "UnitTest";
            var binder              = new HeaderModelBinder();
            var modelBindingContext = GetBindingContext(type);

            modelBindingContext.FieldName = header;
            modelBindingContext.OperationBindingContext.HttpContext.Request.Headers.Add(header, new[] { headerValue });

            // Act
            var result = await binder.BindModelAsync(modelBindingContext);

            // Assert
            Assert.NotEqual(ModelBindingResult.NoResult, result);
            Assert.Equal(headerValue, result.Model);
        }
Пример #8
0
        public async Task HeaderBinder_BindsHeaders_ToStringCollection()
        {
            // Arrange
            var type                = typeof(string[]);
            var header              = "Accept";
            var headerValue         = "application/json,text/json";
            var binder              = new HeaderModelBinder();
            var modelBindingContext = GetBindingContext(type);

            modelBindingContext.FieldName = header;
            modelBindingContext.OperationBindingContext.HttpContext.Request.Headers.Add(header, new[] { headerValue });

            // Act
            var result = await binder.BindModelAsync(modelBindingContext);

            // Assert
            Assert.NotEqual(ModelBindingResult.NoResult, result);
            Assert.Equal(headerValue.Split(','), result.Model);
        }
Пример #9
0
    public async Task HeaderBinder_BindsHeaders_ForCollectionsItCanCreate_WithoutInnerModelBinder(
        Type destinationType)
    {
        // Arrange
        var header         = "Accept";
        var headerValue    = "application/json,text/json";
        var binder         = new HeaderModelBinder(NullLoggerFactory.Instance);
        var bindingContext = CreateContext(destinationType);

        bindingContext.FieldName = header;
        bindingContext.HttpContext.Request.Headers.Add(header, new[] { headerValue });

        // Act
        await binder.BindModelAsync(bindingContext);

        // Assert
        Assert.True(bindingContext.Result.IsModelSet);
        Assert.IsAssignableFrom(destinationType, bindingContext.Result.Model);
        Assert.Equal(headerValue.Split(','), bindingContext.Result.Model as IEnumerable <string>);
    }
Пример #10
0
    public async Task HeaderBinder_BindsHeaders_ToStringType_WithoutInnerModelBinder()
    {
        // Arrange
        var type           = typeof(string);
        var header         = "User-Agent";
        var headerValue    = "UnitTest";
        var bindingContext = CreateContext(type);

        var binder = new HeaderModelBinder(NullLoggerFactory.Instance);

        bindingContext.FieldName = header;
        bindingContext.HttpContext.Request.Headers.Add(header, new[] { headerValue });

        // Act
        await binder.BindModelAsync(bindingContext);

        // Assert
        Assert.True(bindingContext.Result.IsModelSet);
        Assert.Equal(headerValue, bindingContext.Result.Model);
    }
Пример #11
0
    public async Task HeaderBinder_BindsHeaders_ToStringCollection_WithoutInnerModelBinder()
    {
        // Arrange
        var binder      = new HeaderModelBinder(NullLoggerFactory.Instance);
        var type        = typeof(string[]);
        var header      = "Accept";
        var headerValue = "application/json,text/json";

        var bindingContext = CreateContext(type);

        bindingContext.FieldName = header;
        bindingContext.HttpContext.Request.Headers.Add(header, new[] { headerValue });

        // Act
        await binder.BindModelAsync(bindingContext);

        // Assert
        Assert.True(bindingContext.Result.IsModelSet);
        Assert.Equal(headerValue.Split(','), bindingContext.Result.Model);
    }
Пример #12
0
        public async Task HeaderBinder_BindsHeaders_ForCollectionsItCanCreate(Type destinationType)
        {
            // Arrange
            var header              = "Accept";
            var headerValue         = "application/json,text/json";
            var binder              = new HeaderModelBinder();
            var modelBindingContext = GetBindingContext(destinationType);

            modelBindingContext.FieldName = header;
            modelBindingContext.OperationBindingContext.HttpContext.Request.Headers.Add(header, new[] { headerValue });

            // Act
            var result = await binder.BindModelResultAsync(modelBindingContext);

            // Assert
            Assert.NotEqual(default(ModelBindingResult), result);
            Assert.True(result.IsModelSet);
            Assert.IsAssignableFrom(destinationType, result.Model);
            Assert.Equal(headerValue.Split(','), result.Model as IEnumerable <string>);
        }
Пример #13
0
        public async Task HeaderBinder_ReturnsFailedResult_ForCollectionsItCannotCreate()
        {
            // Arrange
            var header              = "Accept";
            var headerValue         = "application/json,text/json";
            var binder              = new HeaderModelBinder();
            var modelBindingContext = GetBindingContext(typeof(ISet <string>));

            modelBindingContext.FieldName = header;
            modelBindingContext.OperationBindingContext.HttpContext.Request.Headers.Add(header, new[] { headerValue });

            // Act
            var result = await binder.BindModelResultAsync(modelBindingContext);

            // Assert
            Assert.NotEqual(default(ModelBindingResult), result);
            Assert.False(result.IsModelSet);
            Assert.Equal("modelName", result.Key);
            Assert.Null(result.Model);
        }
Пример #14
0
        public async Task HeaderBinder_ReturnsNothing_ForNonHeaderBindingSource()
        {
            // Arrange
            var type        = typeof(string);
            var header      = "User-Agent";
            var headerValue = "UnitTest";

            var binder = new HeaderModelBinder();
            var modelBindingContext = GetBindingContext(type);

            modelBindingContext.BindingSource = BindingSource.Body;

            modelBindingContext.FieldName = header;
            modelBindingContext.OperationBindingContext.HttpContext.Request.Headers.Add(header, new[] { headerValue });

            // Act
            var result = await binder.BindModelResultAsync(modelBindingContext);

            // Assert
            Assert.Equal(default(ModelBindingResult), result);
        }