2015-12-09 09:07:48 +00:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
describe("Zotero.Sync.APIClient", function () {
|
|
|
|
Components.utils.import("resource://zotero/config.js");
|
|
|
|
|
|
|
|
var apiKey = Zotero.Utilities.randomString(24);
|
|
|
|
var baseURL = "http://local.zotero/";
|
|
|
|
var server, client;
|
|
|
|
|
|
|
|
function setResponse(response) {
|
|
|
|
setHTTPResponse(server, baseURL, response, {});
|
|
|
|
}
|
|
|
|
|
|
|
|
before(function () {
|
2016-02-29 09:23:00 +00:00
|
|
|
Zotero.HTTP.mock = sinon.FakeXMLHttpRequest;
|
|
|
|
});
|
|
|
|
|
|
|
|
beforeEach(function () {
|
2015-12-09 09:07:48 +00:00
|
|
|
Components.utils.import("resource://zotero/concurrentCaller.js");
|
|
|
|
var caller = new ConcurrentCaller(1);
|
|
|
|
caller.setLogger(msg => Zotero.debug(msg));
|
|
|
|
caller.stopOnError = true;
|
|
|
|
caller.onError = function (e) {
|
|
|
|
Zotero.logError(e);
|
|
|
|
if (e.fatal) {
|
|
|
|
caller.stop();
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
client = new Zotero.Sync.APIClient({
|
|
|
|
baseURL,
|
|
|
|
apiVersion: ZOTERO_CONFIG.API_VERSION,
|
|
|
|
apiKey,
|
|
|
|
caller
|
|
|
|
})
|
2016-02-29 09:23:00 +00:00
|
|
|
|
2015-12-09 09:07:48 +00:00
|
|
|
server = sinon.fakeServer.create();
|
|
|
|
server.autoRespond = true;
|
|
|
|
})
|
|
|
|
|
|
|
|
after(function () {
|
|
|
|
Zotero.HTTP.mock = null;
|
|
|
|
})
|
|
|
|
|
|
|
|
describe("#_checkConnection()", function () {
|
2016-02-29 09:23:00 +00:00
|
|
|
var spy;
|
|
|
|
|
|
|
|
beforeEach(function () {
|
|
|
|
client.failureDelayIntervals = [10];
|
|
|
|
client.failureDelayMax = 15;
|
|
|
|
});
|
|
|
|
afterEach(function () {
|
|
|
|
if (spy) {
|
|
|
|
spy.restore();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should retry on 500 error", function* () {
|
2015-12-09 09:07:48 +00:00
|
|
|
setResponse({
|
|
|
|
method: "GET",
|
|
|
|
url: "error",
|
|
|
|
status: 500,
|
|
|
|
text: ""
|
|
|
|
})
|
2016-02-29 09:23:00 +00:00
|
|
|
var spy = sinon.spy(Zotero.HTTP, "request");
|
2015-12-09 09:07:48 +00:00
|
|
|
var e = yield getPromiseError(client.makeRequest("GET", baseURL + "error"));
|
2016-02-29 09:23:00 +00:00
|
|
|
assert.instanceOf(e, Zotero.HTTP.UnexpectedStatusException);
|
|
|
|
assert.isTrue(spy.calledTwice);
|
2015-12-09 09:07:48 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
it("should catch an interrupted connection", function* () {
|
|
|
|
setResponse({
|
|
|
|
method: "GET",
|
|
|
|
url: "empty",
|
|
|
|
status: 0,
|
|
|
|
text: ""
|
|
|
|
})
|
|
|
|
var e = yield getPromiseError(client.makeRequest("GET", baseURL + "empty"));
|
|
|
|
assert.ok(e);
|
|
|
|
assert.equal(e.message, Zotero.getString('sync.error.checkConnection'));
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|