| a | b | |
|---|
| 0 | + | // Licensed under the Apache License, Version 2.0 (the "License"); you may not |
|---|
| 0 | + | // use this file except in compliance with the License. You may obtain a copy of |
|---|
| 0 | + | // the License at |
|---|
| 0 | + | // |
|---|
| 0 | + | // http://www.apache.org/licenses/LICENSE-2.0 |
|---|
| 0 | + | // |
|---|
| 0 | + | // Unless required by applicable law or agreed to in writing, software |
|---|
| 0 | + | // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
|---|
| 0 | + | // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
|---|
| 0 | + | // License for the specific language governing permissions and limitations under |
|---|
| 0 | + | // the License. |
|---|
| 0 | + | |
|---|
| 0 | + | couchTests.utf_attachments= function(debug) { |
|---|
| 0 | + | var db = new CouchDB("test_suite_db", {"X-Couch-Full-Commit":"false"}); |
|---|
| 0 | + | db.deleteDb(); |
|---|
| 0 | + | db.createDb(); |
|---|
| 0 | + | if (debug) debugger; |
|---|
| 0 | + | |
|---|
| 0 | + | var binAttDoc = { |
|---|
| 0 | + | _id: "bin_doc", |
|---|
| 0 | + | _attachments:{ |
|---|
| 0 | + | "Колян.txt": { |
|---|
| 0 | + | content_type:"text/plain", |
|---|
| 0 | + | data: "VGhpcyBpcyBhIGJhc2U2NCBlbmNvZGVkIHRleHQ=" |
|---|
| 0 | + | } |
|---|
| 0 | + | } |
|---|
| 0 | + | } |
|---|
| 0 | + | |
|---|
| 0 | + | var save_response = db.save(binAttDoc); |
|---|
| 0 | + | T(save_response.ok); |
|---|
| 0 | + | |
|---|
| 0 | + | var xhr = CouchDB.request("GET", "/test_suite_db/bin_doc/foo.txt"); |
|---|
| 0 | + | T(xhr.responseText == "This is a base64 encoded text"); |
|---|
| 0 | + | T(xhr.getResponseHeader("Content-Type") == "text/plain"); |
|---|
| 0 | + | T(xhr.getResponseHeader("Etag") == '"' + save_response.rev + '"'); |
|---|
| 0 | + | |
|---|
| 0 | + | // empty attachment |
|---|
| 0 | + | var binAttDoc2 = { |
|---|
| 0 | + | _id: "bin_doc2", |
|---|
| 0 | + | _attachments:{ |
|---|
| 0 | + | "foo.txt": { |
|---|
| 0 | + | content_type:"text/plain", |
|---|
| 0 | + | data: "" |
|---|
| 0 | + | } |
|---|
| 0 | + | } |
|---|
| 0 | + | } |
|---|
| 0 | + | |
|---|
| 0 | + | T(db.save(binAttDoc2).ok); |
|---|
| 0 | + | |
|---|
| 0 | + | var xhr = CouchDB.request("GET", "/test_suite_db/bin_doc2/foo.txt"); |
|---|
| 0 | + | T(xhr.responseText.length == 0); |
|---|
| 0 | + | T(xhr.getResponseHeader("Content-Type") == "text/plain"); |
|---|
| 0 | + | |
|---|
| 0 | + | // test RESTful doc API |
|---|
| 0 | + | |
|---|
| 0 | + | var xhr = CouchDB.request("PUT", "/test_suite_db/bin_doc2/foo2.txt?rev=" + binAttDoc2._rev, { |
|---|
| 0 | + | body:"This is no base64 encoded text", |
|---|
| 0 | + | headers:{"Content-Type": "text/plain;charset=utf-8"} |
|---|
| 0 | + | }); |
|---|
| 0 | + | T(xhr.status == 201); |
|---|
| 0 | + | TEquals("/bin_doc2/foo2.txt", |
|---|
| 0 | + | xhr.getResponseHeader("Location").substr(-18), |
|---|
| 0 | + | "should return Location header to newly created or updated attachment"); |
|---|
| 0 | + | |
|---|
| 0 | + | var rev = JSON.parse(xhr.responseText).rev; |
|---|
| 0 | + | |
|---|
| 0 | + | binAttDoc2 = db.open("bin_doc2"); |
|---|
| 0 | + | |
|---|
| 0 | + | T(binAttDoc2._attachments["foo.txt"] !== undefined); |
|---|
| 0 | + | T(binAttDoc2._attachments["foo2.txt"] !== undefined); |
|---|
| 0 | + | T(binAttDoc2._attachments["foo2.txt"].content_type == "text/plain;charset=utf-8"); |
|---|
| 0 | + | T(binAttDoc2._attachments["foo2.txt"].length == 30); |
|---|
| 0 | + | |
|---|
| 0 | + | var xhr = CouchDB.request("GET", "/test_suite_db/bin_doc2/foo2.txt"); |
|---|
| 0 | + | T(xhr.responseText == "This is no base64 encoded text"); |
|---|
| 0 | + | T(xhr.getResponseHeader("Content-Type") == "text/plain;charset=utf-8"); |
|---|
| 0 | + | |
|---|
| 0 | + | // test without rev, should fail |
|---|
| 0 | + | var xhr = CouchDB.request("DELETE", "/test_suite_db/bin_doc2/foo2.txt"); |
|---|
| 0 | + | T(xhr.status == 409); |
|---|
| 0 | + | |
|---|
| 0 | + | // test with rev, should not fail |
|---|
| 0 | + | var xhr = CouchDB.request("DELETE", "/test_suite_db/bin_doc2/foo2.txt?rev=" + rev); |
|---|
| 0 | + | T(xhr.status == 200); |
|---|
| 0 | + | TEquals(null, xhr.getResponseHeader("Location"), |
|---|
| 0 | + | "should not return Location header on DELETE request"); |
|---|
| 0 | + | |
|---|
| 0 | + | // test binary data |
|---|
| 0 | + | var bin_data = "JHAPDO*AU£PN ){(3u[d 93DQ9¡€])} ææøo'∂ƒæ≤çæππ•¥∫¶®#†π¶®¥π€ª®˙π8np"; |
|---|
| 0 | + | var xhr = CouchDB.request("PUT", "/test_suite_db/bin_doc3/attachment.txt", { |
|---|
| 0 | + | headers:{"Content-Type":"text/plain;charset=utf-8"}, |
|---|
| 0 | + | body:bin_data |
|---|
| 0 | + | }); |
|---|
| 0 | + | T(xhr.status == 201); |
|---|
| 0 | + | var rev = JSON.parse(xhr.responseText).rev; |
|---|
| 0 | + | |
|---|
| 0 | + | var xhr = CouchDB.request("GET", "/test_suite_db/bin_doc3/attachment.txt"); |
|---|
| 0 | + | T(xhr.responseText == bin_data); |
|---|
| 0 | + | T(xhr.getResponseHeader("Content-Type") == "text/plain;charset=utf-8"); |
|---|
| 0 | + | |
|---|
| 0 | + | var xhr = CouchDB.request("PUT", "/test_suite_db/bin_doc3/attachment.txt", { |
|---|
| 0 | + | headers:{"Content-Type":"text/plain;charset=utf-8"}, |
|---|
| 0 | + | body:bin_data |
|---|
| 0 | + | }); |
|---|
| 0 | + | T(xhr.status == 409); |
|---|
| 0 | + | |
|---|
| 0 | + | var xhr = CouchDB.request("PUT", "/test_suite_db/bin_doc3/attachment.txt?rev=" + rev, { |
|---|
| 0 | + | headers:{"Content-Type":"text/plain;charset=utf-8"}, |
|---|
| 0 | + | body:bin_data |
|---|
| 0 | + | }); |
|---|
| 0 | + | T(xhr.status == 201); |
|---|
| 0 | + | var rev = JSON.parse(xhr.responseText).rev; |
|---|
| 0 | + | |
|---|
| 0 | + | var xhr = CouchDB.request("GET", "/test_suite_db/bin_doc3/attachment.txt"); |
|---|
| 0 | + | T(xhr.responseText == bin_data); |
|---|
| 0 | + | T(xhr.getResponseHeader("Content-Type") == "text/plain;charset=utf-8"); |
|---|
| 0 | + | |
|---|
| 0 | + | var xhr = CouchDB.request("GET", "/test_suite_db/bin_doc3/attachment.txt?rev=" + rev); |
|---|
| 0 | + | T(xhr.responseText == bin_data); |
|---|
| 0 | + | T(xhr.getResponseHeader("Content-Type") == "text/plain;charset=utf-8"); |
|---|
| 0 | + | |
|---|
| 0 | + | var xhr = CouchDB.request("DELETE", "/test_suite_db/bin_doc3/attachment.txt?rev=" + rev); |
|---|
| 0 | + | T(xhr.status == 200); |
|---|
| 0 | + | |
|---|
| 0 | + | var xhr = CouchDB.request("GET", "/test_suite_db/bin_doc3/attachment.txt"); |
|---|
| 0 | + | T(xhr.status == 404); |
|---|
| 0 | + | |
|---|
| 0 | + | // deleted attachment is still accessible with revision |
|---|
| 0 | + | var xhr = CouchDB.request("GET", "/test_suite_db/bin_doc3/attachment.txt?rev=" + rev); |
|---|
| 0 | + | T(xhr.status == 200); |
|---|
| 0 | + | T(xhr.responseText == bin_data); |
|---|
| 0 | + | T(xhr.getResponseHeader("Content-Type") == "text/plain;charset=utf-8"); |
|---|
| 0 | + | |
|---|
| 0 | + | // empty attachments |
|---|
| 0 | + | var xhr = CouchDB.request("PUT", "/test_suite_db/bin_doc4/attachment.txt", { |
|---|
| 0 | + | headers:{"Content-Type":"text/plain;charset=utf-8"}, |
|---|
| 0 | + | body:"" |
|---|
| 0 | + | }); |
|---|
| 0 | + | T(xhr.status == 201); |
|---|
| 0 | + | var rev = JSON.parse(xhr.responseText).rev; |
|---|
| 0 | + | |
|---|
| 0 | + | var xhr = CouchDB.request("GET", "/test_suite_db/bin_doc4/attachment.txt"); |
|---|
| 0 | + | T(xhr.status == 200); |
|---|
| 0 | + | T(xhr.responseText.length == 0); |
|---|
| 0 | + | |
|---|
| 0 | + | // overwrite previsously empty attachment |
|---|
| 0 | + | var xhr = CouchDB.request("PUT", "/test_suite_db/bin_doc4/attachment.txt?rev=" + rev, { |
|---|
| 0 | + | headers:{"Content-Type":"text/plain;charset=utf-8"}, |
|---|
| 0 | + | body:"This is a string" |
|---|
| 0 | + | }); |
|---|
| 0 | + | T(xhr.status == 201); |
|---|
| 0 | + | |
|---|
| 0 | + | var xhr = CouchDB.request("GET", "/test_suite_db/bin_doc4/attachment.txt"); |
|---|
| 0 | + | T(xhr.status == 200); |
|---|
| 0 | + | T(xhr.responseText == "This is a string"); |
|---|
| 0 | + | |
|---|
| 0 | + | // Attachment sparseness COUCHDB-220 |
|---|
| 0 | + | |
|---|
| 0 | + | var docs = [] |
|---|
| 0 | + | for (var i = 0; i < 5; i++) { |
|---|
| 0 | + | var doc = { |
|---|
| 0 | + | _id: (i).toString(), |
|---|
| 0 | + | _attachments:{ |
|---|
| 0 | + | "foo.txt": { |
|---|
| 0 | + | content_type:"text/plain", |
|---|
| 0 | + | data: "VGhpcyBpcyBhIGJhc2U2NCBlbmNvZGVkIHRleHQ=" |
|---|
| 0 | + | } |
|---|
| 0 | + | } |
|---|
| 0 | + | } |
|---|
| 0 | + | docs.push(doc) |
|---|
| 0 | + | } |
|---|
| 0 | + | |
|---|
| 0 | + | var saved = db.bulkSave(docs); |
|---|
| 0 | + | // now delete the docs, and while we are looping over them, remove the |
|---|
| 0 | + | // '_rev' field so we can re-create after deletion. |
|---|
| 0 | + | var to_up = []; |
|---|
| 0 | + | for (i=0;i<saved.length;i++) { |
|---|
| 0 | + | to_up.push({'_id': saved[i]['id'], '_rev': saved[i]['rev'], '_deleted': true}); |
|---|
| 0 | + | delete docs[i]._rev; |
|---|
| 0 | + | } |
|---|
| 0 | + | // delete them. |
|---|
| 0 | + | var saved2 = db.bulkSave(to_up); |
|---|
| 0 | + | // re-create them |
|---|
| 0 | + | var saved3 = db.bulkSave(docs); |
|---|
| 0 | + | |
|---|
| 0 | + | var before = db.info().disk_size; |
|---|
| 0 | + | |
|---|
| 0 | + | // Compact it. |
|---|
| 0 | + | T(db.compact().ok); |
|---|
| 0 | + | T(db.last_req.status == 202); |
|---|
| 0 | + | // compaction isn't instantaneous, loop until done |
|---|
| 0 | + | while (db.info().compact_running) {}; |
|---|
| 0 | + | |
|---|
| 0 | + | var after = db.info().disk_size; |
|---|
| 0 | + | |
|---|
| 0 | + | // Compaction should reduce the database slightly, but not |
|---|
| 0 | + | // orders of magnitude (unless attachments introduce sparseness) |
|---|
| 0 | + | T(after > before * 0.1, "before: " + before + " after: " + after); |
|---|
| 0 | + | |
|---|
| 0 | + | |
|---|
| 0 | + | // test large attachments - COUCHDB-366 |
|---|
| 0 | + | var lorem = CouchDB.request("GET", "/_utils/script/test/lorem.txt").responseText; |
|---|
| 0 | + | |
|---|
| 0 | + | var xhr = CouchDB.request("PUT", "/test_suite_db/bin_doc5/lorem.txt", { |
|---|
| 0 | + | headers:{"Content-Type":"text/plain;charset=utf-8"}, |
|---|
| 0 | + | body:lorem |
|---|
| 0 | + | }); |
|---|
| 0 | + | T(xhr.status == 201); |
|---|
| 0 | + | var rev = JSON.parse(xhr.responseText).rev; |
|---|
| 0 | + | |
|---|
| 0 | + | var xhr = CouchDB.request("GET", "/test_suite_db/bin_doc5/lorem.txt"); |
|---|
| 0 | + | T(xhr.responseText == lorem); |
|---|
| 0 | + | T(xhr.getResponseHeader("Content-Type") == "text/plain;charset=utf-8"); |
|---|
| 0 | + | |
|---|
| 0 | + | // test large inline attachment too |
|---|
| 0 | + | var lorem_b64 = CouchDB.request("GET", "/_utils/script/test/lorem_b64.txt").responseText; |
|---|
| 0 | + | var doc = db.open("bin_doc5", {attachments:true}); |
|---|
| 0 | + | T(doc._attachments["lorem.txt"].data == lorem_b64); |
|---|
| 0 | + | |
|---|
| 0 | + | // test etags for attachments. |
|---|
| 0 | + | var xhr = CouchDB.request("GET", "/test_suite_db/bin_doc5/lorem.txt"); |
|---|
| 0 | + | T(xhr.status == 200); |
|---|
| 0 | + | var etag = xhr.getResponseHeader("etag"); |
|---|
| 0 | + | xhr = CouchDB.request("GET", "/test_suite_db/bin_doc5/lorem.txt", { |
|---|
| 0 | + | headers: {"if-none-match": etag} |
|---|
| 0 | + | }); |
|---|
| 0 | + | T(xhr.status == 304); |
|---|
| 0 | + | |
|---|
| 0 | + | // test COUCHDB-497 - empty attachments |
|---|
| 0 | + | var xhr = CouchDB.request("PUT", "/test_suite_db/bin_doc5/empty.txt?rev="+rev, { |
|---|
| 0 | + | headers:{"Content-Type":"text/plain;charset=utf-8", "Content-Length": "0"}, |
|---|
| 0 | + | body:"" |
|---|
| 0 | + | }); |
|---|
| 0 | + | TEquals(201, xhr.status, "should send 201 Accepted"); |
|---|
| 0 | + | var rev = JSON.parse(xhr.responseText).rev; |
|---|
| 0 | + | var xhr = CouchDB.request("PUT", "/test_suite_db/bin_doc5/empty.txt?rev="+rev, { |
|---|
| 0 | + | headers:{"Content-Type":"text/plain;charset=utf-8"} |
|---|
| 0 | + | }); |
|---|
| 0 | + | TEquals(201, xhr.status, "should send 201 Accepted"); |
|---|
| 0 | + | |
|---|
| 0 | + | // implicit doc creation allows creating docs with a reserved id. COUCHDB-565 |
|---|
| 0 | + | var xhr = CouchDB.request("PUT", "/test_suite_db/_nonexistant/attachment.txt", { |
|---|
| 0 | + | headers: {"Content-Type":"text/plain;charset=utf-8"}, |
|---|
| 0 | + | body: "THIS IS AN ATTACHMENT. BOOYA!" |
|---|
| 0 | + | }); |
|---|
| 0 | + | TEquals(400, xhr.status, "should return error code 400 Bad Request"); |
|---|
| 0 | + | }; |
|---|
| ... | |
|---|