Skip to content

Instantly share code, notes, and snippets.

@jtbandes
Last active April 24, 2024 00:38
Show Gist options
  • Save jtbandes/039be5d61760aeb0eb12389228e643d1 to your computer and use it in GitHub Desktop.
Save jtbandes/039be5d61760aeb0eb12389228e643d1 to your computer and use it in GitHub Desktop.
Playwright Electron onHeadersReceived bug repro for https://github.com/microsoft/playwright/issues/30495
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta http-equiv="Content-Security-Policy" content="default-src 'self' https://example.com; script-src 'self'; style-src 'self' 'unsafe-inline'">
</head>
<body>
Demo page
<script src="./renderer.js"></script>
</body>
</html>
const path = require("node:path");
const electronPath = require("electron");
const { _electron: electron } = require("playwright");
async function main() {
const electronApp = await electron.launch({
args: [path.resolve(__dirname, "main.js")],
// In node.js the electron import gives us the path to the electron binary
executablePath: electronPath,
});
electronApp.process().stdout?.on("data", (data) => {
console.info(`[main stdout] ${data}`);
});
electronApp.process().stderr?.on("data", (data) => {
console.info(`[main stderr] ${data}`);
});
const electronWindow = await electronApp.firstWindow();
await electronWindow.route("https://example.com/**", async (reqRoute, request) => {
console.log(`fulfilling mock request: ${request.url()}\n`);
await reqRoute.fulfill({ json: { foo: "bar" } });
});
electronWindow.on("console", (message) => {
console.info(`[renderer ${message.type()}]`, message.text());
});
await electronApp.evaluate(({ BrowserWindow }) => {
for (const win of BrowserWindow.getAllWindows()) {
win.webContents.openDevTools();
}
});
}
main().catch(console.error);
const { app, BrowserWindow, session } = require("electron");
function createWindow() {
const mainWindow = new BrowserWindow({ width: 800, height: 600 });
mainWindow.webContents.openDevTools();
mainWindow.loadFile("index.html");
}
app.whenReady().then(() => {
createWindow();
// Comment out this block to fix the request
session.defaultSession.webRequest.onHeadersReceived((details, callback) => {
const responseHeaders = {
...details.responseHeaders,
"x-example-header": "test",
};
callback({ responseHeaders });
});
});
{
"devDependencies": {
"electron": "30.0.1",
"playwright": "1.43.1"
}
}
console.log("hi from renderer");
(async function () {
// wait for the .route() mock to be installed
await new Promise((resolve) => setTimeout(resolve, 1000));
try {
const response = await fetch("https://example.com/foo");
console.log("got response:", response, response.status, response.ok, Object.fromEntries(response.headers.entries()));
console.log("json:", await response.json());
} catch (err) {
console.log("failed:", err);
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment