zotero/test/tests/HiddenBrowserTest.js
2023-04-14 11:44:44 -04:00

81 lines
2.9 KiB
JavaScript

describe("HiddenBrowser", function() {
const { HiddenBrowser } = ChromeUtils.import(
"chrome://zotero/content/HiddenBrowser.jsm"
);
describe("#create()", function () {
var httpd;
var port = 16213;
var baseURL = `http://127.0.0.1:${port}/`;
before(function () {
Cu.import("resource://zotero-unit/httpd.js");
httpd = new HttpServer();
httpd.start(port);
});
after(async function () {
await new Promise(resolve => httpd.stop(resolve));
});
it("should fail on non-2xx response with requireSuccessfulStatus", async function () {
let e = await getPromiseError(HiddenBrowser.create(baseURL + 'nonexistent', { requireSuccessfulStatus: true }));
assert.instanceOf(e, Zotero.HTTP.UnexpectedStatusException);
});
});
describe("#getPageData()", function () {
it("should handle local UTF-8 HTML file", async function () {
var path = OS.Path.join(getTestDataDirectory().path, 'test-hidden.html');
var browser = await HiddenBrowser.create(path);
var { characterSet, bodyText } = await HiddenBrowser.getPageData(
browser, ['characterSet', 'bodyText']
);
assert.equal(characterSet, 'UTF-8');
// Should ignore hidden text
assert.equal(bodyText, 'This is a test.');
});
it("should handle local GBK HTML file", async function () {
var path = OS.Path.join(getTestDataDirectory().path, 'charsets', 'gbk.html');
var browser = await HiddenBrowser.create(path);
var { characterSet, bodyText } = await HiddenBrowser.getPageData(
browser, ['characterSet', 'bodyText']
);
assert.equal(characterSet, 'GBK');
assert.equal(bodyText, '主体');
});
it("should handle local GBK text file", async function () {
var path = OS.Path.join(getTestDataDirectory().path, 'charsets', 'gbk.txt');
var browser = await HiddenBrowser.create(path);
var { characterSet, bodyText } = await HiddenBrowser.getPageData(
browser, ['characterSet', 'bodyText']
);
HiddenBrowser.destroy(browser);
assert.equal(characterSet, 'GBK');
assert.equal(bodyText, '这是一个测试文件。');
});
});
describe("#getDocument()", function () {
it("should provide a Document object", async function () {
let path = OS.Path.join(getTestDataDirectory().path, 'test-hidden.html');
let browser = await HiddenBrowser.create(path);
let document = await HiddenBrowser.getDocument(browser);
assert.include(document.documentElement.innerHTML, 'test');
assert.ok(document.location);
assert.strictEqual(document.cookie, '');
});
});
describe("#snapshot()", function () {
it("should return a SingleFile snapshot", async function () {
let path = OS.Path.join(getTestDataDirectory().path, 'test-hidden.html');
let browser = await HiddenBrowser.create(path);
let snapshot = await HiddenBrowser.snapshot(browser);
assert.include(snapshot, 'Page saved with SingleFile');
assert.include(snapshot, 'This is hidden text.');
});
});
});