Module
🛣️ A tiny and fast http request router designed for use with deno and deno deploy
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239import type { ConnInfo } from "https://deno.land/std@0.152.0/http/server.ts";import { assert, assertEquals, assertIsError,} from "https://deno.land/std@0.152.0/testing/asserts.ts";import { METHODS, router } from "./mod.ts";
const TEST_CONN_INFO: ConnInfo = { localAddr: { transport: "tcp", hostname: "test", port: 80, }, remoteAddr: { transport: "tcp", hostname: "test", port: 80, },};
Deno.test("handlers", async ({ step }) => { await step("other", async ({ step }) => { await step("default", async () => { const route = router({ "/test": () => new Response(), }); let response: Response;
response = await route( new Request("https://example.com/"), TEST_CONN_INFO, ); assert(!response.ok); assertEquals(response.body, null); assertEquals(response.status, 404);
response = await route( new Request("https://example.com/test"), TEST_CONN_INFO, ); assert(response.ok); assertEquals(response.body, null); assertEquals(response.status, 200); });
await step("custom", async () => { const route = router({ "/test": () => new Response(), }, () => { return new Response("test", { status: 418, }); }); let response: Response;
response = await route( new Request("https://example.com/"), TEST_CONN_INFO, ); assert(!response.ok); assertEquals(await response.text(), "test"); assertEquals(response.status, 418);
response = await route( new Request("https://example.com/test"), TEST_CONN_INFO, ); assert(response.ok); assertEquals(response.body, null); assertEquals(response.status, 200); }); });
await step("error", async ({ step }) => { await step("default", async () => { const route = router({ "/error": () => { throw new Error("error"); }, });
const response = await route( new Request("https://example.com/error"), TEST_CONN_INFO, ); assert(!response.ok); assertEquals(response.body, null); assertEquals(response.status, 500); });
await step("custom", async () => { const route = router( { "/error/:message": (_req, _ctx, match) => { throw new Error(match.message); }, "/error": () => { throw new Error("error"); }, }, undefined, (_req, _ctx, err) => { assertIsError(err);
return new Response(err.message, { status: 500, }); }, ); let response: Response;
response = await route( new Request("https://example.com/error"), TEST_CONN_INFO, ); assert(!response.ok); assertEquals(await response.text(), "error"); assertEquals(response.status, 500);
response = await route( new Request("https://example.com/error/message"), TEST_CONN_INFO, ); assert(!response.ok); assertEquals(await response.text(), "message"); assertEquals(response.status, 500); }); });
await step("unknown method", async ({ step }) => { await step("default", async () => { const route = router({ "GET@/test": () => new Response(), "PATCH@/test": () => new Response(), }); let response: Response;
response = await route( new Request("https://example.com/test"), TEST_CONN_INFO, ); assert(response.ok); assertEquals(response.body, null); assertEquals(response.status, 200);
response = await route( new Request("https://example.com/test", { method: "PATCH", }), TEST_CONN_INFO, ); assert(response.ok); assertEquals(response.body, null); assertEquals(response.status, 200);
response = await route( new Request("https://example.com/test", { method: "POST", }), TEST_CONN_INFO, ); assert(!response.ok); assertEquals(response.body, null); assertEquals(response.status, 405); assertEquals(response.headers.get("Accept"), "GET, PATCH"); });
await step("custom", async () => { const route = router( { "GET@/test": () => new Response(), "PATCH@/test": () => new Response(), }, undefined, undefined, (_req, _ctx, knownMethods) => { assert(Array.isArray(knownMethods)); assert( knownMethods.every(( method, ) => METHODS.includes( method as ( | "GET" | "HEAD" | "POST" | "PUT" | "DELETE" | "OPTIONS" | "PATCH" ), ) ), ); assertEquals(knownMethods, ["GET", "PATCH"]);
return new Response("unknown method", { status: 405, headers: { Accept: knownMethods.join(", "), }, }); }, ); let response: Response;
response = await route( new Request("https://example.com/test"), TEST_CONN_INFO, ); assert(response.ok); assertEquals(response.body, null); assertEquals(response.status, 200);
response = await route( new Request("https://example.com/test", { method: "PATCH", }), TEST_CONN_INFO, ); assert(response.ok); assertEquals(response.body, null); assertEquals(response.status, 200);
response = await route( new Request("https://example.com/test", { method: "POST", }), TEST_CONN_INFO, ); assert(!response.ok); assertEquals(await response.text(), "unknown method"); assertEquals(response.status, 405); assertEquals(response.headers.get("Accept"), "GET, PATCH"); }); });});