From fe9f8b23a69999077ba0eac9460df822fc6f9678 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Mon, 2 Dec 2024 13:53:33 +0100 Subject: [PATCH] test: add workerd test for unenv/fetch (#361) --- test/workerd/tests.mjs | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/test/workerd/tests.mjs b/test/workerd/tests.mjs index 016f6095..9e85a6fd 100644 --- a/test/workerd/tests.mjs +++ b/test/workerd/tests.mjs @@ -128,3 +128,26 @@ export const workerd_path = { assert.strictEqual(pathPosix.delimiter, ":"); }, }; + +// --- unenv:fetch + +export const unenv_fetch = { + async test() { + // https://srvx.unjs.io/guide/node#reverse-compatibility + // This method is used in h3 v1 and Nitro v2 for server fetch without network roundtrip + internal caching system. + const { createFetch, createCall } = await import("unenv/runtime/fetch"); + const serverFetch = createFetch( + createCall(async (req, res) => { + res.end( + JSON.stringify({ "req.url": req.url, "req.headers": req.headers }), + ); + }), + ); + const res = await serverFetch("/test", { headers: { foo: "bar" } }); + const resBody = await res.json(); + assert.deepEqual(resBody, { + "req.url": "/test", + "req.headers": { foo: "bar", host: "localhost" }, + }); + }, +};