2022-06-11 20:22:22 +00:00
|
|
|
describe("HiddenBrowser", function() {
|
|
|
|
const { HiddenBrowser } = ChromeUtils.import(
|
|
|
|
"chrome://zotero/content/HiddenBrowser.jsm"
|
|
|
|
);
|
|
|
|
|
2023-04-14 15:22:23 +00:00
|
|
|
describe("#create()", function () {
|
|
|
|
var httpd;
|
|
|
|
var port = 16213;
|
|
|
|
var baseURL = `http://127.0.0.1:${port}/`;
|
2023-06-13 03:43:18 +00:00
|
|
|
|
|
|
|
var pngRequested = false;
|
2023-04-14 15:22:23 +00:00
|
|
|
|
|
|
|
before(function () {
|
|
|
|
Cu.import("resource://zotero-unit/httpd.js");
|
|
|
|
httpd = new HttpServer();
|
|
|
|
httpd.start(port);
|
|
|
|
});
|
2023-06-13 03:43:18 +00:00
|
|
|
|
|
|
|
beforeEach(async function () {
|
|
|
|
pngRequested = false;
|
|
|
|
httpd.registerPathHandler(
|
|
|
|
'/remote.png',
|
|
|
|
{
|
|
|
|
handle: function (request, response) {
|
|
|
|
Zotero.debug('Something loaded the image')
|
|
|
|
response.setHeader('Content-Type', 'image/png', false);
|
|
|
|
response.setStatusLine(null, 200, 'OK');
|
|
|
|
response.write('');
|
|
|
|
pngRequested = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
2023-04-14 15:22:23 +00:00
|
|
|
|
|
|
|
after(async function () {
|
|
|
|
await new Promise(resolve => httpd.stop(resolve));
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should fail on non-2xx response with requireSuccessfulStatus", async function () {
|
2023-12-27 09:43:50 +00:00
|
|
|
let browser = new HiddenBrowser();
|
|
|
|
let e = await getPromiseError(browser.load(baseURL + 'nonexistent', { requireSuccessfulStatus: true }));
|
2023-04-14 15:22:23 +00:00
|
|
|
assert.instanceOf(e, Zotero.HTTP.UnexpectedStatusException);
|
|
|
|
});
|
2023-06-13 03:43:18 +00:00
|
|
|
|
|
|
|
it("should prevent a remote request with blockRemoteResources", async function () {
|
|
|
|
let path = OS.Path.join(getTestDataDirectory().path, 'test-hidden.html');
|
2023-12-27 09:43:50 +00:00
|
|
|
let browser = new HiddenBrowser({ blockRemoteResources: true });
|
|
|
|
await browser.load(path);
|
|
|
|
await browser.getPageData(['characterSet', 'bodyText']);
|
|
|
|
browser.destroy();
|
2023-06-13 03:43:18 +00:00
|
|
|
assert.isFalse(pngRequested);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should allow a remote request without blockRemoteResources", async function () {
|
|
|
|
let path = OS.Path.join(getTestDataDirectory().path, 'test-hidden.html');
|
2023-12-27 09:43:50 +00:00
|
|
|
let browser = new HiddenBrowser({ blockRemoteResources: false });
|
|
|
|
await browser.load(path);
|
|
|
|
await browser.getPageData(['characterSet', 'bodyText']);
|
|
|
|
browser.destroy();
|
2023-06-13 03:43:18 +00:00
|
|
|
assert.isTrue(pngRequested);
|
|
|
|
});
|
2023-04-14 15:22:23 +00:00
|
|
|
});
|
|
|
|
|
2022-06-11 20:22:22 +00:00
|
|
|
describe("#getPageData()", function () {
|
|
|
|
it("should handle local UTF-8 HTML file", async function () {
|
|
|
|
var path = OS.Path.join(getTestDataDirectory().path, 'test-hidden.html');
|
2023-12-27 09:43:50 +00:00
|
|
|
var browser = new HiddenBrowser();
|
|
|
|
await browser.load(path);
|
|
|
|
var { characterSet, bodyText } = await browser.getPageData(['characterSet', 'bodyText']);
|
|
|
|
browser.destroy();
|
2022-06-11 20:22:22 +00:00
|
|
|
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');
|
2023-12-27 09:43:50 +00:00
|
|
|
var browser = new HiddenBrowser();
|
|
|
|
await browser.load(path);
|
|
|
|
var { characterSet, bodyText } = await browser.getPageData(['characterSet', 'bodyText']);
|
|
|
|
browser.destroy();
|
2022-06-11 20:22:22 +00:00
|
|
|
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');
|
2023-12-27 09:43:50 +00:00
|
|
|
var browser = new HiddenBrowser();
|
|
|
|
await browser.load(path);
|
|
|
|
var { characterSet, bodyText } = await browser.getPageData(['characterSet', 'bodyText']);
|
|
|
|
browser.destroy();
|
2022-06-11 20:22:22 +00:00
|
|
|
assert.equal(characterSet, 'GBK');
|
|
|
|
assert.equal(bodyText, '这是一个测试文件。');
|
|
|
|
});
|
|
|
|
});
|
2023-04-14 15:37:07 +00:00
|
|
|
|
|
|
|
describe("#getDocument()", function () {
|
|
|
|
it("should provide a Document object", async function () {
|
|
|
|
let path = OS.Path.join(getTestDataDirectory().path, 'test-hidden.html');
|
2023-12-27 09:43:50 +00:00
|
|
|
var browser = new HiddenBrowser();
|
|
|
|
await browser.load(path);
|
|
|
|
let document = await browser.getDocument();
|
2023-04-14 15:37:07 +00:00
|
|
|
assert.include(document.documentElement.innerHTML, 'test');
|
|
|
|
assert.ok(document.location);
|
|
|
|
assert.strictEqual(document.cookie, '');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("#snapshot()", function () {
|
2024-02-21 11:43:45 +00:00
|
|
|
var httpd1;
|
|
|
|
var httpd2;
|
|
|
|
var port1 = 16213;
|
|
|
|
var port2 = 16214;
|
|
|
|
var baseURL1 = `http://127.0.0.1:${port1}/`;
|
|
|
|
var baseURL2 = `http://127.0.0.1:${port2}/`;
|
|
|
|
|
|
|
|
before(function () {
|
|
|
|
Cu.import("resource://zotero-unit/httpd.js");
|
|
|
|
// Create two servers with two separate origins
|
|
|
|
httpd1 = new HttpServer();
|
|
|
|
httpd1.start(port1);
|
|
|
|
httpd2 = new HttpServer();
|
|
|
|
httpd2.start(port2);
|
|
|
|
});
|
|
|
|
|
|
|
|
beforeEach(async function () {
|
|
|
|
httpd1.registerPathHandler(
|
|
|
|
'/parent',
|
|
|
|
{
|
|
|
|
handle: function (request, response) {
|
|
|
|
response.setHeader('Content-Type', 'text/html', false);
|
|
|
|
response.setStatusLine(null, 200, 'OK');
|
|
|
|
response.write(`
|
|
|
|
<p>This is text in the parent.</p>
|
|
|
|
<iframe src="${baseURL2}child">
|
|
|
|
`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
httpd2.registerPathHandler(
|
|
|
|
'/child',
|
|
|
|
{
|
|
|
|
handle: function (request, response) {
|
|
|
|
response.setHeader('Content-Type', 'text/html', false);
|
|
|
|
response.setStatusLine(null, 200, 'OK');
|
|
|
|
response.write('<p>This is text in the child.</p>');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
after(async function () {
|
|
|
|
await new Promise(resolve => httpd1.stop(resolve));
|
|
|
|
await new Promise(resolve => httpd2.stop(resolve));
|
|
|
|
});
|
|
|
|
|
2023-04-14 15:37:07 +00:00
|
|
|
it("should return a SingleFile snapshot", async function () {
|
|
|
|
let path = OS.Path.join(getTestDataDirectory().path, 'test-hidden.html');
|
2023-12-27 09:43:50 +00:00
|
|
|
var browser = new HiddenBrowser();
|
|
|
|
await browser.load(path);
|
|
|
|
let snapshot = await browser.snapshot();
|
2023-04-14 15:37:07 +00:00
|
|
|
assert.include(snapshot, 'Page saved with SingleFile');
|
|
|
|
assert.include(snapshot, 'This is hidden text.');
|
|
|
|
});
|
2024-02-21 11:43:45 +00:00
|
|
|
|
|
|
|
it("should successfully import a snapshot, skipping a cross-origin iframe", async function () {
|
|
|
|
let url = baseURL1 + 'parent';
|
|
|
|
let browser = new HiddenBrowser();
|
|
|
|
await browser.load(url);
|
|
|
|
let snapshot = await browser.snapshot();
|
|
|
|
assert.include(snapshot, 'This is text in the parent.');
|
|
|
|
// Child frame will be skipped
|
|
|
|
assert.notInclude(snapshot, 'This is text in the child.');
|
|
|
|
});
|
2023-04-14 15:37:07 +00:00
|
|
|
});
|
|
|
|
});
|