Commit b8faaed2 authored by Sven Franck's avatar Sven Franck

webDav Storage GET new API and qunit tests added

parent ad6aae46
......@@ -400,15 +400,16 @@ jIO.addStorageType('dav', function (spec, my) {
};
/**
* Loads a document from a distant dav storage.
* Get a document or attachment from distant storage
* @method get
* @param {object} command The JIO command
*/
that.get = function (command) {
var doc = command.getDocId(),
var docid = command.getDocId(), doc,
secured_docid;
// no docId
if (!(typeof doc === "string" && doc !== "")) {
if (!(typeof docid === "string" && docid !== "")) {
that.error({
"status": 405,
"statusText": "Method Not Allowed",
......@@ -432,22 +433,24 @@ jIO.addStorageType('dav', function (spec, my) {
secured_docid = priv.secureDocId(command.getDocId());
url = priv.url + '/' + secured_docid;
// get attachment
if (typeof command.getAttachmentId() === "string") {
secured_attachmentid = priv.secureDocId(command.getAttachmentId());
attachment_url = url + '/' + secured_attachmentid;
// get attachment
$.ajax({
url: url + '?_=' + Date.now(),
type: type,
data: command.getDoc(),
url: attachment_url + '?_=' + Date.now(),
type: 'GET',
async: true,
crossdomain: true,
dataType: 'text',
crossdomain : true,
headers : {
Authorization: 'Basic ' + Base64.encode(
priv.username + ':' + priv.password
)
},
// xhrFields: {withCredentials: 'true'},
success: function (response) {
that.success(response)
doc = JSON.parse(response);
that.success(doc);
},
error: function (type) {
that.error({
......@@ -463,18 +466,21 @@ jIO.addStorageType('dav', function (spec, my) {
// get document
$.ajax({
url: url + '?_=' + Date.now(),
type: type,
data: command.getDoc(),
type: 'GET',
async: true,
crossdomain: true,
dataType: 'text',
crossdomain : true,
headers : {
Authorization: 'Basic ' + Base64.encode(
priv.username + ':' + priv.password
)
},
// xhrFields: {withCredentials: 'true'},
success: function (response) {
that.success(response)
// metadata_only should not be handled by jIO, as it is a
// webDav only option, shouldn't it?
// ditto for content_only
doc = JSON.parse(response);
that.success(doc);
},
error: function (type) {
that.error({
......@@ -489,91 +495,6 @@ jIO.addStorageType('dav', function (spec, my) {
}
};
that.get = function (command) {
var secured_docid = priv.secureDocId(command.getDocId()),
doc = {},
getContent = function () {
$.ajax({
url: priv.url + '/' + priv.secured_username + '/' +
priv.secured_application_name + '/' + secured_docid + '?_=' +
Date.now(),
type: "GET",
async: true,
dataType: 'text', // TODO is it necessary ?
headers: {
'Authorization': 'Basic ' + Base64.encode(priv.username + ':' +
priv.password)
},
// xhrFields: {withCredentials: 'true'}, // cross domain
success: function (content) {
doc.content = content;
that.success(doc);
},
error: function (type) {
type.error = type.statusText; // TODO : to lower case
if (type.status === 404) {
type.message = 'Document "' + command.getDocId() +
'" not found.';
type.reason = 'missing';
that.error(type);
} else {
type.reason =
'An error occured when trying to get "' +
command.getDocId() + '"';
type.message = type.reason + '.';
that.retry(type);
}
}
});
};
doc._id = command.getDocId();
// NOTE : if (command.getOption('content_only') { return getContent(); }
// Get properties
$.ajax({
url: priv.url + '/' + priv.secured_username + '/' +
priv.secured_application_name + '/' +
secured_docid + '?_=' + Date.now(),
type: "PROPFIND",
async: true,
dataType: 'xml',
headers: {
'Authorization': 'Basic ' + Base64.encode(priv.username + ':' +
priv.password)
},
success: function (xmlData) {
$(xmlData).find('lp1\\:getlastmodified, getlastmodified').each(
function () {
doc._last_modified = new Date($(this).text()).getTime();
}
);
$(xmlData).find('lp1\\:creationdate, creationdate').each(
function () {
doc._creation_date = new Date($(this).text()).getTime();
}
);
if (!command.getOption('metadata_only')) {
getContent();
} else {
that.success(doc);
}
},
error: function (type) {
if (type.status === 404) {
type.message = 'Cannot find "' + command.getDocId() +
'" informations.';
type.reason = 'missing';
that.error(type);
} else {
type.reason = 'Cannot get "' + command.getDocId() +
'" informations';
type.message = type.reason + '.';
that.retry(type);
}
}
});
};
/**
* Gets a document list from a distant dav storage.
* @method allDocs
......
......@@ -2121,7 +2121,7 @@ test ("PutAttachment", function(){
o.jio = JIO.newJio({
"type": "dav",
"username": "davput",
"username": "davputattm",
"password": "checkpwd",
"url": "https://ca-davstorage:8080"
});
......@@ -2177,6 +2177,62 @@ test ("PutAttachment", function(){
// check for credentials in sinon
});
test ("Get", function(){
var o = generateTools(this);
o.jio = JIO.newJio({
"type": "dav",
"username": "davget",
"password": "checkpwd",
"url": "https://ca-davstorage:8080"
});
// get inexistent document
o.addFakeServerResponse("GET", "get1", 404, "HTML RESPONSE");
o.spy(o, "status", 404, "Get non existing document");
o.jio.get("get1", o.f);
o.clock.tick(5000);
o.server.respond();
// get inexistent attachment
o.addFakeServerResponse("GET", "get1/get2", 404, "HTML RESPONSE");
o.spy(o, "status", 404, "Get non existing attachment");
o.jio.get("get1/get2", o.f);
o.clock.tick(5000);
o.server.respond();
// get document
o.answer = JSON.stringify({"_id": "get3", "title": "some title"});
o.addFakeServerResponse("GET", "get3", 200, o.answer);
o.spy(o, "value", {"_id": "get3", "title": "some title"}, "Get document");
o.jio.get("get3", o.f);
o.clock.tick(5000);
o.server.respond();
// get inexistent attachment (document exists)
o.addFakeServerResponse("GET", "get3/getx", 404, "HTML RESPONSE");
o.spy(o, "status", 404, "Get non existing attachment (doc exists)");
o.jio.get("get3/getx", o.f);
o.clock.tick(5000);
o.server.respond();
// get attachment
o.answer = JSON.stringify({"_id": "get4", "title": "some attachment"});
o.addFakeServerResponse("GET", "get3/get4", 200, o.answer);
o.spy(o, "value", {"_id": "get4", "title": "some attachment"},
"Get attachment");
o.jio.get("get3/get4", o.f);
o.clock.tick(5000);
o.server.respond();
o.jio.stop();
// do the same tests live webDav-Server/simulate CORS
// check for credentials in sinon
});
/*
// note: http errno:
// 200 OK
......@@ -2203,64 +2259,6 @@ test ("PutAttachment", function(){
[200,{},'']);
*/
/*
test ('Document load', function () {
// Test if DavStorage can load documents.
var o = {};
o.davload = getXML('responsexml/davload'),
o.clock = this.sandbox.useFakeTimers();
o.clock.tick(base_tick);
o.t = this;
o.mytest = function (message,doc,errprop,errget) {
var server = o.t.sandbox.useFakeServer();
server.respondWith (
"PROPFIND",
/https:\/\/ca-davstorage:8080\/davload\/jiotests\/file(\?.*|$)/,
[errprop,{'Content-Type':'text/xml; charset="utf-8"'},
o.davload]);
server.respondWith (
"GET",
/https:\/\/ca-davstorage:8080\/davload\/jiotests\/file(\?.*|$)/,
[errget,{},'content']);
o.f = function (err,val) {
if (err) {
err = err.status;
}
deepEqual (err || val,doc,message);
};
o.t.spy(o,'f');
o.jio.get('file',{max_retry:1},o.f);
o.clock.tick(1000);
server.respond();
if (!o.f.calledOnce) {
if (o.f.called) {
ok(false, 'too much results');
} else {
ok(false, 'no response');
}
}
};
o.jio = JIO.newJio({type:'dav',username:'davload',
password:'checkpwd',
url:'https://ca-davstorage:8080',
application_name:'jiotests'});
// note: http errno:
// 200 OK
// 201 Created
// 204 No Content
// 207 Multi Status
// 403 Forbidden
// 404 Not Found
// load an inexistant document.
o.mytest ('load inexistant document',404,404,404);
// load a document.
o.mytest ('load document',{_id:'file',content:'content',
_last_modified:1335953199000,
_creation_date:1335953202000},207,200);
o.jio.stop();
});
test ('Get Document List', function () {
// Test if DavStorage can get a list a document.
......
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