mirror of
https://github.com/bitwarden/browser
synced 2026-02-13 23:13:36 +00:00
Include fetch override in auth fixtures
This commit is contained in:
@@ -9,6 +9,8 @@ type BrowserName = "chromium" | "firefox" | "webkit";
|
||||
|
||||
import { Play, SingleUserScene, SingleUserSceneTemplate } from "@bitwarden/playwright-helpers";
|
||||
|
||||
import { addInitScriptForPlayId } from "./page-extension";
|
||||
|
||||
const hostname = new URL(webServerBaseUrl).hostname;
|
||||
const dataDir = process.env.PLAYWRIGHT_DATA_DIR ?? "playwright-data";
|
||||
// Ensure data directory exists
|
||||
@@ -79,6 +81,7 @@ export class AuthFixture {
|
||||
}
|
||||
const context = await this._browser.newContext();
|
||||
this._page = await context.newPage();
|
||||
await addInitScriptForPlayId(this._page, process.env.PLAY_ID!);
|
||||
}
|
||||
return this._page;
|
||||
}
|
||||
|
||||
@@ -2,28 +2,29 @@ import { Page, TestFixture } from "@playwright/test";
|
||||
|
||||
export function pageExtension(): TestFixture<Page, { page: Page; playId: string }> {
|
||||
return async ({ page, playId }, use) => {
|
||||
await page.addInitScript(
|
||||
({ p }) => {
|
||||
const originalFetch = window.fetch;
|
||||
window.fetch = async function (
|
||||
input: string | URL | globalThis.Request,
|
||||
init?: RequestInit,
|
||||
) {
|
||||
// Build a Request that takes into account both the input and any provided init overrides
|
||||
const baseRequest =
|
||||
input instanceof globalThis.Request
|
||||
? init
|
||||
? new Request(input, init)
|
||||
: input
|
||||
: new Request(input, init);
|
||||
|
||||
baseRequest.headers.set("x-play-id", p);
|
||||
|
||||
return originalFetch(baseRequest);
|
||||
};
|
||||
},
|
||||
{ p: playId },
|
||||
);
|
||||
await addInitScriptForPlayId(page, playId);
|
||||
await use(page);
|
||||
};
|
||||
}
|
||||
|
||||
export function addInitScriptForPlayId(page: Page, playId: string): Promise<void> {
|
||||
return page.addInitScript(
|
||||
({ p }) => {
|
||||
const originalFetch = window.fetch;
|
||||
window.fetch = async function (input: string | URL | globalThis.Request, init?: RequestInit) {
|
||||
// Build a Request that takes into account both the input and any provided init overrides
|
||||
const baseRequest =
|
||||
input instanceof globalThis.Request
|
||||
? init
|
||||
? new Request(input, init)
|
||||
: input
|
||||
: new Request(input, init);
|
||||
|
||||
baseRequest.headers.set("x-play-id", p);
|
||||
|
||||
return originalFetch(baseRequest);
|
||||
};
|
||||
},
|
||||
{ p: playId },
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user