Cancel redirects for the same requestIds and urls if originating from the same tabId
Fixes #940
This commit is contained in:
@@ -19,6 +19,9 @@ module.exports = () => {
|
||||
},
|
||||
onCompleted: {
|
||||
addListener: sinon.stub()
|
||||
},
|
||||
onErrorOccurred: {
|
||||
addListener: sinon.stub()
|
||||
}
|
||||
},
|
||||
windows: {
|
||||
|
||||
+9
-7
@@ -18,19 +18,21 @@ module.exports = {
|
||||
});
|
||||
},
|
||||
|
||||
async openNewTab(tab, options = {isAsync: true}) {
|
||||
async openNewTab(tab, options = {}) {
|
||||
if (options.resetHistory) {
|
||||
background.browser.tabs.create.resetHistory();
|
||||
background.browser.tabs.remove.resetHistory();
|
||||
}
|
||||
background.browser.tabs.get.resolves(tab);
|
||||
background.browser.webRequest.onBeforeRequest.addListener.yield({
|
||||
background.browser.tabs.onCreated.addListener.yield(tab);
|
||||
const [promise] = background.browser.webRequest.onBeforeRequest.addListener.yield({
|
||||
frameId: 0,
|
||||
tabId: tab.id,
|
||||
url: tab.url,
|
||||
requestId: options.requestId
|
||||
});
|
||||
background.browser.tabs.onCreated.addListener.yield(tab);
|
||||
if (!options.isAsync) {
|
||||
return;
|
||||
}
|
||||
await nextTick();
|
||||
|
||||
return promise;
|
||||
}
|
||||
},
|
||||
|
||||
|
||||
+139
-2
@@ -22,8 +22,7 @@ describe("#940", () => {
|
||||
active: true
|
||||
};
|
||||
helper.browser.openNewTab(newTab, {
|
||||
requestId: 1,
|
||||
isAsync: false
|
||||
requestId: 1
|
||||
});
|
||||
|
||||
// other addon sees the same request
|
||||
@@ -40,4 +39,142 @@ describe("#940", () => {
|
||||
background.browser.tabs.create.should.have.been.calledOnce;
|
||||
});
|
||||
});
|
||||
|
||||
describe("when redirects change requestId midflight", () => {
|
||||
let promiseResults;
|
||||
beforeEach(async () => {
|
||||
// init
|
||||
const activeTab = {
|
||||
id: 1,
|
||||
cookieStoreId: "firefox-container-1",
|
||||
url: "https://www.youtube.com",
|
||||
index: 0
|
||||
};
|
||||
await helper.browser.initializeWithTab(activeTab);
|
||||
// assign the activeTab.url
|
||||
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 () => {
|
||||
// http://youtube.com is not assigned, no cancel, no reopening
|
||||
expect(promiseResults[0]).to.deep.equal({});
|
||||
|
||||
// https://youtube.com is not assigned, no cancel, no reopening
|
||||
expect(promiseResults[1]).to.deep.equal({});
|
||||
|
||||
// https://www.youtube.com is assigned, this triggers reopening, cancel
|
||||
expect(promiseResults[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({
|
||||
cancel: true
|
||||
});
|
||||
|
||||
background.browser.tabs.create.should.have.been.calledOnce;
|
||||
});
|
||||
|
||||
it("should uncancel after webRequest.onCompleted", async () => {
|
||||
const [promise1] = background.browser.webRequest.onCompleted.addListener.yield({
|
||||
tabId: 2
|
||||
});
|
||||
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;
|
||||
});
|
||||
|
||||
it("should uncancel after webRequest.onErrorOccurred", async () => {
|
||||
const [promise1] = background.browser.webRequest.onErrorOccurred.addListener.yield({
|
||||
tabId: 2
|
||||
});
|
||||
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;
|
||||
});
|
||||
|
||||
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 promise2;
|
||||
|
||||
background.browser.tabs.create.should.have.been.calledTwice;
|
||||
}).timeout(2002);
|
||||
|
||||
it("should not influence the canceled url in other tabs", async () => {
|
||||
const newTab = {
|
||||
id: 123,
|
||||
cookieStoreId: "firefox-default",
|
||||
url: "https://www.youtube.com",
|
||||
index: 10,
|
||||
active: true
|
||||
};
|
||||
await helper.browser.openNewTab(newTab, {
|
||||
requestId: 321
|
||||
});
|
||||
|
||||
background.browser.tabs.create.should.have.been.calledTwice;
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
+2
-2
@@ -91,11 +91,11 @@ global.buildPopupDom = async (options = {}) => {
|
||||
global.afterEach(() => {
|
||||
if (global.background) {
|
||||
global.background.dom.window.close();
|
||||
delete global.background.dom;
|
||||
delete global.background;
|
||||
}
|
||||
|
||||
if (global.popup) {
|
||||
global.popup.dom.window.close();
|
||||
delete global.popup.dom;
|
||||
delete global.popup;
|
||||
}
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user