diff options
author | Marvin Hagemeister <marvin@deno.com> | 2024-07-09 17:46:10 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-07-09 17:46:10 +0200 |
commit | 07613a6bf26d9112d47fda9e502425395bd78105 (patch) | |
tree | 5ad911d495cfdbff1f6f9cfe63865571ab7e1fa5 /tests/unit_node/http_test.ts | |
parent | 0425dabb84b41d2b7d3204068ce8a1f4a397bce6 (diff) |
fix(node/http): support all `.writeHead()` signatures (#24469)
Implement the missing `.writeHead()` signatures from Node's
`ServerResponse` class that we didn't support.
Fixes https://github.com/denoland/deno/issues/24468
Diffstat (limited to 'tests/unit_node/http_test.ts')
-rw-r--r-- | tests/unit_node/http_test.ts | 89 |
1 files changed, 88 insertions, 1 deletions
diff --git a/tests/unit_node/http_test.ts b/tests/unit_node/http_test.ts index af88d5f9c..a053f3a27 100644 --- a/tests/unit_node/http_test.ts +++ b/tests/unit_node/http_test.ts @@ -1,7 +1,7 @@ // Copyright 2018-2024 the Deno authors. All rights reserved. MIT license. import EventEmitter from "node:events"; -import http, { type RequestOptions } from "node:http"; +import http, { type RequestOptions, type ServerResponse } from "node:http"; import url from "node:url"; import https from "node:https"; import net from "node:net"; @@ -142,6 +142,93 @@ Deno.test("[node/http] chunked response", async () => { } }); +Deno.test("[node/http] .writeHead()", async (t) => { + async function testWriteHead( + onRequest: (res: ServerResponse) => void, + onResponse: (res: Response) => void, + ) { + const { promise, resolve } = Promise.withResolvers<void>(); + const server = http.createServer((_req, res) => { + onRequest(res); + res.end(); + }); + server.listen(async () => { + const res = await fetch( + // deno-lint-ignore no-explicit-any + `http://127.0.0.1:${(server.address() as any).port}/`, + ); + await res.body?.cancel(); + + onResponse(res); + + server.close(() => resolve()); + }); + + await promise; + } + + await t.step("send status code", async () => { + await testWriteHead( + (res) => res.writeHead(404), + (res) => { + assertEquals(res.status, 404); + }, + ); + }); + + // TODO(@marvinhagemeister): hyper doesn't support custom status text + // await t.step("send status + custom status text", async () => { + // await testWriteHead( + // (res) => res.writeHead(404, "some text"), + // (res) => { + // assertEquals(res.status, 404); + // assertEquals(res.statusText, "some text"); + // }, + // ); + // }); + + await t.step("send status + custom status text + headers obj", async () => { + await testWriteHead( + (res) => res.writeHead(404, "some text", { foo: "bar" }), + (res) => { + assertEquals(res.status, 404); + // TODO(@marvinhagemeister): hyper doesn't support custom + // status text + // assertEquals(res.statusText, "some text"); + assertEquals(res.headers.get("foo"), "bar"); + }, + ); + }); + + await t.step("send status + headers obj", async () => { + await testWriteHead( + (res) => { + res.writeHead(200, { + foo: "bar", + bar: ["foo1", "foo2"], + foobar: 1, + }); + }, + (res) => { + assertEquals(res.status, 200); + assertEquals(res.headers.get("foo"), "bar"); + assertEquals(res.headers.get("bar"), "foo1, foo2"); + assertEquals(res.headers.get("foobar"), "1"); + }, + ); + }); + + await t.step("send status + headers array", async () => { + await testWriteHead( + (res) => res.writeHead(200, [["foo", "bar"]]), + (res) => { + assertEquals(res.status, 200); + assertEquals(res.headers.get("foo"), "bar"); + }, + ); + }); +}); + // Test empty chunks: https://github.com/denoland/deno/issues/17194 Deno.test("[node/http] empty chunk in the middle of response", async () => { const { promise, resolve } = Promise.withResolvers<void>(); |