Files
sweet/packages/fresh/src/app_test.tsx
T
fry69 3010e27097 fix: add basePath support in mountApp() (#3395)
Fix for mountApp() with the new basePath handling

---
Generated PR message:

I successfully investigated and implemented a fix for mountApp() to make
it compatible with basePath handling. Here's what I found and fixed:

  The Problem

The original mountApp() implementation completely ignored the inner
app's basePath. When mounting an app like this:

```ts
  const innerApp = new App({ basePath: "/api" })
    .get("/users", () => new Response("users"));

  const app = new App()
    .mountApp("/v1", innerApp);
```

The route would become /v1/users instead of the expected /v1/api/users
because the inner app's /api basePath was lost.

  Root Cause

The issue was in the mountApp() method at packages/fresh/src/app.ts:333.
The original code only used:

```ts
  pattern: mergePath(path, cmd.pattern, true)
```

This only considered the mount path (/v1) and the command pattern
(/users), but ignored app.config.basePath (/api).

  The Fix

I modified the mountApp() method to properly handle the inner app's
basePath:

```ts
  // Apply the inner app's basePath if it exists
  let effectivePattern = cmd.pattern;
  if (app.config.basePath) {
    effectivePattern = mergePath(app.config.basePath, cmd.pattern, false);
  }

  const clone = {
    ...cmd,
    pattern: mergePath(path, effectivePattern, true),
    includeLastSegment: cmd.pattern === "/" || cmd.includeLastSegment,
  };
```

The key insight was to only apply the basePath transformation when the
inner app actually has a basePath (not empty), which preserves backward
  compatibility with existing code that doesn't use basePath.

  Test Coverage

  I added comprehensive test cases covering:

1. Inner app with basePath: /v1 + /api basePath + /users route =
/v1/api/users
2. Main app with basePath + mounted app: /main basePath + /sub mount +
/data route = /main/sub/data
3. Both main and inner basePath: /main + /services + /api/v2 + /users =
/main/services/api/v2/users

  Backward Compatibility

  The fix maintains full backward compatibility:
  - ✅ All existing mountApp() tests pass
  - ✅ All existing basePath tests pass
  - ✅ New functionality works as expected

  Now the three basePath mechanisms work correctly together:

  1. Vite base config → Static asset URLs
  2. App basePath option → Route matching (now works with mountApp!)
  3. External mounting → Framework integration

The solution correctly handles the scenario you described where
vite.config.ts, main.ts, and external mounting all work together
seamlessly.
2025-09-12 10:06:06 +02:00

852 lines
23 KiB
TypeScript

import { expect } from "@std/expect";
import { App } from "./app.ts";
import { FakeServer } from "./test_utils.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() compose apps with .route()", async () => {
const innerApp = new App<{ text: string }>()
.use((ctx) => {
ctx.state.text = "A";
return ctx.next();
})
.route("/", { handler: (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");
});
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 - .mountApp() fallback route", async () => {
let called = "";
const innerApp = new App<{ text: string }>()
.use(function Inner(ctx) {
called += "_Inner";
return ctx.next();
})
.get("/", (ctx) => new Response(ctx.state.text));
const app = new App<{ text: string }>()
.use(function Outer(ctx) {
called += "Outer";
return ctx.next();
})
.mountApp("/", innerApp);
const server = new FakeServer(app.handler());
const res = await server.get("/invalid");
await res.body?.cancel();
expect(called).toEqual("Outer_Inner");
});
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);
});
Deno.test("App - sets error on context", async () => {
const thrown: [unknown, unknown][] = [];
const app = new App()
.use(async (ctx) => {
try {
return await ctx.next();
} catch (err) {
thrown.push([err, ctx.error]);
throw err;
}
})
.use(async (ctx) => {
try {
return await ctx.next();
} catch (err) {
thrown.push([err, ctx.error]);
throw err;
}
})
.get("/", () => {
throw "<mock error>";
});
const server = new FakeServer(app.handler());
const res = await server.get("/");
await res.body?.cancel();
expect(thrown.length).toEqual(2);
expect(thrown[0][0]).toEqual(thrown[0][1]);
expect(thrown[1][0]).toEqual(thrown[1][1]);
});
Deno.test("App - support setting request init in ctx.render()", async () => {
const app = new App<{ text: string }>()
.get("/", (ctx) => {
return ctx.render(<div>ok</div>, {
status: 416,
headers: { "X-Foo": "foo" },
});
});
const server = new FakeServer(app.handler());
const res = await server.get("/");
await res.body?.cancel();
expect(res.status).toEqual(416);
expect(res.headers.get("X-Foo")).toEqual("foo");
});
Deno.test("App - throw when middleware returns no response", async () => {
const app = new App<{ text: string }>()
.get(
"/",
// deno-lint-ignore no-explicit-any
(() => {}) as any,
);
const server = new FakeServer(app.handler());
const res = await server.get("/");
const text = await res.text();
expect(res.status).toEqual(500);
expect(text).toContain("Internal server error");
});
Deno.test("App - overwrite default 404 handler", async () => {
const app = new App()
.notFound(() => new Response("bar", { status: 404 }))
.get("/foo", () => new Response("foo"))
.get("/thrower", () => {
throw new HttpError(404);
});
const server = new FakeServer(app.handler());
let res = await server.get("/invalid");
expect(await res.text()).toEqual("bar");
res = await server.get("/thrower");
expect(await res.text()).toEqual("bar");
});
Deno.test("App - uses error route", async () => {
const app = new App()
.onError("*", () => new Response("error route", { status: 200 }))
.get("/thrower", () => {
throw new HttpError(404);
});
const server = new FakeServer(app.handler());
let res = await server.get("/invalid");
expect(await res.text()).toEqual("error route");
res = await server.get("/thrower");
expect(await res.text()).toEqual("error route");
});
Deno.test("App - uses notFound route on 404", async () => {
const app = new App()
.notFound(() => new Response("not found route", { status: 404 }))
.onError("*", () => new Response("error route", { status: 500 }))
.get("/thrower", () => {
throw new HttpError(404);
})
.get("/thrower_2", () => {
throw new HttpError(500);
});
const server = new FakeServer(app.handler());
let res = await server.get("/invalid");
expect(await res.text()).toEqual("not found route");
res = await server.get("/thrower");
expect(await res.text()).toEqual("not found route");
res = await server.get("/thrower_2");
expect(await res.text()).toEqual("error route");
});
// Issue: https://github.com/denoland/fresh/issues/3115
Deno.test("App - .route() with basePath", async () => {
const app = new App({ basePath: "/foo/bar" })
.route("/", { handler: () => new Response("ok") });
const server = new FakeServer(app.handler());
let res = await server.delete("/");
await res.body?.cancel();
expect(res.status).toEqual(404);
res = await server.delete("/foo");
await res.body?.cancel();
expect(res.status).toEqual(404);
res = await server.delete("/foo/bar");
expect(await res.text()).toEqual("ok");
expect(res.status).toEqual(200);
});
Deno.test("App - .all() with *", async () => {
const app = new App()
.all("*", () => new Response("ok"));
const server = new FakeServer(app.handler());
const res = await server.get("/");
expect(await res.text()).toEqual("ok");
});
Deno.test("App - .route() with *", async () => {
const app = new App()
.route("*", { handler: () => new Response("ok") });
const server = new FakeServer(app.handler());
const res = await server.get("/");
expect(await res.text()).toEqual("ok");
});
Deno.test("App - .use() - lazy", async () => {
const app = new App<{ text: string }>()
// deno-lint-ignore require-await
.use(async () => {
return (ctx) => {
ctx.state.text = "ok";
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("ok");
});
Deno.test("App - .route() - lazy", async () => {
const app = new App()
// deno-lint-ignore require-await
.route("/", async () => {
return { handler: () => new Response("ok") };
});
const server = new FakeServer(app.handler());
const res = await server.get("/");
expect(await res.text()).toEqual("ok");
});
Deno.test("App - .get/post/patch/put/delete/head/all() - lazy", async () => {
const app = new App()
.get("/", () => Promise.resolve(() => new Response("ok")))
.post("/", () => Promise.resolve(() => new Response("ok")))
.patch("/", () => Promise.resolve(() => new Response("ok")))
.delete("/", () => Promise.resolve(() => new Response("ok")))
.put("/", () => Promise.resolve(() => new Response("ok")))
.head("/", () => Promise.resolve(() => new Response("ok")))
.all("/", () => Promise.resolve(() => new Response("ok")));
const server = new FakeServer(app.handler());
let res = await server.get("/");
expect(await res.text()).toEqual("ok");
res = await server.post("/");
expect(await res.text()).toEqual("ok");
res = await server.put("/");
expect(await res.text()).toEqual("ok");
res = await server.patch("/");
expect(await res.text()).toEqual("ok");
res = await server.delete("/");
expect(await res.text()).toEqual("ok");
res = await server.head("/");
expect(await res.text()).toEqual("ok");
});
Deno.test("App - .appWrapper()", async () => {
const app = new App()
.appWrapper(({ Component }) => (
<>
app/<Component />
</>
))
.get("/", (ctx) => ctx.render(<>index</>));
const server = new FakeServer(app.handler());
const res = await server.get("/");
expect(await res.text()).toContain("<body>app/index<");
});
Deno.test.ignore("App - .layout()", async () => {
const app = new App()
.layout("/", ({ Component }) => (
<>
layout/<Component />
</>
))
.layout("/foo", ({ Component }) => (
<>
foo/<Component />
</>
))
.get("/foo", (ctx) => ctx.render(<>index</>));
const server = new FakeServer(app.handler());
// The `/foo` layout is not applied for GET `/foo`, is that correct?
const res = await server.get("/foo");
expect(await res.text()).toContain("<body>layout/foo/index<");
});
Deno.test("App - .appWrapper() + .layout()", async () => {
const app = new App()
.appWrapper(({ Component }) => (
<>
app/<Component />
</>
))
.layout("/", ({ Component }) => (
<>
layout/<Component />
</>
))
.get("/", (ctx) => ctx.render(<>index</>));
const server = new FakeServer(app.handler());
const res = await server.get("/");
expect(await res.text()).toContain("<body>app/layout/index<");
});
Deno.test("App - ctx.render() without app wrapper or layout", async () => {
const app = new App()
.appWrapper(({ Component }) => (
<>
app/<Component />
</>
))
.layout("/", ({ Component }) => (
<>
layout/<Component />
</>
))
.get("/", (ctx) =>
ctx.render(<>index</>, undefined, {
skipAppWrapper: true,
skipInheritedLayouts: true,
}));
const server = new FakeServer(app.handler());
const res = await server.get("/");
expect(await res.text()).toContain("<body>index<");
});
Deno.test("App - .mountApp() with basePath", async () => {
const innerApp = new App({ basePath: "/api" })
.get("/users", () => new Response("users"));
const app = new App()
.get("/", () => new Response("home"))
.mountApp("/v1", innerApp);
const server = new FakeServer(app.handler());
let res = await server.get("/");
expect(await res.text()).toEqual("home");
// Fixed behavior: inner app's basePath is now applied
res = await server.get("/v1/api/users");
expect(await res.text()).toEqual("users");
// Routes without the full basePath should not work
res = await server.get("/v1/users");
expect(res.status).toEqual(404);
});
Deno.test("App - .mountApp() with main app basePath", async () => {
const innerApp = new App()
.get("/data", () => new Response("data"));
const app = new App({ basePath: "/main" })
.get("/", () => new Response("home"))
.mountApp("/sub", innerApp);
const server = new FakeServer(app.handler());
let res = await server.get("/main");
expect(await res.text()).toEqual("home");
res = await server.get("/main/sub/data");
expect(await res.text()).toEqual("data");
// Should not work without main basePath
res = await server.get("/sub/data");
expect(res.status).toEqual(404);
});
Deno.test("App - .mountApp() with both main and inner basePath", async () => {
const innerApp = new App({ basePath: "/api/v2" })
.get("/users", () => new Response("users"))
.get("/posts", () => new Response("posts"));
const app = new App({ basePath: "/main" })
.get("/", () => new Response("home"))
.mountApp("/services", innerApp);
const server = new FakeServer(app.handler());
let res = await server.get("/main");
expect(await res.text()).toEqual("home");
// Both basePaths should be applied: main basePath + mount path + inner basePath
res = await server.get("/main/services/api/v2/users");
expect(await res.text()).toEqual("users");
res = await server.get("/main/services/api/v2/posts");
expect(await res.text()).toEqual("posts");
// Partial paths should not work
res = await server.get("/services/api/v2/users");
expect(res.status).toEqual(404);
res = await server.get("/main/services/users");
expect(res.status).toEqual(404);
});