feat: add middleware destination (#1123)

Closes #521
This commit is contained in:
Leo Kettmeir
2023-04-20 13:14:12 +02:00
committed by GitHub
parent f3f6f227c4
commit 9dd026e0f5
5 changed files with 159 additions and 64 deletions
+10
View File
@@ -50,8 +50,18 @@ async function rootHandler(
return resp;
}
async function kindHandler(
_req: Request,
ctx: MiddlewareHandlerContext,
) {
const resp = await ctx.next();
resp.headers.set("destination", ctx.destination);
return resp;
}
export const handler: MiddlewareHandler | MiddlewareHandler[] = [
rootHandler,
logHandler,
kindHandler,
corsHandler,
];
+32
View File
@@ -494,6 +494,38 @@ Deno.test({
},
});
Deno.test("middleware destination", async (t) => {
await t.step("internal", async () => {
const resp = await router(
new Request("https://fresh.deno.dev/_frsh/refresh.js"),
);
assert(resp);
assertEquals(resp.headers.get("destination"), "internal");
await resp.body?.cancel();
});
await t.step("static", async () => {
const resp = await router(new Request("https://fresh.deno.dev/foo.txt"));
assert(resp);
assertEquals(resp.headers.get("destination"), "static");
await resp.body?.cancel();
});
await t.step("route", async () => {
const resp = await router(new Request("https://fresh.deno.dev/"));
assert(resp);
assertEquals(resp.headers.get("destination"), "route");
await resp.body?.cancel();
});
await t.step("notFound", async () => {
const resp = await router(new Request("https://fresh.deno.dev/bar/bar"));
assert(resp);
assertEquals(resp.headers.get("destination"), "notFound");
await resp.body?.cancel();
});
});
Deno.test("experimental Deno.serve", {
sanitizeOps: false,
sanitizeResources: false,