From c48e8ef02d8ba7a93dbb3e72c7be3441cb6bacbf Mon Sep 17 00:00:00 2001 From: Jan Lehnardt Date: Sun, 6 Jul 2008 18:52:56 +0000 Subject: Add RESTful API for document attachments as per http://groups.google.com/group/couchdb/browse_thread/thread/c84c5f35afb5db2a with not yet comprehensive tests. git-svn-id: https://svn.apache.org/repos/asf/incubator/couchdb/trunk@674334 13f79535-47bb-0310-9956-ffa450edef68 --- share/www/script/couch.js | 10 ++++++++ share/www/script/couch_tests.js | 55 +++++++++++++++++++++++++++++++++++++++-- 2 files changed, 63 insertions(+), 2 deletions(-) (limited to 'share/www') diff --git a/share/www/script/couch.js b/share/www/script/couch.js index 861fd070..38c9cf34 100644 --- a/share/www/script/couch.js +++ b/share/www/script/couch.js @@ -80,6 +80,16 @@ function CouchDB(name) { return result; } + // Deletes an attachment from a document + this.deleteDocAttachment = function(doc, attachment_name) { + var req = request("DELETE", this.uri + encodeURIComponent(doc._id) + "/" + attachment_name + "?rev=" + doc._rev); + var result = JSON.parse(req.responseText); + if (req.status != 200) + throw result; + doc._rev = result.rev; //record rev in input document + return result; + } + this.bulkSave = function(docs, options) { var req = request("POST", this.uri + "_bulk_docs" + encodeOptions(options), { body: JSON.stringify({"docs": docs}) diff --git a/share/www/script/couch_tests.js b/share/www/script/couch_tests.js index 213c5cfc..a0e6b2ff 100644 --- a/share/www/script/couch_tests.js +++ b/share/www/script/couch_tests.js @@ -556,8 +556,59 @@ var tests = { T(db.save(binAttDoc).ok); var xhr = CouchDB.request("GET", "/test_suite_db/bin_doc/foo.txt"); - T(xhr.responseText == "This is a base64 encoded text") - T(xhr.getResponseHeader("Content-Type") == "text/plain") + T(xhr.responseText == "This is a base64 encoded text"); + T(xhr.getResponseHeader("Content-Type") == "text/plain"); + + // test RESTful doc API + + // test without rev, should fail + var xhr = CouchDB.request("DELETE", "/test_suite_db/bin_doc/foo.txt"); + T(xhr.status == 412); + + db.deleteDocAttachment({_id:"bin_doc"}, "foo.txt"); + var xhr = CouchDB.request("GET", "/test_suite_db/bin_doc/foo.txt"); + T(xhr.status == 404); + + var xhr = CouchDB.request("PUT", "/test_suite_db/bin_doc/foo.txt", { + body: "This is not base64 encoded text", + headers:{"Content-Type":"text/plain;charset=utf-8"} + }); + T(xhr.status == 201); + + var xhr = CouchDB.request("GET", "/test_suite_db/bin_doc/foo.txt"); + T(xhr.responseText == "This is not base64 encoded text"); + T(xhr.getResponseHeader("Content-Type") == "text/plain;charset=utf-8"); + + // test binary data + var xhr = CouchDB.request("GET", "/favicon.ico"); + var bin_data = xhr.responseText; + // bin_data = 123; + var xhr = CouchDB.request("PUT", "/test_suite_db/bin_doc/favicon.ico", { + headers:{"Content-Type":"image/vnd.microsoft.icon"}, + body:bin_data + }); + T(xhr.status == 201); + var rev = JSON.parse(xhr.responseText).rev; + + var xhr = CouchDB.request("GET", "/test_suite_db/bin_doc/favicon.ico"); + T(xhr.responseText == bin_data); + T(xhr.getResponseHeader("Content-Type") == "image/vnd.microsoft.icon"); + + var xhr = CouchDB.request("PUT", "/test_suite_db/bin_doc/favicon.ico", { + headers:{"Content-Type":"image/vnd.microsoft.icon"}, + body:bin_data + }); + T(xhr.status == 412); + + var xhr = CouchDB.request("PUT", "/test_suite_db/bin_doc/favicon.ico?rev="+rev, { + headers:{"Content-Type":"image/vnd.microsoft.icon"}, + body:bin_data + }); + T(xhr.status == 201); + var rev = JSON.parse(xhr.responseText).rev; + + var xhr = CouchDB.request("DELETE", "/test_suite_db/bin_doc/foo.txt?rev="+rev); + T(xhr.status == 200); }, content_negotiation: function(debug) { -- cgit v1.2.3