From 56e8ed5eb1a6f2880c96fc8f08e35a6dd83aad8b Mon Sep 17 00:00:00 2001 From: Luca Casonato Date: Mon, 12 Aug 2024 12:01:37 +0200 Subject: [PATCH] fix(ext/node): don't concat set-cookie in ServerResponse.appendHeader (#25000) Follow-on to https://github.com/denoland/deno/pull/24216/files#r1642188672 --- ext/node/polyfills/http.ts | 5 ++--- tests/unit_node/http_test.ts | 21 +++++++++++++++++++++ 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/ext/node/polyfills/http.ts b/ext/node/polyfills/http.ts index e0ddedef8f..575c6517e4 100644 --- a/ext/node/polyfills/http.ts +++ b/ext/node/polyfills/http.ts @@ -1439,12 +1439,11 @@ export class ServerResponse extends NodeWritable { } appendHeader(name: string, value: string | string[]) { - if (Array.isArray(value)) { - this.#hasNonStringHeaders = true; - } if (this.#headers[name] === undefined) { + if (Array.isArray(value)) this.#hasNonStringHeaders = true; this.#headers[name] = value; } else { + this.#hasNonStringHeaders = true; if (!Array.isArray(this.#headers[name])) { this.#headers[name] = [this.#headers[name]]; } diff --git a/tests/unit_node/http_test.ts b/tests/unit_node/http_test.ts index 3d53e09f94..c0514eebd9 100644 --- a/tests/unit_node/http_test.ts +++ b/tests/unit_node/http_test.ts @@ -1118,6 +1118,27 @@ Deno.test("[node/http] ServerResponse appendHeader", async () => { await promise; }); +Deno.test("[node/http] ServerResponse appendHeader set-cookie", async () => { + const { promise, resolve } = Promise.withResolvers(); + const server = http.createServer((_req, res) => { + res.appendHeader("Set-Cookie", "a=b"); + res.appendHeader("Set-Cookie", "c=d"); + res.end("Hello World"); + }); + + server.listen(async () => { + const { port } = server.address() as { port: number }; + const res = await fetch(`http://localhost:${port}`); + assertEquals(res.headers.getSetCookie(), ["a=b", "c=d"]); + assertEquals(await res.text(), "Hello World"); + server.close(() => { + resolve(); + }); + }); + + await promise; +}); + Deno.test("[node/http] IncomingMessage override", () => { const req = new http.IncomingMessage(new net.Socket()); // https://github.com/dougmoscrop/serverless-http/blob/3aaa6d0fe241109a8752efb011c242d249f32368/lib/request.js#L20-L30