Skip to content

Commit

Permalink
Fix mock server
Browse files Browse the repository at this point in the history
  • Loading branch information
ije committed Mar 26, 2023
1 parent d86e9de commit 2fc75ea
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions server/mock.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { Router } from "../framework/core/router.ts";
import { isPlainObject } from "../shared/util.ts";
import { createContext } from "./context.ts";
import { createContext, NEXT } from "./context.ts";
import { path } from "./deps.ts";
import { getAppDir } from "./helpers.ts";
import { createHtmlResponse, loadIndexHtml } from "./html.ts";
Expand Down Expand Up @@ -51,17 +51,24 @@ export class MockServer {
const { middlewares, origin } = this.#options;
const url = new URL(input, origin ?? "http://localhost/");
const req = new Request(url.href, init);
const ctx = createContext(() => Promise.resolve(new Response(null)), {
req,
connInfo: {
localAddr: { transport: "tcp", hostname: "localhost", port: 80 },
remoteAddr: { transport: "tcp", hostname: "localhost", port: 80 },
},
sessionOptions: this.#options.session,
});
const next = (i: number): Promise<Response> | Response => {
if (Array.isArray(middlewares) && i < middlewares.length) {
const mw = middlewares[i];
const ctx = createContext(req, next.bind(null, i + 1), { session: this.#options.session });
try {
Reflect.set(ctx, NEXT, next.bind(null, i + 1));
return mw.fetch(req, ctx);
} catch (err) {
throw new Error(`Middleare${mw.name ? `(${mw.name})` : ""}:`, err);
}
}
const ctx = createContext(req, () => Promise.resolve(new Response(null)), { session: this.#options.session });
return this.#handler(req, ctx);
};
return next(0);
Expand Down

0 comments on commit 2fc75ea

Please sign in to comment.