Skip to content

Commit

Permalink
test: add workerd test for unenv/fetch (#361)
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 authored Dec 2, 2024
1 parent 48160f4 commit fe9f8b2
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions test/workerd/tests.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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" },
});
},
};

0 comments on commit fe9f8b2

Please sign in to comment.