Esempio n. 1
0
        public void EmptyBufferSetMultipleTimes()
        {
            var f = new DiskFileUpload("d1", "d1", "application/json", null, null, 100);

            f.SetContent(Unpooled.Empty);

            Assert.NotNull(f.GetFile());
            Assert.Equal(0, f.GetFile().Length);
            f.SetContent(Unpooled.Empty);
            Assert.NotNull(f.GetFile());
            Assert.Equal(0, f.GetFile().Length);
            f.Delete();
        }
Esempio n. 2
0
        public void NonEmptyBufferSetMultipleTimes()
        {
            var f = new DiskFileUpload("d1", "d1", "application/json", null, null, 100);

            f.SetContent(Unpooled.WrappedBuffer(new byte[] { 1, 2, 3, 4 }));

            Assert.NotNull(f.GetFile());
            Assert.Equal(4, f.GetFile().Length);
            f.SetContent(Unpooled.WrappedBuffer(new byte[] { 1, 2 }));
            Assert.NotNull(f.GetFile());
            Assert.Equal(2, f.GetFile().Length);
            f.Delete();
        }
Esempio n. 3
0
        public void SetContentFromInputStream()
        {
            string         json = "{\"hello\":\"world\",\"foo\":\"bar\"}";
            DiskFileUpload f1   = new DiskFileUpload("file3", "file3", "application/json", null, null, 0);

            try
            {
                byte[] bytes = Encoding.UTF8.GetBytes(json);
                var    buf   = Unpooled.WrappedBuffer(bytes);
                var    bs    = new ByteBufferStream(buf);
                try
                {
                    f1.SetContent(bs);
                    Assert.Equal(json, f1.GetString());
                    Assert.True(bytes.AsSpan().SequenceEqual(f1.GetBytes()));
                    var file = f1.GetFile();
                    Assert.Equal(bytes.Length, file.Length);
                    Assert.True(bytes.AsSpan().SequenceEqual(DoReadFile(file, bytes.Length)));
                }
                finally
                {
                    buf.Release();
                }
            }
            finally
            {
                f1.Delete();
            }
        }
Esempio n. 4
0
        public void SetSetContentFromFileExceptionally()
        {
            long           maxSize = 4;
            DiskFileUpload f1      = new DiskFileUpload("file5", "file5", "application/json", null, null, 0);

            f1.MaxSize = maxSize;
            try
            {
                f1.SetContent(Unpooled.WrappedBuffer(new byte[(int)maxSize]));
                var originalFile = f1.GetFile();
                Assert.NotNull(originalFile);
                Assert.Equal(maxSize, originalFile.Length);
                Assert.Equal(maxSize, f1.Length);
                byte[] bytes = new byte[8];
                (new Random()).NextBytes(bytes);

                var tmpFs = File.Create(Path.GetTempFileName(), 4096, FileOptions.DeleteOnClose);
                try
                {
                    tmpFs.Write(bytes, 0, bytes.Length);
                    tmpFs.Position = 0;
                    f1.SetContent(tmpFs);
                    Assert.False(true); // should not reach here!
                }
                catch (IOException)
                {
                    Assert.NotNull(f1.GetFile());
                    Assert.NotEqual(originalFile, f1.GetFile());
                    Assert.Equal(maxSize, f1.Length);
                }
            }
            finally
            {
                f1.Delete();
            }
        }
Esempio n. 5
0
        public void SpecificCustomBaseDir()
        {
            var baseDir = "target/DiskFileUploadTest/testSpecificCustomBaseDir";
            var fullDir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, baseDir);

            if (!Directory.Exists(fullDir))
            {
                Directory.CreateDirectory(fullDir); // we don't need to clean it since it is in volatile files anyway
            }
            DiskFileUpload f =
                new DiskFileUpload("d1", "d1", "application/json", null, null, 100,
                                   fullDir, false);

            f.SetContent(Unpooled.Empty);

            Assert.StartsWith(fullDir.TrimEnd('\\', '/').Replace(@"\", "/"), f.GetFile().Name.Replace(@"\", "/"));
            Assert.True(File.Exists(f.GetFile().Name));
            Assert.Equal(0, f.GetFile().Length);
            f.Delete();
        }
Esempio n. 6
0
        public void SetContentFromByteBuf()
        {
            DiskFileUpload f1 = new DiskFileUpload("file2", "file2", "application/json", null, null, 0);

            try
            {
                string json  = "{\"hello\":\"world\"}";
                byte[] bytes = Encoding.UTF8.GetBytes(json);
                f1.SetContent(Unpooled.WrappedBuffer(bytes));
                Assert.Equal(json, f1.GetString());
                Assert.True(bytes.AsSpan().SequenceEqual(f1.GetBytes()));
                var file = f1.GetFile();
                Assert.Equal(bytes.Length, file.Length);
                file.Position = 0;
                Assert.True(bytes.AsSpan().SequenceEqual(DoReadFile(file, bytes.Length)));
            }
            finally
            {
                f1.Delete();
            }
        }
Esempio n. 7
0
        public void Delete()
        {
            string json = "{\"foo\":\"bar\"}";

            byte[]         bytes   = Encoding.UTF8.GetBytes(json);
            FileStream     tmpFile = null;
            DiskFileUpload f1      = new DiskFileUpload("file4", "file4", "application/json", null, null, 0);

            try
            {
                Assert.Null(f1.GetFile());
                f1.SetContent(Unpooled.WrappedBuffer(bytes));
                Assert.NotNull(tmpFile = f1.GetFile());
            }
            finally
            {
                f1.Delete();
                Assert.Null(f1.GetFile());
                Assert.NotNull(tmpFile);
                Assert.False(File.Exists(tmpFile.Name));
            }
        }