summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--cli/tests/unit_node/http_test.ts29
-rw-r--r--ext/node/polyfills/http.ts7
2 files changed, 35 insertions, 1 deletions
diff --git a/cli/tests/unit_node/http_test.ts b/cli/tests/unit_node/http_test.ts
new file mode 100644
index 000000000..c32bf62f2
--- /dev/null
+++ b/cli/tests/unit_node/http_test.ts
@@ -0,0 +1,29 @@
+// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
+
+import http from "node:http";
+import { assertEquals } from "../../../test_util/std/testing/asserts.ts";
+import { assertSpyCalls, spy } from "../../../test_util/std/testing/mock.ts";
+import { deferred } from "../../../test_util/std/async/deferred.ts";
+
+Deno.test("[node/http] ServerResponse _implicitHeader", async () => {
+ const d = deferred<void>();
+ const server = http.createServer((_req, res) => {
+ const writeHeadSpy = spy(res, "writeHead");
+ // deno-lint-ignore no-explicit-any
+ (res as any)._implicitHeader();
+ assertSpyCalls(writeHeadSpy, 1);
+ writeHeadSpy.restore();
+ res.end("Hello World");
+ });
+
+ server.listen(async () => {
+ const { port } = server.address() as { port: number };
+ const res = await fetch(`http://localhost:${port}`);
+ assertEquals(await res.text(), "Hello World");
+ server.close(() => {
+ d.resolve();
+ });
+ });
+
+ await d;
+});
diff --git a/ext/node/polyfills/http.ts b/ext/node/polyfills/http.ts
index ab5e2b438..12d71277f 100644
--- a/ext/node/polyfills/http.ts
+++ b/ext/node/polyfills/http.ts
@@ -485,7 +485,7 @@ export class ServerResponse extends NodeWritable {
return this.#headers.has(name);
}
- writeHead(status: number, headers: Record<string, string>) {
+ writeHead(status: number, headers: Record<string, string> = {}) {
this.statusCode = status;
for (const k in headers) {
if (Object.hasOwn(headers, k)) {
@@ -540,6 +540,11 @@ export class ServerResponse extends NodeWritable {
// @ts-expect-error The signature for cb is stricter than the one implemented here
return super.end(chunk, encoding, cb);
}
+
+ // Undocumented API used by `npm:compression`.
+ _implicitHeader() {
+ this.writeHead(this.statusCode);
+ }
}
// TODO(@AaronO): optimize