Commit 58115408 authored by Caleb James DeLisle's avatar Caleb James DeLisle

Fixed the test failures on phantomjs and added a configuration option to let...

Fixed the test failures on phantomjs and added a configuration option to let the user choose between getting an attachment as text or as a Blob, defaulting to text.
parent ca3163aa
/*jslint indent: 2, maxlen: 80, sloppy: true, nomen: true, vars: true */ /*jslint indent: 2,
maxlen: 80,
sloppy: true,
nomen: true,
vars: true,
plusplus: true
*/
/*global /*global
jIO: true, jIO: true,
$: true, $: true,
...@@ -92,6 +98,28 @@ jIO.addStorageType('xwiki', function (spec, my) { ...@@ -92,6 +98,28 @@ jIO.addStorageType('xwiki', function (spec, my) {
+ priv.wiki + '/spaces/' + parts.space + '/pages/' + parts.page; + priv.wiki + '/spaces/' + parts.space + '/pages/' + parts.page;
}; };
/**
* Make an HTML5 Blob object.
* Equivilant to the `new Blob()` constructor.
* Will fall back on deprecated BlobBuilder if necessary.
*/
var makeBlob = function (contentArray, options) {
var i, bb, BB;
try {
// use the constructor if possible.
return new Blob(contentArray, options);
} catch (err) {
// fall back on the blob builder.
BB = (window.MozBlobBuilder || window.WebKitBlobBuilder
|| window.BlobBuilder);
bb = new BB();
for (i = 0; i < contentArray.length; i++) {
bb.append(contentArray[i]);
}
return bb.getBlob(options ? options.type : undefined);
}
};
/* /*
* Wrapper for the xwikistorage based on localstorage JiO store. * Wrapper for the xwikistorage based on localstorage JiO store.
*/ */
...@@ -104,14 +132,11 @@ jIO.addStorageType('xwiki', function (spec, my) { ...@@ -104,14 +132,11 @@ jIO.addStorageType('xwiki', function (spec, my) {
* json object and err being the error if any. * json object and err being the error if any.
*/ */
getItem: function (docId, andThen) { getItem: function (docId, andThen) {
$.ajax({
url: getDocRestURL(docId), var success = function (jqxhr) {
type: "GET", var out = {};
async: true, try {
dataType: 'xml', var xd = $(jqxhr.responseText);
success: function (xmlData) {
var out = {};
var xd = $(xmlData);
xd.find('modified').each(function () { xd.find('modified').each(function () {
out._last_modified = Date.parse($(this).text()); out._last_modified = Date.parse($(this).text());
}); });
...@@ -125,21 +150,42 @@ jIO.addStorageType('xwiki', function (spec, my) { ...@@ -125,21 +150,42 @@ jIO.addStorageType('xwiki', function (spec, my) {
out.content = $(this).text(); out.content = $(this).text();
}); });
out._id = docId; out._id = docId;
andThen(out, null); andThen(out, null);
}, } catch (err) {
error: function (jqxhr, err, cause) { andThen(null, {
status: 500,
statusText: "internal error",
error: err,
message: err.message,
reason: ""
});
}
};
$.ajax({
url: getDocRestURL(docId),
type: "GET",
async: true,
dataType: 'xml',
// Use complete instead of success and error because phantomjs
// sometimes causes error to be called with html return code 200.
complete: function (jqxhr) {
if (jqxhr.status === 404) { if (jqxhr.status === 404) {
andThen(null, null); andThen(null, null);
return; return;
} }
andThen(null, { if (jqxhr.status !== 200) {
"status": jqxhr.status, andThen(null, {
"statusText": jqxhr.statusText, "status": jqxhr.status,
"error": err, "statusText": jqxhr.statusText,
"message": "Failed to get document [" + docId + "]", "error": "",
"reason": cause "message": "Failed to get document [" + docId + "]",
}); "reason": ""
});
return;
}
success(jqxhr);
} }
}); });
}, },
...@@ -166,8 +212,11 @@ jIO.addStorageType('xwiki', function (spec, my) { ...@@ -166,8 +212,11 @@ jIO.addStorageType('xwiki', function (spec, my) {
if (contentType.indexOf(';') > -1) { if (contentType.indexOf(';') > -1) {
contentType = contentType.substring(0, contentType.indexOf(';')); contentType = contentType.substring(0, contentType.indexOf(';'));
} }
var blob = new Blob([xhr.response], {type: contentType}); if (priv.useBlobs) {
andThen(blob); andThen(makeBlob([xhr.response], {type: contentType}));
} else {
andThen(xhr.response);
}
} else { } else {
andThen(null, { andThen(null, {
"status": xhr.status, "status": xhr.status,
...@@ -253,7 +302,7 @@ jIO.addStorageType('xwiki', function (spec, my) { ...@@ -253,7 +302,7 @@ jIO.addStorageType('xwiki', function (spec, my) {
} }
var parts = getParts(docId); var parts = getParts(docId);
var blob = (content.constructor === "function Blob() { [native code] }") var blob = (content.constructor === "function Blob() { [native code] }")
? content : new Blob([content], {type: mimeType}); ? content : makeBlob([content], {type: mimeType});
var fd = new FormData(); var fd = new FormData();
fd.append("filepath", blob, fileName); fd.append("filepath", blob, fileName);
fd.append("form_token", formToken); fd.append("form_token", formToken);
...@@ -408,6 +457,10 @@ jIO.addStorageType('xwiki', function (spec, my) { ...@@ -408,6 +457,10 @@ jIO.addStorageType('xwiki', function (spec, my) {
// Which URL to load for getting the Anti-CSRF form token, used for testing. // Which URL to load for getting the Anti-CSRF form token, used for testing.
priv.formTokenPath = spec.formTokenPath || priv.xwikiurl; priv.formTokenPath = spec.formTokenPath || priv.xwikiurl;
// If true then Blob objects will be returned by
// getAttachment() rather than strings.
priv.useBlobs = spec.useBlobs || false;
that.specToStore = function () { that.specToStore = function () {
return { return {
......
...@@ -6280,9 +6280,8 @@ test ('Get revision List', function () { ...@@ -6280,9 +6280,8 @@ test ('Get revision List', function () {
o.jio.stop(); o.jio.stop();
}); });
*/ */
/*
;(function() { ;(function() {
// These tests will only run if we are running the suite inside of XWiki.
module ('Jio XWikiStorage'); module ('Jio XWikiStorage');
var setUp = function(that, liveTest) { var setUp = function(that, liveTest) {
var o = generateTools(that); var o = generateTools(that);
...@@ -6343,10 +6342,11 @@ test ("Post", function () { ...@@ -6343,10 +6342,11 @@ test ("Post", function () {
o.assertReqs(3, "put -> 1 request to get csrf token, 1 to get doc and 1 to post data"); o.assertReqs(3, "put -> 1 request to get csrf token, 1 to get doc and 1 to post data");
// post but document already exists (post = error!, put = ok) // post but document already exists (post = error!, put = ok)
o.answer = JSON.stringify({"_id": "myFile", "title": "hello there"}); o.answer = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>' +
o.addFakeServerResponse("xwiki", "GET", "myFile", 200, o.answer); '<page xmlns="http://www.xwiki.org"><title>hello there</title></page>';
o.addFakeServerResponse("xwiki", "GET", "myFile2", 200, o.answer);
o.spy (o, "status", 409, "Post but document already exists"); o.spy (o, "status", 409, "Post but document already exists");
o.jio.post({"_id": "myFile", "title": "hello again"}, o.f); o.jio.post({"_id": "myFile2", "title": "hello again"}, o.f);
o.clock.tick(5000); o.clock.tick(5000);
o.server.respond(); o.server.respond();
o.assertReqs(1, "post w/ existing doc -> 1 request to get doc then fail"); o.assertReqs(1, "post w/ existing doc -> 1 request to get doc then fail");
...@@ -6374,14 +6374,15 @@ test ("Put", function(){ ...@@ -6374,14 +6374,15 @@ test ("Put", function(){
o.assertReqs(3, "put normal doc -> 1 req to get doc, 1 for csrf token, 1 to post"); o.assertReqs(3, "put normal doc -> 1 req to get doc, 1 for csrf token, 1 to post");
// put but document already exists = update // put but document already exists = update
o.answer = JSON.stringify({"_id": "put1", "title": "myPut1"}); o.answer = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>' +
o.addFakeServerResponse("xwiki", "GET", "put1", 200, o.answer); '<page xmlns="http://www.xwiki.org"><title>mtPut1</title></page>';
o.addFakeServerResponse("xwiki", "POST", "put1", 201, "HTML RESPONSE"); o.addFakeServerResponse("xwiki", "GET", "put2", 200, o.answer);
o.spy (o, "value", {"ok": true, "id": "put1"}, "Updated the document"); o.addFakeServerResponse("xwiki", "POST", "put2", 201, "HTML RESPONSE");
o.jio.put({"_id": "put1", "title": "myPut2abcdedg"}, o.f); o.spy (o, "value", {"ok": true, "id": "put2"}, "Updated the document");
o.jio.put({"_id": "put2", "title": "myPut2abcdedg"}, o.f);
o.clock.tick(5000); o.clock.tick(5000);
o.server.respond(); o.server.respond();
o.assertReqs(3, "put normal doc -> 1 req to get doc, 1 for csrf token, 1 to post"); o.assertReqs(3, "put update doc -> 1 req to get doc, 1 for csrf token, 1 to post");
o.jio.stop(); o.jio.stop();
}); });
...@@ -6411,7 +6412,8 @@ test ("PutAttachment", function(){ ...@@ -6411,7 +6412,8 @@ test ("PutAttachment", function(){
o.assertReqs(1, "put attach w/o existing document -> 1 request to get doc"); o.assertReqs(1, "put attach w/o existing document -> 1 request to get doc");
// putAttachment with document without data // putAttachment with document without data
o.answer = JSON.stringify({"_id": "putattmt1", "title": "myPutAttm1"}); o.answer = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>' +
'<page xmlns="http://www.xwiki.org"><title>myPutAttm</title></page>';
o.addFakeServerResponse("xwiki", "GET", "putattmt1", 200, o.answer); o.addFakeServerResponse("xwiki", "GET", "putattmt1", 200, o.answer);
o.addFakeServerResponse("xwiki", "POST", "putattmt1/putattmt2", 201,"HTML"+ o.addFakeServerResponse("xwiki", "POST", "putattmt1/putattmt2", 201,"HTML"+
+ "RESPONSE"); + "RESPONSE");
...@@ -6496,7 +6498,8 @@ test ("Remove", function(){ ...@@ -6496,7 +6498,8 @@ test ("Remove", function(){
o.assertReqs(2, "remove nonexistant attach -> 1 request for csrf and 1 for doc"); o.assertReqs(2, "remove nonexistant attach -> 1 request for csrf and 1 for doc");
// remove document // remove document
o.answer = JSON.stringify({"_id": "remove3", "title": "some doc"}); //o.answer = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>' +
// '<page xmlns="http://www.xwiki.org"><title>some doc</title></page>';
//o.addFakeServerResponse("xwiki", "GET", "remove3", 200, o.answer); //o.addFakeServerResponse("xwiki", "GET", "remove3", 200, o.answer);
o.addFakeServerResponse("xwiki", "POST", "bin/delete/Main/remove3", o.addFakeServerResponse("xwiki", "POST", "bin/delete/Main/remove3",
200, "HTML RESPONSE"); 200, "HTML RESPONSE");
...@@ -6570,7 +6573,7 @@ test ("AllDocs", function () { ...@@ -6570,7 +6573,7 @@ test ("AllDocs", function () {
o.jio.stop(); o.jio.stop();
}); });
*\/ */
var nThen = function(next) { var nThen = function(next) {
var funcs = []; var funcs = [];
...@@ -6610,6 +6613,7 @@ var nThen = function(next) { ...@@ -6610,6 +6613,7 @@ var nThen = function(next) {
if (window.location.href.match(/xwiki\/bin\/view/)) (function() { if (window.location.href.match(/xwiki\/bin\/view/)) (function() {
// This test will only be run if we are inside of a live XWiki instance.
test ("XWiki Live Server setup", function () { test ("XWiki Live Server setup", function () {
var o = setUp(this); var o = setUp(this);
...@@ -6667,7 +6671,7 @@ test ("XWiki Live Server setup", function () { ...@@ -6667,7 +6671,7 @@ test ("XWiki Live Server setup", function () {
/*o.jio.allDocs({"include_docs":true}, /*o.jio.allDocs({"include_docs":true},
function(s){console.log(s);}, function(s){console.log(s);},
function ( e ) {console.log(e); function ( e ) {console.log(e);
}, o.f);*\/ }, o.f);*/
}).nThen(function(waitFor) { }).nThen(function(waitFor) {
...@@ -6714,7 +6718,7 @@ test ("XWiki Live Server setup", function () { ...@@ -6714,7 +6718,7 @@ test ("XWiki Live Server setup", function () {
})(); // Live XWiki })(); // Live XWiki
})(); // xwiki })(); // xwiki
*/
}; // end thisfun }; // end thisfun
......
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