public void BindModelCanHandleMultipleFilesWithSameKey()
        {
            // Arrange
            HttpPostedFileBase foo_0 = GetFile("foo_0", 100);
            HttpPostedFileBase foo_1 = GetFile("", 0); // this file shouldn't be part of the returned list since it was empty
            HttpPostedFileBase foo_2 = GetFile("foo_2", 200);

            Mock <HttpContextBase> mockHttpContext = new Mock <HttpContextBase>();

            mockHttpContext.Expect(c => c.Request.Files.AllKeys).Returns(new string[] { "foo", "FOO", "foo[0]", "Foo" });
            mockHttpContext.Expect(c => c.Request.Files[0]).Returns(foo_0);
            mockHttpContext.Expect(c => c.Request.Files[1]).Returns(foo_1);
            mockHttpContext.Expect(c => c.Request.Files[3]).Returns(foo_2);
            ControllerContext controllerContext = GetControllerContext(mockHttpContext.Object);

            FileCollectionModelBinder binder         = new FileCollectionModelBinder();
            ModelBindingContext       bindingContext = new ModelBindingContext()
            {
                ModelName = "foo", ModelType = typeof(IList <HttpPostedFileBase>)
            };

            // Act
            object result = binder.BindModel(controllerContext, bindingContext);

            // Assert
            Assert.IsInstanceOfType(result, typeof(IList <HttpPostedFileBase>));
            IList <HttpPostedFileBase> castResult = (IList <HttpPostedFileBase>)result;

            Assert.IsTrue(castResult.SequenceEqual(new HttpPostedFileBase[] { foo_0, null, foo_2 }));
        }
        private static void TestBindModelSupportForCollectionType <T>() where T : IEnumerable <HttpPostedFileBase>
        {
            // Arrange
            HttpPostedFileBase foo_0 = GetFile("foo_0", 100);
            HttpPostedFileBase foo_1 = GetFile("", 0); // this file shouldn't be part of the returned collection since it was empty
            HttpPostedFileBase foo_2 = GetFile("foo_2", 200);

            Mock <HttpContextBase> mockHttpContext = new Mock <HttpContextBase>();

            mockHttpContext.Expect(c => c.Request.Files["foo[0]"]).Returns(foo_0);
            mockHttpContext.Expect(c => c.Request.Files["foo[1]"]).Returns(foo_1);
            mockHttpContext.Expect(c => c.Request.Files["foo[2]"]).Returns(foo_2);
            ControllerContext controllerContext = GetControllerContext(mockHttpContext.Object);

            FileCollectionModelBinder binder         = new FileCollectionModelBinder();
            ModelBindingContext       bindingContext = new ModelBindingContext()
            {
                ModelName = "foo", ModelType = typeof(T)
            };

            // Act
            object result = binder.BindModel(controllerContext, bindingContext);

            // Assert
            Assert.IsInstanceOfType(result, typeof(T));
            T castResult = (T)result;

            Assert.IsTrue(castResult.SequenceEqual(new HttpPostedFileBase[] { foo_0, null, foo_2 }));
        }
        public void BindModelThrowsIfModelTypeIsNotSupportedCollectionType()
        {
            // Arrange
            HttpPostedFileBase foo_0 = GetFile("foo_0", 100);
            HttpPostedFileBase foo_1 = GetFile("", 0); // this file shouldn't be part of the returned collection since it was empty
            HttpPostedFileBase foo_2 = GetFile("foo_2", 200);

            Mock <HttpContextBase> mockHttpContext = new Mock <HttpContextBase>();

            mockHttpContext.Expect(c => c.Request.Files["foo[0]"]).Returns(foo_0);
            mockHttpContext.Expect(c => c.Request.Files["foo[1]"]).Returns(foo_1);
            mockHttpContext.Expect(c => c.Request.Files["foo[2]"]).Returns(foo_2);
            ControllerContext controllerContext = GetControllerContext(mockHttpContext.Object);

            FileCollectionModelBinder binder         = new FileCollectionModelBinder();
            ModelBindingContext       bindingContext = new ModelBindingContext()
            {
                ModelName = "foo", ModelType = typeof(string)
            };

            // Act & assert
            ExceptionHelper.ExpectInvalidOperationException(
                delegate {
                binder.BindModel(controllerContext, bindingContext);
            },
                "This model binder does not support the model type 'System.String'.");
        }
        public void BindModelThrowsIfControllerContextIsNull()
        {
            // Arrange
            FileCollectionModelBinder binder = new FileCollectionModelBinder();

            // Act & assert
            ExceptionHelper.ExpectArgumentNullException(
                delegate {
                binder.BindModel(null, null);
            }, "controllerContext");
        }
        public void BindModelThrowsIfBindingContextIsNull()
        {
            // Arrange
            FileCollectionModelBinder binder            = new FileCollectionModelBinder();
            ControllerContext         controllerContext = GetControllerContext(new Mock <HttpContextBase>().Object);

            // Act & assert
            ExceptionHelper.ExpectArgumentNullException(
                delegate {
                binder.BindModel(controllerContext, null);
            }, "bindingContext");
        }
        public void RegisterBinderPopulatesModelBinderDictionary()
        {
            // Arrange
            ModelBinderDictionary binderDict = new ModelBinderDictionary();

            // Act
            FileCollectionModelBinder.RegisterBinder(binderDict);

            // Assert
            Assert.AreEqual(6, binderDict.Count);
            Assert.IsInstanceOfType(binderDict.GetBinder(typeof(HttpPostedFileBase[])), typeof(FileCollectionModelBinder));
            Assert.IsInstanceOfType(binderDict.GetBinder(typeof(IEnumerable <HttpPostedFileBase>)), typeof(FileCollectionModelBinder));
            Assert.IsInstanceOfType(binderDict.GetBinder(typeof(ICollection <HttpPostedFileBase>)), typeof(FileCollectionModelBinder));
            Assert.IsInstanceOfType(binderDict.GetBinder(typeof(IList <HttpPostedFileBase>)), typeof(FileCollectionModelBinder));
            Assert.IsInstanceOfType(binderDict.GetBinder(typeof(Collection <HttpPostedFileBase>)), typeof(FileCollectionModelBinder));
            Assert.IsInstanceOfType(binderDict.GetBinder(typeof(List <HttpPostedFileBase>)), typeof(FileCollectionModelBinder));
        }
        public void BindModelReturnsNullIfNoFileInputElementsMatchingKeyInRequest()
        {
            // Arrange
            Mock <HttpContextBase> mockHttpContext = new Mock <HttpContextBase>();

            mockHttpContext.Expect(c => c.Request.Files.AllKeys).Returns(new string[] { "bar", "baz", "quux" });
            ControllerContext controllerContext = GetControllerContext(mockHttpContext.Object);

            FileCollectionModelBinder binder         = new FileCollectionModelBinder();
            ModelBindingContext       bindingContext = new ModelBindingContext()
            {
                ModelName = "foo"
            };

            // Act
            object result = binder.BindModel(controllerContext, bindingContext);

            // Assert
            Assert.IsNull(result);
        }