fix: middleware state reset between middlewares (#1500)

This commit is contained in:
Marvin Hagemeister
2023-07-24 11:03:11 +02:00
committed by GitHub
parent 0b96db4bae
commit 38d4b90091
10 changed files with 277 additions and 13 deletions
+1
View File
@@ -19,3 +19,4 @@ export {
default as puppeteer,
Page,
} from "https://deno.land/x/puppeteer@16.2.0/mod.ts";
export { DOMParser } from "https://esm.sh/linkedom@0.15.1";
+14 -8
View File
@@ -45,10 +45,13 @@ import * as $39 from "./routes/params.tsx";
import * as $40 from "./routes/props/[id].tsx";
import * as $41 from "./routes/state-in-props/_middleware.ts";
import * as $42 from "./routes/state-in-props/index.tsx";
import * as $43 from "./routes/static.tsx";
import * as $44 from "./routes/status_overwrite.tsx";
import * as $45 from "./routes/umlaut-äöüß.tsx";
import * as $46 from "./routes/wildcard.tsx";
import * as $43 from "./routes/state-middleware/_middleware.ts";
import * as $44 from "./routes/state-middleware/foo/_middleware.ts";
import * as $45 from "./routes/state-middleware/foo/index.tsx";
import * as $46 from "./routes/static.tsx";
import * as $47 from "./routes/status_overwrite.tsx";
import * as $48 from "./routes/umlaut-äöüß.tsx";
import * as $49 from "./routes/wildcard.tsx";
import * as $$0 from "./islands/Counter.tsx";
import * as $$1 from "./islands/MultipleCounters.tsx";
import * as $$2 from "./islands/ReturningNull.tsx";
@@ -104,10 +107,13 @@ const manifest = {
"./routes/props/[id].tsx": $40,
"./routes/state-in-props/_middleware.ts": $41,
"./routes/state-in-props/index.tsx": $42,
"./routes/static.tsx": $43,
"./routes/status_overwrite.tsx": $44,
"./routes/umlaut-äöüß.tsx": $45,
"./routes/wildcard.tsx": $46,
"./routes/state-middleware/_middleware.ts": $43,
"./routes/state-middleware/foo/_middleware.ts": $44,
"./routes/state-middleware/foo/index.tsx": $45,
"./routes/static.tsx": $46,
"./routes/status_overwrite.tsx": $47,
"./routes/umlaut-äöüß.tsx": $48,
"./routes/wildcard.tsx": $49,
},
islands: {
"./islands/Counter.tsx": $$0,
@@ -0,0 +1,6 @@
import { MiddlewareHandlerContext } from "$fresh/server.ts";
export const handler = (_req: Request, ctx: MiddlewareHandlerContext) => {
ctx.state = { handler1: "it works" };
return ctx.next();
};
@@ -0,0 +1,6 @@
import { MiddlewareHandlerContext } from "$fresh/server.ts";
export const handler = (_req: Request, ctx: MiddlewareHandlerContext) => {
ctx.state.handler2 = "it works";
return ctx.next();
};
@@ -0,0 +1,10 @@
import { HandlerContext, PageProps } from "$fresh/server.ts";
export const handler = (_req: Request, ctx: HandlerContext) => {
ctx.state.handler3 = "it works";
return ctx.render();
};
export default function Page(props: PageProps) {
return <pre>{JSON.stringify(props.state,null,2)}</pre>;
}
+20 -1
View File
@@ -9,7 +9,7 @@ import {
import manifest from "./fixture/fresh.gen.ts";
import options from "./fixture/options.ts";
import { BUILD_ID } from "../src/server/build_id.ts";
import { startFreshServer, withPageName } from "./test_utils.ts";
import { parseHtml, startFreshServer, withPageName } from "./test_utils.ts";
import { assertMatch } from "https://deno.land/std@0.193.0/testing/asserts.ts";
const ctx = await ServerContext.fromManifest(manifest, options);
@@ -563,6 +563,25 @@ Deno.test({
},
});
Deno.test({
name: "/middleware - should pass state through all middlewares",
fn: async () => {
const resp = await handler(
new Request("https://fresh.deno.dev/state-middleware/foo"),
);
assert(resp);
assertEquals(resp.status, Status.OK);
const body = await resp.text();
const doc = parseHtml(body);
assertEquals(JSON.parse(doc.querySelector("pre").textContent), {
handler1: "it works",
handler2: "it works",
handler3: "it works",
});
},
});
Deno.test({
name: "/middleware - middlewareParams",
fn: async () => {
+5 -1
View File
@@ -1,4 +1,8 @@
import { delay, Page, puppeteer, TextLineStream } from "./deps.ts";
import { delay, DOMParser, Page, puppeteer, TextLineStream } from "./deps.ts";
export function parseHtml(input: string) {
return new DOMParser().parseFromString(input, "text/html");
}
export async function startFreshServer(options: Deno.CommandOptions) {
const { serverProcess, lines, address } = await spawnServer(options);