30 lines
1.6 KiB
JavaScript
30 lines
1.6 KiB
JavaScript
import { writeFile } from 'node:fs/promises';
|
|
import puppeteer from 'puppeteer-core';
|
|
|
|
const endpoint = process.env.CDP_ENDPOINT ?? 'http://127.0.0.1:9222';
|
|
const baseUrl = process.env.TEST_BASE_URL ?? 'http://127.0.0.1:18080';
|
|
const outPath = process.env.COOKIE_EXPORT_PATH ?? '/tmp/browser-cookie-export.json';
|
|
const cookieName = process.env.COOKIE_NAME ?? 'phase06_cookie';
|
|
const cookieValue = process.env.COOKIE_VALUE ?? 'roundtrip-ok';
|
|
|
|
const browser = await puppeteer.connect({ browserURL: endpoint });
|
|
const page = await browser.newPage();
|
|
await page.goto(`${baseUrl}/set`, { waitUntil: 'domcontentloaded' });
|
|
const echoResponse = await page.goto(`${baseUrl}/echo`, { waitUntil: 'domcontentloaded' });
|
|
const echo = await echoResponse?.json();
|
|
if (!echo?.cookie?.includes(`${cookieName}=${cookieValue}`)) {
|
|
await browser.disconnect();
|
|
throw new Error(`seed cookie was not sent to echo endpoint: ${JSON.stringify(echo)}`);
|
|
}
|
|
const client = await page.target().createCDPSession();
|
|
const { cookies } = await client.send('Network.getAllCookies');
|
|
const matching = cookies.filter((cookie) => cookie.name === cookieName);
|
|
if (!matching.some((cookie) => cookie.value === cookieValue)) {
|
|
await browser.disconnect();
|
|
throw new Error(`exported cookies missing expected value: ${JSON.stringify(matching)}`);
|
|
}
|
|
await writeFile(outPath, JSON.stringify({ baseUrl, cookieName, cookieValue, cookies: matching }, null, 2));
|
|
await page.close();
|
|
await browser.disconnect();
|
|
console.log(JSON.stringify({ ok: true, outPath, exported: matching.length, echoCookie: echo.cookie }));
|
|
process.exit(0);
|