Ejemplo n.º 1
0
        public void CleanFileTest()
        {
            using (var file = File.Open(testFileValid, FileMode.Open, FileAccess.Read))
            {
                var postedFile = new Mock <HttpPostedFileBase>();
                postedFile.Setup(foo => foo.FileName).Returns(Path.GetFileName(file.Name));
                postedFile.Setup(foo => foo.ContentType).Returns("image/png");
                postedFile.Setup(foo => foo.ContentLength).Returns((int)file.Length);
                postedFile.Setup(foo => foo.InputStream).Returns(file);

                var target = new ImageField();

                Assert.AreSame(postedFile.Object, target.Clean(postedFile.Object));

                // Test MaxWidth
                target.MaxWidth = 600;
                AssertExtras.Raises <ValidationException>(delegate()
                {
                    target.Clean(postedFile.Object);
                }).WithMessage("Ensure this image is no more than 600px wide (it is 640px).");

                target.MaxWidth = 640;
                Assert.AreSame(postedFile.Object, target.Clean(postedFile.Object));

                // Test MaxHeight
                target.MaxHeight = 400;
                AssertExtras.Raises <ValidationException>(delegate()
                {
                    target.Clean(postedFile.Object);
                }).WithMessage("Ensure this image is no more than 400px high (it is 480px).");

                target.MaxHeight = 480;
                Assert.AreSame(postedFile.Object, target.Clean(postedFile.Object));
            }
        }
Ejemplo n.º 2
0
        public void CleanFileTest1()
        {
            using (var file = File.Open(testFileInvalid, FileMode.Open, FileAccess.Read))
            {
                var postedFile = new Mock <HttpPostedFileBase>();
                postedFile.Setup(foo => foo.FileName).Returns(Path.GetFileName(file.Name));
                postedFile.Setup(foo => foo.ContentType).Returns("text/plain");
                postedFile.Setup(foo => foo.ContentLength).Returns((int)file.Length);
                postedFile.Setup(foo => foo.InputStream).Returns(file);

                var target = new ImageField();

                // Test MaxSize property
                AssertExtras.Raises <ValidationException>(delegate()
                {
                    target.Clean(postedFile.Object);
                }).WithMessage("Upload a valid image. The file you uploaded was either not an image or a corrupted image.");
            }
        }