From 4faf660a4dfe5849fae96ea7eb15379069c27405 Mon Sep 17 00:00:00 2001 From: "Damien F. Katz" Date: Sat, 14 Nov 2009 01:14:59 +0000 Subject: Better testing of multipart/related git-svn-id: https://svn.apache.org/repos/asf/couchdb/trunk@836087 13f79535-47bb-0310-9956-ffa450edef68 --- share/Makefile.am | 1 + share/www/script/couch_tests.js | 1 + share/www/script/test/attachments_multipart.js | 185 +++++++++++++++++++++++++ 3 files changed, 187 insertions(+) create mode 100644 share/www/script/test/attachments_multipart.js (limited to 'share') diff --git a/share/Makefile.am b/share/Makefile.am index 4adce5db..34711b11 100644 --- a/share/Makefile.am +++ b/share/Makefile.am @@ -97,6 +97,7 @@ nobase_dist_localdata_DATA = \ www/script/sha1.js \ www/script/test/all_docs.js \ www/script/test/attachments.js \ + www/script/test/attachments_multipart.js \ www/script/test/attachment_names.js \ www/script/test/attachment_paths.js \ www/script/test/attachment_views.js \ diff --git a/share/www/script/couch_tests.js b/share/www/script/couch_tests.js index 5c1e2bc3..d991c05d 100644 --- a/share/www/script/couch_tests.js +++ b/share/www/script/couch_tests.js @@ -30,6 +30,7 @@ loadTest("basics.js"); // keep sorted loadTest("all_docs.js"); loadTest("attachments.js"); +loadTest("attachments_multipart.js"); loadTest("attachment_names.js"); loadTest("attachment_paths.js"); loadTest("attachment_views.js"); diff --git a/share/www/script/test/attachments_multipart.js b/share/www/script/test/attachments_multipart.js new file mode 100644 index 00000000..dbe2cff6 --- /dev/null +++ b/share/www/script/test/attachments_multipart.js @@ -0,0 +1,185 @@ +// Licensed under the Apache License, Version 2.0 (the "License"); you may not +// use this file except in compliance with the License. You may obtain a copy of +// the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +// License for the specific language governing permissions and limitations under +// the License. + +couchTests.attachments_multipart= function(debug) { + var db = new CouchDB("test_suite_db", {"X-Couch-Full-Commit":"false"}); + db.deleteDb(); + db.createDb(); + if (debug) debugger; + + // mime multipart + + xhr = CouchDB.request("PUT", "/test_suite_db/multipart", { + headers: {"Content-Type": "multipart/related;boundary=\"abc123\""}, + body: + "--abc123\r\n" + + "content-type: application/json\r\n" + + "\r\n" + + JSON.stringify({ + "body":"This is a body.", + "_attachments":{ + "foo.txt": { + "follows":true, + "content_type":"text/plain", + "length":21 + }, + "bar.txt": { + "follows":true, + "content_type":"text/plain", + "length":20 + }, + "baz.txt": { + "follows":true, + "content_type":"text/plain", + "length":19 + } + } + }) + + "\r\n--abc123\r\n" + + "\r\n" + + "this is 21 chars long" + + "\r\n--abc123\r\n" + + "\r\n" + + "this is 20 chars lon" + + "\r\n--abc123\r\n" + + "\r\n" + + "this is 19 chars lo" + + "\r\n--abc123--" + }); + + var result = JSON.parse(xhr.responseText); + + T(result.ok) + + + + TEquals(201, xhr.status, "should send 201 Accepted"); + + xhr = CouchDB.request("GET", "/test_suite_db/multipart/foo.txt"); + + T(xhr.responseText == "this is 21 chars long"); + + xhr = CouchDB.request("GET", "/test_suite_db/multipart/bar.txt"); + + T(xhr.responseText == "this is 20 chars lon"); + + xhr = CouchDB.request("GET", "/test_suite_db/multipart/baz.txt"); + + T(xhr.responseText == "this is 19 chars lo"); + + // now edit an attachment + + var doc = db.open("multipart"); + + T(doc._attachments["foo.txt"].stub == true); + T(doc._attachments["bar.txt"].stub == true); + T(doc._attachments["baz.txt"].stub == true); + + //lets change attachment bar + delete doc._attachments["bar.txt"].stub; // remove stub member (or could set to false) + doc._attachments["bar.txt"].length = 18; + doc._attachments["bar.txt"].follows = true; + //lets delete attachment baz: + delete doc._attachments["baz.txt"]; + + var xhr = CouchDB.request("PUT", "/test_suite_db/multipart", { + headers: {"Content-Type": "multipart/related;boundary=\"abc123\""}, + body: + "--abc123\r\n" + + "content-type: application/json\r\n" + + "\r\n" + + JSON.stringify(doc) + + "\r\n--abc123\r\n" + + "\r\n" + + "this is 18 chars l" + + "\r\n--abc123--" + }); + + xhr = CouchDB.request("GET", "/test_suite_db/multipart/bar.txt"); + + T(xhr.responseText == "this is 18 chars l"); + + xhr = CouchDB.request("GET", "/test_suite_db/multipart/baz.txt"); + T(xhr.status == 404); + + xhr = CouchDB.request("GET", "/test_suite_db/multipart?attachments=true", + {headers:{"accept": "multipart/related,*/*;"}}); + + var headers = xhr.getAllResponseHeaders(); + + var ctype = xhr.getResponseHeader("Content-Type"); + + var ctypeArgs = ctype.split("; ").slice(1); + var boundary = null; + for(var i=0; i