2024-08-07 12:35:13 +00:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
|
|
|
|
2024-09-12 03:50:47 +00:00
|
|
|
import { type Route, route } from "./unstable_route.ts";
|
2024-08-07 12:35:13 +00:00
|
|
|
import { assertEquals } from "../assert/equals.ts";
|
|
|
|
|
|
|
|
const routes: Route[] = [
|
|
|
|
{
|
|
|
|
pattern: new URLPattern({ pathname: "/about" }),
|
|
|
|
handler: (request: Request) => new Response(new URL(request.url).pathname),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
pattern: new URLPattern({ pathname: "/users/:id" }),
|
|
|
|
handler: (_request, _info, params) =>
|
|
|
|
new Response(params?.pathname.groups.id),
|
|
|
|
},
|
2024-08-22 12:05:39 +00:00
|
|
|
{
|
|
|
|
pattern: new URLPattern({ pathname: "/users/:id" }),
|
|
|
|
method: "POST",
|
|
|
|
handler: () => new Response("Done"),
|
|
|
|
},
|
2024-09-18 06:28:27 +00:00
|
|
|
{
|
|
|
|
pattern: new URLPattern({ pathname: "/resource" }),
|
|
|
|
method: ["GET", "HEAD"],
|
|
|
|
handler: (request: Request) =>
|
|
|
|
new Response(request.method === "HEAD" ? null : "Ok"),
|
|
|
|
},
|
2024-08-07 12:35:13 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
function defaultHandler(request: Request) {
|
|
|
|
return new Response(new URL(request.url).pathname, { status: 404 });
|
|
|
|
}
|
|
|
|
|
|
|
|
Deno.test("route()", async (t) => {
|
|
|
|
const handler = route(routes, defaultHandler);
|
|
|
|
|
|
|
|
await t.step("handles static routes", async () => {
|
|
|
|
const request = new Request("http://example.com/about");
|
2024-08-07 13:12:53 +00:00
|
|
|
const response = await handler(request);
|
2024-08-07 12:35:13 +00:00
|
|
|
assertEquals(response?.status, 200);
|
|
|
|
assertEquals(await response?.text(), "/about");
|
|
|
|
});
|
|
|
|
|
|
|
|
await t.step("handles dynamic routes", async () => {
|
2024-08-22 12:05:39 +00:00
|
|
|
const request1 = new Request("http://example.com/users/123");
|
|
|
|
const response1 = await handler(request1);
|
|
|
|
assertEquals(await response1?.text(), "123");
|
|
|
|
assertEquals(response1?.status, 200);
|
|
|
|
|
|
|
|
const request2 = new Request("http://example.com/users/123", {
|
2024-08-07 12:35:13 +00:00
|
|
|
method: "POST",
|
|
|
|
});
|
2024-08-22 12:05:39 +00:00
|
|
|
const response2 = await handler(request2);
|
|
|
|
assertEquals(await response2?.text(), "Done");
|
|
|
|
assertEquals(response2?.status, 200);
|
2024-08-07 12:35:13 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
await t.step("handles default handler", async () => {
|
|
|
|
const request = new Request("http://example.com/not-found");
|
2024-08-07 13:12:53 +00:00
|
|
|
const response = await handler(request);
|
2024-08-07 12:35:13 +00:00
|
|
|
assertEquals(response?.status, 404);
|
|
|
|
assertEquals(await response?.text(), "/not-found");
|
|
|
|
});
|
2024-09-18 06:28:27 +00:00
|
|
|
|
|
|
|
await t.step("handles multiple methods", async () => {
|
|
|
|
const getMethodRequest = new Request("http://example.com/resource");
|
|
|
|
const getMethodResponse = await handler(getMethodRequest);
|
|
|
|
assertEquals(getMethodResponse?.status, 200);
|
|
|
|
assertEquals(await getMethodResponse?.text(), "Ok");
|
|
|
|
|
|
|
|
const headMethodRequest = new Request("http://example.com/resource", {
|
|
|
|
method: "HEAD",
|
|
|
|
});
|
|
|
|
const headMethodResponse = await handler(headMethodRequest);
|
|
|
|
assertEquals(headMethodResponse?.status, 200);
|
|
|
|
assertEquals(await headMethodResponse?.text(), "");
|
|
|
|
});
|
2024-08-07 12:35:13 +00:00
|
|
|
});
|