diff --git a/src/inline-engine.ts b/src/inline-engine.ts index c156b0fa36..508dbe5ee2 100644 --- a/src/inline-engine.ts +++ b/src/inline-engine.ts @@ -258,13 +258,14 @@ async function toInlineReq(input: FetchInput, init?: RequestInit): Promise { + req.resume(); + req.on("end", () => { + const status = Number(req.url?.slice(1)); + res.writeHead(status, { "content-type": "text/plain", "x-inline-test": "preserved" }); + res.end(status === 200 || status === 201 ? "upstream content" : undefined); + }); +}); +upstream.listen(0, "127.0.0.1"); +await once(upstream, "listening"); +const origin = `http://127.0.0.1:${(upstream.address() as AddressInfo).port}`; +const ie = await import("../src/inline-engine.ts"); + +after(async () => { + upstream.closeAllConnections(); + await new Promise((resolve) => upstream.close(() => resolve())); + try { + const { flushAllSessions } = await import("../src/session.ts"); + await flushAllSessions(); + } catch { /* engine never booted in this run */ } + ie._resetInlineEngineForTest(); + if (PREV_STATE === undefined) delete process.env.XDG_STATE_HOME; + else process.env.XDG_STATE_HOME = PREV_STATE; + if (PREV_DATA === undefined) delete process.env.XDG_DATA_HOME; + else process.env.XDG_DATA_HOME = PREV_DATA; + if (PREV_CONFIG === undefined) delete process.env.XDG_CONFIG_HOME; + else process.env.XDG_CONFIG_HOME = PREV_CONFIG; + fs.rmSync(STATE_ROOT, { recursive: true, force: true }); + fs.rmSync(DATA_ROOT, { recursive: true, force: true }); + fs.rmSync(CONFIG_ROOT, { recursive: true, force: true }); +}); + +for (const status of [204, 205, 304]) { + test(`inline HTTP ${status} has a null Response body`, { timeout: 30_000 }, async () => { + const engine = await ie.getInlineEngine(); + const response = await engine.dispatch(`${ie.INLINE_ORIGIN}/bili/${origin}/${status}`, {}, globalThis.fetch); + assert.equal(response.status, status); + assert.equal(response.body, null); + assert.equal(await response.text(), ""); + assert.equal(response.headers.get("x-inline-test"), "preserved"); + }); +} + +test("inline HEAD has a null body even when the status allows a GET body", { timeout: 30_000 }, async () => { + const engine = await ie.getInlineEngine(); + const response = await engine.dispatch(`${ie.INLINE_ORIGIN}/bili/${origin}/200`, { method: "HEAD" }, globalThis.fetch); + assert.equal(response.status, 200); + assert.equal(response.body, null); + assert.equal(await response.text(), ""); + assert.equal(response.headers.get("x-inline-test"), "preserved"); +}); + +for (const status of [200, 201]) { + test(`inline GET ${status} keeps its readable response content`, { timeout: 30_000 }, async () => { + const engine = await ie.getInlineEngine(); + const response = await engine.dispatch(`${ie.INLINE_ORIGIN}/bili/${origin}/${status}`, {}, globalThis.fetch); + assert.equal(response.status, status); + assert.ok(response.body); + assert.equal(await response.text(), "upstream content"); + }); +}