public async Task FormFileModelBinder_ExpectMultipleFiles_BindSuccessful()
        {
            // Arrange
            var formFiles      = GetTwoFiles();
            var httpContext    = GetMockHttpContext(GetMockFormCollection(formFiles));
            var bindingContext = GetBindingContext(typeof(IEnumerable <IFormFile>), httpContext);
            var binder         = new FormFileModelBinder();

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

            // Assert
            Assert.NotEqual(default(ModelBindingResult), result);
            Assert.True(result.IsModelSet);

            var entry = bindingContext.ValidationState[result.Model];

            Assert.True(entry.SuppressValidation);
            Assert.Equal("file", entry.Key);
            Assert.Null(entry.Metadata);

            var files = Assert.IsAssignableFrom <IList <IFormFile> >(result.Model);

            Assert.Equal(2, files.Count);
        }
        public async Task FormFileModelBinder_UsesFieldNameForTopLevelObject(bool isTopLevel, string expected)
        {
            // Arrange
            var formFiles = new FormFileCollection();

            formFiles.Add(GetMockFormFile("FieldName", "file1.txt"));
            formFiles.Add(GetMockFormFile("ModelName", "file1.txt"));
            var httpContext = GetMockHttpContext(GetMockFormCollection(formFiles));

            var bindingContext = GetBindingContext(typeof(IFormFile), httpContext);

            bindingContext.IsTopLevelObject = isTopLevel;
            bindingContext.FieldName        = "FieldName";
            bindingContext.ModelName        = "ModelName";

            var binder = new FormFileModelBinder();

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

            // Assert
            Assert.NotEqual(default(ModelBindingResult), result);
            Assert.True(result.IsModelSet);
            var file = Assert.IsAssignableFrom <IFormFile>(result.Model);

            Assert.Equal(expected, file.Name);
        }
        public async Task FormFileModelBinder_ReturnsNothing_ForUnsupportedDestinationTypes(Type destinationType)
        {
            // Arrange
            var formFiles      = GetTwoFiles();
            var httpContext    = GetMockHttpContext(GetMockFormCollection(formFiles));
            var bindingContext = GetBindingContext(destinationType, httpContext);
            var binder         = new FormFileModelBinder();

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

            // Assert
            Assert.Equal(default(ModelBindingResult), result);
        }
        public async Task FormFileModelBinder_ReturnsFailedResult_ForCollectionsItCannotCreate()
        {
            // Arrange
            var binder         = new FormFileModelBinder();
            var formFiles      = GetTwoFiles();
            var httpContext    = GetMockHttpContext(GetMockFormCollection(formFiles));
            var bindingContext = GetBindingContext(typeof(ISet <IFormFile>), httpContext);

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

            // Assert
            Assert.NotEqual(default(ModelBindingResult), result);
            Assert.False(result.IsModelSet);
            Assert.Null(result.Model);
        }
        public async Task FormFileModelBinder_ReturnsFailedResult_ForReadOnlyDestination()
        {
            // Arrange
            var binder         = new FormFileModelBinder();
            var formFiles      = GetTwoFiles();
            var httpContext    = GetMockHttpContext(GetMockFormCollection(formFiles));
            var bindingContext = GetBindingContextForReadOnlyArray(httpContext);

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

            // Assert
            Assert.NotEqual(default(ModelBindingResult), result);
            Assert.False(result.IsModelSet);
            Assert.Null(result.Model);
        }
        public async Task FormFileModelBinder_ExpectSingleFile_BindFirstFile()
        {
            // Arrange
            var formFiles      = GetTwoFiles();
            var httpContext    = GetMockHttpContext(GetMockFormCollection(formFiles));
            var bindingContext = GetBindingContext(typeof(IFormFile), httpContext);
            var binder         = new FormFileModelBinder();

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

            // Assert
            Assert.NotEqual(default(ModelBindingResult), result);
            var file = Assert.IsAssignableFrom <IFormFile>(result.Model);

            Assert.Equal("file1.txt", file.FileName);
        }
        public async Task FormFileModelBinder_BindsFiles_ForCollectionsItCanCreate(Type destinationType)
        {
            // Arrange
            var binder         = new FormFileModelBinder();
            var formFiles      = GetTwoFiles();
            var httpContext    = GetMockHttpContext(GetMockFormCollection(formFiles));
            var bindingContext = GetBindingContext(destinationType, httpContext);

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

            // Assert
            Assert.NotEqual(default(ModelBindingResult), result);
            Assert.True(result.IsModelSet);
            Assert.IsAssignableFrom(destinationType, result.Model);
            Assert.Equal(formFiles, result.Model as IEnumerable <IFormFile>);
        }
        public async Task FormFileModelBinder_ReturnsFailedResult_WithNoFileNameAndZeroLength()
        {
            // Arrange
            var formFiles = new FormFileCollection();

            formFiles.Add(GetMockFormFile("file", ""));
            var httpContext    = GetMockHttpContext(GetMockFormCollection(formFiles));
            var bindingContext = GetBindingContext(typeof(IFormFile), httpContext);
            var binder         = new FormFileModelBinder();

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

            // Assert
            Assert.NotEqual(default(ModelBindingResult), result);
            Assert.False(result.IsModelSet);
            Assert.Null(result.Model);
        }
        public async Task FormFileModelBinder_ReturnsFailedResult_WithEmptyContentDisposition()
        {
            // Arrange
            var formFiles = new FormFileCollection();

            formFiles.Add(new Mock <IFormFile>().Object);
            var httpContext    = GetMockHttpContext(GetMockFormCollection(formFiles));
            var bindingContext = GetBindingContext(typeof(IFormFile), httpContext);
            var binder         = new FormFileModelBinder();

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

            // Assert
            Assert.NotEqual(default(ModelBindingResult), result);
            Assert.False(result.IsModelSet);
            Assert.Null(result.Model);
        }
        public async Task FormFileModelBinder_SuppressesValidation()
        {
            // Arrange
            var formFiles = new FormFileCollection();

            formFiles.Add(GetMockFormFile("file", "file1.txt"));
            var httpContext    = GetMockHttpContext(GetMockFormCollection(formFiles));
            var bindingContext = GetBindingContext(typeof(IEnumerable <IFormFile>), httpContext);
            var binder         = new FormFileModelBinder();

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

            // Assert
            Assert.NotEqual(default(ModelBindingResult), result);
            Assert.True(result.IsModelSet);

            var entry = bindingContext.ValidationState[result.Model];

            Assert.True(entry.SuppressValidation);
            Assert.Equal("file", entry.Key);
            Assert.Null(entry.Metadata);
        }