Exemplo n.º 1
1
        public async Task FormFileModelBinder_ReturnsNoResult_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.BindModelAsync(bindingContext);

            // Assert
            Assert.NotEqual(ModelBindingResult.NoResult, result);
            Assert.Null(result.Model);
        }
Exemplo n.º 2
0
        public async Task FormFileModelBinder_ExpectMultipleFiles_BindSuccessful()
        {
            // Arrange
            var formFiles = new FormFileCollection();
            formFiles.Add(GetMockFormFile("file", "file1.txt"));
            formFiles.Add(GetMockFormFile("file", "file2.txt"));
            var httpContext = GetMockHttpContext(GetMockFormCollection(formFiles));
            var bindingContext = GetBindingContext(typeof(IEnumerable<IFormFile>), httpContext);
            var binder = new FormFileModelBinder();

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

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

            var entry = bindingContext.ValidationState[result.Model];
            Assert.True(entry.SuppressValidation);
            Assert.Null(entry.Key);
            Assert.Null(entry.Metadata);

            var files = Assert.IsAssignableFrom<IList<IFormFile>>(result.Model);
            Assert.Equal(2, files.Count);
        }
Exemplo n.º 3
0
        public async Task FormFileModelBinder_ExpectMultipleFiles_BindSuccessful()
        {
            // Arrange
            var formFiles = new FormFileCollection();

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

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

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

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

            Assert.True(entry.SuppressValidation);
            Assert.Null(entry.Key);
            Assert.Null(entry.Metadata);

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

            Assert.Equal(2, files.Count);
        }
Exemplo n.º 4
0
        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.BindModelAsync(bindingContext);

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

            ContentDispositionHeaderValue contentDisposition;

            ContentDispositionHeaderValue.TryParse(file.ContentDisposition, out contentDisposition);
            Assert.Equal(expected, contentDisposition.Name);
        }
Exemplo n.º 5
0
        public async Task FormFileModelBinder_ReturnsNoResult_WhenNoFilePosted()
        {
            // Arrange
            var formFiles      = new FormFileCollection();
            var httpContext    = GetMockHttpContext(GetMockFormCollection(formFiles));
            var bindingContext = GetBindingContext(typeof(IFormFile), httpContext);
            var binder         = new FormFileModelBinder();

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

            // Assert
            Assert.NotEqual(ModelBindingResult.NoResult, result);
            Assert.Null(result.Model);
        }
Exemplo n.º 6
0
        public async Task FormFileModelBinder_ReturnsNoResult_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.BindModelAsync(bindingContext);

            // Assert
            Assert.NotEqual(ModelBindingResult.NoResult, result);
            Assert.Null(result.Model);
        }
Exemplo n.º 7
0
        public async Task FormFileModelBinder_ReturnsNoResult_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.BindModelAsync(bindingContext);

            // Assert
            Assert.NotEqual(ModelBindingResult.NoResult, result);
            Assert.Null(result.Model);
        }
Exemplo n.º 8
0
        public async Task FormFileModelBinder_ReturnsNoResult_WhenNamesDontMatch()
        {
            // 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
            var result = await binder.BindModelAsync(bindingContext);

            // Assert
            Assert.NotEqual(ModelBindingResult.NoResult, result);
            Assert.Null(result.Model);
        }
Exemplo n.º 9
0
        public async Task FormFileModelBinder_FilesWithQuotedContentDisposition_BindSuccessful()
        {
            // Arrange
            var formFiles = new FormFileCollection();
            formFiles.Add(GetMockFormFileWithQuotedContentDisposition("file", "file1.txt"));
            formFiles.Add(GetMockFormFileWithQuotedContentDisposition("file", "file2.txt"));
            var httpContext = GetMockHttpContext(GetMockFormCollection(formFiles));
            var bindingContext = GetBindingContext(typeof(IEnumerable<IFormFile>), httpContext);
            var binder = new FormFileModelBinder();

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

            // Assert
            Assert.NotEqual(ModelBindingResult.NoResult, result);
            var files = Assert.IsAssignableFrom<IList<IFormFile>>(result.Model);
            Assert.Equal(2, files.Count);
        }
Exemplo n.º 10
0
        public async Task FormFileModelBinder_ExpectSingleFile_BindFirstFile()
        {
            // Arrange
            var formFiles = new FormFileCollection();
            formFiles.Add(GetMockFormFile("file", "file1.txt"));
            formFiles.Add(GetMockFormFile("file", "file2.txt"));
            var httpContext = GetMockHttpContext(GetMockFormCollection(formFiles));
            var bindingContext = GetBindingContext(typeof(IFormFile), httpContext);
            var binder = new FormFileModelBinder();

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

            // Assert
            Assert.NotNull(result);
            var file = Assert.IsAssignableFrom<IFormFile>(result.Model);
            Assert.Equal("form-data; name=file; filename=file1.txt",
                         file.ContentDisposition);
        }
Exemplo n.º 11
0
        public async Task FormFileModelBinder_ExpectMultipleFiles_BindSuccessful()
        {
            // Arrange
            var formFiles = new FormFileCollection();

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

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

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

            Assert.Equal(2, files.Count);
        }
Exemplo n.º 12
0
        public async Task FormFileModelBinder_FilesWithQuotedContentDisposition_BindSuccessful()
        {
            // Arrange
            var formFiles = new FormFileCollection();

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

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

            // Assert
            Assert.NotEqual(ModelBindingResult.NoResult, result);
            var files = Assert.IsAssignableFrom <IList <IFormFile> >(result.Model);

            Assert.Equal(2, files.Count);
        }
Exemplo n.º 13
0
        public async Task FormFileModelBinder_ExpectSingleFile_BindFirstFile()
        {
            // Arrange
            var formFiles = new FormFileCollection();

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

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

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

            Assert.Equal("form-data; name=file; filename=file1.txt",
                         file.ContentDisposition);
        }
Exemplo n.º 14
0
        public async Task FormFileModelBinder_ReturnsNoResult_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.BindModelAsync(bindingContext);

            // Assert
            Assert.NotEqual(ModelBindingResult.NoResult, result);
            Assert.Null(result.Model);
        }
Exemplo n.º 15
0
        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.BindModelAsync(bindingContext);

            // Assert
            Assert.NotEqual(ModelBindingResult.NoResult, result);
            Assert.True(result.IsModelSet);
            var file = Assert.IsAssignableFrom<IFormFile>(result.Model);
            
            ContentDispositionHeaderValue contentDisposition;
            ContentDispositionHeaderValue.TryParse(file.ContentDisposition, out contentDisposition);
            Assert.Equal(expected, contentDisposition.Name);
        }
Exemplo n.º 16
0
        public async Task FormFileModelBinder_ReturnsNoResult_WhenNamesDontMatch()
        {
            // 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
            var result = await binder.BindModelAsync(bindingContext);

            // Assert
            Assert.NotEqual(ModelBindingResult.NoResult, result);
            Assert.Null(result.Model);
        }
Exemplo n.º 17
0
        public async Task FormFileModelBinder_ReturnsNoResult_WhenNoFilePosted()
        {
            // Arrange
            var formFiles = new FormFileCollection();
            var httpContext = GetMockHttpContext(GetMockFormCollection(formFiles));
            var bindingContext = GetBindingContext(typeof(IFormFile), httpContext);
            var binder = new FormFileModelBinder();

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

            // Assert
            Assert.NotEqual(ModelBindingResult.NoResult, result);
            Assert.Null(result.Model);
        }