// Guest node:http acceptance proofs: a guest server listens on a port, the page // reaches it through the SW virtual origin at /~/@/http//. import { test, expect, type Page } from "@playwright/test"; import { appUrl } from "./http-golden"; async function bootServer(page: Page, path = "/demo/http.html"): Promise { await page.goto(path); await expect(page.frameLocator("#app").locator("#hi")).toBeVisible({ timeout: 20_000 }); return await appUrl(page); } test("iframe renders the guest server's HTML", async ({ page }) => { await bootServer(page); await expect(page.frameLocator("#app").locator("#hi")).toHaveText("Hello from GET /"); await expect(page.locator("#out")).toHaveText("listening on 3000\n"); await expect(page.locator("#exit")).toHaveText("…"); }); test("POST body reaches the guest and its response headers reach the page", async ({ page }) => { const base = await bootServer(page); const res = await page.evaluate(async (base) => { const r = await fetch(base + "echo?x=1", { method: "POST", body: "ping" }); return { status: r.status, text: await r.text(), nib: r.headers.get("x-nib") }; }, base); expect(res).toEqual({ status: 200, text: "ping", nib: "1" }); }); test("a port nobody listens on answers 502", async ({ page }) => { const base = await bootServer(page); const res = await page.evaluate(async (base) => { const r = await fetch(base.replace("/http/3000/", "/http/4000/")); return { status: r.status, text: await r.text() }; }, base); expect(res).toEqual({ status: 502, text: "ERR_NIB_NO_LISTENER: nothing is listening on port 4000\n" }); }); test("server.close() lets the process exit 0 and frees the port", async ({ page }) => { const base = await bootServer(page); const bye = await page.evaluate(async (base) => (await fetch(base + "bye")).text(), base); expect(bye).toBe("bye\n"); await expect(page.locator("#exit")).toHaveText("0", { timeout: 20_000 }); await expect(page.locator("#out")).toHaveText("listening on 3000\nclosed\n"); const status = await page.evaluate(async (base) => (await fetch(base)).status, base); expect(status).toBe(502); }); test("ESM server via import http from 'node:http' answers too", async ({ page }) => { await bootServer(page, "/demo/http.html?esm"); await expect(page.frameLocator("#app").locator("#hi")).toHaveText("Hello from GET / (esm)"); await expect(page.locator("#err")).toHaveText(""); });