Adapt existing tests to use webextensions-jsdom

This commit is contained in:
stoically
2019-12-18 09:19:01 +01:00
committed by Jonathan Kingston
parent 77fd843b3d
commit 5eb79949e9
3 changed files with 153 additions and 180 deletions
+112 -124
View File
@@ -1,112 +1,82 @@
describe("#940", () => {
describe("when other onBeforeRequestHandlers are faster and redirect with the same requestId", () => {
it("should not open two confirm pages", async () => {
// init
const activeTab = {
id: 1,
await helper.browser.initializeWithTab({
cookieStoreId: "firefox-container-1",
url: "http://example.com",
index: 0
};
await helper.browser.initializeWithTab(activeTab);
// assign the activeTab.url
url: "http://example.com"
});
await helper.popup.clickElementById("container-page-assigned");
// start request and don't await the requests at all
// so the second request below is actually comparable to an actual redirect that also fires immediately
const newTab = {
id: 2,
cookieStoreId: "firefox-default",
url: activeTab.url,
index: 1,
active: true
};
helper.browser.openNewTab(newTab, {
requestId: 1
const responses = {};
await helper.browser.openNewTab({
url: "http://example.com"
}, {
options: {
webRequestRedirects: ["https://example.com"],
webRequestError: true,
instantRedirects: true
},
responses
});
// other addon sees the same request
// and redirects to the https version of activeTab.url
// since it's a redirect the request has the same requestId
background.browser.webRequest.onBeforeRequest.addListener.yield({
frameId: 0,
tabId: newTab.id,
url: "https://example.com",
requestId: 1
const result = await responses.webRequest.onBeforeRequest[1];
expect(result).to.deep.equal({
cancel: true
});
await nextTick();
background.browser.tabs.create.should.have.been.calledOnce;
});
});
describe("when redirects change requestId midflight", () => {
let promiseResults;
let newTab;
const newTabResponses = {};
const redirectedRequest = async (options = {}) => {
global.clock = sinon.useFakeTimers();
newTab = await helper.browser.openNewTab({
url: "http://youtube.com"
}, {
options: Object.assign({
webRequestRedirects: [
"https://youtube.com",
"https://www.youtube.com",
{
url: "https://www.youtube.com",
webRequest: {
requestId: 2
}
}
],
webRequestError: true,
instantRedirects: true
}, options),
responses: newTabResponses
});
};
beforeEach(async () => {
// init
const activeTab = {
id: 1,
await helper.browser.initializeWithTab({
cookieStoreId: "firefox-container-1",
url: "https://www.youtube.com",
index: 0
};
await helper.browser.initializeWithTab(activeTab);
// assign the activeTab.url
url: "https://www.youtube.com"
});
await helper.popup.clickElementById("container-page-assigned");
// http://youtube.com
const newTab = {
id: 2,
cookieStoreId: "firefox-default",
url: "http://youtube.com",
index: 1,
active: true
};
const promise1 = helper.browser.openNewTab(newTab, {
requestId: 1
});
// https://youtube.com
const [promise2] = background.browser.webRequest.onBeforeRequest.addListener.yield({
frameId: 0,
tabId: newTab.id,
url: "https://youtube.com",
requestId: 1
});
// https://www.youtube.com
const [promise3] = background.browser.webRequest.onBeforeRequest.addListener.yield({
frameId: 0,
tabId: newTab.id,
url: "https://www.youtube.com",
requestId: 1
});
// https://www.youtube.com
const [promise4] = background.browser.webRequest.onBeforeRequest.addListener.yield({
frameId: 0,
tabId: newTab.id,
url: "https://www.youtube.com",
requestId: 2
});
promiseResults = await Promise.all([promise1, promise2, promise3, promise4]);
});
it("should not open two confirm pages", async () => {
await redirectedRequest();
// http://youtube.com is not assigned, no cancel, no reopening
expect(promiseResults[0]).to.deep.equal({});
expect(await newTabResponses.webRequest.onBeforeRequest[0]).to.deep.equal({});
// https://youtube.com is not assigned, no cancel, no reopening
expect(promiseResults[1]).to.deep.equal({});
expect(await newTabResponses.webRequest.onBeforeRequest[1]).to.deep.equal({});
// https://www.youtube.com is assigned, this triggers reopening, cancel
expect(promiseResults[2]).to.deep.equal({
expect(await newTabResponses.webRequest.onBeforeRequest[2]).to.deep.equal({
cancel: true
});
// https://www.youtube.com is assigned, this was a redirect, cancel early, no reopening
expect(promiseResults[3]).to.deep.equal({
expect(await newTabResponses.webRequest.onBeforeRequest[3]).to.deep.equal({
cancel: true
});
@@ -114,67 +84,85 @@ describe("#940", () => {
});
it("should uncancel after webRequest.onCompleted", async () => {
const [promise1] = background.browser.webRequest.onCompleted.addListener.yield({
tabId: 2
await redirectedRequest();
// remove onCompleted listeners because in the real world this request would never complete
// and thus might trigger unexpected behavior because the tab gets removed when reopening
background.browser.webRequest.onCompleted.addListener = sinon.stub();
background.browser.tabs.create.resetHistory();
// we create a tab with the same id and use the same request id to see if uncanceled
await helper.browser.openNewTab({
id: newTab.id,
url: "https://www.youtube.com"
}, {
options: {
webRequest: {
requestId: newTabResponses.webRequest.request.requestId
}
}
});
await promise1;
const [promise2] = background.browser.webRequest.onBeforeRequest.addListener.yield({
frameId: 0,
tabId: 2,
url: "https://www.youtube.com",
requestId: 123
});
await promise2;
background.browser.tabs.create.should.have.been.calledTwice;
background.browser.tabs.create.should.have.been.calledOnce;
});
it("should uncancel after webRequest.onErrorOccurred", async () => {
const [promise1] = background.browser.webRequest.onErrorOccurred.addListener.yield({
tabId: 2
await redirectedRequest();
background.browser.tabs.create.resetHistory();
// we create a tab with the same id and use the same request id to see if uncanceled
await helper.browser.openNewTab({
id: newTab.id,
url: "https://www.youtube.com"
}, {
options: {
webRequest: {
requestId: newTabResponses.webRequest.request.requestId
},
webRequestError: true
}
});
await promise1;
// request to assigned url in same tab
const [promise2] = background.browser.webRequest.onBeforeRequest.addListener.yield({
frameId: 0,
tabId: 2,
url: "https://www.youtube.com",
requestId: 123
});
await promise2;
background.browser.tabs.create.should.have.been.calledTwice;
background.browser.tabs.create.should.have.been.calledOnce;
});
it("should uncancel after 2 seconds", async () => {
await new Promise(resolve => setTimeout(resolve, 2000));
// request to assigned url in same tab
const [promise2] = background.browser.webRequest.onBeforeRequest.addListener.yield({
frameId: 0,
tabId: 2,
url: "https://www.youtube.com",
requestId: 123
await redirectedRequest({
webRequestDontYield: ["onCompleted", "onErrorOccurred"]
});
await promise2;
global.clock.tick(2000);
background.browser.tabs.create.should.have.been.calledTwice;
}).timeout(2002);
background.browser.tabs.create.resetHistory();
// we create a tab with the same id and use the same request id to see if uncanceled
await helper.browser.openNewTab({
id: newTab.id,
url: "https://www.youtube.com"
}, {
options: {
webRequest: {
requestId: newTabResponses.webRequest.request.requestId
},
webRequestError: true
}
});
background.browser.tabs.create.should.have.been.calledOnce;
});
it("should not influence the canceled url in other tabs", async () => {
const newTab = {
id: 123,
await redirectedRequest();
background.browser.tabs.create.resetHistory();
await helper.browser.openNewTab({
cookieStoreId: "firefox-default",
url: "https://www.youtube.com",
index: 10,
active: true
};
await helper.browser.openNewTab(newTab, {
requestId: 321
url: "https://www.youtube.com"
}, {
options: {
webRequestError: true
}
});
background.browser.tabs.create.should.have.been.calledTwice;
background.browser.tabs.create.should.have.been.calledOnce;
});
afterEach(() => {
global.clock.restore();
});
});
});