Last active
June 5, 2020 14:48
-
-
Save danielweck/068be622e1687c6f85e1409562098332 to your computer and use it in GitHub Desktop.
Electron WebRequest.onBeforeSendHeaders bug repro test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { app, BrowserWindow } = require("electron"); | |
const testURL = async (headersPassthrough, url, done) => { | |
console.log("-----------------------------------"); | |
console.log(url); | |
console.log("-----------------------------------"); | |
const browserWindow = new BrowserWindow(); | |
// https://www.electronjs.org/docs/api/web-request#webrequestonbeforesendheadersfilter-listener | |
// "When provided, request will be made with these headers." | |
browserWindow.webContents.session.webRequest.onBeforeSendHeaders( | |
(details, callback) => { | |
if (headersPassthrough) { | |
details.requestHeaders["X-request-header-2"] = "yes2"; | |
} | |
console.log( | |
"onBeforeSendHeaders requestHeaders: ", | |
details.url, | |
details.requestHeaders | |
); | |
callback({ | |
cancel: false, | |
requestHeaders: headersPassthrough | |
? details.requestHeaders | |
: undefined, | |
}); | |
} | |
); | |
// https://www.electronjs.org/docs/api/web-request#webrequestonheadersreceivedfilter-listener | |
// "When provided, the server is assumed to have responded with these headers." | |
browserWindow.webContents.session.webRequest.onHeadersReceived( | |
(details, callback) => { | |
if (headersPassthrough) { | |
details.responseHeaders["X-response-header"] = "yes"; | |
} | |
console.log( | |
"onHeadersReceived responseHeaders: ", | |
details.url, | |
details.responseHeaders | |
); | |
callback({ | |
responseHeaders: headersPassthrough | |
? details.responseHeaders | |
: undefined, | |
}); | |
} | |
); | |
browserWindow.webContents.once("did-finish-load", async () => { | |
const html = await browserWindow.webContents.executeJavaScript( | |
"document.documentElement.innerHTML" | |
); | |
const i = html.indexOf("{"); | |
const json = html.substr(i, html.lastIndexOf("}") - i + 1); | |
const headers = JSON.parse(json); | |
console.log(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"); | |
console.log(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"); | |
console.log(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"); | |
console.log(">>>>>>>>>>>>> HEADERS FROM: ", url, headers); | |
console.log(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"); | |
// fetch("https://httpbin.org/response-headers").then((res) => { | |
// }).catch((err) => { | |
// }); | |
// const res = await fetch("https://httpbin.org/response-headers"); | |
// process.stdout.print(res.headers + "\n"); | |
// return JSON.stringify(res.headers); | |
// const f = () => { | |
// return "test"; | |
// }; | |
// f(); | |
console.log("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"); | |
console.log("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"); | |
console.log("FETCH WILL REQUEST: ", url); | |
console.log("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"); | |
console.log("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"); | |
const fetchResponseHeadersStr = await browserWindow.webContents.executeJavaScript(` | |
fetch("${url}").then((res) => { | |
const obj = {}; | |
for (const pair of res.headers.entries()) { | |
obj[pair[0]] = pair[1]; | |
} | |
return JSON.stringify(obj); | |
}).catch((err) => { | |
return err.toString(); | |
}); | |
`); | |
console.log("FETCH RESPONSE HEADERS: ", JSON.parse(fetchResponseHeadersStr)); | |
console.log(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"); | |
console.log(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"); | |
console.log(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"); | |
done(); | |
}); | |
console.log("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"); | |
console.log("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"); | |
console.log("BROWSER WINDOW WILL REQUEST: ", url); | |
console.log("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"); | |
console.log("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"); | |
await browserWindow.webContents.loadURL(url, { | |
extraHeaders: "X-request-header-1: yes1", | |
}); | |
}; | |
app.once("ready", async () => { | |
console.log("############################################################"); | |
console.log("############################################################"); | |
console.log("############################################################"); | |
console.log("############################################################"); | |
console.log("############################################################"); | |
console.log("###### TEST WITH UNDEFINED HEADERS IN CALLBACK ..."); | |
console.log("############################################################"); | |
await testURL(false, "https://httpbin.org/headers", async () => { | |
await testURL( | |
false, | |
"https://httpbin.org/response-headers", | |
async () => { | |
await new Promise((res, rej) => { | |
setTimeout(() => { | |
res(); | |
}, 1000) | |
}); | |
console.log( | |
"############################################################" | |
); | |
console.log( | |
"############################################################" | |
); | |
console.log( | |
"############################################################" | |
); | |
console.log( | |
"############################################################" | |
); | |
console.log( | |
"###### TEST WITH PASSTHROUGH HEADERS IN CALLBACK ..." | |
); | |
console.log( | |
"############################################################" | |
); | |
await testURL(true, "https://httpbin.org/headers", async () => { | |
await testURL( | |
true, | |
"https://httpbin.org/response-headers", | |
() => { | |
console.log("DONE, EXITING ..."); | |
app.quit(); | |
} | |
); | |
}); | |
} | |
); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment