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);
        }
Пример #2
0
        public async Task FormFileModelBinder_UsesFieldNameForTopLevelObject(bool isTopLevel, string expected)
        {
            // Arrange
            var formFiles = new FormFileCollection
            {
                GetMockFormFile("FieldName", "file1.txt"),
                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(NullLoggerFactory.Instance);

            // Act
            await binder.BindModelAsync(bindingContext);

            // Assert
            Assert.True(bindingContext.Result.IsModelSet);
            var file = Assert.IsAssignableFrom <IFormFile>(bindingContext.Result.Model);

            Assert.Equal(expected, file.Name);
        }
        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.False(entry.SuppressValidation);
            Assert.Equal("file", entry.Key);
            Assert.Null(entry.Metadata);

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

            Assert.Equal(2, files.Count);
        }
Пример #4
0
        public async Task FormFileModelBinder_ReturnsResult_ForReadOnlyDestination()
        {
            // Arrange
            var binder         = new FormFileModelBinder(NullLoggerFactory.Instance);
            var formFiles      = GetTwoFiles();
            var httpContext    = GetMockHttpContext(GetMockFormCollection(formFiles));
            var bindingContext = GetBindingContextForReadOnlyArray(httpContext);

            // Act
            await binder.BindModelAsync(bindingContext);

            // Assert
            Assert.True(bindingContext.Result.IsModelSet);
            Assert.NotNull(bindingContext.Result.Model);
        }
Пример #5
0
        public async Task FormFileModelBinder_ReturnsFailedResult_ForCollectionsItCannotCreate()
        {
            // Arrange
            var binder         = new FormFileModelBinder(NullLoggerFactory.Instance);
            var formFiles      = GetTwoFiles();
            var httpContext    = GetMockHttpContext(GetMockFormCollection(formFiles));
            var bindingContext = GetBindingContext(typeof(ISet <IFormFile>), httpContext);

            // Act
            await binder.BindModelAsync(bindingContext);

            // Assert
            Assert.False(bindingContext.Result.IsModelSet);
            Assert.Null(bindingContext.Result.Model);
        }
Пример #6
0
        public async Task FormFileModelBinder_ReturnsFailedResult_WhenNoFilePosted()
        {
            // Arrange
            var formFiles      = new FormFileCollection();
            var httpContext    = GetMockHttpContext(GetMockFormCollection(formFiles));
            var bindingContext = GetBindingContext(typeof(IFormFile), httpContext);
            var binder         = new FormFileModelBinder();

            // Act
            await binder.BindModelAsync(bindingContext);

            // Assert
            Assert.False(bindingContext.Result.IsModelSet);
            Assert.Null(bindingContext.Result.Model);
        }
        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);
        }
Пример #9
0
        public async Task FormFileModelBinder_BindsFiles_ForCollectionsItCanCreate(Type destinationType)
        {
            // Arrange
            var binder         = new FormFileModelBinder(NullLoggerFactory.Instance);
            var formFiles      = GetTwoFiles();
            var httpContext    = GetMockHttpContext(GetMockFormCollection(formFiles));
            var bindingContext = GetBindingContext(destinationType, httpContext);

            // Act
            await binder.BindModelAsync(bindingContext);

            // Assert
            Assert.True(bindingContext.Result.IsModelSet);
            Assert.IsAssignableFrom(destinationType, bindingContext.Result.Model);
            Assert.Equal(formFiles, bindingContext.Result.Model as IEnumerable <IFormFile>);
        }
Пример #10
0
        public async Task FormFileModelBinder_ReturnsFailedResult_WhenNamesDoNotMatch()
        {
            // Arrange
            var formFiles = new FormFileCollection();

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

            // Act
            await binder.BindModelAsync(bindingContext);

            // Assert
            Assert.False(bindingContext.Result.IsModelSet);
            Assert.Null(bindingContext.Result.Model);
        }
Пример #11
0
        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>);
        }
Пример #12
0
        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
            await binder.BindModelAsync(bindingContext);

            // Assert
            Assert.False(bindingContext.Result.IsModelSet);
            Assert.Null(bindingContext.Result.Model);
        }
Пример #13
0
        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
            await binder.BindModelAsync(bindingContext);

            // Assert
            Assert.False(bindingContext.Result.IsModelSet);
            Assert.Null(bindingContext.Result.Model);
        }
Пример #14
0
        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);
        }
Пример #15
0
        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(NullLoggerFactory.Instance);

            // Act
            await binder.BindModelAsync(bindingContext);

            // Assert
            Assert.True(bindingContext.Result.IsModelSet);
            var file = Assert.IsAssignableFrom <IFormFile>(bindingContext.Result.Model);

            Assert.Equal("file1.txt", file.FileName);
        }
Пример #16
0
        public async Task FormFileModelBinder_SingleFileWithinTopLevelPoco_BindSuccessfully_WithShortenedModelName()
        {
            // Arrange
            const string propertyName = nameof(NestedFormFiles.Files);
            var          formFiles    = new FormFileCollection
            {
                GetMockFormFile($"FileList.{propertyName}", "file1.txt")
            };

            var httpContext = GetMockHttpContext(GetMockFormCollection(formFiles));
            var binder      = new FormFileModelBinder(NullLoggerFactory.Instance);

            // Mimic ParameterBinder overwriting ModelName on top level model then ComplexTypeModelBinder entering a
            // nested context for the NestedFormFiles property. In this non-top-level binding case, FormFileModelBinder
            // tries ModelName then falls back to add an (OriginalModelName + ".") prefix.
            var propertyInfo = typeof(NestedFormFiles).GetProperty(propertyName);
            var metadata     = new EmptyModelMetadataProvider().GetMetadataForProperty(
                propertyInfo,
                propertyInfo.PropertyType);
            var bindingContext = DefaultModelBindingContext.CreateBindingContext(
                new ActionContext {
                HttpContext = httpContext
            },
                Mock.Of <IValueProvider>(),
                metadata,
                bindingInfo: null,
                modelName: "FileList");

            bindingContext.IsTopLevelObject = false;
            bindingContext.Model            = new FileList();
            bindingContext.ModelName        = propertyName;

            // Act
            await binder.BindModelAsync(bindingContext);

            // Assert
            Assert.True(bindingContext.Result.IsModelSet);

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

            Assert.False(entry.SuppressValidation);
            Assert.Equal($"FileList.{propertyName}", entry.Key);
            Assert.Null(entry.Metadata);
        }
Пример #17
0
        public async Task FormFileModelBinder_SingleFileWithinTopLevelPoco_BindSuccessfully()
        {
            // Arrange
            const string propertyName = nameof(NestedFormFiles.Files);
            var          formFiles    = new FormFileCollection
            {
                GetMockFormFile($"{propertyName}", "file1.txt")
            };

            var httpContext = GetMockHttpContext(GetMockFormCollection(formFiles));
            var binder      = new FormFileModelBinder(NullLoggerFactory.Instance);

            // In this non-top-level binding case, FormFileModelBinder tries ModelName and succeeds.
            var propertyInfo = typeof(NestedFormFiles).GetProperty(propertyName);
            var metadata     = new EmptyModelMetadataProvider().GetMetadataForProperty(
                propertyInfo,
                propertyInfo.PropertyType);
            var bindingContext = DefaultModelBindingContext.CreateBindingContext(
                new ActionContext {
                HttpContext = httpContext
            },
                Mock.Of <IValueProvider>(),
                metadata,
                bindingInfo: null,
                modelName: "FileList");

            bindingContext.IsTopLevelObject = false;
            bindingContext.Model            = new FileList();
            bindingContext.ModelName        = propertyName;

            // Act
            await binder.BindModelAsync(bindingContext);

            // Assert
            Assert.True(bindingContext.Result.IsModelSet);

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

            Assert.False(entry.SuppressValidation);
            Assert.Equal($"{propertyName}", entry.Key);
            Assert.Null(entry.Metadata);
        }
Пример #18
0
        public async Task FormFileModelBinder_SingleFile_BindSuccessful()
        {
            // 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
            await binder.BindModelAsync(bindingContext);

            // Assert
            Assert.True(bindingContext.Result.IsModelSet);

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

            Assert.False(entry.SuppressValidation);
            Assert.Equal("file", entry.Key);
            Assert.Null(entry.Metadata);
        }
Пример #19
0
        public async Task FormFileModelBinder_SingleFileWithinTopLevelDictionary_BindSuccessfully_WithShortenedModelName()
        {
            // Arrange
            var formFiles = new FormFileCollection
            {
                GetMockFormFile("FileDictionary[myFile]", "file1.txt")
            };

            var httpContext = GetMockHttpContext(GetMockFormCollection(formFiles));
            var binder      = new FormFileModelBinder(NullLoggerFactory.Instance);

            // Mimic ParameterBinder overwriting ModelName on top level model then DictionaryModelBinder entering a
            // nested context for the KeyValuePair.Value property. In this non-top-level binding case,
            // FormFileModelBinder tries ModelName then falls back to add an OriginalModelName prefix.
            var bindingContext = DefaultModelBindingContext.CreateBindingContext(
                new ActionContext {
                HttpContext = httpContext
            },
                Mock.Of <IValueProvider>(),
                new EmptyModelMetadataProvider().GetMetadataForType(typeof(IFormFile)),
                bindingInfo: null,
                modelName: "FileDictionary");

            bindingContext.IsTopLevelObject = false;
            bindingContext.ModelName        = "[myFile]";

            // Act
            await binder.BindModelAsync(bindingContext);

            // Assert
            Assert.True(bindingContext.Result.IsModelSet);

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

            Assert.False(entry.SuppressValidation);
            Assert.Equal("FileDictionary[myFile]", entry.Key);
            Assert.Null(entry.Metadata);
        }
Пример #20
0
        public async Task FormFileModelBinder_SingleFileAtTopLevel_BindSuccessfully_WithEmptyModelName()
        {
            // Arrange
            var formFiles = new FormFileCollection
            {
                GetMockFormFile("file", "file1.txt")
            };

            var httpContext = GetMockHttpContext(GetMockFormCollection(formFiles));
            var binder      = new FormFileModelBinder(NullLoggerFactory.Instance);

            // Mimic ParameterBinder overwriting ModelName on top level model. In this top-level binding case,
            // FormFileModelBinder uses FieldName from the get-go. (OriginalModelName will be checked but ignored.)
            var bindingContext = DefaultModelBindingContext.CreateBindingContext(
                new ActionContext {
                HttpContext = httpContext
            },
                Mock.Of <IValueProvider>(),
                new EmptyModelMetadataProvider().GetMetadataForType(typeof(IFormFile)),
                bindingInfo: null,
                modelName: "file");

            bindingContext.ModelName = string.Empty;

            // Act
            await binder.BindModelAsync(bindingContext);

            // Assert
            Assert.True(bindingContext.Result.IsModelSet);

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

            Assert.False(entry.SuppressValidation);
            Assert.Equal("file", entry.Key);
            Assert.Null(entry.Metadata);
        }
Пример #21
0
        public async Task FormFileModelBinder_SingleFileWithinTopLevelDictionary_BindSuccessfully()
        {
            // Arrange
            var formFiles = new FormFileCollection
            {
                GetMockFormFile("[myFile]", "file1.txt")
            };

            var httpContext = GetMockHttpContext(GetMockFormCollection(formFiles));
            var binder      = new FormFileModelBinder(NullLoggerFactory.Instance);

            // In this non-top-level binding case, FormFileModelBinder tries ModelName and succeeds.
            var bindingContext = DefaultModelBindingContext.CreateBindingContext(
                new ActionContext {
                HttpContext = httpContext
            },
                Mock.Of <IValueProvider>(),
                new EmptyModelMetadataProvider().GetMetadataForType(typeof(IFormFile)),
                bindingInfo: null,
                modelName: "FileDictionary");

            bindingContext.IsTopLevelObject = false;
            bindingContext.ModelName        = "[myFile]";

            // Act
            await binder.BindModelAsync(bindingContext);

            // Assert
            Assert.True(bindingContext.Result.IsModelSet);

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

            Assert.False(entry.SuppressValidation);
            Assert.Equal("[myFile]", entry.Key);
            Assert.Null(entry.Metadata);
        }