Skip to content

Commit

Permalink
fix: self mount error
Browse files Browse the repository at this point in the history
  • Loading branch information
marvinhagemeister committed Mar 27, 2024
1 parent b087850 commit 0ce250a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,12 @@ export class FreshApp<State> implements App<State> {
// - `app.mounApp("*", otherApp)`
const isSelf = path === "*" || path === "/";
if (isSelf) {
const first = this._router._routes[0];
if (first.method === "ALL" && first.path === "*") {
first.handlers.push(...middlewares);
const selfRoutes = this._router._routes;
if (
selfRoutes.length > 0 && selfRoutes[0].method === "ALL" &&
selfRoutes[0].path === "*"
) {
selfRoutes[0].handlers.push(...middlewares);
} else {
this._router._routes.unshift({
handlers: middlewares,
Expand Down
17 changes: 17 additions & 0 deletions src/app_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -316,3 +316,20 @@ Deno.test(
expect(await res.text()).toEqual("AB");
},
);

Deno.test("FreshApp - .mountApp() self mount empty", async () => {
const innerApp = new FreshApp<{ text: string }>()
.use((ctx) => {
ctx.state.text = "A";
return ctx.next();
})
.get("/foo", (ctx) => new Response(ctx.state.text));

const app = new FreshApp<{ text: string }>()
.mountApp("/", innerApp);

const server = new FakeServer(await app.handler());

const res = await server.get("/foo");
expect(await res.text()).toEqual("A");
});

0 comments on commit 0ce250a

Please sign in to comment.