fix: allow multiple set-cookie headers to be set before render (#2357)
closes #2346 The primary issue was the snippet `headers[key] = value`. This caused only one `set-cookie` to make it through. Switching to a `Headers` object allowed me to use `append` when appropriate, allowing for multiple `set-cookie` entries. I also cleaned up one of the tests to make use of steps, because I originally broke it and couldn't immediately tell which part was failing.
This commit is contained in:
+10
-9
@@ -785,10 +785,10 @@ function sendResponse(
|
|||||||
},
|
},
|
||||||
) {
|
) {
|
||||||
const [body, uuid, csp] = resp;
|
const [body, uuid, csp] = resp;
|
||||||
const headers: Record<string, string> = {
|
const headers: Headers = new Headers({
|
||||||
"content-type": "text/html; charset=utf-8",
|
"content-type": "text/html; charset=utf-8",
|
||||||
"x-fresh-uuid": uuid,
|
"x-fresh-uuid": uuid,
|
||||||
};
|
});
|
||||||
|
|
||||||
if (csp) {
|
if (csp) {
|
||||||
if (options.isDev) {
|
if (options.isDev) {
|
||||||
@@ -799,24 +799,25 @@ function sendResponse(
|
|||||||
}
|
}
|
||||||
const directive = serializeCSPDirectives(csp.directives);
|
const directive = serializeCSPDirectives(csp.directives);
|
||||||
if (csp.reportOnly) {
|
if (csp.reportOnly) {
|
||||||
headers["content-security-policy-report-only"] = directive;
|
headers.set("content-security-policy-report-only", directive);
|
||||||
} else {
|
} else {
|
||||||
headers["content-security-policy"] = directive;
|
headers.set("content-security-policy", directive);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (options.headers) {
|
if (options.headers) {
|
||||||
if (Array.isArray(options.headers)) {
|
if (Array.isArray(options.headers)) {
|
||||||
for (let i = 0; i < options.headers.length; i++) {
|
for (const [key, value] of options.headers) {
|
||||||
const item = options.headers[i];
|
headers.append(key, value);
|
||||||
headers[item[0]] = item[1];
|
|
||||||
}
|
}
|
||||||
} else if (options.headers instanceof Headers) {
|
} else if (options.headers instanceof Headers) {
|
||||||
options.headers.forEach((value, key) => {
|
options.headers.forEach((value, key) => {
|
||||||
headers[key] = value;
|
headers.append(key, value);
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
Object.assign(headers, options.headers);
|
for (const [key, value] of Object.entries(options.headers)) {
|
||||||
|
headers.append(key, value);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,8 @@
|
|||||||
{
|
{
|
||||||
"lock": false,
|
"lock": false,
|
||||||
|
"tasks": {
|
||||||
|
"start": "deno run -A --watch=static/,routes/ dev.ts"
|
||||||
|
},
|
||||||
"imports": {
|
"imports": {
|
||||||
"$fresh/": "../../",
|
"$fresh/": "../../",
|
||||||
"preact": "https://esm.sh/preact@10.19.6",
|
"preact": "https://esm.sh/preact@10.19.6",
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
// This file SHOULD be checked into source version control.
|
// This file SHOULD be checked into source version control.
|
||||||
// This file is automatically updated during development when running `dev.ts`.
|
// This file is automatically updated during development when running `dev.ts`.
|
||||||
|
|
||||||
|
import * as $cookiePasser from "./routes/cookiePasser.tsx";
|
||||||
import * as $head_style from "./routes/head_style.tsx";
|
import * as $head_style from "./routes/head_style.tsx";
|
||||||
import * as $header_arr from "./routes/header_arr.tsx";
|
import * as $header_arr from "./routes/header_arr.tsx";
|
||||||
import * as $header_instance from "./routes/header_instance.tsx";
|
import * as $header_instance from "./routes/header_instance.tsx";
|
||||||
@@ -12,6 +13,7 @@ import { type Manifest } from "$fresh/server.ts";
|
|||||||
|
|
||||||
const manifest = {
|
const manifest = {
|
||||||
routes: {
|
routes: {
|
||||||
|
"./routes/cookiePasser.tsx": $cookiePasser,
|
||||||
"./routes/head_style.tsx": $head_style,
|
"./routes/head_style.tsx": $head_style,
|
||||||
"./routes/header_arr.tsx": $header_arr,
|
"./routes/header_arr.tsx": $header_arr,
|
||||||
"./routes/header_instance.tsx": $header_instance,
|
"./routes/header_instance.tsx": $header_instance,
|
||||||
|
|||||||
@@ -0,0 +1,18 @@
|
|||||||
|
import { FreshContext, Handlers } from "$fresh/server.ts";
|
||||||
|
|
||||||
|
export const handler: Handlers = {
|
||||||
|
async GET(_req: Request, ctx: FreshContext) {
|
||||||
|
const headers = new Headers();
|
||||||
|
headers.append("Set-Cookie", "foo=bar");
|
||||||
|
headers.append("Set-Cookie", "baz=1234");
|
||||||
|
return await ctx.render({}, { headers });
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
export default function Home() {
|
||||||
|
return (
|
||||||
|
<div>
|
||||||
|
hello
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
+25
-10
@@ -32,19 +32,25 @@ Deno.test("doesn't leak data across renderers", async () => {
|
|||||||
await Promise.all(promises);
|
await Promise.all(promises);
|
||||||
});
|
});
|
||||||
|
|
||||||
Deno.test("render headers passed to ctx.render()", async () => {
|
Deno.test("render headers passed to ctx.render()", async (t) => {
|
||||||
await withFakeServe("./tests/fixture_render/main.ts", async (server) => {
|
await withFakeServe("./tests/fixture_render/main.ts", async (server) => {
|
||||||
let res = await server.get("/header_arr");
|
await t.step("header_arr", async () => {
|
||||||
assertEquals(res.headers.get("x-foo"), "Hello world!");
|
const res = await server.get("/header_arr");
|
||||||
await res.body?.cancel();
|
assertEquals(res.headers.get("x-foo"), "Hello world!");
|
||||||
|
await res.body?.cancel();
|
||||||
|
});
|
||||||
|
|
||||||
res = await server.get("/header_obj");
|
await t.step("header_obj", async () => {
|
||||||
assertEquals(res.headers.get("x-foo"), "Hello world!");
|
const res = await server.get("/header_obj");
|
||||||
await res.body?.cancel();
|
assertEquals(res.headers.get("x-foo"), "Hello world!");
|
||||||
|
await res.body?.cancel();
|
||||||
|
});
|
||||||
|
|
||||||
res = await server.get("/header_instance");
|
await t.step("header_instance", async () => {
|
||||||
assertEquals(res.headers.get("x-foo"), "Hello world!");
|
const res = await server.get("/header_instance");
|
||||||
await res.body?.cancel();
|
assertEquals(res.headers.get("x-foo"), "Hello world!");
|
||||||
|
await res.body?.cancel();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -87,3 +93,12 @@ Deno.test("Ensure manifest has valid specifiers", async () => {
|
|||||||
},
|
},
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Deno.test("render multiple set-cookie headers passed to ctx.render()", async () => {
|
||||||
|
await withFakeServe("./tests/fixture_render/dev.ts", async (server) => {
|
||||||
|
const res = await server.get("/cookiePasser");
|
||||||
|
const cookies = res.headers.getSetCookie();
|
||||||
|
assertEquals(cookies, ["foo=bar", "baz=1234"]);
|
||||||
|
await res.body?.cancel();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user