Skip to content

Commit

Permalink
fix(ext/node): add ServerResponse#appendHeader (denoland#24216)
Browse files Browse the repository at this point in the history
  • Loading branch information
littledivy authored Jun 21, 2024
1 parent fc197a7 commit 416df1e
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
20 changes: 20 additions & 0 deletions ext/node/polyfills/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1420,6 +1420,26 @@ export class ServerResponse extends NodeWritable {
return this;
}

appendHeader(name: string, value: string | string[]) {
if (Array.isArray(value)) {
this.#hasNonStringHeaders = true;
}
if (this.#headers[name] === undefined) {
this.#headers[name] = value;
} else {
if (!Array.isArray(this.#headers[name])) {
this.#headers[name] = [this.#headers[name]];
}
const header = this.#headers[name];
if (Array.isArray(value)) {
header.push(...value);
} else {
header.push(value);
}
}
return this;
}

getHeader(name: string) {
return this.#headers[name];
}
Expand Down
26 changes: 26 additions & 0 deletions tests/unit_node/http_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -968,6 +968,32 @@ Deno.test("[node/http] ServerResponse getHeader", async () => {
await promise;
});

Deno.test("[node/http] ServerResponse appendHeader", async () => {
const { promise, resolve } = Promise.withResolvers<void>();
const server = http.createServer((_req, res) => {
res.setHeader("foo", "bar");
res.appendHeader("foo", "baz");
res.appendHeader("foo", ["qux"]);
res.appendHeader("foo", ["quux"]);
res.appendHeader("Set-Cookie", "a=b");
res.appendHeader("Set-Cookie", ["c=d", "e=f"]);
res.end("Hello World");
});

server.listen(async () => {
const { port } = server.address() as { port: number };
const res = await fetch(`http://localhost:${port}`);
assertEquals(res.headers.get("foo"), "bar, baz, qux, quux");
assertEquals(res.headers.getSetCookie(), ["a=b", "c=d", "e=f"]);
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
Expand Down

0 comments on commit 416df1e

Please sign in to comment.