Commit a91c592e authored by Tristan Cavelier's avatar Tristan Cavelier

Dav storage completed + tests

parent 9e5c6ae6
...@@ -204,6 +204,34 @@ jIO.addStorageType("dav", function (spec, my) { ...@@ -204,6 +204,34 @@ jIO.addStorageType("dav", function (spec, my) {
return doc_id; return doc_id;
}; };
/**
* Convert a file name to a document id (and attachment id if there)
* @method fileNameToIds
* @param {string} file_name The file name to convert
* @return {array} ["document id", "attachment id"] or ["document id"]
*/
priv.fileNameToIds = function (file_name) {
var separator_index = -1, split = file_name.split(".");
split.slice(0, -1).forEach(function (file_name_part, index) {
if (file_name_part.slice(-1) !== "_") {
separator_index = index;
}
});
if (separator_index === -1) {
return [priv.restoreName(priv.restoreName(
file_name
).split("_.").join("."))];
}
return [
priv.restoreName(priv.restoreName(
split.slice(0, separator_index + 1).join(".")
).split("_.").join(".")),
priv.restoreName(priv.restoreName(
split.slice(separator_index + 1).join(".")
).split("_.").join("."))
];
};
/** /**
* Removes the last character if it is a "/". "/a/b/c/" become "/a/b/c" * Removes the last character if it is a "/". "/a/b/c/" become "/a/b/c"
* @method removeSlashIfLast * @method removeSlashIfLast
...@@ -246,13 +274,8 @@ jIO.addStorageType("dav", function (spec, my) { ...@@ -246,13 +274,8 @@ jIO.addStorageType("dav", function (spec, my) {
*/ */
priv.ajax = function (doc_id, attachment_id, method, ajax_object) { priv.ajax = function (doc_id, attachment_id, method, ajax_object) {
var new_ajax_object = JSON.parse(JSON.stringify(ajax_object) || "{}"); var new_ajax_object = JSON.parse(JSON.stringify(ajax_object) || "{}");
console.log(priv.makeAjaxObject(
priv.idsToFileName(doc_id, attachment_id),
method,
new_ajax_object
));
return $.ajax(priv.makeAjaxObject( return $.ajax(priv.makeAjaxObject(
priv.idsToFileName(doc_id, attachment_id), priv.idsToFileName(doc_id || '', attachment_id),
method, method,
new_ajax_object new_ajax_object
));//.always(then || function () {}); ));//.always(then || function () {});
...@@ -281,7 +304,7 @@ jIO.addStorageType("dav", function (spec, my) { ...@@ -281,7 +304,7 @@ jIO.addStorageType("dav", function (spec, my) {
error.statusText = "Conflicts"; error.statusText = "Conflicts";
break; break;
case 24: case 24:
error.statusText = "Broken Document"; error.statusText = "Corrupted Document";
break; break;
} }
error.error = error.statusText.toLowerCase().split(" ").join("_"); error.error = error.statusText.toLowerCase().split(" ").join("_");
...@@ -345,39 +368,229 @@ jIO.addStorageType("dav", function (spec, my) { ...@@ -345,39 +368,229 @@ jIO.addStorageType("dav", function (spec, my) {
// DAV REQUESTS // // DAV REQUESTS //
/** /**
* Retrieve a document * Retrieve a document file
* @method dav.getDocument * @method dav.getDocument
* @param {string} doc_id The document id * @param {string} doc_id The document id
* @param {function} then The callback(err, response)
*/ */
dav.getDocument = function (doc_id) { dav.getDocument = function (doc_id) {
var doc, jql = priv.makeJQLikeCallback(); var doc, jql = priv.makeJQLikeCallback(), error = null;
priv.ajax(doc_id, undefined, "GET").always(function (one, state, three) { priv.ajax(doc_id, undefined, "GET").always(function (one, state, three) {
if (state !== "success") { if (state !== "success") {
jql.respond(priv.ajaxErrorToJioError( error = priv.ajaxErrorToJioError(
one, one,
"Cannot retrieve document", "Cannot retrieve document",
"Unknown error" "Unknown"
), undefined); );
} else { if (one.status === 404) {
error.reason = "Not Found";
}
return jql.respond(error, undefined);
}
try { try {
doc = JSON.parse(one); doc = JSON.parse(one);
} catch (e) { } catch (e) {
return jql.respond(priv.createError( return jql.respond(priv.createError(
24, 24,
"Cannot parse document", "Cannot parse document",
"Document is broken" "Document is corrupted"
), undefined); ), undefined);
} }
// document health is good // document health is good
jql.respond(undefined, doc); return jql.respond(undefined, doc);
});
return jql.to_return;
};
/**
* Retrieve an attachment file
* @method dav.getAttachment
* @param {string} doc_id The document id
* @param {string} attachment_id The attachment id
*/
dav.getAttachment = function (doc_id, attachment_id) {
var jql = priv.makeJQLikeCallback(), error = null;
priv.ajax(
doc_id,
attachment_id,
"GET"
).always(function (one, state, three) {
if (state !== "success") {
error = priv.ajaxErrorToJioError(
one,
"Cannot retrieve attachment",
"Unknown"
);
if (one.status === 404) {
error.reason = "Not Found";
} }
return jql.respond(error, undefined);
}
return jql.respond(undefined, one);
}); });
return jql.to_return; return jql.to_return;
}; };
/**
* Uploads a document file
* @method dav.putDocument
* @param {object} doc The document object
*/
dav.putDocument = function (doc) { dav.putDocument = function (doc) {
// TODO var jql = priv.makeJQLikeCallback();
priv.ajax(doc._id, undefined, "PUT", {
"dataType": "text",
"data": JSON.stringify(doc)
}).always(function (one, state, three) {
if (state !== "success") {
return jql.respond(priv.ajaxErrorToJioError(
one,
"Cannot upload document",
"Unknown"
), undefined);
}
jql.respond(undefined, {"ok": true, "id": doc._id});
});
return jql.to_return;
};
/**
* Uploads an attachment file
* @method dav.putAttachment
* @param {string} doc_id The document id
* @param {string} attachment_id The attachment id
* @param {string} data The attachment data
*/
dav.putAttachment = function (doc_id, attachment_id, data) {
var jql = priv.makeJQLikeCallback();
priv.ajax(doc_id, attachment_id, "PUT", {
"dataType": "text",
"data": data
}).always(function (one, state, three) {
if (state !== "success") {
return jql.respond(priv.ajaxErrorToJioError(
one,
"Cannot upload attachment",
"Unknown"
), undefined);
}
return jql.respond(undefined, {
"ok": true,
"id": doc_id,
"attachment": attachment_id
});
});
return jql.to_return;
};
/**
* Deletes a document file
* @method dav.removeDocument
* @param {string} doc_id The document id
*/
dav.removeDocument = function (doc_id) {
var jql = priv.makeJQLikeCallback(), error = null;
priv.ajax(
doc_id,
undefined,
"DELETE"
).always(function (one, state, three) {
if (state !== "success") {
error = priv.ajaxErrorToJioError(
one,
"Cannot delete document",
"Unknown"
);
if (one.status === 404) {
error.reason = "Not Found";
}
return jql.respond(error, undefined);
}
jql.respond(undefined, {"ok": true, "id": doc_id});
});
return jql.to_return;
};
/**
* Deletes an attachment file
* @method dav.removeAttachment
* @param {string} doc_id The document id
* @param {string} attachment_id The attachment id
*/
dav.removeAttachment = function (doc_id, attachment_id) {
var jql = priv.makeJQLikeCallback(), error = null;
priv.ajax(
doc_id,
attachment_id,
"DELETE"
).always(function (one, state, three) {
if (state !== "success") {
error = priv.ajaxErrorToJioError(
one,
"Cannot delete attachment",
"Unknown"
);
if (one.status === 404) {
error.reason = "Not Found";
}
return jql.respond(error, undefined);
}
jql.respond(undefined, {"ok": true, "id": doc_id});
});
return jql.to_return;
};
/**
* Get a list of document file
* @method dav.allDocs
*/
dav.allDocs = function () {
var jql = priv.makeJQLikeCallback(), rows = [];
priv.ajax(undefined, undefined, "PROPFIND", {
"dataType": "xml",
"headers": {"Depth": 1}
}).always(function (one, state, three) {
var response, len;
if (state !== "success") {
return jql.respond(priv.ajaxErrorToJioError(
one,
"Cannot get the document list",
"Unknown"
), undefined);
}
response = $(one).find("D\\:response, response");
len = response.length;
if (len === 1) {
return jql.respond({"total_rows": 0, "rows": []});
}
response.each(function (i, data) {
var row;
if (i > 0) { // exclude parent folder
row = {
"id": "",
"key": "",
"value": {}
};
$(data).find("D\\:href, href").each(function () {
row.id = $(this).text().split('/').slice(-1)[0];
row.id = priv.fileNameToIds(row.id);
if (row.id.length !== 1) {
row = undefined;
} else {
row.id = row.id[0];
row.key = row.id;
}
});
if (row !== undefined) {
rows.push(row);
}
}
});
jql.respond(undefined, {
"total_rows": rows.length,
"rows": rows
});
});
return jql.to_return;
}; };
// JIO COMMANDS // // JIO COMMANDS //
...@@ -414,46 +627,32 @@ jIO.addStorageType("dav", function (spec, my) { ...@@ -414,46 +627,32 @@ jIO.addStorageType("dav", function (spec, my) {
*/ */
that.post = function (command) { that.post = function (command) {
var doc_id = command.getDocId() || priv.generateUuid(); var doc_id = command.getDocId() || priv.generateUuid();
priv.ajax(doc_id, undefined, "GET").always(function (one, state, three) { dav.getDocument(doc_id).always(function (err, response) {
if (state !== "success") { if (err) {
if (one.status === 404) { if (err.status === 404) {
// the document does not already exist // the document does not already exist
// updating document // updating document
priv.ajax(doc_id, undefined, "PUT", { var doc = command.cloneDoc();
"dataType": "text", doc._id = doc_id;
"data": JSON.stringify(command.cloneDoc()) return dav.putDocument(doc).always(function (err, response) {
}).always(function (one, state, three) { if (err) {
if (state !== "success") { return that.retry(err);
// an error occured
that.retry(priv.ajaxErrorToJioError(
one,
"An error occured when trying to PUT data",
"Unknown"
));
} else {
// document updated
that.success({
"ok": true,
"id": doc_id
});
} }
return that.success(response);
}); });
} else { }
if (err.status === 24) {
return that.error(err);
}
// an error occured // an error occured
that.retry(priv.ajaxErrorToJioError( return that.retry(err);
one,
"An error occured when trying to GET data",
"Unknown"
));
} }
} else {
// the document already exists // the document already exists
that.error(priv.createError( return that.error(priv.createError(
405, 405,
"Cannot create document", "Cannot create document",
"Document already exists" "Document already exists"
)); ));
}
}); });
}; };
...@@ -463,25 +662,13 @@ jIO.addStorageType("dav", function (spec, my) { ...@@ -463,25 +662,13 @@ jIO.addStorageType("dav", function (spec, my) {
* @param {object} command The JIO command * @param {object} command The JIO command
*/ */
that.put = function (command) { that.put = function (command) {
var doc_id = command.getDocId(); dav.putDocument(command.cloneDoc()).always(function (err, response) {
priv.ajax(doc_id, undefined, "PUT", { if (err) {
"dataType": "text",
"data": JSON.stringify(command.cloneDoc())
}).always(function (one, state, three) {
if (state !== "success") {
// an error occured // an error occured
that.retry(priv.ajaxErrorToJioError( return that.retry(err);
one,
"Cannot update document",
"Unknown error"
));
} else {
// document updated
that.success({
"ok": true,
"id": doc_id
});
} }
// document updated
return that.success(response);
}); });
}; };
...@@ -493,29 +680,17 @@ jIO.addStorageType("dav", function (spec, my) { ...@@ -493,29 +680,17 @@ jIO.addStorageType("dav", function (spec, my) {
that.putAttachment = function (command) { that.putAttachment = function (command) {
var doc = null, doc_id = command.getDocId(), attachment_id, tmp; var doc = null, doc_id = command.getDocId(), attachment_id, tmp;
attachment_id = command.getAttachmentId(); attachment_id = command.getAttachmentId();
priv.ajax(doc_id, undefined, "GET").always(function (one, state, three) { dav.getDocument(doc_id).always(function (err, response) {
if (state !== "success") { if (err) {
// document not found or error // document not found or error
tmp = that.retry; tmp = that.retry;
if (one.status === 404) { if (err.status === 404 ||
err.status === 24) {
tmp = that.error; tmp = that.error;
} }
tmp(priv.ajaxErrorToJioError( return tmp(err);
one,
"Cannot update document",
"Unknown error"
));
} else {
try {
doc = JSON.parse(one);
} catch (e) {
return that.error(priv.createError(
24,
"Cannot upload attachment",
"Document is broken"
));
} }
// document health is good doc = response;
doc._attachments = doc._attachments || {}; doc._attachments = doc._attachments || {};
doc._attachments[attachment_id] = { doc._attachments[attachment_id] = {
"length": command.getAttachmentLength(), "length": command.getAttachmentLength(),
...@@ -523,40 +698,24 @@ jIO.addStorageType("dav", function (spec, my) { ...@@ -523,40 +698,24 @@ jIO.addStorageType("dav", function (spec, my) {
"content_type": command.getAttachmentMimeType() "content_type": command.getAttachmentMimeType()
}; };
// put the attachment // put the attachment
priv.ajax(doc_id, attachment_id, "PUT", { dav.putAttachment(
"dataType": "text", doc_id,
"data": command.getAttachmentData() attachment_id,
}).always(function (one, state, three) { command.getAttachmentData()
if (state !== "success") { ).always(function (err, response) {
if (err) {
// an error occured // an error occured
that.retry(priv.ajaxErrorToJioError( return that.retry(err);
one, }
"An error occured when trying to PUT data",
"Unknown"
));
} else {
// update the document // update the document
priv.ajax(doc_id, undefined, "PUT", { dav.putDocument(doc).always(function (err, response) {
"dataType": "text", if (err) {
"data": JSON.stringify(doc) return that.retry(err);
}).always(function (one, state, three) {
if (state !== "success") {
that.retry(priv.ajaxErrorToJioError(
one,
"An error occured when trying to PUT data",
"Unknown"
));
} else {
that.success({
"ok": true,
"id": doc_id,
"attachment": attachment_id
});
} }
response.attachment = attachment_id;
return that.success(response);
}); });
}
}); });
}
}); });
}; };
...@@ -568,7 +727,8 @@ jIO.addStorageType("dav", function (spec, my) { ...@@ -568,7 +727,8 @@ jIO.addStorageType("dav", function (spec, my) {
that.get = function (command) { that.get = function (command) {
dav.getDocument(command.getDocId()).always(function (err, response) { dav.getDocument(command.getDocId()).always(function (err, response) {
if (err) { if (err) {
if (err.status === 404) { if (err.status === 404 ||
err.status === 24) {
return that.error(err); return that.error(err);
} }
return that.retry(err); return that.retry(err);
...@@ -578,176 +738,120 @@ jIO.addStorageType("dav", function (spec, my) { ...@@ -578,176 +738,120 @@ jIO.addStorageType("dav", function (spec, my) {
}; };
/** /**
* Remove a document or attachment * Get an attachment
* @method remove * @method getAttachment
* @param {object} command The JIO command * @param {object} command The JIO command
*/ */
that._remove = function (command) { that.getAttachment = function (command) {
var docid = command.getDocId(), doc, url, dav.getAttachment(
secured_docid, secured_attachmentid, attachment_url, command.getDocId(),
attachment_list = [], i, j, k = 1, deleteAttachment, ajax_object; command.getAttachmentId()
).always(function (err, response) {
if (priv.support(docid)) { if (err) {
return; if (err.status === 404) {
return that.error(err);
} }
return that.retry(err);
secured_docid = priv.secureDocId(command.getDocId());
url = priv.url + '/' + priv.underscoreFileExtenisons(secured_docid);
// remove attachment
if (typeof command.getAttachmentId() === "string") {
secured_attachmentid = priv.secureDocId(command.getAttachmentId());
attachment_url = url + '.' + priv.underscoreFileExtenisons(
secured_attachmentid
);
ajax_object = {
url: attachment_url + '?_=' + Date.now(),
type: "DELETE",
success: function () {
// retrieve underlying document
ajax_object = {
url: url + '?_=' + Date.now(),
type: "GET",
dataType: "text",
success: function (response) {
// underlying document
doc = JSON.parse(response);
// update doc._attachments
if (typeof doc._attachments === "object") {
if (typeof doc._attachments[command.getAttachmentId()] ===
"object") {
delete doc._attachments[command.getAttachmentId()];
if (priv.objectIsEmpty(doc._attachments)) {
delete doc._attachments;
} }
// PUT back to server return that.success(response);
ajax_object = {
url: url + '?_=' + Date.now(),
type: 'PUT',
data: JSON.stringify(doc),
success: function () {
that.success({
"ok": true,
"id": command.getDocId() + '/' +
command.getAttachmentId()
}); });
}, };
error: function () {
that.error(priv.createError(409, /**
"Cannot modify document", "Error saving attachment" * Remove a document
)); * @method remove
* @param {object} command The JIO command
*/
that.remove = function (command) {
var doc_id = command.getDocId(), count = 0, end;
end = function () {
count -= 1;
if (count === 0) {
that.success({"ok": true, "id": doc_id});
} }
}; };
priv.ajax(ajax_object); dav.getDocument(doc_id).always(function (err, response) {
} else { var attachment_id = null;
// sure this if-else is needed? if (err) {
that.error(priv.createError(404, if (err.status === 404) {
"Cannot find document", "Error updating attachment" return that.error(err);
));
} }
} else { if (err.status !== 24) { // 24 -> corrupted document
// no attachments, we are done return that.retry(err);
that.success({
"ok": true,
"id": command.getDocId() + '/' + command.getAttachmentId()
});
} }
}, response = {};
error: function () {
that.error(priv.createError(404,
"Cannot find the document", "Document does not exist"
));
} }
}; count += 2;
priv.ajax(ajax_object); dav.removeDocument(doc_id).always(function (err, response) {
}, if (err) {
error: function () { if (err.status === 404) {
that.error(priv.createError(404, return that.error(err);
"Cannot find the attachment", "Error removing attachment"
));
} }
}; return that.retry(err);
priv.ajax(ajax_object); }
// remove document incl. all attachments return end();
} else {
ajax_object = {
url: url + '?_=' + Date.now(),
type: 'GET',
dataType: 'text',
success: function (response) {
var x;
doc = JSON.parse(response);
// prepare attachment loop
if (typeof doc._attachments === "object") {
// prepare list of attachments
for (x in doc._attachments) {
if (doc._attachments.hasOwnProperty(x)) {
attachment_list.push(x);
}
}
}
// delete document
ajax_object = {
url: url + '?_=' + Date.now(),
type: 'DELETE',
success: function () {
j = attachment_list.length;
// no attachments, done
if (j === 0) {
that.success({
"ok": true,
"id": command.getDocId()
});
} else {
deleteAttachment = function (attachment_url, j, k) {
ajax_object = {
url: attachment_url + '?_=' + Date.now(),
type: 'DELETE',
success: function () {
// all deleted, return response, need k as async couter
if (j === k) {
that.success({
"ok": true,
"id": command.getDocId()
}); });
} else { for (attachment_id in response._attachments) {
k += 1; if (response._attachments.hasOwnProperty(attachment_id)) {
count += 1;
dav.removeAttachment(
doc_id,
attachment_id
).always(end);
} }
},
error: function () {
that.error(priv.createError(404,
"Cannot find attachment", "Error removing attachment"
));
} }
end();
});
}; };
priv.ajax(ajax_object);
}; /**
for (i = 0; i < j; i += 1) { * Remove an attachment
secured_attachmentid = priv.secureDocId(attachment_list[i]); * @method removeAttachment
attachment_url = url + '.' + priv.underscoreFileExtenisons( * @param {object} command The JIO command
secured_attachmentid */
); that.removeAttachment = function (command) {
deleteAttachment(attachment_url, j, k); var doc_id = command.getDocId(), doc, attachment_id;
attachment_id = command.getAttachmentId();
dav.getDocument(doc_id).always(function (err, response) {
var still_has_attachments;
if (err) {
if (err.status === 404 ||
err.status === 24) {
return that.error(err);
} }
return that.retry(err);
} }
}, doc = response;
error: function () { if (typeof (doc._attachments || {})[attachment_id] !== "object") {
that.error(priv.createError(404, return that.error(priv.createError(
"Cannot find the document", "Error removing document" 404,
"Cannot remove attachment",
"Not Found"
)); ));
} }
}; delete doc._attachments[attachment_id];
priv.ajax(ajax_object); // check if there is still attachments
}, for (still_has_attachments in doc._attachments) {
error: function () { if (doc._attachments.hasOwnProperty(still_has_attachments)) {
that.error(priv.createError(404, break;
"Cannot find the document", "Document does not exist"
));
} }
};
priv.ajax(ajax_object);
} }
if (still_has_attachments === undefined) {
delete doc._attachments;
}
doc._id = doc_id;
dav.putDocument(doc).always(function (err, response) {
if (err) {
return that.retry(err);
}
dav.removeAttachment(
doc_id,
attachment_id
).always(function (err, response) {
that.success({"ok": true, "id": doc_id, "attachment": attachment_id});
});
});
});
}; };
/** /**
...@@ -757,112 +861,41 @@ jIO.addStorageType("dav", function (spec, my) { ...@@ -757,112 +861,41 @@ jIO.addStorageType("dav", function (spec, my) {
* @method allDocs * @method allDocs
* @param {object} command The JIO command * @param {object} command The JIO command
*/ */
//{ that.allDocs = function (command) {
// "total_rows": 4, var count = 0, end, rows;
// "rows": [ end = function () {
// { count -= 1;
// "id": "otherdoc", if (count === 0) {
// "key": "otherdoc", that.success(rows);
// "value": {
// "rev": "1-3753476B70A49EA4D8C9039E7B04254C"
// }
// },{...}
// ]
//}
that._allDocs = function (command) {
var rows = [], url,
am = priv.newAsyncModule(),
o = {},
ajax_object;
o.getContent = function (file) {
var docid = priv.secureDocId(file.id),
url = priv.url + '/' + docid;
ajax_object = {
url: url + '?_=' + Date.now(),
type: 'GET',
dataType: 'text',
success: function (content) {
file.doc = JSON.parse(content);
rows.push(file);
am.call(o, 'success');
},
error: function (type) {
that.error(priv.createError(404,
"Cannot find the document", "Can't get document from storage"
));
am.call(o, 'error', [type]);
} }
}; };
priv.ajax(ajax_object); dav.allDocs().always(function (err, response) {
}; if (err) {
o.getDocumentList = function () { return that.retry(err);
url = priv.url + '/';
ajax_object = {
url: url + '?_=' + Date.now(),
type: "PROPFIND",
dataType: "xml",
headers : { depth: '1' },
success: function (xml) {
var response = $(xml).find('D\\:response, response'),
len = response.length;
if (len === 1) {
return am.call(o, 'success');
} }
am.wait(o, 'success', len - 2); if (command.getOption("include_docs") === true) {
response.each(function (i, data) { count += 1;
if (i > 0) { // exclude parent folder rows = response;
var file = { rows.rows.forEach(function (row) {
value: {} count += 1;
}; dav.getDocument(
$(data).find('D\\:href, href').each(function () { row.id
var split = $(this).text().split('/'); ).always(function (err, response) {
file.id = split[split.length - 1]; if (err) {
file.id = priv.restoreSlashes(file.id); if (err.status === 404 || err.status === 24) {
file.key = file.id; return that.error(err);
});
if (command.getOption('include_docs')) {
am.call(o, 'getContent', [file]);
} else {
rows.push(file);
am.call(o, 'success');
} }
return that.retry(err);
} }
row.doc = response;
end();
}); });
},
error: function (type) {
that.error(priv.createError(404,
"Cannot find the document", "Can't get document list"
));
am.call(o, 'retry', [type]);
}
};
priv.ajax(ajax_object);
};
o.retry = function (error) {
am.neverCall(o, 'retry');
am.neverCall(o, 'success');
am.neverCall(o, 'error');
that.retry(error);
};
o.error = function (error) {
am.neverCall(o, 'retry');
am.neverCall(o, 'success');
am.neverCall(o, 'error');
that.error(error);
};
o.success = function () {
am.neverCall(o, 'retry');
am.neverCall(o, 'success');
am.neverCall(o, 'error');
that.success({
total_rows: rows.length,
rows: rows
}); });
}; end();
// first get the XML list } else {
am.call(o, 'getDocumentList'); that.success(response);
}
});
}; };
priv.__init__(spec); priv.__init__(spec);
......
...@@ -3634,21 +3634,6 @@ test ("Put", function(){ ...@@ -3634,21 +3634,6 @@ test ("Put", function(){
o.tick(o); o.tick(o);
o.server.restore(); o.server.restore();
o.jio.stop();
});
test ("PutAttachment", function(){
var o = generateTools(this);
o.jio = JIO.newJio({
"type": "dav",
"url": "https://ca-davstorage:8080",
"auth_type": "basic",
"username": "admin",
"password": "pwd"
});
// putAttachment without document id => 20 Id Required // putAttachment without document id => 20 Id Required
o.spy(o, "status", 20, "PutAttachment without doc id -> 20"); o.spy(o, "status", 20, "PutAttachment without doc id -> 20");
o.jio.putAttachment({"_attachment": "body.html"}, o.f); o.jio.putAttachment({"_attachment": "body.html"}, o.f);
...@@ -3777,97 +3762,241 @@ test ("Get", function(){ ...@@ -3777,97 +3762,241 @@ test ("Get", function(){
o.tick(o); o.tick(o);
o.server.restore(); o.server.restore();
// get inexistent attachment
o.server = sinon.fakeServer.create();
o.server.respondWith(
"GET",
/https:\/\/ca-davstorage:8080\/http:%252F%252F100%2525_\.json\.body_\.html/,
[
404,
{"Content-Type": "text/html"},
"<h1>Not Found</h1>"
]
);
o.spy(o, "status", 404, "Get inexistent attachment -> 404");
o.jio.getAttachment({
"_id": "http://100%.json",
"_attachment": "body.html"
}, {"max_retry": 1}, o.f);
o.clock.tick(1000);
o.server.respond();
o.tick(o);
o.server.restore();
// get attachment
o.server = sinon.fakeServer.create();
o.server.respondWith(
"GET",
/https:\/\/ca-davstorage:8080\/http:%252F%252F100%2525_\.json\.body_\.html/,
[
200,
{"Content-Type": "text/plain"},
"My Attachment Content"
]
);
o.spy(o, "value", "My Attachment Content", "Get attachment");
o.jio.getAttachment({
"_id": "http://100%.json",
"_attachment": "body.html"
}, {"max_retry": 1}, o.f);
o.clock.tick(1000);
o.server.respond();
o.tick(o);
o.server.restore();
o.jio.stop(); o.jio.stop();
}); });
/*
test ("Remove", function(){ test ("Remove", function(){
var o = generateTools(this); var o = generateTools(this);
o.jio = JIO.newJio({ o.jio = JIO.newJio({
"type": "dav", "type": "dav",
"username": "davremove", "url": "https://ca-davstorage:8080",
"password": "checkpwd", "auth_type": "basic",
"url": "https://ca-davstorage:8080" "username": "admin",
"password": "pwd"
}); });
// remove inexistent document // remove inexistent document
o.addFakeServerResponse("dav", "GET", "remove1", 404, "HTML RESPONSE"); o.server = sinon.fakeServer.create();
o.spy(o, "status", 404, "Remove non existening document"); o.server.respondWith(
o.jio.remove({"_id": "remove1"}, o.f); "GET",
o.clock.tick(5000); /https:\/\/ca-davstorage:8080\/http:%252F%252F100%2525_\.json/,
[
404,
{"Content-Type": "text/html"},
"<h1>Not Found</h1>"
]
);
o.spy(o, "status", 404, "Remove inexistent document -> 404");
o.jio.remove({"_id": "http://100%.json"}, {"max_retry": 1}, o.f);
o.clock.tick(1000);
o.server.respond(); o.server.respond();
o.tick(o);
o.server.restore();
// remove inexistent document/attachment // remove document
o.addFakeServerResponse("dav", "GET", "remove1.remove2", 404, "HTML" + o.server = sinon.fakeServer.create();
"RESPONSE"); o.server.respondWith(
o.spy(o, "status", 404, "Remove inexistent document/attachment"); "GET",
o.jio.remove({"_id": "remove1/remove2"}, o.f); /https:\/\/ca-davstorage:8080\/http:%252F%252F100%2525_\.json/,
o.clock.tick(5000); [
200,
{"Content-Type": "text/html"},
"{My corrupted document}"
]
);
o.server.respondWith(
"DELETE",
/https:\/\/ca-davstorage:8080\/http:%252F%252F100%2525_\.json/,
[
200,
{"Content-Type": "text/plain"},
"<h1>Deleted</h1>"
]
);
o.spy(o, "value", {"ok": true, "id": "http://100%.json"},
"Remove document");
o.jio.remove({"_id": "http://100%.json"}, {"max_retry": 1}, o.f);
o.clock.tick(1000);
o.server.respond(); o.server.respond();
o.tick(o);
o.server.restore();
// remove document // remove inexistent attachment
o.answer = JSON.stringify({"_id": "remove3", "title": "some doc"}); o.server = sinon.fakeServer.create();
o.addFakeServerResponse("dav", "GET", "remove3", 200, o.answer); o.server.respondWith(
o.addFakeServerResponse("dav", "DELETE", "remove3", 200, "HTML RESPONSE"); "GET",
o.spy(o, "value", {"ok": true, "id": "remove3"}, "Remove document"); /https:\/\/ca-davstorage:8080\/http:%252F%252F100%2525_\.json/,
o.jio.remove({"_id": "remove3"}, o.f); [
o.clock.tick(5000); 200,
{"Content-Type": "text/plain"},
"{}"
]
);
o.spy(o, "status", 404, "Remove inexistent attachment -> 404");
o.jio.removeAttachment({
"_id": "http://100%.json",
"_attachment": "body.html"
}, {"max_retry": 1}, o.f);
o.clock.tick(1000);
o.server.respond(); o.server.respond();
o.tick(o);
// o.server.respond();
// o.server.respond();
o.server.restore();
o.answer = JSON.stringify({ // remove attachment
"_id": "remove4", o.server = sinon.fakeServer.create();
"title": "some doc", o.server.respondWith(
"GET",
/https:\/\/ca-davstorage:8080\/http:%252F%252F100%2525_\.json/,
[
200,
{"Content-Type": "text/plain"},
JSON.stringify({
"_attachments": { "_attachments": {
"remove5": { "body.html": {
"length": 4, "length": 32,
"digest": "md5-d41d8cd98f00b204e9800998ecf8427e" "digest": "md5-dontcare",
"content_type": "text/html"
} }
} }
}); })
// remove attachment ]
o.addFakeServerResponse("dav", "GET", "remove4", 200, o.answer); );
o.addFakeServerResponse("dav", "PUT", "remove4", 201, "HTML RESPONSE"); o.server.respondWith(
o.addFakeServerResponse("dav", "DELETE", "remove4.remove5", 200, "HTML"+ "PUT",
"RESPONSE"); /https:\/\/ca-davstorage:8080\/http:%252F%252F100%2525_\.json/,
o.spy(o, "value", {"ok": true, "id": "remove4/remove5"}, [
"Remove attachment"); 200,
o.jio.remove({"_id": "remove4/remove5"}, o.f); {"Content-Type": "text/html"},
o.clock.tick(5000); "<h1>OK</h1>"
]
);
o.server.respondWith(
"DELETE",
/https:\/\/ca-davstorage:8080\/http:%252F%252F100%2525_\.json.body_\.html/,
[
200,
{"Content-Type": "text/html"},
"<h1>OK</h1>"
]
);
o.spy(o, "value", {
"ok": true,
"id": "http://100%.json",
"attachment": "body.html"
}, "Remove attachment");
o.jio.removeAttachment({
"_id": "http://100%.json",
"_attachment": "body.html"
}, {"max_retry": 1}, o.f);
o.clock.tick(1000);
o.server.respond(); o.server.respond();
o.tick(o);
o.server.restore();
o.answer = JSON.stringify({ // remove document with multiple attachments
"_id": "remove6", o.server = sinon.fakeServer.create();
"title": "some other doc", o.server.respondWith(
"GET",
/https:\/\/ca-davstorage:8080\/http:%252F%252F100%2525_\.json/,
[
200,
{"Content-Type": "text/html"},
JSON.stringify({
"_attachments": { "_attachments": {
"remove7": { "body.html": {
"length": 4, "length": 32,
"digest": "md5-d41d8cd98f00b204e9800998ecf8427e" "digest": "md5-dontcare",
}, "content_type": "text/html"
"remove8": {
"length": 4,
"digest": "md5-e41d8cd98f00b204e9800998ecf8427e"
}, },
"remove9": { "other": {
"length": 4, "length": 3,
"digest": "md5-f41d8cd98f00b204e9800998ecf8427e" "digest": "md5-dontcare-again",
"content_type": "text/plain"
} }
} }
}); })
// remove document with multiple attachments ]
o.addFakeServerResponse("dav", "GET", "remove6", 200, o.answer); );
o.addFakeServerResponse("dav", "DELETE", "remove6.remove7", 200, "HTML"+ o.server.respondWith(
"RESPONSE"); "DELETE",
o.addFakeServerResponse("dav", "DELETE", "remove6.remove8", 200, "HTML"+ /https:\/\/ca-davstorage:8080\/http:%252F%252F100%2525_\.json/,
"RESPONSE"); [
o.addFakeServerResponse("dav", "DELETE", "remove6.remove9", 200, "HTML"+ 200,
"RESPONSE"); {"Content-Type": "text/plain"},
o.addFakeServerResponse("dav", "DELETE", "remove6", 200, "HTML RESPONSE"); "<h1>Deleted</h1>"
o.spy(o, "value", {"ok": true, "id": "remove6"}, ]
"Remove document with multiple attachments"); );
o.jio.remove({"_id": "remove6"}, o.f); o.server.respondWith(
o.clock.tick(5000); "DELETE",
/https:\/\/ca-davstorage:8080\/http:%252F%252F100%2525_\.json\.body_\.html/,
[
200,
{"Content-Type": "text/plain"},
"<h1>Deleted</h1>"
]
);
o.server.respondWith(
"DELETE",
/https:\/\/ca-davstorage:8080\/http:%252F%252F100%2525_\.json\.other/,
[
200,
{"Content-Type": "text/plain"},
"<h1>Deleted</h1>"
]
);
o.spy(o, "value", {"ok": true, "id": "http://100%.json"},
"Remove document containing multiple attachments");
o.jio.remove({"_id": "http://100%.json"}, {"max_retry": 1}, o.f);
o.clock.tick(1000);
o.server.respond(); o.server.respond();
o.tick(o);
o.server.restore();
o.jio.stop(); o.jio.stop();
}); });
...@@ -3880,43 +4009,92 @@ test ("AllDocs", function () { ...@@ -3880,43 +4009,92 @@ test ("AllDocs", function () {
o.jio = JIO.newJio({ o.jio = JIO.newJio({
"type": "dav", "type": "dav",
"username": "davall", "url": "https://ca-davstorage:8080",
"password": "checkpwd", "auth_type": "basic",
"url": "https://ca-davstorage:8080" "username": "admin",
"password": "pwd"
}); });
// get allDocs, no content // get all documents
o.addFakeServerResponse("dav", "PROPFIND", "", 200, davlist); o.server = sinon.fakeServer.create();
o.thisShouldBeTheAnswer = { o.server.respondWith(
"PROPFIND",
/https:\/\/ca-davstorage:8080\//,
[
200,
{"Content-Type": "text/xml"},
davlist
]
);
o.spy(o, "value", {
"rows": [ "rows": [
{"id": "alldocs1", "key": "alldocs1", "value": {}}, {"id": "http://100%.json", "key": "http://100%.json", "value": {}},
{"id": "alldocs2", "key": "alldocs2", "value": {}} {"id": "ISBN:1038729410372", "key": "ISBN:1038729410372", "value": {}}
], ],
"total_rows": 2 "total_rows": 2
} }, "allDocs");
o.spy(o, "value", o.thisShouldBeTheAnswer, "allDocs (no content)");
o.jio.allDocs(o.f); o.jio.allDocs(o.f);
o.clock.tick(5000); o.clock.tick(1000);
o.server.respond(); o.server.respond();
o.tick(o);
o.server.restore();
// allDocs with option include // allDocs with option include_docs
o.all1 = {"_id": "allDocs1", "title": "a doc title"}; o.server = sinon.fakeServer.create();
o.all2 = {"_id": "allDocs2", "title": "another doc title"}; o.server.respondWith(
o.thisShouldBeTheAnswer = { "PROPFIND",
"rows": [ /https:\/\/ca-davstorage:8080\//,
{"id": "alldocs1", "key": "alldocs1", "value": {}, "doc": o.all1}, [
{"id": "alldocs2", "key": "alldocs2", "value": {}, "doc": o.all2} 200,
], {"Content-Type": "text/xml"},
davlist
]
);
o.doc1 = {"_id": "http://100%.json", "_attachments": {
"body.html": {
"length": 32,
"digest": "md5-doncare",
"content_type": "text/html"
}
}};
o.doc2 = {"_id": "ISBN:1038729410372", "title": "Book Title"};
o.server.respondWith(
"GET",
/https:\/\/ca-davstorage:8080\/http:%252F%252F100%2525_\.json/,
[
200,
{"Content-Type": "text/plain"},
JSON.stringify(o.doc1)
]
);
o.server.respondWith(
"GET",
/https:\/\/ca-davstorage:8080\/ISBN:1038729410372/,
[
200,
{"Content-Type": "text/plain"},
JSON.stringify(o.doc2)
]
);
o.spy(o, "value", {
"rows": [{
"id": "http://100%.json",
"key": "http://100%.json",
"value": {},
"doc": o.doc1
}, {
"id": "ISBN:1038729410372",
"key": "ISBN:1038729410372",
"value": {},
"doc": o.doc2
}],
"total_rows": 2 "total_rows": 2
} }, "allDocs (include_docs)");
o.addFakeServerResponse("dav", "GET", "alldocs1", 200, o.jio.allDocs({"include_docs": true}, o.f);
JSON.stringify(o.all1)); o.clock.tick(1000);
o.addFakeServerResponse("dav", "GET", "alldocs2", 200,
JSON.stringify(o.all2));
o.spy(o, "value", o.thisShouldBeTheAnswer, "allDocs (include_docs)");
o.jio.allDocs({"include_docs":true}, o.f);
o.clock.tick(5000);
o.server.respond(); o.server.respond();
o.tick(o);
o.server.restore();
o.jio.stop(); o.jio.stop();
}); });
......
...@@ -25,7 +25,7 @@ ...@@ -25,7 +25,7 @@
</D:propstat> </D:propstat>
</D:response> </D:response>
<D:response xmlns:lp1="DAV:" xmlns:lp2="http://apache.org/dav/props/"> <D:response xmlns:lp1="DAV:" xmlns:lp2="http://apache.org/dav/props/">
<D:href>/some/path/alldocs1</D:href> <D:href>/some/path/http:%252F%252F100%2525_.json</D:href>
<D:propstat> <D:propstat>
<D:prop> <D:prop>
<lp1:resourcetype/> <lp1:resourcetype/>
...@@ -50,7 +50,32 @@ ...@@ -50,7 +50,32 @@
</D:propstat> </D:propstat>
</D:response> </D:response>
<D:response xmlns:lp1="DAV:" xmlns:lp2="http://apache.org/dav/props/"> <D:response xmlns:lp1="DAV:" xmlns:lp2="http://apache.org/dav/props/">
<D:href>/some/path/alldocs2</D:href> <D:href>/some/path/ISBN:1038729410372</D:href>
<D:propstat>
<D:prop>
<lp1:resourcetype/>
<lp1:creationdate>2012-05-01T17:41:13Z</lp1:creationdate>
<lp1:getcontentlength>223</lp1:getcontentlength>
<lp1:getlastmodified>Wed, 02 May 2012 10:48:33 GMT</lp1:getlastmodified>
<lp1:getetag>"c9-4bf0d1aeb9e43"</lp1:getetag>
<lp2:executable>F</lp2:executable>
<D:supportedlock>
<D:lockentry>
<D:lockscope><D:exclusive/></D:lockscope>
<D:locktype><D:write/></D:locktype>
</D:lockentry>
<D:lockentry>
<D:lockscope><D:shared/></D:lockscope>
<D:locktype><D:write/></D:locktype>
</D:lockentry>
</D:supportedlock>
<D:lockdiscovery/>
</D:prop>
<D:status>HTTP/1.1 200 OK</D:status>
</D:propstat>
</D:response>
<D:response xmlns:lp1="DAV:" xmlns:lp2="http://apache.org/dav/props/">
<D:href>/some/path/http:%252F%252F100%2525_.json.body_.html</D:href>
<D:propstat> <D:propstat>
<D:prop> <D:prop>
<lp1:resourcetype/> <lp1:resourcetype/>
...@@ -75,4 +100,3 @@ ...@@ -75,4 +100,3 @@
</D:propstat> </D:propstat>
</D:response> </D:response>
</D:multistatus> </D:multistatus>
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment