diff options
author | Filipe David Borba Manana <fdmanana@apache.org> | 2010-12-22 19:46:12 +0000 |
---|---|---|
committer | Filipe David Borba Manana <fdmanana@apache.org> | 2010-12-22 19:46:12 +0000 |
commit | 214b6f9b9d4c9cd9182d2ce56b9a5ffd9f6d0864 (patch) | |
tree | 766bbd1fe6c3d2819303debd8a1b75cfd8805e6f /share | |
parent | 0067f2e76ddcb1143d17de73fee878f68286aaae (diff) |
Merged revision 1052047 from trunk:
Allow a multipart/mixed document GET to send the attachments in encoded (compressed) form
Currently this API is not used internally but it's a very important one for the new replicator.
git-svn-id: https://svn.apache.org/repos/asf/couchdb/branches/1.1.x@1052048 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'share')
-rw-r--r-- | share/www/script/test/attachments_multipart.js | 128 |
1 files changed, 127 insertions, 1 deletions
diff --git a/share/www/script/test/attachments_multipart.js b/share/www/script/test/attachments_multipart.js index 96fe344f..8ff2fd4a 100644 --- a/share/www/script/test/attachments_multipart.js +++ b/share/www/script/test/attachments_multipart.js @@ -278,5 +278,131 @@ couchTests.attachments_multipart= function(debug) { T(doc._attachments['bar.txt'].follows == true); 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(); }; |