diff options
author | Adam Kocoloski <adam@cloudant.com> | 2011-08-15 15:45:15 -0400 |
---|---|---|
committer | Adam Kocoloski <adam@cloudant.com> | 2011-08-15 15:45:15 -0400 |
commit | 6ffe1675dd7b004e48891956a6bdbe32899ce80c (patch) | |
tree | 57326d9a498481e65bb0db38c66daf10896801f1 /rel/overlay/share/www/script/test/attachments_multipart.js | |
parent | 52ff89ff7996e839b9e2f91fd76184d362a8aeb0 (diff) | |
parent | fdd1a5d0bc48b49b0df5c9217beff9574011283c (diff) |
Merge branch '11554-merge-couchdb-1.1'
Diffstat (limited to 'rel/overlay/share/www/script/test/attachments_multipart.js')
-rw-r--r-- | rel/overlay/share/www/script/test/attachments_multipart.js | 169 |
1 files changed, 167 insertions, 2 deletions
diff --git a/rel/overlay/share/www/script/test/attachments_multipart.js b/rel/overlay/share/www/script/test/attachments_multipart.js index f173d2bb..2fc8e3bd 100644 --- a/rel/overlay/share/www/script/test/attachments_multipart.js +++ b/rel/overlay/share/www/script/test/attachments_multipart.js @@ -78,15 +78,19 @@ couchTests.attachments_multipart= function(debug) { // now edit an attachment - var doc = db.open("multipart"); + var doc = db.open("multipart", {att_encoding_info: true}); var firstrev = doc._rev; T(doc._attachments["foo.txt"].stub == true); T(doc._attachments["bar.txt"].stub == true); T(doc._attachments["baz.txt"].stub == true); + TEquals("undefined", typeof doc._attachments["foo.txt"].encoding); + TEquals("undefined", typeof doc._attachments["bar.txt"].encoding); + TEquals("gzip", doc._attachments["baz.txt"].encoding); //lets change attachment bar delete doc._attachments["bar.txt"].stub; // remove stub member (or could set to false) + delete doc._attachments["bar.txt"].digest; // remove the digest (it's for the gzip form) doc._attachments["bar.txt"].length = 18; doc._attachments["bar.txt"].follows = true; //lets delete attachment baz: @@ -104,6 +108,7 @@ couchTests.attachments_multipart= function(debug) { "this is 18 chars l" + "\r\n--abc123--" }); + TEquals(201, xhr.status); xhr = CouchDB.request("GET", "/test_suite_db/multipart/bar.txt"); @@ -115,8 +120,11 @@ couchTests.attachments_multipart= function(debug) { // now test receiving multipart docs function getBoundary(xhr) { + if (xhr instanceof XMLHttpRequest) { var ctype = xhr.getResponseHeader("Content-Type"); - + } else { + var ctype = xhr.headers['Content-Type']; + } var ctypeArgs = ctype.split("; ").slice(1); var boundary = null; for(var i=0; i<ctypeArgs.length; i++) { @@ -134,7 +142,11 @@ couchTests.attachments_multipart= function(debug) { function parseMultipart(xhr) { var boundary = getBoundary(xhr); + if (xhr instanceof XMLHttpRequest) { var mimetext = xhr.responseText; + } else { + var mimetext = xhr.body; + } // strip off leading boundary var leading = "--" + boundary + "\r\n"; var last = "\r\n--" + boundary + "--"; @@ -208,6 +220,33 @@ couchTests.attachments_multipart= function(debug) { T(sections[1].body == "this is 18 chars l"); + // try the atts_since parameter together with the open_revs parameter + xhr = CouchDB.request( + "GET", + '/test_suite_db/multipart?open_revs=["' + + doc._rev + '"]&atts_since=["' + firstrev + '"]', + {headers: {"accept": "multipart/mixed"}} + ); + + T(xhr.status === 200); + + sections = parseMultipart(xhr); + // 1 section, with a multipart/related Content-Type + T(sections.length === 1); + T(sections[0].headers['Content-Type'].indexOf('multipart/related;') === 0); + + var innerSections = parseMultipart(sections[0]); + // 2 inner sections: a document body section plus an attachment data section + T(innerSections.length === 2); + T(innerSections[0].headers['content-type'] === 'application/json'); + + doc = JSON.parse(innerSections[0].body); + + T(doc._attachments['foo.txt'].stub === true); + T(doc._attachments['bar.txt'].follows === true); + + T(innerSections[1].body === "this is 18 chars l"); + // try it with a rev that doesn't exist (should get all attachments) xhr = CouchDB.request("GET", "/test_suite_db/multipart?atts_since=[\"1-2897589\"]", @@ -245,4 +284,130 @@ couchTests.attachments_multipart= function(debug) { T(sections[1].body == "this is 18 chars l"); + + // check that with the document multipart/mixed API it's possible to receive + // attachments in compressed form (if they're stored in compressed form) + + var server_config = [ + { + section: "attachments", + key: "compression_level", + value: "8" + }, + { + section: "attachments", + key: "compressible_types", + value: "text/plain" + } + ]; + + function testMultipartAttCompression() { + var doc = { _id: "foobar" }; + var lorem = + CouchDB.request("GET", "/_utils/script/test/lorem.txt").responseText; + var helloData = "hello world"; + + TEquals(true, db.save(doc).ok); + + var firstRev = doc._rev; + var xhr = CouchDB.request( + "PUT", + "/" + db.name + "/" + doc._id + "/data.bin?rev=" + firstRev, + { + body: helloData, + headers: {"Content-Type": "application/binary"} + } + ); + TEquals(201, xhr.status); + + var secondRev = db.open(doc._id)._rev; + xhr = CouchDB.request( + "PUT", + "/" + db.name + "/" + doc._id + "/lorem.txt?rev=" + secondRev, + { + body: lorem, + headers: {"Content-Type": "text/plain"} + } + ); + TEquals(201, xhr.status); + + var thirdRev = db.open(doc._id)._rev; + + xhr = CouchDB.request( + "GET", + '/' + db.name + '/' + doc._id + '?open_revs=["' + thirdRev + '"]', + { + headers: { + "Accept": "multipart/mixed", + "X-CouchDB-Send-Encoded-Atts": "true" + } + } + ); + TEquals(200, xhr.status); + + var sections = parseMultipart(xhr); + // 1 section, with a multipart/related Content-Type + TEquals(1, sections.length); + TEquals(0, + sections[0].headers['Content-Type'].indexOf('multipart/related;')); + + var innerSections = parseMultipart(sections[0]); + // 3 inner sections: a document body section plus 2 attachment data sections + TEquals(3, innerSections.length); + TEquals('application/json', innerSections[0].headers['content-type']); + + doc = JSON.parse(innerSections[0].body); + + TEquals(true, doc._attachments['lorem.txt'].follows); + TEquals("gzip", doc._attachments['lorem.txt'].encoding); + TEquals(true, doc._attachments['data.bin'].follows); + T(doc._attachments['data.bin'] !== "gzip"); + + if (innerSections[1].body === helloData) { + T(innerSections[2].body !== lorem); + } else if (innerSections[2].body === helloData) { + T(innerSections[1].body !== lorem); + } else { + T(false, "Could not found data.bin attachment data"); + } + + // now test that it works together with the atts_since parameter + + xhr = CouchDB.request( + "GET", + '/' + db.name + '/' + doc._id + '?open_revs=["' + thirdRev + '"]' + + '&atts_since=["' + secondRev + '"]', + { + headers: { + "Accept": "multipart/mixed", + "X-CouchDB-Send-Encoded-Atts": "true" + } + } + ); + TEquals(200, xhr.status); + + sections = parseMultipart(xhr); + // 1 section, with a multipart/related Content-Type + TEquals(1, sections.length); + TEquals(0, + sections[0].headers['Content-Type'].indexOf('multipart/related;')); + + innerSections = parseMultipart(sections[0]); + // 2 inner sections: a document body section plus 1 attachment data section + TEquals(2, innerSections.length); + TEquals('application/json', innerSections[0].headers['content-type']); + + doc = JSON.parse(innerSections[0].body); + + TEquals(true, doc._attachments['lorem.txt'].follows); + TEquals("gzip", doc._attachments['lorem.txt'].encoding); + TEquals("undefined", typeof doc._attachments['data.bin'].follows); + TEquals(true, doc._attachments['data.bin'].stub); + T(innerSections[1].body !== lorem); + } + + run_on_modified_server(server_config, testMultipartAttCompression); + + // cleanup + db.deleteDb(); }; |