zotero/test/tests/httpTest.js
Dan Stillman 819be60796 Switch to XMLHttpRequest for processDocuments()
processDocuments() now uses an XHR 'document' request, wrapped to
provide a 'location' property, and uses promises for a simpler call
signature (though the old one will continue to work, for existing
translators). 'done' and 'exception' can now be handled via promises,
and in the translator sandbox an optional noCompleteOnError argument
instructs it not to automatically cancel the translation process with an
error (e.g., for supplementary materials).

Since we do need a hidden browser in some situations (e.g., for saving
snapshots), the old hidden-browser-based processDocuments() is still
available as Zotero.HTTP.loadDocuments().

This hopefully also fixes various problems with document property access
in translation-server.
2017-10-05 17:18:42 -04:00

76 lines
1.9 KiB
JavaScript

describe("Zotero.HTTP", function () {
var httpd;
var port = 16213;
before(function* () {
Components.utils.import("resource://zotero-unit/httpd.js");
httpd = new HttpServer();
httpd.start(port);
httpd.registerPathHandler(
'/test.html',
{
handle: function (request, response) {
response.setStatusLine(null, 200, "OK");
response.write("<html><body><p>Test</p><p>Test 2</p></body></html>");
}
}
);
});
after(function* () {
var defer = new Zotero.Promise.defer();
httpd.stop(() => defer.resolve());
yield defer.promise;
});
describe("#processDocuments()", function () {
it("should provide a document object", function* () {
var called = false;
var url = `http://127.0.0.1:${port}/test.html`;
yield Zotero.HTTP.processDocuments(
url,
function (doc) {
assert.equal(doc.location.href, url);
assert.equal(doc.querySelector('p').textContent, 'Test');
var p = doc.evaluate('//p', doc, null, XPathResult.ANY_TYPE, null).iterateNext();
assert.equal(p.textContent, 'Test');
called = true;
}
);
assert.isTrue(called);
});
});
describe("#loadDocuments()", function () {
var win;
before(function* () {
// TEMP: createHiddenBrowser currently needs a parent window
win = yield loadBrowserWindow();
});
after(function* () {
win.close();
});
it("should provide a document object", function* () {
var called = false;
var url = `http://127.0.0.1:${port}/test.html`;
yield new Zotero.Promise((resolve) => {
Zotero.HTTP.loadDocuments(
url,
function (doc) {
assert.equal(doc.location.href, url);
assert.equal(doc.querySelector('p').textContent, 'Test');
var p = doc.evaluate('//p', doc, null, XPathResult.ANY_TYPE, null).iterateNext();
assert.equal(p.textContent, 'Test');
called = true;
},
resolve
);
});
assert.isTrue(called);
});
});
});