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:
Reed von Redwitz
2024-03-12 12:04:42 +01:00
committed by GitHub
parent b7281792a0
commit 5ec06350e2
5 changed files with 58 additions and 19 deletions
+10 -9
View File
@@ -785,10 +785,10 @@ function sendResponse(
},
) {
const [body, uuid, csp] = resp;
const headers: Record<string, string> = {
const headers: Headers = new Headers({
"content-type": "text/html; charset=utf-8",
"x-fresh-uuid": uuid,
};
});
if (csp) {
if (options.isDev) {
@@ -799,24 +799,25 @@ function sendResponse(
}
const directive = serializeCSPDirectives(csp.directives);
if (csp.reportOnly) {
headers["content-security-policy-report-only"] = directive;
headers.set("content-security-policy-report-only", directive);
} else {
headers["content-security-policy"] = directive;
headers.set("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];
for (const [key, value] of options.headers) {
headers.append(key, value);
}
} else if (options.headers instanceof Headers) {
options.headers.forEach((value, key) => {
headers[key] = value;
headers.append(key, value);
});
} else {
Object.assign(headers, options.headers);
for (const [key, value] of Object.entries(options.headers)) {
headers.append(key, value);
}
}
}