import { expect } from "@std/expect"; import { App, getIslandRegistry, setBuildCache } from "./app.ts"; import { FakeServer } from "./test_utils.ts"; import { ProdBuildCache } from "./build_cache.ts"; import { HttpError } from "./error.ts"; Deno.test("App - .use()", async () => { const app = new App<{ text: string }>() .use((ctx) => { ctx.state.text = "A"; return ctx.next(); }) .use((ctx) => { ctx.state.text += "B"; return ctx.next(); }) .get("/", (ctx) => new Response(ctx.state.text)); const server = new FakeServer(app.handler()); const res = await server.get("/"); expect(await res.text()).toEqual("AB"); }); Deno.test("App - .use() #2", async () => { const app = new App<{ text: string }>() .use(() => new Response("ok #1")) .get("/foo/bar", () => new Response("ok #2")) .get("/", () => new Response("ok #3")); const server = new FakeServer(app.handler()); const res = await server.get("/"); expect(await res.text()).toEqual("ok #1"); }); Deno.test("App - .get()", async () => { const app = new App() .get("/", () => new Response("ok")) .get("/foo", () => new Response("ok")); const server = new FakeServer(app.handler()); let res = await server.get("/"); expect(await res.text()).toEqual("ok"); res = await server.get("/foo"); expect(await res.text()).toEqual("ok"); }); Deno.test("App - .get() with basePath", async () => { const app = new App({ basePath: "/foo/bar" }) .get("/", () => new Response("ok")) .get("/foo", () => new Response("ok")); const server = new FakeServer(app.handler()); let res = await server.get("/"); expect(res.status).toEqual(404); res = await server.get("/foo"); expect(res.status).toEqual(404); res = await server.get("/foo/bar"); expect(res.status).toEqual(200); res = await server.get("/foo/bar/foo"); expect(res.status).toEqual(200); }); Deno.test("App - .post()", async () => { const app = new App<{ text: string }>() .get("/", () => new Response("fail")) .get("/foo", () => new Response("fail")) .post("/", () => new Response("ok")) .post("/foo", () => new Response("ok")); const server = new FakeServer(app.handler()); let res = await server.post("/"); expect(await res.text()).toEqual("ok"); res = await server.post("/foo"); expect(await res.text()).toEqual("ok"); }); Deno.test("App - .post() with basePath", async () => { const app = new App({ basePath: "/foo/bar" }) .post("/", () => new Response("ok")) .post("/foo", () => new Response("ok")); const server = new FakeServer(app.handler()); let res = await server.post("/"); expect(res.status).toEqual(404); res = await server.post("/foo"); expect(res.status).toEqual(404); res = await server.post("/foo/bar"); expect(res.status).toEqual(200); res = await server.post("/foo/bar/foo"); expect(res.status).toEqual(200); }); Deno.test("App - .patch()", async () => { const app = new App<{ text: string }>() .get("/", () => new Response("fail")) .get("/foo", () => new Response("fail")) .patch("/", () => new Response("ok")) .patch("/foo", () => new Response("ok")); const server = new FakeServer(app.handler()); let res = await server.patch("/"); expect(await res.text()).toEqual("ok"); res = await server.patch("/foo"); expect(await res.text()).toEqual("ok"); }); Deno.test("App - .patch() with basePath", async () => { const app = new App({ basePath: "/foo/bar" }) .patch("/", () => new Response("ok")) .patch("/foo", () => new Response("ok")); const server = new FakeServer(app.handler()); let res = await server.patch("/"); expect(res.status).toEqual(404); res = await server.patch("/foo"); expect(res.status).toEqual(404); res = await server.patch("/foo/bar"); expect(res.status).toEqual(200); res = await server.patch("/foo/bar/foo"); expect(res.status).toEqual(200); }); Deno.test("App - .put()", async () => { const app = new App<{ text: string }>() .get("/", () => new Response("fail")) .get("/foo", () => new Response("fail")) .put("/", () => new Response("ok")) .put("/foo", () => new Response("ok")); const server = new FakeServer(app.handler()); let res = await server.put("/"); expect(await res.text()).toEqual("ok"); res = await server.put("/foo"); expect(await res.text()).toEqual("ok"); }); Deno.test("App - .put() with basePath", async () => { const app = new App({ basePath: "/foo/bar" }) .put("/", () => new Response("ok")) .put("/foo", () => new Response("ok")); const server = new FakeServer(app.handler()); let res = await server.put("/"); expect(res.status).toEqual(404); res = await server.put("/foo"); expect(res.status).toEqual(404); res = await server.put("/foo/bar"); expect(res.status).toEqual(200); res = await server.put("/foo/bar/foo"); expect(res.status).toEqual(200); }); Deno.test("App - .delete()", async () => { const app = new App<{ text: string }>() .get("/", () => new Response("fail")) .get("/foo", () => new Response("fail")) .delete("/", () => new Response("ok")) .delete("/foo", () => new Response("ok")); const server = new FakeServer(app.handler()); let res = await server.delete("/"); expect(await res.text()).toEqual("ok"); res = await server.delete("/foo"); expect(await res.text()).toEqual("ok"); }); Deno.test("App - .delete() with basePath", async () => { const app = new App({ basePath: "/foo/bar" }) .delete("/", () => new Response("ok")) .delete("/foo", () => new Response("ok")); const server = new FakeServer(app.handler()); let res = await server.delete("/"); expect(res.status).toEqual(404); res = await server.delete("/foo"); expect(res.status).toEqual(404); res = await server.delete("/foo/bar"); expect(res.status).toEqual(200); res = await server.delete("/foo/bar/foo"); expect(res.status).toEqual(200); }); Deno.test("App - wrong method match", async () => { const app = new App<{ text: string }>() .get("/", () => new Response("ok")) .post("/", () => new Response("ok")); const server = new FakeServer(app.handler()); let res = await server.put("/"); expect(res.status).toEqual(405); expect(await res.text()).toEqual("Method Not Allowed"); res = await server.post("/"); expect(res.status).toEqual(200); expect(await res.text()).toEqual("ok"); }); Deno.test("App - methods with middleware", async () => { const app = new App<{ text: string }>() .use((ctx) => { ctx.state.text = "A"; return ctx.next(); }) .get("/", (ctx) => new Response(ctx.state.text)) .post("/", (ctx) => new Response(ctx.state.text)); const server = new FakeServer(app.handler()); let res = await server.get("/"); expect(await res.text()).toEqual("A"); res = await server.post("/"); expect(await res.text()).toEqual("A"); }); Deno.test("App - .mountApp() compose apps", async () => { const innerApp = new App<{ text: string }>() .use((ctx) => { ctx.state.text = "A"; return ctx.next(); }) .get("/", (ctx) => new Response(ctx.state.text)) .post("/", (ctx) => new Response(ctx.state.text)); const app = new App<{ text: string }>() .get("/", () => new Response("ok")) .mountApp("/foo", innerApp); const server = new FakeServer(app.handler()); let res = await server.get("/"); expect(await res.text()).toEqual("ok"); res = await server.get("/foo"); expect(await res.text()).toEqual("A"); res = await server.post("/foo"); expect(await res.text()).toEqual("A"); }); Deno.test("App - .mountApp() self mount, no middleware", async () => { const innerApp = new App<{ text: string }>() .use((ctx) => { ctx.state.text = "A"; return ctx.next(); }) .get("/foo", (ctx) => new Response(ctx.state.text)) .post("/foo", (ctx) => new Response(ctx.state.text)); const app = new App<{ text: string }>() .get("/", () => new Response("ok")) .mountApp("/", innerApp); const server = new FakeServer(app.handler()); let res = await server.get("/"); expect(await res.text()).toEqual("ok"); res = await server.get("/foo"); expect(await res.text()).toEqual("A"); res = await server.post("/foo"); expect(await res.text()).toEqual("A"); }); Deno.test( "App - .mountApp() self mount, with middleware", async () => { const innerApp = new App<{ text: string }>() .use(function B(ctx) { ctx.state.text += "B"; return ctx.next(); }) .get("/foo", (ctx) => new Response(ctx.state.text)) .post("/foo", (ctx) => new Response(ctx.state.text)); const app = new App<{ text: string }>() .use(function A(ctx) { ctx.state.text = "A"; return ctx.next(); }) .get("/", () => new Response("ok")) .mountApp("/", innerApp); const server = new FakeServer(app.handler()); let res = await server.get("/"); expect(await res.text()).toEqual("ok"); res = await server.get("/foo"); expect(await res.text()).toEqual("AB"); res = await server.post("/foo"); expect(await res.text()).toEqual("AB"); }, ); Deno.test( "App - .mountApp() self mount, different order", async () => { const innerApp = new App<{ text: string }>() .use(function B(ctx) { ctx.state.text += "B"; return ctx.next(); }) .get("/foo", (ctx) => new Response(ctx.state.text)) .post("/foo", (ctx) => new Response(ctx.state.text)); const app = new App<{ text: string }>() .use(function A(ctx) { ctx.state.text = "A"; return ctx.next(); }) .get("/", () => new Response("ok")) .mountApp("/", innerApp); const server = new FakeServer(app.handler()); let res = await server.get("/"); expect(await res.text()).toEqual("ok"); res = await server.get("/foo"); expect(await res.text()).toEqual("AB"); res = await server.post("/foo"); expect(await res.text()).toEqual("AB"); }, ); Deno.test("App - .mountApp() self mount empty", async () => { const innerApp = new App<{ text: string }>() .use((ctx) => { ctx.state.text = "A"; return ctx.next(); }) .get("/foo", (ctx) => new Response(ctx.state.text)); const app = new App<{ text: string }>() .mountApp("/", innerApp); const server = new FakeServer(app.handler()); const res = await server.get("/foo"); expect(await res.text()).toEqual("A"); }); Deno.test( "App - .mountApp() self mount with middleware", async () => { const innerApp = new App<{ text: string }>() .use(function Inner(ctx) { ctx.state.text += "_Inner"; return ctx.next(); }) .get("/", (ctx) => new Response(ctx.state.text)); const app = new App<{ text: string }>() .use(function Outer(ctx) { ctx.state.text = "Outer"; return ctx.next(); }) .mountApp("/", innerApp); const server = new FakeServer(app.handler()); const res = await server.get("/"); expect(await res.text()).toEqual("Outer_Inner"); }, ); // https://github.com/denoland/fresh/issues/3033 Deno.test( "App - .mountApp() self mount with dynamic routes", async () => { const innerApp = new App<{ text: string }>() .get("/", () => new Response("list authors")) .get("/:name", (ctx) => new Response(`show: ${ctx.params.name}`)); const app = new App<{ text: string }>() .mountApp("/api/authors", innerApp); const server = new FakeServer(app.handler()); let res = await server.get("/api/authors"); expect(await res.text()).toEqual("list authors"); res = await server.get("/api/authors/foo"); expect(await res.text()).toEqual("show: foo"); }, ); Deno.test("App - catches errors", async () => { let thrownErr: unknown | null = null; const app = new App<{ text: string }>() .use(async (ctx) => { ctx.state.text = "A"; try { return await ctx.next(); } catch (err) { thrownErr = err; throw err; } }) .get("/", () => { throw new Error("fail"); }); const server = new FakeServer(app.handler()); const res = await server.get("/"); expect(res.status).toEqual(500); expect(thrownErr).toBeInstanceOf(Error); }); // TODO: Find a better way to test this Deno.test.ignore("App - finish setup", async () => { const app = new App<{ text: string }>() .get("/", (ctx) => { return ctx.render(