feat: support overwriting default 404 handler (#3081)
Fixes https://github.com/denoland/fresh/issues/1918
This commit is contained in:
+8
-2
@@ -242,7 +242,13 @@ export class App<State> {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
handler(): (
|
/**
|
||||||
|
* Create handler function for `Deno.serve` or to be used in
|
||||||
|
* testing.
|
||||||
|
* @param {() => Promise<Response>} [nextFn] overwrite default 404 handler
|
||||||
|
* @returns {Deno.ServeHandler}
|
||||||
|
*/
|
||||||
|
handler(nextFn?: () => Promise<Response>): (
|
||||||
request: Request,
|
request: Request,
|
||||||
info?: Deno.ServeHandlerInfo,
|
info?: Deno.ServeHandlerInfo,
|
||||||
) => Promise<Response> {
|
) => Promise<Response> {
|
||||||
@@ -273,7 +279,7 @@ export class App<State> {
|
|||||||
|
|
||||||
const next = matched.patternMatch && !matched.methodMatch
|
const next = matched.patternMatch && !matched.methodMatch
|
||||||
? DEFAULT_NOT_ALLOWED_METHOD
|
? DEFAULT_NOT_ALLOWED_METHOD
|
||||||
: DEFAULT_NOT_FOUND;
|
: nextFn ?? DEFAULT_NOT_FOUND;
|
||||||
|
|
||||||
const { params, handlers, pattern } = matched;
|
const { params, handlers, pattern } = matched;
|
||||||
const ctx = new FreshReqContext<State>(
|
const ctx = new FreshReqContext<State>(
|
||||||
|
|||||||
@@ -532,3 +532,15 @@ Deno.test("FreshApp - adding Island should convert to valid export names", () =>
|
|||||||
fn: component3,
|
fn: component3,
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Deno.test("FreshApp - overwrite default 404 handler", async () => {
|
||||||
|
const app = new App().get("/foo", () => new Response("foo"));
|
||||||
|
|
||||||
|
const server = new FakeServer(
|
||||||
|
app.handler(() => Promise.resolve(new Response("bar"))),
|
||||||
|
);
|
||||||
|
|
||||||
|
const res = await server.get("/invalid");
|
||||||
|
const text = await res.text();
|
||||||
|
expect(text).toEqual("bar");
|
||||||
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user