...
 
Commits (6)
......@@ -169,6 +169,8 @@ module.exports = function (grunt) {
'src/jio.storage/uuidstorage.js',
'src/jio.storage/memorystorage.js',
'src/jio.storage/zipstorage.js',
'src/jio.storage/parserstorage.js',
'src/jio.storage/httpstorage.js',
'src/jio.storage/dropboxstorage.js',
'src/jio.storage/davstorage.js',
'src/jio.storage/gdrivestorage.js',
......
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Monitor Coverage Scenario</title>
<script src="../node_modules/rsvp/dist/rsvp-2.0.4.js"></script>
<script src="../dist/jio-latest.js"></script>
<script src="../src/jio.storage/replicatedopmltreestorage.js"></script>
<script src="../src/jio.storage/webhttpstorage.js"></script>
<link rel="stylesheet" href="../node_modules/grunt-contrib-qunit/test/libs/qunit.css" type="text/css" media="screen"/>
<script src="../node_modules/grunt-contrib-qunit/test/libs/qunit.js" type="text/javascript"></script>
<script src="../node_modules/sinon/pkg/sinon.js" type="text/javascript"></script>
<script src="scenario_monitor.js"></script>
</head>
<body>
<h1 id="qunit-header">Monitor Coverage Scenario</h1>
<h2 id="qunit-banner"></h2>
<div id="qunit-testrunner-toolbar"></div>
<h2 id="qunit-userAgent"></h2>
<ol id="qunit-tests"></ol>
<div id="qunit-fixture">test markup, will be hidden</div>
</body>
</html>
\ No newline at end of file
This diff is collapsed.
/*global RSVP, Blob*/
/*jslint nomen: true*/
(function (jIO, RSVP, Blob) {
"use strict";
function HttpStorage(spec) {
if (spec.hasOwnProperty('catch_error')) {
this._catch_error = spec.catch_error;
} else {
this._catch_error = false;
}
}
HttpStorage.prototype.get = function (id) {
var context = this;
return new RSVP.Queue()
.push(function () {
return jIO.util.ajax({
type: 'HEAD',
url: id
});
})
.push(undefined, function (error) {
if (context._catch_error) {
return error;
}
if ((error.target !== undefined) &&
(error.target.status === 404)) {
throw new jIO.util.jIOError("Cannot find url " + id, 404);
}
throw error;
})
.push(function (response) {
var key_list = ["Content-Disposition", "Content-Type", "Date",
"Last-Modified", "Vary", "Cache-Control", "Etag",
"Accept-Ranges", "Content-Range"],
i,
key,
value,
result = {};
result.Status = response.target.status;
for (i = 0; i < key_list.length; i += 1) {
key = key_list[i];
value = response.target.getResponseHeader(key);
if (value !== null) {
result[key] = value;
}
}
return result;
});
};
HttpStorage.prototype.allAttachments = function () {
return {enclosure: {}};
};
HttpStorage.prototype.getAttachment = function (id, name) {
var context = this;
if (name !== 'enclosure') {
throw new jIO.util.jIOError("Forbidden attachment: "
+ id + " , " + name,
400);
}
return new RSVP.Queue()
.push(function () {
return jIO.util.ajax({
type: 'GET',
url: id,
dataType: "blob"
});
})
.push(undefined, function (error) {
if (context._catch_error) {
return error;
}
if ((error.target !== undefined) &&
(error.target.status === 404)) {
throw new jIO.util.jIOError("Cannot find url " + id, 404);
}
throw error;
})
.push(function (response) {
return new Blob(
[response.target.response || response.target.responseText],
{"type": response.target.getResponseHeader('Content-Type') ||
"application/octet-stream"}
);
});
};
jIO.addStorage('http', HttpStorage);
}(jIO, RSVP, Blob));
\ No newline at end of file
/*
* Copyright 2016, Nexedi SA
* Released under the LGPL license.
* http://www.gnu.org/licenses/lgpl.html
*/
/*jslint nomen: true*/
/*global jIO, RSVP, DOMParser */
(function (jIO, RSVP, DOMParser) {
"use strict";
var dummy = {getAttribute: function () { return; }};
function getOpmlOutlineAsDict(outline) {
var outline_dict = {
title: outline.getAttribute('title') || dummy.textContent,
htmlurl: outline.getAttribute('htmlUrl') || dummy.textContent,
xmlurl: outline.getAttribute('xmlUrl') || dummy.textContent,
url: outline.getAttribute('url') || dummy.textContent,
text: outline.getAttribute('text') || dummy.textContent,
type: outline.getAttribute('type') || dummy.textContent,
version: outline.getAttribute('version') || dummy.textContent,
created: outline.getAttribute('created') || dummy.textContent,
category: outline.getAttribute('category') || dummy.textContent
};
return outline_dict;
}
function getOpmlDocumentId(outline) {
return outline.getAttribute('url') ||
outline.getAttribute('htmlUrl') ||
outline.getAttribute('xmlUrl') || '';
}
function getOpmlHeadAsDict(doc) {
var element;
function getElementsByTagName(name) {
return (doc.getElementsByTagName(name)[0] || dummy).textContent;
}
element = {
opml_title: getElementsByTagName('title'),
created_date: getElementsByTagName('dateCreated'),
modified_date: getElementsByTagName('dateModified'),
owner_name: getElementsByTagName('ownerName'),
owner_email: getElementsByTagName('ownerEmail'),
opml_link: getElementsByTagName('link')
};
return element;
}
function getOpmlElement(doc, id) {
var outline_list = doc.getElementsByTagName('outline'),
i,
max,
element = getOpmlHeadAsDict(doc);
for (i = 0, max = outline_list.length; i < max; i += 1) {
if (outline_list[i].getAttribute('htmlUrl') === id ||
outline_list[i].getAttribute('xmlUrl') === id ||
outline_list[i].getAttribute('url') === id) {
Object.assign(element, getOpmlOutlineAsDict(outline_list[i]));
break;
}
}
return element;
}
function getOpmlElementList(doc, options) {
var i,
max,
tmp_id,
push_method,
opml_head = {},
opml_list = [],
outline_list = doc.getElementsByTagName('outline');
if (options.include_docs === true) {
opml_head = getOpmlHeadAsDict(doc);
push_method = function (id, outline) {
var element = getOpmlOutlineAsDict(outline);
Object.assign(element, opml_head);
opml_list.push({
"id": id,
"value": {},
"doc": element
});
};
} else {
push_method = function (id) {
opml_list.push({
"id": id,
"value": {}
});
};
}
for (i = 0, max = outline_list.length; i < max; i += 1) {
if (!outline_list[i].hasChildNodes()) {
tmp_id = getOpmlDocumentId(outline_list[i]);
if (tmp_id !== '') {
push_method(tmp_id, outline_list[i]);
}
}
}
return opml_list;
}
function parseOpmlFeed(feed_url, id, options) {
return new RSVP.Queue()
.push(function () {
return jIO.util.ajax({
type: "GET",
url: feed_url,
dataType: "text"
});
})
.push(function (response) {
var element,
result;
if (!response.target.responseText) {
throw new jIO.util.jIOError("Cannot find document", 404);
}
element = new DOMParser().parseFromString(
response.target.responseText,
"text/xml"
);
if (id !== undefined && id !== null) {
result = getOpmlElement(element, id);
if (result === undefined) {
throw new jIO.util.jIOError("Cannot find document", 404);
}
return result;
}
if (!options) {
options = {};
}
return getOpmlElementList(element, options);
}, function (error) {
if ((error.target !== undefined) &&
(error.target.status === 404)) {
throw new jIO.util.jIOError("Cannot find document", 404);
}
throw error;
});
}
/**
* The JIO OPML Storage extension
*
* @class OPMLStorage
* @constructor
*/
function OPMLStorage(spec) {
if (typeof spec.url !== 'string') {
throw new TypeError("OPMLStorage 'url' is not of type string");
}
this._url = spec.url;
}
OPMLStorage.prototype.get = function (id) {
return parseOpmlFeed(this._url, id);
};
OPMLStorage.prototype.hasCapacity = function (capacity) {
return (capacity === "list") || (capacity === "include");
};
OPMLStorage.prototype.buildQuery = function (options) {
return parseOpmlFeed(this._url, undefined, options);
};
jIO.addStorage('opml', OPMLStorage);
}(jIO, RSVP, DOMParser));
/*jslint nomen: true*/
/*global jIO, DOMParser */
(function (jIO, DOMParser) {
"use strict";
/////////////////////////////////////////////////////////////
// OPML Parser
/////////////////////////////////////////////////////////////
function OPMLParser(txt) {
this._dom_parser = new DOMParser().parseFromString(txt, 'text/xml');
}
OPMLParser.prototype.parseHead = function () {
// fetch all children instead
var channel_element = this._dom_parser.querySelector("opml > head"),
tag_element,
i,
result = {};
for (i = channel_element.childNodes.length - 1; i >= 0; i -= 1) {
tag_element = channel_element.childNodes[i];
result[tag_element.tagName] = tag_element.textContent;
}
return result;
};
OPMLParser.prototype.parseOutline = function (result_list, outline_element,
prefix, include, id) {
var attribute,
i,
child,
result = {};
if ((id === prefix) || (id === undefined)) {
result_list.push({
id: prefix,
value: {}
});
if (include) {
for (i = outline_element.attributes.length - 1; i >= 0; i -= 1) {
attribute = outline_element.attributes[i];
if (attribute.value) {
result[attribute.name] = attribute.value;
}
}
result_list[result_list.length - 1].doc = result;
}
}
for (i = outline_element.childNodes.length - 1; i >= 0; i -= 1) {
child = outline_element.childNodes[i];
if (child.tagName === 'outline') {
this.parseOutline(result_list, child, prefix + '/' + i, include, id);
}
}
};
OPMLParser.prototype.getDocumentList = function (include, id) {
var result_list,
item_list = this._dom_parser.querySelectorAll("body > outline"),
i;
if ((id === '/0') || (id === undefined)) {
result_list = [{
id: '/0',
value: {}
}];
if (include) {
result_list[0].doc = this.parseHead();
}
} else {
result_list = [];
}
for (i = 0; i < item_list.length; i += 1) {
this.parseOutline(result_list, item_list[i], '/1/' + i, include, id);
}
return result_list;
};
/////////////////////////////////////////////////////////////
// RSS Parser
/////////////////////////////////////////////////////////////
function RSSParser(txt) {
this._dom_parser = new DOMParser().parseFromString(txt, 'text/xml');
}
RSSParser.prototype.parseElement = function (element) {
var tag_element,
i,
j,
attribute,
result = {};
for (i = element.childNodes.length - 1; i >= 0; i -= 1) {
tag_element = element.childNodes[i];
if (tag_element.tagName !== 'item') {
result[tag_element.tagName] = tag_element.textContent;
for (j = tag_element.attributes.length - 1; j >= 0; j -= 1) {
attribute = tag_element.attributes[j];
if (attribute.value) {
result[tag_element.tagName + '_' + attribute.name] =
attribute.value;
}
}
}
}
return result;
};
RSSParser.prototype.getDocumentList = function (include, id) {
var result_list,
item_list = this._dom_parser.querySelectorAll("rss > channel > item"),
i;
if ((id === '/0') || (id === undefined)) {
result_list = [{
id: '/0',
value: {}
}];
if (include) {
result_list[0].doc = this.parseElement(
this._dom_parser.querySelector("rss > channel")
);
}
} else {
result_list = [];
}
for (i = 0; i < item_list.length; i += 1) {
if ((id === '/0/' + i) || (id === undefined)) {
result_list.push({
id: '/0/' + i,
value: {}
});
if (include) {
result_list[result_list.length - 1].doc =
this.parseElement(item_list[i]);
}
}
}
return result_list;
};
/////////////////////////////////////////////////////////////
// Helpers
/////////////////////////////////////////////////////////////
var parser_dict = {
'rss': RSSParser,
'opml': OPMLParser
};
function getParser(storage) {
return storage._sub_storage.getAttachment(storage._document_id,
storage._attachment_id,
{format: 'text'})
.push(function (txt) {
return new parser_dict[storage._parser_name](txt);
});
}
/////////////////////////////////////////////////////////////
// Storage
/////////////////////////////////////////////////////////////
function ParserStorage(spec) {
this._attachment_id = spec.attachment_id;
this._document_id = spec.document_id;
this._parser_name = spec.parser;
this._sub_storage = jIO.createJIO(spec.sub_storage);
}
ParserStorage.prototype.hasCapacity = function (capacity) {
return (capacity === "list") || (capacity === 'include');
};
ParserStorage.prototype.buildQuery = function (options) {
if (options === undefined) {
options = {};
}
return getParser(this)
.push(function (parser) {
return parser.getDocumentList((options.include_docs || false));
});
};
ParserStorage.prototype.get = function (id) {
return getParser(this)
.push(function (parser) {
var result_list = parser.getDocumentList(true, id);
if (result_list.length) {
return result_list[0].doc;
}
throw new jIO.util.jIOError(
"Cannot find parsed document: " + id,
404
);
});
};
jIO.addStorage('parser', ParserStorage);
}(jIO, DOMParser));
\ No newline at end of file
This diff is collapsed.
/*
* Copyright 2016, Nexedi SA
* Released under the LGPL license.
* http://www.gnu.org/licenses/lgpl.html
*/
/*jslint nomen: true*/
/*global jIO, RSVP, DOMParser */
// JIO Rss Storage Description :
// {
// type: "rss",
// url: {string}
// }
(function (jIO, RSVP, DOMParser) {
"use strict";
var dummy = {getAttribute: function () { return; }};
function selectElement(element, selector) {
return (element.querySelector(selector) || dummy);
}
function getRssHeaderItemDict(element) {
var rss_channel = element.querySelector("rss > channel"),
feed_channel;
feed_channel = {
siteTitle: selectElement(
rss_channel,
"title"
).textContent,
reference: selectElement(
rss_channel,
"description"
).textContent,
siteLink: selectElement(
rss_channel,
"link"
).textContent,
lastBuildDate: selectElement(
rss_channel,
"lastBuildDate"
).textContent,
siteGenerator: selectElement(
rss_channel,
"generator"
).textContent,
siteDocs: selectElement(
rss_channel,
"docs"
).textContent
};
return feed_channel;
}
function getFeedItem(entry) {
var item;
item = {
link: selectElement(entry, "link").textContent,
date: selectElement(entry, "pubDate").textContent,
title: selectElement(entry, "title").textContent,
author: selectElement(entry, "author").textContent,
category: selectElement(entry, "category").textContent,
comments: selectElement(entry, "comments").textContent,
sourceUrl: selectElement(entry, "source").getAttribute('url'),
source: selectElement(entry, "source").textContent,
description: selectElement(entry, "description").textContent,
guid: selectElement(entry, "guid").textContent
};
return item;
}
function getRssFeedEntry(element, id) {
var item,
rss_guid_list,
i;
if (id !== undefined && id !== null) {
rss_guid_list = element.querySelectorAll("rss>channel>item>guid");
for (i = 0; i < rss_guid_list.length; i += 1) {
if (rss_guid_list[i].textContent === id) {
item = getFeedItem(rss_guid_list[i].parentNode);
Object.assign(item, getRssHeaderItemDict(element));
break;
}
}
}
return item;
}
function getRssFeedEntryList(element, options) {
var rss_entry_list,
item_list = [],
push_method,
feed_channel;
if (options.include_docs === true) {
feed_channel = getRssHeaderItemDict(element);
push_method = function (id, entry) {
var item;
if (!id) {
return;
}
item = getFeedItem(entry);
Object.assign(item, feed_channel);
item_list.push({
"id": id,
"value": {},
"doc": item
});
};
} else {
push_method = function (id) {
if (!id) {
return;
}
item_list.push({
"id": id,
"value": {}
});
};
}
rss_entry_list = element.querySelectorAll("rss > channel > item");
[].forEach.call(rss_entry_list, function (entry) {
push_method(
selectElement(entry, "guid").textContent,
entry
);
});
return item_list;
}
function parseRssFeed(feed_url, id, options) {
return new RSVP.Queue()
.push(function () {
return jIO.util.ajax({
type: "GET",
url: feed_url,
dataType: "text"
});
})
.push(function (response) {
var element,
item;
if (!response.target.responseText) {
throw new jIO.util.jIOError("Cannot find document", 404);
}
element = new DOMParser().parseFromString(
response.target.responseText,
"text/xml"
);
if (id !== undefined && id !== null) {
item = getRssFeedEntry(element, id);
if (item === undefined) {
throw new jIO.util.jIOError("Cannot find document", 404);
}
return item;
}
if (!options) {
options = {};
}
return getRssFeedEntryList(element, options);
}, function (error) {
if ((error.target !== undefined) &&
(error.target.status === 404)) {
throw new jIO.util.jIOError("Cannot find document", 404);
}
throw error;
});
}
/**
* The JIO RSSStorage Storage extension
*
* @class RSSStorage
* @constructor
*/
function RSSStorage(spec) {
if (typeof spec.url !== 'string') {
throw new TypeError("RSSStorage 'url' is not of type string");
}
this._url = spec.url;
}
RSSStorage.prototype.get = function (id) {
return parseRssFeed(this._url, id);
};
RSSStorage.prototype.hasCapacity = function (capacity) {
return (capacity === "list") || (capacity === 'include');
};
RSSStorage.prototype.buildQuery = function (options) {
return parseRssFeed(this._url, undefined, options);
};
jIO.addStorage('rss', RSSStorage);
}(jIO, RSVP, DOMParser));
/*
* Copyright 2016, Nexedi SA
* Released under the LGPL license.
* http://www.gnu.org/licenses/lgpl.html
*/
/*jslint nomen: true*/
/*global jIO, RSVP */
(function (jIO, RSVP) {
"use strict";
function ajax(storage, options) {
if (options === undefined) {
options = {};
}
if (storage._authorization !== undefined) {
if (options.headers === undefined) {
options.headers = {};
}
options.headers.Authorization = storage._authorization;
}
if (storage._with_credentials !== undefined) {
if (options.xhrFields === undefined) {
options.xhrFields = {};
}
options.xhrFields.withCredentials = storage._with_credentials;
}
return new RSVP.Queue()
.push(function () {
return jIO.util.ajax(options);
});
}
function restrictDocumentId(id) {
var slash_index = id.indexOf("/");
if (slash_index !== 0 && slash_index !== -1) {
throw new jIO.util.jIOError("id " + id + " is forbidden (no begin /)",
400);
}
if (id.lastIndexOf("/") === (id.length - 1)) {
throw new jIO.util.jIOError("id " + id + " is forbidden (no end /)",
400);
}
return id;
}
function getJsonDocument(context, id) {
return new RSVP.Queue()
.push(function () {
return ajax(context, {
type: "GET",
url: context._url + "/" + id + ".json",
dataType: "text"
});
})
.push(function (response) {
return {id: id, doc: JSON.parse(response.target.responseText)};
}, function (error) {
if ((error.target !== undefined) &&
(error.target.status === 404)) {
throw new jIO.util.jIOError("Cannot find document '" + id + "'", 404);
}
throw error;
});
}
/**
* The JIO WEB HTTP Storage extension
*
* @class WEBHTTPStorage
* @constructor
*/
function WEBHTTPStorage(spec) {
if (typeof spec.url !== 'string') {
throw new TypeError("WEBHTTPStorage 'url' is not of type string");
}
this._url = spec.url.replace(new RegExp("[/]+$"), "");
if (typeof spec.basic_login === 'string') {
this._authorization = "Basic " + spec.basic_login;
}
this._with_credentials = spec.with_credentials;
}
WEBHTTPStorage.prototype.get = function (id) {
var context = this;
id = restrictDocumentId(id);
return getJsonDocument(context, id)
.push(function (element) {
return element.doc;
}, function (error) {
if ((error.target !== undefined) &&
(error.target.status === 404)) {
throw new jIO.util.jIOError("Cannot find document", 404);
}
throw error;
});
};
WEBHTTPStorage.prototype.hasCapacity = function (capacity) {
return (capacity === "list") || (capacity === "include");
};
WEBHTTPStorage.prototype.buildQuery = function (options) {
var context = this,
item_list = [],
push_item;
if (options.include_docs === true) {
push_item = function (id, item) {
item_list.push({
"id": id,
"value": {},
"doc": item
});
};
} else {
push_item = function (id) {
item_list.push({
"id": id,
"value": {}
});
};
}
return new RSVP.Queue()
.push(function () {
return ajax(context, {
type: "GET",
url: context._url + "/_document_list",
dataType: "text"
});
})
.push(function (response) {
var document_list = [],
promise_list = [],
i;
document_list = response.target.responseText.split('\n');
for (i = 0; i < document_list.length; i += 1) {
if (document_list[i]) {
promise_list.push(getJsonDocument(context, document_list[i]));
}
}
return RSVP.all(promise_list);
}, function (error) {
if ((error.target !== undefined) &&
(error.target.status === 404)) {
throw new jIO.util.jIOError("Cannot find document", 404);
}
throw error;
})
.push(function (result_list) {
var i;
for (i = 0; i < result_list.length; i += 1) {
push_item(result_list[i].id, result_list[i].doc);
}
return item_list;
});
};
jIO.addStorage('webhttp', WEBHTTPStorage);
}(jIO, RSVP));
/*jslint nomen: true */
/*global Blob, sinon*/
(function (jIO, QUnit, Blob, sinon) {
"use strict";
var test = QUnit.test,
stop = QUnit.stop,
start = QUnit.start,
ok = QUnit.ok,
expect = QUnit.expect,
deepEqual = QUnit.deepEqual,
equal = QUnit.equal,
module = QUnit.module,
domain = "https://example.org";
/////////////////////////////////////////////////////////////////
// davStorage constructor
/////////////////////////////////////////////////////////////////
module("httpStorage.constructor");
test("default parameters", function () {
var jio = jIO.createJIO({
type: "http"
});
equal(jio.__type, "http");
deepEqual(jio.__storage._catch_error, false);
});
test("Storage store catch_error", function () {
var jio = jIO.createJIO({
type: "http",
catch_error: true
});
equal(jio.__type, "http");
deepEqual(jio.__storage._catch_error, true);
});
/////////////////////////////////////////////////////////////////
// httpStorage.get
/////////////////////////////////////////////////////////////////
module("httpStorage.get", {
setup: function () {
this.server = sinon.fakeServer.create();
this.server.autoRespond = true;
this.server.autoRespondAfter = 5;
this.jio = jIO.createJIO({
type: "http"
});
},
teardown: function () {
this.server.restore();
delete this.server;
}
});
test("get document", function () {
var id = domain + "/id1/";
this.server.respondWith("HEAD", id, [200, {
"Content-Type": "text/xml-foo"
}, '']);
stop();
expect(1);
this.jio.get(id)
.then(function (result) {
deepEqual(result, {
"Content-Type": "text/xml-foo",
"Status": 200
}, "Check document");
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
test("get document with a not expected status", function () {
var id = domain + "/id1/";
this.server.respondWith("HEAD", id, [500, {
"Content-Type": "text/xml-foo"
}, '']);
stop();
expect(1);
this.jio.get(id)
.then(function (result) {
ok(false, result);
})
.fail(function (error) {
equal(error.target.status, 500);
})
.always(function () {
start();
});
});
test("get document with 404 status", function () {
var id = domain + "/id1/";
stop();
expect(3);
this.jio.get(id)
.then(function (result) {
ok(false, result);
})
.fail(function (error) {
ok(error instanceof jIO.util.jIOError);
equal(error.message, "Cannot find url " + id);
equal(error.status_code, 404);
})
.always(function () {
start();
});
});
test("get document with a not expected status and catch error", function () {
var id = domain + "/id1/";
this.server.respondWith("HEAD", id, [500, {
"Content-Type": "text/xml-foo"
}, '']);
this.jio = jIO.createJIO({
type: "http",
catch_error: true
});
stop();
expect(1);
this.jio.get(id)
.then(function (result) {
deepEqual(result, {
"Content-Type": "text/xml-foo",
"Status": 500
}, "Check document");
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
/////////////////////////////////////////////////////////////////
// httpStorage.allAttachments
/////////////////////////////////////////////////////////////////
module("httpStorage.allAttachments", {
setup: function () {
this.server = sinon.fakeServer.create();
this.server.autoRespond = true;
this.server.autoRespondAfter = 5;
this.jio = jIO.createJIO({
type: "http"
});
},
teardown: function () {
this.server.restore();
delete this.server;
}
});
test("get document with attachment", function () {
stop();
expect(1);
this.jio.allAttachments('/id')
.then(function (result) {
deepEqual(result, {
enclosure: {}
}, "Check document");
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
/////////////////////////////////////////////////////////////////
// httpStorage.getAttachment
/////////////////////////////////////////////////////////////////
module("httpStorage.getAttachment", {
setup: function () {
this.server = sinon.fakeServer.create();
this.server.autoRespond = true;
this.server.autoRespondAfter = 5;
this.jio = jIO.createJIO({
type: "http"
});
},
teardown: function () {
this.server.restore();
delete this.server;
}
});
test("forbidden attachment", function () {
var id = domain + "/id1/";
stop();
expect(3);
this.jio.getAttachment(
id,
"attachment1"
)
.fail(function (error) {
ok(error instanceof jIO.util.jIOError);
equal(
error.message,
"Forbidden attachment: https://example.org/id1/ , attachment1"
);
equal(error.status_code, 400);
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
test("get attachment", function () {
var id = domain + "/id1/";
this.server.respondWith("GET", id, [200, {
"Content-Type": "text/xml-foo"
}, "foo\nbaré"]);
stop();
expect(3);
this.jio.getAttachment(id, 'enclosure')
.then(function (result) {
ok(result instanceof Blob, "Data is Blob");
deepEqual(result.type, "text/xml-foo", "Check mimetype");
return jIO.util.readBlobAsText(result);
})
.then(function (result) {
equal(result.target.result, "foo\nbaré",
"Attachment correctly fetched");
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
test("get attachment with a not expected status", function () {
var id = domain + "/id1/";
this.server.respondWith("GET", id, [500, {
"Content-Type": "text/xml-foo"
}, '']);
stop();
expect(1);
this.jio.getAttachment(id, 'enclosure')
.then(function (result) {
ok(false, result);
})
.fail(function (error) {
equal(error.target.status, 500);
})
.always(function () {
start();
});
});
test("get attachment with 404 status", function () {
var id = domain + "/id1/";
stop();
expect(3);
this.jio.getAttachment(id, 'enclosure')
.then(function (result) {
ok(false, result);
})
.fail(function (error) {
ok(error instanceof jIO.util.jIOError);
equal(error.message, "Cannot find url " + id);
equal(error.status_code, 404);
})
.always(function () {
start();
});
});
test("get attachment with unexpected status and catch error", function () {
var id = domain + "/id1/";
this.server.respondWith("GET", id, [500, {
"Content-Type": "text/xml-foo"
}, 'foo\nbaré']);
this.jio = jIO.createJIO({
type: "http",
catch_error: true
});
stop();
expect(3);
this.jio.getAttachment(id, 'enclosure')
.then(function (result) {
ok(result instanceof Blob, "Data is Blob");
deepEqual(result.type, "text/xml-foo", "Check mimetype");
return jIO.util.readBlobAsText(result);
})
.then(function (result) {
equal(result.target.result, "foo\nbaré",
"Attachment correctly fetched");
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
}(jIO, QUnit, Blob, sinon));
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
......@@ -41,7 +41,7 @@
<script src="jio.storage/uuidstorage.tests.js"></script>
<script src="jio.storage/replicatestorage.tests.js"></script>
<script src="jio.storage/shastorage.tests.js"></script>
<script src="jio.storage/parserstorage.tests.js"></script>
<!--script src="jio.storage/qiniustorage.tests.js"></script-->
<!--script src="jio.storage/indexstorage.tests.js"></script-->
<script src="jio.storage/cryptstorage.tests.js"></script>
......@@ -49,6 +49,7 @@
<script src="jio.storage/zipstorage.tests.js"></script>
<script src="jio.storage/gdrivestorage.tests.js"></script>
<script src="jio.storage/websqlstorage.tests.js"></script>
<script src="jio.storage/httpstorage.tests.js"></script>
<!--script src="../lib/jquery/jquery.min.js"></script>
<script src="../src/jio.storage/xwikistorage.js"></script>
<script src="jio.storage/xwikistorage.tests.js"></script-->
......