diff --git a/cli/tests/unit_node/http_test.ts b/cli/tests/unit_node/http_test.ts index baebd46780..a841360523 100644 --- a/cli/tests/unit_node/http_test.ts +++ b/cli/tests/unit_node/http_test.ts @@ -707,3 +707,31 @@ Deno.test( await promise; }, ); + +Deno.test( + "[node/http] client end with callback", + { permissions: { net: true } }, + async () => { + const promise = deferred(); + let body = ""; + + const request = http.request( + "http://localhost:4545/http_version", + (resp) => { + resp.on("data", (chunk) => { + body += chunk; + }); + + resp.on("end", () => { + promise.resolve(); + }); + }, + ); + request.on("error", promise.reject); + request.end(); + + await promise; + + assertEquals(body, "HTTP/1.1"); + }, +); diff --git a/ext/node/polyfills/http.ts b/ext/node/polyfills/http.ts index e3419e88b1..6a1ea71051 100644 --- a/ext/node/polyfills/http.ts +++ b/ext/node/polyfills/http.ts @@ -599,6 +599,15 @@ class ClientRequest extends OutgoingMessage { // deno-lint-ignore no-explicit-any end(chunk?: any, encoding?: any, cb?: any): this { + if (typeof chunk === "function") { + cb = chunk; + chunk = null; + encoding = null; + } else if (typeof encoding === "function") { + cb = encoding; + encoding = null; + } + this.finished = true; if (chunk !== undefined && chunk !== null) { this.write(chunk, encoding); @@ -617,12 +626,12 @@ class ClientRequest extends OutgoingMessage { } core.tryClose(this._bodyWriteRid); + } - try { - cb?.(); - } catch (_) { - // - } + try { + cb?.(); + } catch (_) { + // } })(), ]);