fix: returned handler headers only working with plain objects (#3447)

We only dealt with returned headers as objects instead of all potential
shapes of `Headersinit`.

Fixes https://github.com/denoland/fresh/issues/3445
This commit is contained in:
Marvin Hagemeister
2025-09-16 09:45:11 +02:00
committed by GitHub
parent 15067345ed
commit cc95155047
2 changed files with 58 additions and 3 deletions
+44
View File
@@ -637,6 +637,50 @@ Deno.test("App - .route() with *", async () => {
expect(await res.text()).toEqual("ok");
});
Deno.test("App - .route() handler returns headers", async () => {
const app = new App()
.route("/", {
handler: () => {
const headers = new Headers();
headers.set("X-Foo", "foo");
return { data: {}, headers };
},
component() {
return <h1>foo</h1>;
},
})
.route("/obj", {
handler: () => {
return { data: {}, headers: { "X-Foo": "foo" } };
},
component() {
return <h1>foo</h1>;
},
})
.route("/arr", {
handler: () => {
return { data: {}, headers: [["X-Foo", "foo"]] };
},
component() {
return <h1>foo</h1>;
},
});
const server = new FakeServer(app.handler());
let res = await server.get("/");
expect(await res.text()).toContain("<h1>foo</h1>");
expect(res.headers.get("X-Foo")).toEqual("foo");
res = await server.get("/obj");
expect(await res.text()).toContain("<h1>foo</h1>");
expect(res.headers.get("X-Foo")).toEqual("foo");
res = await server.get("/arr");
expect(await res.text()).toContain("<h1>foo</h1>");
expect(res.headers.get("X-Foo")).toEqual("foo");
});
Deno.test("App - .use() - lazy", async () => {
const app = new App<{ text: string }>()
// deno-lint-ignore require-await
+14 -3
View File
@@ -189,9 +189,20 @@ export async function renderRoute<State>(
if (typeof res.status === "number") {
status = res.status;
}
if (res.headers) {
for (const [name, value] of Object.entries(res.headers)) {
headers.set(name, value);
if (res.headers !== undefined) {
if (res.headers instanceof Headers) {
res.headers.forEach((value, key) => {
headers.set(key, value);
});
} else if (Array.isArray(res.headers)) {
for (let i = 0; i < res.headers.length; i++) {
const entry = res.headers[i];
headers.set(entry[0], entry[1]);
}
} else {
for (const [name, value] of Object.entries(res.headers)) {
headers.set(name, value);
}
}
}