Beispiel #1
0
        public void TestMultipartDocumentReader()
        {
            var mime    = GetType().GetResourceAsStream("Multipart1.mime").ReadAllBytes();
            var headers = new Dictionary <string, string> {
                { "Content-Type", "multipart/mixed; boundary=\"BOUNDARY\"" }
            };

            var dict = default(IDictionary <string, object>);

            Assert.DoesNotThrow(() => dict = MultipartDocumentReader.ReadToDatabase(mime, headers, database));

            AssertDictionariesAreEqual(new Dictionary <string, object> {
                { "_id", "THX-1138" },
                { "_rev", "1-foobar" },
                { "_attachments", new Dictionary <string, object> {
                      { "mary.txt", new Dictionary <string, object> {
                            { "type", "text/doggerel" },
                            { "length", 52 },
                            { "follows", true },
                            { "digest", "sha1-Jcy8i3K9HZ8UGLO9j+KNbLLjm7M=" }
                        } }
                  } }
            }, dict);

            var attachment = dict.Get("_attachments").AsDictionary <string, object>().Get("mary.txt").AsDictionary <string, object>();
            var writer     = database.AttachmentWriterForAttachment(attachment);

            Assert.IsNotNull(writer);
            Assert.AreEqual(52, writer.GetLength());

            mime = GetType().GetResourceAsStream("MultipartBinary.mime").ReadAllBytes();
            headers["Content-Type"]        = "multipart/mixed; boundary=\"dc0bf3cdc9a6c6e4c46fe2a361c8c5d7\"";
            Assert.DoesNotThrow(() => dict = MultipartDocumentReader.ReadToDatabase(mime, headers, database));
            AssertDictionariesAreEqual(new Dictionary <string, object> {
                { "_id", "038c536dc29ff0f4127705879700062c" },
                { "_rev", "3-e715bcf1865f8283ab1f0ba76e7a92ba" },
                { "_attachments", new Dictionary <string, object> {
                      { "want3.jpg", new Dictionary <string, object> {
                            { "content_type", "image/jpeg" },
                            { "revpos", 3 },
                            { "length", 24758 },
                            { "follows", true },
                            { "digest", "sha1-mmlbbSUTrKoaD67j7Hyjgq2y1aI=" }
                        } },
                      { "Toad.gif", new Dictionary <string, object> {
                            { "content_type", "image/gif" },
                            { "revpos", 2 },
                            { "length", 6566 },
                            { "follows", true },
                            { "digest", "sha1-Y8ppBwk8w1j6nP5rwmeB8FwPtgg=" }
                        } }
                  } }
            }, dict);

            attachment = dict.Get("_attachments").AsDictionary <string, object>().Get("Toad.gif").AsDictionary <string, object>();
            writer     = database.AttachmentWriterForAttachment(attachment);
            Assert.IsNotNull(writer);
            Assert.AreEqual(6566, writer.GetLength());
            attachment = dict.Get("_attachments").AsDictionary <string, object>().Get("want3.jpg").AsDictionary <string, object>();
            writer     = database.AttachmentWriterForAttachment(attachment);
            Assert.IsNotNull(writer);
            Assert.AreEqual(24758, writer.GetLength());

            // Read data that's equivalent to the last one except the JSON is gzipped:
            mime = GetType().GetResourceAsStream("MultipartGZipped.mime").ReadAllBytes();
            headers["Content-Type"] = "multipart/mixed; boundary=\"d7a34c160fd136b5baf17055012e611abcb45dd3fe39fb81831ffd5dc920\"";
            var unzippedDict = default(IDictionary <string, object>);

            Assert.DoesNotThrow(() => unzippedDict = MultipartDocumentReader.ReadToDatabase(mime, headers, database));
            AssertDictionariesAreEqual(dict, unzippedDict);
        }