From 37ca97c918f4b5316e4293d8f1001bb87b8dfb0c Mon Sep 17 00:00:00 2001 From: Christopher Lenz Date: Tue, 9 Sep 2008 11:12:36 +0000 Subject: Fix for attachment content-type conversion from/to binary in RESTful API. git-svn-id: https://svn.apache.org/repos/asf/incubator/couchdb/trunk@693435 13f79535-47bb-0310-9956-ffa450edef68 --- share/www/script/couch.js | 30 +++++++++++++++--------------- share/www/script/couch_tests.js | 20 +++++++++++++------- 2 files changed, 28 insertions(+), 22 deletions(-) (limited to 'share') diff --git a/share/www/script/couch.js b/share/www/script/couch.js index 2f15737d..cf26bb2e 100644 --- a/share/www/script/couch.js +++ b/share/www/script/couch.js @@ -243,22 +243,22 @@ CouchDB.request = function(method, uri, options) { CouchDB.uuids_cache = []; CouchDB.newUuids = function(n) { - if (CouchDB.uuids_cache.length >= n) { - var uuids = CouchDB.uuids_cache.slice(CouchDB.uuids_cache.length - n); - if(CouchDB.uuids_cache.length - n == 0) { - CouchDB.uuids_cache = []; - } else { - CouchDB.uuids_cache = - CouchDB.uuids_cache.slice(0, CouchDB.uuids_cache.length - n); - } - return uuids; + if (CouchDB.uuids_cache.length >= n) { + var uuids = CouchDB.uuids_cache.slice(CouchDB.uuids_cache.length - n); + if(CouchDB.uuids_cache.length - n == 0) { + CouchDB.uuids_cache = []; } else { - var req = CouchDB.request("POST", "/_uuids?count=" + (100 + n)); - var result = JSON.parse(req.responseText); - if (req.status != 200) - throw result; CouchDB.uuids_cache = - CouchDB.uuids_cache.concat(result.uuids.slice(0, 100)); - return result.uuids.slice(100); + CouchDB.uuids_cache.slice(0, CouchDB.uuids_cache.length - n); } + return uuids; + } else { + var req = CouchDB.request("POST", "/_uuids?count=" + (100 + n)); + var result = JSON.parse(req.responseText); + if (req.status != 200) + throw result; + CouchDB.uuids_cache = + CouchDB.uuids_cache.concat(result.uuids.slice(0, 100)); + return result.uuids.slice(100); } +} diff --git a/share/www/script/couch_tests.js b/share/www/script/couch_tests.js index b35e66ce..3d061ed5 100644 --- a/share/www/script/couch_tests.js +++ b/share/www/script/couch_tests.js @@ -682,10 +682,9 @@ var tests = { T(xhr.responseText == "This is a base64 encoded text"); T(xhr.getResponseHeader("Content-Type") == "text/plain"); - // empty attachment var binAttDoc2 = { - _id: "bin_doc5", + _id: "bin_doc2", _attachments:{ "foo.txt": { content_type:"text/plain", @@ -696,19 +695,26 @@ var tests = { T(db.save(binAttDoc2).ok); - var xhr = CouchDB.request("GET", "/test_suite_db/bin_doc5/foo.txt"); + var xhr = CouchDB.request("GET", "/test_suite_db/bin_doc2/foo.txt"); T(xhr.responseText.length == 0); T(xhr.getResponseHeader("Content-Type") == "text/plain"); - + // test RESTful doc API - - var xhr = CouchDB.request("PUT", "/test_suite_db/bin_doc2/foo2.txt", { + + var xhr = CouchDB.request("PUT", "/test_suite_db/bin_doc2/foo2.txt?rev=" + binAttDoc2._rev, { body:"This is no base64 encoded text", headers:{"Content-Type": "text/plain;charset=utf-8"} }); T(xhr.status == 201); var rev = JSON.parse(xhr.responseText).rev; - + + binAttDoc2 = db.open("bin_doc2"); + console.log(JSON.stringify(binAttDoc2)); + T(binAttDoc2._attachments["foo.txt"] !== undefined); + T(binAttDoc2._attachments["foo2.txt"] !== undefined); + T(binAttDoc2._attachments["foo2.txt"].content_type == "text/plain;charset=utf-8"); + T(binAttDoc2._attachments["foo2.txt"].length == 30); + var xhr = CouchDB.request("GET", "/test_suite_db/bin_doc2/foo2.txt"); T(xhr.responseText == "This is no base64 encoded text"); T(xhr.getResponseHeader("Content-Type") == "text/plain;charset=utf-8"); -- cgit v1.2.3