fix: ctx.render headers not working (#1927)

This commit is contained in:
Marvin Hagemeister
2023-10-12 23:30:18 +02:00
committed by GitHub
parent 8d90c0ec81
commit bb8921ac85
10 changed files with 165 additions and 2 deletions
+17 -1
View File
@@ -1422,9 +1422,25 @@ function sendResponse(
headers["content-security-policy"] = directive;
}
}
if (options.headers) {
if (Array.isArray(options.headers)) {
for (let i = 0; i < options.headers.length; i++) {
const item = options.headers[i];
headers[item[0]] = item[1];
}
} else if (options.headers instanceof Headers) {
options.headers.forEach((value, key) => {
headers[key] = value;
});
} else {
Object.assign(headers, options.headers);
}
}
return new Response(body, {
status: options.status,
statusText: options.statusText,
headers: options.headers ? { ...headers, ...options.headers } : headers,
headers,
});
}
+13
View File
@@ -0,0 +1,13 @@
{
"lock": false,
"imports": {
"$fresh/": "../../",
"preact": "https://esm.sh/preact@10.15.1",
"preact/": "https://esm.sh/preact@10.15.1/",
"preact-render-to-string": "https://esm.sh/*preact-render-to-string@6.2.1"
},
"compilerOptions": {
"jsx": "react-jsx",
"jsxImportSource": "preact"
}
}
+5
View File
@@ -0,0 +1,5 @@
#!/usr/bin/env -S deno run -A --watch=static/,routes/
import dev from "$fresh/dev.ts";
await dev(import.meta.url, "./main.ts");
+21
View File
@@ -0,0 +1,21 @@
// DO NOT EDIT. This file is generated by Fresh.
// This file SHOULD be checked into source version control.
// This file is automatically updated during development when running `dev.ts`.
import * as $0 from "./routes/header_arr.tsx";
import * as $1 from "./routes/header_instance.tsx";
import * as $2 from "./routes/header_obj.tsx";
import * as $3 from "./routes/index.tsx";
const manifest = {
routes: {
"./routes/header_arr.tsx": $0,
"./routes/header_instance.tsx": $1,
"./routes/header_obj.tsx": $2,
"./routes/index.tsx": $3,
},
islands: {},
baseUrl: import.meta.url,
};
export default manifest;
+10
View File
@@ -0,0 +1,10 @@
/// <reference no-default-lib="true" />
/// <reference lib="dom" />
/// <reference lib="dom.iterable" />
/// <reference lib="dom.asynciterable" />
/// <reference lib="deno.ns" />
import { start } from "$fresh/server.ts";
import manifest from "./fresh.gen.ts";
await start(manifest);
@@ -0,0 +1,16 @@
import type { Handlers } from "$fresh/server.ts";
export const handler: Handlers<unknown, unknown> = {
GET(_, ctx) {
const headers = [["x-foo", "Hello world!"]] as [string, string][];
return ctx.render(undefined, { headers });
},
};
export default function Home() {
return (
<div>
Should have <code>X-Foo</code> header set.
</div>
);
}
@@ -0,0 +1,17 @@
import type { Handlers } from "$fresh/server.ts";
export const handler: Handlers<unknown, unknown> = {
GET(_, ctx) {
const headers = new Headers();
headers.set("x-foo", "Hello world!");
return ctx.render(undefined, { headers });
},
};
export default function Home() {
return (
<div>
Should have <code>X-Foo</code> header set.
</div>
);
}
@@ -0,0 +1,18 @@
import type { Handlers } from "$fresh/server.ts";
export const handler: Handlers<unknown, unknown> = {
GET(_, ctx) {
const headers = {
"x-foo": "Hello world!",
};
return ctx.render(undefined, { headers });
},
};
export default function Home() {
return (
<div>
Should have <code>X-Foo</code> header set.
</div>
);
}
+27
View File
@@ -0,0 +1,27 @@
import * as path from "https://deno.land/std@0.192.0/path/mod.ts";
const __dirname = path.dirname(path.fromFileUrl(import.meta.url));
const links: string[] = [];
for (const file of Deno.readDirSync(__dirname)) {
if (file.name.startsWith("index")) continue;
const name = path.basename(file.name, path.extname(file.name));
links.push(name);
}
export default function Home() {
return (
<div>
<h1>Tests</h1>
<ul>
{links.sort().map((link) => {
return (
<li key={link}>
<a href={`/${link}`}>{link}</a>
</li>
);
})}
</ul>
</div>
);
}
+21 -1
View File
@@ -1,4 +1,8 @@
import { assertSelector, parseHtml } from "$fresh/tests/test_utils.ts";
import {
assertSelector,
parseHtml,
withFakeServe,
} from "$fresh/tests/test_utils.ts";
import { assertEquals } from "$std/testing/asserts.ts";
import { createHandler } from "$fresh/server.ts";
import manifest from "./fixture/fresh.gen.ts";
@@ -26,3 +30,19 @@ Deno.test("doesn't leak data across renderers", async () => {
}
await Promise.all(promises);
});
Deno.test("render headers passed to ctx.render()", async () => {
await withFakeServe("./tests/fixture_render/main.ts", async (server) => {
let res = await server.get("/header_arr");
assertEquals(res.headers.get("x-foo"), "Hello world!");
await res.body?.cancel();
res = await server.get("/header_obj");
assertEquals(res.headers.get("x-foo"), "Hello world!");
await res.body?.cancel();
res = await server.get("/header_instance");
assertEquals(res.headers.get("x-foo"), "Hello world!");
await res.body?.cancel();
});
});