Beispiel #1
0
        public void ParseValueAndSingleFile()
        {
            string data = @"--AaB03x
Content-Disposition: form-data; name=""TestValue""

Pass
--AaB03x
Content-Disposition: form-data; name=""TestFile""; filename=""file1.txt""
Content-Type: text/plain

Pass
--AaB03x--";

            FormData form;

            using (var input = new MemoryStream(Encoding.UTF8.GetBytes(data))) {
                form = FormData.ParseMultipart(input, "AaB03x")
                       .Result;
            }
            form.IsValid.ShouldBeTrue();
            form.Values.Count.ShouldEqual(1);
            form.Files.Count.ShouldEqual(1);
            form["TestValue"].ShouldEqual("Pass");
            var file = form.Files[0];

            file.FieldName.ShouldEqual("TestFile");
            file.FileName.ShouldEqual("file1.txt");
            file.ContentType.ShouldEqual("text/plain");
            file.ContentLength.ShouldEqual(4);
            file.InputStream.ReadAll()
            .ShouldEqual("Pass");
        }
Beispiel #2
0
        public void BadFormStart()
        {
            FormData form;

            using (var input = new MemoryStream(Encoding.UTF8.GetBytes(@"Bad Form Here"))) {
                form = FormData.ParseMultipart(input, "AaB03x")
                       .Result;
            }
            form.IsValid.ShouldBeFalse();
            form.Values.Count.ShouldEqual(0);
            form.Files.Count.ShouldEqual(0);
        }
Beispiel #3
0
        public void ParseValueAndMultipleFiles()
        {
            string data = @"--AaB03x
Content-Disposition: form-data; name=""Test""

Pass
--AaB03x
Content-Disposition: form-data; name=""files""
Content-Type: multipart/mixed; boundary=""BbC04y""

--BbC04y
Content-Disposition: file; filename=""file1.txt""
Content-Type: text/plain

Pass
--BbC04y
Content-Disposition: file; filename=""file2.gif""
Content-Type: image/gif
Content-Transfer-Encoding: binary

...contents of file2.gif...
--BbC04y--
--AaB03x--";

            FormData form;

            using (var input = new MemoryStream(Encoding.UTF8.GetBytes(data))) {
                form = FormData.ParseMultipart(input, "AaB03x")
                       .Result;
            }
            form.IsValid.ShouldBeTrue();
            form.Values.Count.ShouldEqual(1);
            form.Files.Count.ShouldEqual(2);
            form["Test"].ShouldEqual("Pass");
            var file = form.Files[0];

            file.FieldName.ShouldEqual("files");
            file.FileName.ShouldEqual("file1.txt");
            file.ContentType.ShouldEqual("text/plain");
            file.ContentLength.ShouldEqual(4);
            file.InputStream.ReadAll()
            .ShouldEqual("Pass");
            file = form.Files[1];
            file.FieldName.ShouldEqual("files");
            file.FileName.ShouldEqual("file2.gif");
            file.ContentType.ShouldEqual("image/gif");
            file.ContentLength.ShouldEqual(27);
            file.InputStream.ReadAll()
            .ShouldEqual("...contents of file2.gif...");
        }
Beispiel #4
0
        public void ParseSingleValue()
        {
            string data = @"--AaB03x
Content-Disposition: form-data; name=""Test""

Pass
--AaB03x--";

            FormData form;

            using (var input = new MemoryStream(Encoding.UTF8.GetBytes(data))) {
                form = FormData.ParseMultipart(input, "AaB03x")
                       .Result;
            }
            form.IsValid.ShouldBeTrue();
            form.Values.Count.ShouldEqual(1);
            form.Files.Count.ShouldEqual(0);
            form["Test"].ShouldEqual("Pass");
        }
Beispiel #5
0
        public void CorruptForm()
        {
            string data = @"--AaB03x
Content-Disposition: form-data; name=""Failure""

Not Here
--AaB03x
Corrupt...
";

            FormData form;

            using (var input = new MemoryStream(Encoding.UTF8.GetBytes(data))) {
                form = FormData.ParseMultipart(input, "AaB03x")
                       .Result;
            }
            form.IsValid.ShouldBeFalse();
            form.Values.Count.ShouldEqual(1);
            form.Files.Count.ShouldEqual(0);
            form["Failure"].ShouldEqual("Not Here");
        }
Beispiel #6
0
        public void ParseSingleEncodedValue()
        {
            var input = new MemoryStream();

            input.Write(Encoding.UTF8.GetBytes(@"--AaB03x
Content-Disposition: form-data; name=""Test""
Content-Type: text/plain; charset=windows-1250

"));
            input.Write(Encoding.GetEncoding("windows-1250")
                        .GetBytes("Pass €"));
            input.Write(Encoding.UTF8.GetBytes(@"
--AaB03x--"));
            input.SeekToBegin();
            FormData form = FormData.ParseMultipart(input, "AaB03x")
                            .Result;

            form.IsValid.ShouldBeTrue();
            form.Values.Count.ShouldEqual(1);
            form.Files.Count.ShouldEqual(0);
            form["Test"].ShouldEqual("Pass €");
            input.Close();
        }